From c60459fe17955e03b02b7d7b4c61440c94179c03 Mon Sep 17 00:00:00 2001 From: Gabriel Volpe Date: Thu, 10 Jun 2021 12:46:17 +0200 Subject: [PATCH] Feature: JSON parser & renderer --- data/json.settings | 3 +++ output/json.nix | 17 +++++++++++++++++ src/DConf.hs | 9 ++++++++- src/DConf/Data.hs | 1 + src/Nix.hs | 11 ++++++++--- 5 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 data/json.settings create mode 100644 output/json.nix diff --git a/data/json.settings b/data/json.settings new file mode 100644 index 0000000..997fcb4 --- /dev/null +++ b/data/json.settings @@ -0,0 +1,3 @@ +[org/gnome/shell/extensions/sound-output-device-chooser] +hide-on-single-device=true +ports-settings='{"version":2,"ports":[]}' diff --git a/output/json.nix b/output/json.nix new file mode 100644 index 0000000..facb565 --- /dev/null +++ b/output/json.nix @@ -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":[]} + ''; + }; + + }; +} diff --git a/src/DConf.hs b/src/DConf.hs index dabefc7..831bb00 100644 --- a/src/DConf.hs +++ b/src/DConf.hs @@ -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 []") @@ -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 '=') diff --git a/src/DConf/Data.hs b/src/DConf/Data.hs index fa3d548..fde4bd6 100644 --- a/src/DConf/Data.hs +++ b/src/DConf/Data.hs @@ -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) diff --git a/src/Nix.hs b/src/Nix.hs index 459cc20..ac2fc45 100644 --- a/src/Nix.hs +++ b/src/Nix.hs @@ -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 @@ -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 <> "''"