gdritter repos config-ini / b2e882b
Documentation for raw types Getty Ritter 6 years ago
1 changed file(s) with 48 addition(s) and 5 deletion(s). Collapse all Expand all
11 module Data.Ini.Config.Raw
2 ( Ini(..)
2 ( -- $main
3 -- * INI types
4 Ini(..)
35 , IniSection(..)
46 , IniValue(..)
57 , BlankLine(..)
8 -- * serializing and deserializing
69 , parseIni
710 , printIni
811 ) where
2023 import Text.Megaparsec.Text
2124
2225 -- | An 'Ini' value is a mapping from section names to
23 -- 'IniSection' values.
26 -- 'IniSection' values. The section names in this mapping are
27 -- normalized to lower-case and stripped of whitespace. This
28 -- sequence retains the ordering of the original source file.
2429 newtype Ini = Ini
2530 { fromIni :: Seq (Text, IniSection)
2631 } deriving (Eq, Show)
2732
2833 -- | An 'IniSection' consists of a name, a mapping of key-value pairs,
29 -- and metadata about where the section starts and ends in the file.
34 -- and metadata about where the section starts and ends in the
35 -- file. The section names found in 'isName' are *not* normalized to
36 -- lower-case or stripped of whitespace, and thus should appear
37 -- exactly as they appear in the original source file.
3038 data IniSection = IniSection
3139 { isName :: Text
40 -- ^ The name of the section, as it appears in the
41 -- original INI source
3242 , isVals :: Seq (Text, IniValue)
43 -- ^ The key-value mapping within that section. Key
44 -- names here are normalized to lower-case and
45 -- stripped of whitespace. This sequence retains
46 -- the ordering of the original source file.
3347 , isStartLine :: Int
48 -- ^ The line on which the section begins. This
49 -- field is ignored when serializing, and is only
50 -- used for error messages produced when parsing
51 -- and deserializing an INI structure.
3452 , isEndLine :: Int
53 -- ^ The line on which the section ends. This field
54 -- is ignored when serializing, and is only used
55 -- for error messages produced when parsing and
56 -- deserializing an INI structure.
3557 , isComments :: Seq BlankLine
58 -- ^ The blank lines and comments that appear prior
59 -- to the section head declaration, retained for
60 -- pretty-printing identical INI files.
3661 } deriving (Eq, Show)
3762
3863 -- | An 'IniValue' represents a key-value mapping, and also stores the
39 -- line number where it appears.
64 -- line number where it appears. The key names and values found in
65 -- 'vName' and 'vValue' respectively are _not_ normalized to
66 -- lower-case or stripped of whitespace, and thus should appear
67 -- exactly as they appear in the original source file.
4068 data IniValue = IniValue
4169 { vLineNo :: Int
70 -- ^ The line on which the key/value mapping
71 -- appears. This field is ignored when
72 -- serializing, and is only used for error
73 -- messages produced when parsing and
74 -- deserializing an INI structure.
4275 , vName :: Text
76 -- ^ The name of the key, as it appears in the INI source.
4377 , vValue :: Text
78 -- ^ The value of the key
4479 , vComments :: Seq BlankLine
4580 , vCommentedOut :: Bool
4681 -- ^ Right now, this will never show up in a parsed INI file, but
5792 | BlankLine
5893 deriving (Eq, Show)
5994
60 -- | Parse a 'Text' value into an 'Ini' value.
95 -- | Parse a 'Text' value into an 'Ini' value, retaining a maximal
96 -- amount of structure as needed to reconstruct the original INI file.
6197 parseIni :: Text -> Either String Ini
6298 parseIni t = case runParser pIni "ini file" t of
6399 Left err -> Left (parseErrorPretty err)
134170 getCurrentLine = (fromIntegral . unPos . sourceLine) `fmap` getPosition
135171
136172
173 -- | Serialize an INI file to text, complete with any comments which
174 -- appear in the INI structure, and retaining the aesthetic details
175 -- which are present in the INI file.
137176 printIni :: Ini -> Text
138177 printIni = LazyText.toStrict . Builder.toLazyText . F.foldMap build . fromIni
139178 where
153192 Builder.singleton (vDelimiter val) <>
154193 Builder.fromText (vValue val) <>
155194 Builder.singleton '\n'
195
196 -- | $main
197 -- This module is subject to change in the future, and therefore
198 -- should not be relied upon to have a consistent API.