gdritter repos ndbl / 163f0ed
Removed multimap repr in anticipation of better tooling Getty Ritter 9 years ago
2 changed file(s) with 26 addition(s) and 63 deletion(s). Collapse all Expand all
22 -- utilities used in the Plan 9 operating system. An NDBL file is a sequence
33 -- of groups, where each group is a multiset of key-value pairs. This will
44 -- cover the basics of NDBL; for a more in-depth explanation, consult
5 -- [https://github.com/aisamanra/ndbl](the github page).
5 -- <https://github.com/aisamanra/ndbl the github page>.
66 --
77 -- Grouping in NDBL is done by
88 -- indentation: a new group is started by listing a key-value pair without
3232 -- file=file1.txt
3333 -- file=file2.txt
3434 -- @
35 --
36 -- Be aware that NDBL guarantees that
37 --
38 -- @
39 -- fromJust . decode . encode == id
40 -- @
41 --
42 -- but does NOT guarantee that
43 --
44 -- @
45 -- encode . fromJust . decode == id
46 -- @
3547
36 module Data.NDBL ( -- * Convenience Types
37 NDBL
38 , Pair
39 -- * MultiMap Representation
40 -- $mm
41 , decode
48 module Data.NDBL ( decode
4249 , encode
43 -- * List-Of-List Representation
44 -- $ll
45 , decodeList
46 , encodeList
47 -- * Flat List Representation
48 -- $fl
49 , decodeFlat
50 , encodeFlat
5150 ) where
5251
53 import Data.MultiMap (MultiMap)
54 import qualified Data.MultiMap as M
55 import Data.Text (Text)
52 import Data.Text (Text)
5653
5754 import Data.NDBL.Parse
5855 import Data.NDBL.Print
5956
60 type NDBL = [MultiMap Text Text]
61 type Pair = (Text, Text)
62
63 -- $mm
64 -- The most convenient way of parsing an NDBL file is as a
65 -- multimap. In this case, the set of groups is given in-order
66 -- as a list, and each individual group is represented as a
67 -- multimap with text keys and values.
68 --
69 -- This does mean that the empty string is a possible value for the multimap,
70 -- although all the keys will be at least one character long.
71
72 decode :: Text -> Maybe NDBL
57 -- | Decode an NDBL document to a list of lists of key-value pairs.
58 decode :: Text -> Maybe [[(Text, Text)]]
7359 decode t = case pNDBL t of
74 Right r -> Just (map M.fromList r)
75 Left _ -> Nothing
76
77 encode :: NDBL -> Text
78 encode = pretty . map M.toList
79
80 -- $ll
81 -- Not every application wants to bring in a dependency on the
82 -- multimap package, so functions that deal with lists of lists
83 -- of tuples are also provided.
84
85 decodeList :: Text -> Maybe [[Pair]]
86 decodeList t = case pNDBL t of
8760 Right r -> Just r
8861 Left _ -> Nothing
8962
90 encodeList :: [[Pair]] -> Text
91 encodeList = pretty
63 -- | Decode an NDBL document to a list of lists of key-value pairs,
64 -- supplying the parse error from "attoparsec" if decoding fails.
65 decodeEither :: Text -> Either String [[(Text, Text)]]
66 decodeEither = pNDBL
9267
93 -- $fl
94 -- Often a config file doesn't need grouping, so these are offered
95 -- for pure utility.
96
97 decodeFlat :: Text -> Maybe [Pair]
98 decodeFlat t = case pNDBL t of
99 Right r -> Just $ concat r
100 Left _ -> Nothing
101
102 encodeFlat :: [Pair] -> Text
103 encodeFlat = pretty . map (:[])
68 -- | Encode an NDBL document to its 'Text' representation.
69 encode :: [[(Text, Text)]] -> Text
70 encode = pretty
1 -- Initial ndb-like.cabal generated by cabal init. For further
2 -- documentation, see http://haskell.org/cabal/users-guide/
3
41 name: ndbl
5 version: 0.1.0.0
2 version: 0.1.0.1
63 synopsis: A simple interface to the NDBL config format.
74 description: A simplistic interface to a config file format
85 meant to resemble the configuration file of
1916 exposed-modules: Data.NDBL
2017 other-modules: Data.NDBL.Parse, Data.NDBL.Print
2118 build-depends: base >=4.6 && <4.7,
22 multimap,
2319 attoparsec,
2420 text,
2521 pretty
26 default-language: Haskell2010
22 default-language: Haskell2010