Skip to content

Commit

Permalink
Feature: JSON parser & renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
gvolpe committed Jun 10, 2021
1 parent 6bf3d7d commit c60459f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
3 changes: 3 additions & 0 deletions data/json.settings
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[org/gnome/shell/extensions/sound-output-device-chooser]
hide-on-single-device=true
ports-settings='{"version":2,"ports":[]}'
17 changes: 17 additions & 0 deletions output/json.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated via dconf2nix: https://github.com/gvolpe/dconf2nix
{ lib, ... }:

let
mkTuple = lib.hm.gvariant.mkTuple;
in
{
dconf.settings = {
"org/gnome/shell/extensions/sound-output-device-chooser" = {
hide-on-single-device = true;
ports-settings = ''
{"version":2,"ports":[]}
'';
};

};
}
9 changes: 8 additions & 1 deletion src/DConf.hs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ vList = try $ do
((vTupleInList <|> dconf) `sepBy` (string "," >> spaces))
(char ']')

vJson :: Parsec Text () Value
vJson = try $ do
try (lookAhead $ string "'{")
char '\''
js <- manyTill anyToken (try $ lookAhead $ char '\'')
Json (T.pack js) <$ char '\''

vEmptyList :: Parsec Text () Value
vEmptyList =
EmptyList <$ try (string "@as []")
Expand All @@ -105,7 +112,7 @@ dconfHeader = do
where tokens = choice $ many1 <$> [char '/', char '-', char ':', alphaNum]

dconfValue :: Parsec Text () Value
dconfValue = vListOfVariant <|> vList <|> vEmptyList <|> dconf
dconfValue = vListOfVariant <|> vList <|> vEmptyList <|> vJson <|> dconf

vKey :: Parsec Text () Key
vKey = Key . T.pack <$> manyTill (choice [alphaNum, char '-']) (char '=')
Expand Down
1 change: 1 addition & 0 deletions src/DConf/Data.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ data Value = S Text -- String
| T Value Value -- Tuple
| TL Value Value -- Tuple within a list
| L [Value] -- List of values
| Json Text -- Json value
| EmptyList -- Empty list (aka '@as []')
deriving (Eq, Show)

Expand Down
11 changes: 8 additions & 3 deletions src/Nix.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ normalizeHeader :: Header -> Root -> Header
normalizeHeader "/" (Root r) = T.dropWhileEnd (== '/') (normalizeRoot r)
normalizeHeader h (Root r) = normalizeRoot r <> h

mkSpaces :: Int -> T.Text
mkSpaces = T.pack . (flip replicate ' ')

renderEntry :: Entry -> Root -> Nix
renderEntry (Entry h c) root =
let header = " \"" <> normalizeHeader h root <> "\" = {\n"
let header = mkSpaces 4 <> "\"" <> normalizeHeader h root <> "\" = {\n"
body = Map.toList c >>= \(Key k, v) ->
T.unpack $ " " <> k <> " = " <> unNix (renderValue v) <> "\n"
close = " };\n\n"
T.unpack $ mkSpaces 6 <> k <> " = " <> unNix (renderValue v) <> "\n"
close = mkSpaces 4 <> "};\n\n"
in Nix $ header <> T.pack body <> close

renderValue :: Value -> Nix
Expand All @@ -65,3 +68,5 @@ renderValue raw = Nix $ renderValue' raw <> ";"
renderValue' (L xs) =
let ls = T.concat ((<> " ") <$> (renderValue' <$> xs)) in "[ " <> ls <> "]"
renderValue' EmptyList = "[]"
renderValue' (Json v) =
"''\n" <> mkSpaces 8 <> T.strip v <> "\n" <> mkSpaces 6 <> "''"

0 comments on commit c60459f

Please sign in to comment.