Skip to content

Commit

Permalink
Improve support for typed values
Browse files Browse the repository at this point in the history
Using new `mkTyped` constructor, not yet supported by home-manager.
dict is also still broken for now.
  • Loading branch information
jtojnar committed Apr 15, 2024
1 parent d4d731d commit 0c4d4ef
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 5 deletions.
10 changes: 10 additions & 0 deletions data/typed.settings
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[typed]
empty-dict=@a{sv} {}
empty-array-dict=@a{sv} []
just-str=@ms 'hello'
ui32=uint32 7
at-u=@u 5
empty-arr=@a(dd) []
just-empty-str=@ms ""
doubletime=uint32 uint32 7
var-empty-arr=<@ai []>
2 changes: 1 addition & 1 deletion output/dconf.nix
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ with lib.hm.gvariant;
};

"org/gnome/shell/world-clocks" = {
locations = [];
locations = mkArray "v" [];
};

"org/gnome/software" = {
Expand Down
22 changes: 22 additions & 0 deletions output/typed.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated via dconf2nix: https://github.com/nix-commmunity/dconf2nix
{ lib, ... }:

with lib.hm.gvariant;

{
dconf.settings = {
"typed" = {
at-u = mkTyped "u" 5;
doubletime = mkUint32 7;
empty-arr = mkArray "(dd)" [];
empty-array-dict = mkArray "{sv}" [];
empty-dict = mkTyped "a{sv}" {
};
just-empty-str = mkTyped "ms" "";
just-str = mkTyped "ms" "hello";
ui32 = mkUint32 7;
var-empty-arr = mkVariant (mkArray "i" []);
};

};
}
2 changes: 1 addition & 1 deletion output/variant.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ with lib.hm.gvariant;
string = mkVariant "#polari";
true = mkVariant true;
tuple = mkVariant (mkTuple [ (mkUint32 2) "ABC" ]);
typed = mkVariant [];
typed = mkVariant (mkArray "i" []);
uint32 = mkVariant (mkUint32 2);
variant = mkVariant (mkVariant 7);
};
Expand Down
30 changes: 27 additions & 3 deletions src/DConf.hs
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,37 @@ vVariant = fmap V $ bracket "<" ">" value
vList :: Parsec Text () Value
vList = fmap L $ bracket "[" "]" $ commaSeparated $ value

-- https://gitlab.gnome.org/GNOME/glib/-/blob/4607dd77a1944c6b29dff1e76c195f28a8ca12af/docs/reference/glib/gvariant-specification-1.0.rst#type-strings
typeString :: Parsec Text () String
typeString = baseType <|> containerType
where
baseType = choice (map string ["b", "y", "n", "q", "i", "u", "x", "t", "h", "d", "s", "o", "g"])
containerType = variantType <|> maybeType <|> arrayType <|> tupleType <|> dictEntryType
types = many typeString
variantType = string "v"
arrayType = do
a <- string "a"
t <- typeString
return (a ++ t)
maybeType = do
m <- string "m"
t <- typeString
return (m ++ t)
tupleType = do
tt <- bracket "(" ")" types
return ("(" ++ concat tt ++ ")")
dictEntryType = bracket "{" "}" $ do
k <- baseType
v <- typeString
return ("{" ++ k ++ v ++ "}")

vTyped :: Parsec Text () Value
vTyped = do
_ <- char '@'
_ <- anyChar
_ <- anyChar
t <- typeString
_ <- spaces
value
v <- value
return (Ty t v)

vJson :: Parsec Text () Value
vJson = try $ bracket "'" "'" $ do
Expand Down
1 change: 1 addition & 0 deletions src/DConf/Data.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ data Value = S Text -- String
| I64 Int -- Int64
| D Double -- Double
| T [Value] -- Tuple of n-arity
| Ty String Value -- Typed value
| L [Value] -- List of values
| V Value -- Variant
| R [(Text,Value)] -- Record
Expand Down
7 changes: 7 additions & 0 deletions src/Nix.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ renderValue raw = Nix $ renderValue' raw <> ";"
needsParen (D x) = x < 0
needsParen (I32 _) = True
needsParen (T _) = True
-- will be rendered as @[]@
needsParen (Ty "as" (L [])) = False
needsParen (Ty _ _) = True
needsParen (V _) = True
needsParen _ = False

Expand All @@ -77,6 +80,10 @@ renderValue raw = Nix $ renderValue' raw <> ";"
renderValue' (I64 v) = "mkInt64 " <> T.pack (show v)
renderValue' (L xs) = renderList xs
renderValue' (T xs) = "mkTuple " <> renderList xs
-- In home-manager, @mkValue []@ emits @\@as []@
renderValue' (Ty "as" (L [])) = renderList []
renderValue' (Ty ('a':t) (L v)) = "mkArray " <> T.pack (show t) <> " " <> renderList v
renderValue' (Ty t v) = "mkTyped " <> T.pack (show t) <> " " <> renderItem v -- TODO: add mkTyped to h-m
renderValue' (V v) = "mkVariant " <> renderItem v
renderValue' (Json v) =
"''\n" <> mkSpaces 8 <> T.strip v <> "\n" <> mkSpaces 6 <> "''"
Expand Down
11 changes: 11 additions & 0 deletions test/DConf2NixTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,17 @@ prop_dconf2nix_tuples :: Property
prop_dconf2nix_tuples =
withTests (10 :: TestLimit) dconf2nixTuples

dconf2nixTyped :: Property
dconf2nixTyped =
let input = "data/typed.settings"
output = "output/typed.nix"
root = Root T.empty
in baseProperty input output root

prop_dconf2nix_typed :: Property
prop_dconf2nix_typed =
withTests (10 :: TestLimit) dconf2nixTyped

dconf2nixUnicode :: Property
dconf2nixUnicode =
let input = "data/unicode.settings"
Expand Down

0 comments on commit 0c4d4ef

Please sign in to comment.