1
0
Fork 0
mirror of https://github.com/nix-community/home-manager synced 2024-12-20 16:59:47 +01:00

lib: add two new gvariant types

Add GVariant variant and dictionary entry types
This commit is contained in:
Liam Petrie 2022-09-10 19:28:38 -07:00 committed by Robert Helgesson
parent 9da6c0232e
commit 864ff685fe
No known key found for this signature in database
GPG key ID: 36BDAA14C2797E89
4 changed files with 39 additions and 2 deletions

View file

@ -167,9 +167,11 @@ Builds a GVariant array containing the given list of elements, where each elemen
- `hm.gvariant.type.int64` - `hm.gvariant.type.int64`
- `hm.gvariant.type.uint64` - `hm.gvariant.type.uint64`
- `hm.gvariant.type.double` - `hm.gvariant.type.double`
- `hm.gvariant.type.variant`
- `hm.gvariant.type.arrayOf type` - `hm.gvariant.type.arrayOf type`
- `hm.gvariant.type.maybeOf type` - `hm.gvariant.type.maybeOf type`
- `hm.gvariant.type.tupleOf types` - `hm.gvariant.type.tupleOf types`
- `hm.gvariant.type.dictionaryEntryOf types`
-- --
+ +
where `type` and `types` are themselves a type and list of types, respectively. where `type` and `types` are themselves a type and list of types, respectively.
@ -185,3 +187,9 @@ Builds a GVariant maybe value containing the given GVariant element.
+ +
`hm.gvariant.mkTuple elements`::: `hm.gvariant.mkTuple elements`:::
Builds a GVariant tuple containing the given list of elements, where each element is a GVariant value. Builds a GVariant tuple containing the given list of elements, where each element is a GVariant value.
+
`hm.gvariant.mkVariant element`:::
Builds a GVariant variant which contains the value of a GVariant element.
+
`hm.gvariant.mkDictionaryEntry elements`:::
Builds a GVariant dictionary entry containing the given list of elements, where each element is a GVariant value.

View file

@ -20,6 +20,7 @@ let
arrayOf = t: "a${t}"; arrayOf = t: "a${t}";
maybeOf = t: "m${t}"; maybeOf = t: "m${t}";
tupleOf = ts: "(${concatStrings ts})"; tupleOf = ts: "(${concatStrings ts})";
dictionaryEntryOf = ts: "{${concatStrings ts}}";
string = "s"; string = "s";
boolean = "b"; boolean = "b";
uchar = "y"; uchar = "y";
@ -30,6 +31,7 @@ let
int64 = "x"; int64 = "x";
uint64 = "t"; uint64 = "t";
double = "d"; double = "d";
variant = "v";
}; };
# Returns the GVariant type of a given Nix value. If no type can be # Returns the GVariant type of a given Nix value. If no type can be
@ -74,13 +76,13 @@ in rec {
isGVariant = v: v._type or "" == "gvariant"; isGVariant = v: v._type or "" == "gvariant";
isArray = hasPrefix "a"; isArray = hasPrefix "a";
isDictionaryEntry = hasPrefix "{";
isMaybe = hasPrefix "m"; isMaybe = hasPrefix "m";
isTuple = hasPrefix "("; isTuple = hasPrefix "(";
# Returns the GVariant value that most closely matches the given Nix # Returns the GVariant value that most closely matches the given Nix
# value. If no GVariant value can be found then `null` is returned. # value. If no GVariant value can be found then `null` is returned.
#
# No support for dictionaries, maybe types, or variants.
mkValue = v: mkValue = v:
if builtins.isBool v then if builtins.isBool v then
mkBoolean v mkBoolean v
@ -105,6 +107,21 @@ in rec {
mkEmptyArray = elemType: mkArray elemType [ ]; mkEmptyArray = elemType: mkArray elemType [ ];
mkVariant = elem:
let gvarElem = mkValue elem;
in mkPrimitive type.variant gvarElem // {
__toString = self: "@${self.type} <${toString self.value}>";
};
mkDictionaryEntry = elems:
let
gvarElems = map mkValue elems;
dictionaryType = type.dictionaryEntryOf (map (e: e.type) gvarElems);
in mkPrimitive dictionaryType gvarElems // {
__toString = self:
"@${self.type} {${concatMapStringsSep "," toString self.value}}";
};
mkNothing = elemType: mkMaybe elemType null; mkNothing = elemType: mkMaybe elemType null;
mkJust = elem: let gvarElem = mkValue elem; in mkMaybe gvarElem.type gvarElem; mkJust = elem: let gvarElem = mkValue elem; in mkMaybe gvarElem.type gvarElem;

View file

@ -95,6 +95,10 @@ in rec {
mergeOneOption loc defs mergeOneOption loc defs
else if gvar.isMaybe sharedDefType && allChecked then else if gvar.isMaybe sharedDefType && allChecked then
mergeOneOption loc defs mergeOneOption loc defs
else if gvar.isDictionaryEntry sharedDefType && allChecked then
mergeOneOption loc defs
else if gvar.type.variant == sharedDefType && allChecked then
mergeOneOption loc defs
else if gvar.type.string == sharedDefType && allChecked then else if gvar.type.string == sharedDefType && allChecked then
types.str.merge loc defs types.str.merge loc defs
else if gvar.type.double == sharedDefType && allChecked then else if gvar.type.double == sharedDefType && allChecked then

View file

@ -50,6 +50,11 @@ in {
{ maybe1 = mkNothing type.string; } { maybe1 = mkNothing type.string; }
{ maybe2 = mkJust (mkUint32 4); } { maybe2 = mkJust (mkUint32 4); }
{ variant1 = mkVariant "foo"; }
{ variant2 = mkVariant 42; }
{ dictionaryEntry = mkDictionaryEntry [ 1 [ "foo" ] ]; }
]; ];
home.file."result.txt".text = let home.file."result.txt".text = let
@ -65,6 +70,7 @@ in {
array1 = @as ['one','two'] array1 = @as ['one','two']
array2 = @au [1,2] array2 = @au [1,2]
bool = true bool = true
dictionaryEntry = @{ias} {1,@as ['foo']}
emptyArray1 = @as [] emptyArray1 = @as []
emptyArray2 = @au [] emptyArray2 = @au []
escapedString = '\'\\\n' escapedString = '\'\\\n'
@ -79,6 +85,8 @@ in {
uint16 = @q 42 uint16 = @q 42
uint32 = @u 42 uint32 = @u 42
uint64 = @t 42 uint64 = @t 42
variant1 = @v <'foo'>
variant2 = @v <42>
'' ''
} }
''; '';