Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve support for typed values #94

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
9 changes: 9 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,12 @@ 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 []
-- In home-manager, arrays are always typed when using @mkArray@.
renderValue' (Ty ('a':t) (L v)) = "mkArray " <> T.pack (show t) <> " " <> renderList v
-- TODO: add mkTyped to h-m
renderValue' (Ty t v) = "mkTyped " <> T.pack (show t) <> " " <> renderItem v
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