fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Liam Petrielib: add two new gvariant types864ff685

master
4.4 KiB176 linesraw
1# A partial and basic implementation of GVariant formatted strings.
2#
3# Note, this API is not considered fully stable and it might therefore
4# change in backwards incompatible ways without prior notice.
5
6{ lib }:
7
8let
9  inherit (lib)
10    concatMapStringsSep concatStrings escape hasPrefix head replaceStrings;
11
12  mkPrimitive = t: v: {
13    _type = "gvariant";
14    type = t;
15    value = v;
16    __toString = self: "@${self.type} ${toString self.value}";
17  };
18
19  type = {
20    arrayOf = t: "a${t}";
21    maybeOf = t: "m${t}";
22    tupleOf = ts: "(${concatStrings ts})";
23    dictionaryEntryOf = ts: "{${concatStrings ts}}";
24    string = "s";
25    boolean = "b";
26    uchar = "y";
27    int16 = "n";
28    uint16 = "q";
29    int32 = "i";
30    uint32 = "u";
31    int64 = "x";
32    uint64 = "t";
33    double = "d";
34    variant = "v";
35  };
36
37  # Returns the GVariant type of a given Nix value. If no type can be
38  # found for the value then the empty string is returned.
39  typeOf = v:
40    with type;
41    if builtins.isBool v then
42      boolean
43    else if builtins.isInt v then
44      int32
45    else if builtins.isFloat v then
46      double
47    else if builtins.isString v then
48      string
49    else if builtins.isList v then
50      let elemType = elemTypeOf v;
51      in if elemType == "" then "" else arrayOf elemType
52    else if builtins.isAttrs v && v ? type then
53      v.type
54    else
55      "";
56
57  elemTypeOf = vs:
58    if builtins.isList vs then
59      if vs == [ ] then "" else typeOf (head vs)
60    else
61      "";
62
63  mkMaybe = elemType: elem:
64    mkPrimitive (type.maybeOf elemType) elem // {
65      __toString = self:
66        if self.value == null then
67          "@${self.type} nothing"
68        else
69          "just ${toString self.value}";
70    };
71
72in rec {
73
74  inherit type typeOf;
75
76  isGVariant = v: v._type or "" == "gvariant";
77
78  isArray = hasPrefix "a";
79  isDictionaryEntry = hasPrefix "{";
80  isMaybe = hasPrefix "m";
81  isTuple = hasPrefix "(";
82
83  # Returns the GVariant value that most closely matches the given Nix
84  # value. If no GVariant value can be found then `null` is returned.
85
86  mkValue = v:
87    if builtins.isBool v then
88      mkBoolean v
89    else if builtins.isInt v then
90      mkInt32 v
91    else if builtins.isFloat v then
92      mkDouble v
93    else if builtins.isString v then
94      mkString v
95    else if builtins.isList v then
96      if v == [ ] then mkArray type.string [ ] else mkArray (elemTypeOf v) v
97    else if builtins.isAttrs v && (v._type or "") == "gvariant" then
98      v
99    else
100      null;
101
102  mkArray = elemType: elems:
103    mkPrimitive (type.arrayOf elemType) (map mkValue elems) // {
104      __toString = self:
105        "@${self.type} [${concatMapStringsSep "," toString self.value}]";
106    };
107
108  mkEmptyArray = elemType: mkArray elemType [ ];
109
110  mkVariant = elem:
111    let gvarElem = mkValue elem;
112    in mkPrimitive type.variant gvarElem // {
113      __toString = self: "@${self.type} <${toString self.value}>";
114    };
115
116  mkDictionaryEntry = elems:
117    let
118      gvarElems = map mkValue elems;
119      dictionaryType = type.dictionaryEntryOf (map (e: e.type) gvarElems);
120    in mkPrimitive dictionaryType gvarElems // {
121      __toString = self:
122        "@${self.type} {${concatMapStringsSep "," toString self.value}}";
123    };
124
125  mkNothing = elemType: mkMaybe elemType null;
126
127  mkJust = elem: let gvarElem = mkValue elem; in mkMaybe gvarElem.type gvarElem;
128
129  mkTuple = elems:
130    let
131      gvarElems = map mkValue elems;
132      tupleType = type.tupleOf (map (e: e.type) gvarElems);
133    in mkPrimitive tupleType gvarElems // {
134      __toString = self:
135        "@${self.type} (${concatMapStringsSep "," toString self.value})";
136    };
137
138  mkBoolean = v:
139    mkPrimitive type.boolean v // {
140      __toString = self: if self.value then "true" else "false";
141    };
142
143  mkString = v:
144    let sanitize = s: replaceStrings [ "\n" ] [ "\\n" ] (escape [ "'" "\\" ] s);
145    in mkPrimitive type.string v // {
146      __toString = self: "'${sanitize self.value}'";
147    };
148
149  mkObjectpath = v:
150    mkPrimitive type.string v // {
151      __toString = self: "objectpath '${escape [ "'" ] self.value}'";
152    };
153
154  mkUchar = mkPrimitive type.uchar;
155
156  mkInt16 = mkPrimitive type.int16;
157
158  mkUint16 = mkPrimitive type.uint16;
159
160  mkInt32 = v:
161    mkPrimitive type.int32 v // {
162      __toString = self: toString self.value;
163    };
164
165  mkUint32 = mkPrimitive type.uint32;
166
167  mkInt64 = mkPrimitive type.int64;
168
169  mkUint64 = mkPrimitive type.uint64;
170
171  mkDouble = v:
172    mkPrimitive type.double v // {
173      __toString = self: toString self.value;
174    };
175
176}