gdritter repos config-ini / f724933
Some doc changes Getty Ritter 8 years ago
1 changed file(s) with 17 addition(s) and 9 deletion(s). Collapse all Expand all
6565 runExceptT mote ini
6666
6767 -- | Find a named section in the INI file and parse it with the provided
68 -- section parser, failing if the section does not exist.
68 -- section parser, failing if the section does not exist. In order to
69 -- support classic INI files with capitalized section names, section
70 -- lookup is __case-insensitive__.
6971 --
7072 -- >>> parseIniFile "[ONE]\nx = hello\n" $ section "ONE" (field "x")
7173 -- Right "hello"
72 -- >>> parseIniFile "[ONE]\nx = hello\n" $ section "TWO" (field "y")
74 -- >>> parseIniFile "[ONE]\nx = hello\n" $ section "TWO" (field "x")
7375 -- Left "No top-level section named \"TWO\""
7476 section :: Text -> SectionParser a -> IniParser a
7577 section name (SectionParser thunk) = IniParser $ ExceptT $ \(Ini ini) ->
7981
8082 -- | Find a named section in the INI file and parse it with the provided
8183 -- section parser, returning 'Nothing' if the section does not exist.
84 -- In order to
85 -- support classic INI files with capitalized section names, section
86 -- lookup is __case-insensitive__.
8287 --
8388 -- >>> parseIniFile "[ONE]\nx = hello\n" $ sectionMb "ONE" (field "x")
8489 -- Right (Just "hello")
85 -- >>> parseIniFile "[ONE]\nx = hello\n" $ sectionMb "TWO" (field "y")
90 -- >>> parseIniFile "[ONE]\nx = hello\n" $ sectionMb "TWO" (field "x")
8691 -- Right Nothing
8792 sectionMb :: Text -> SectionParser a -> IniParser (Maybe a)
8893 sectionMb name (SectionParser thunk) = IniParser $ ExceptT $ \(Ini ini) ->
9297
9398 -- | Find a named section in the INI file and parse it with the provided
9499 -- section parser, returning a default value if the section does not exist.
100 -- In order to
101 -- support classic INI files with capitalized section names, section
102 -- lookup is __case-insensitive__.
95103 --
96104 -- >>> parseIniFile "[ONE]\nx = hello\n" $ sectionDef "ONE" "def" (field "x")
97105 -- Right "hello"
98 -- >>> parseIniFile "[ONE]\nx = hello\n" $ sectionDef "TWO" "def" (field "y")
106 -- >>> parseIniFile "[ONE]\nx = hello\n" $ sectionDef "TWO" "def" (field "x")
99107 -- Right "def"
100108 sectionDef :: Text -> a -> SectionParser a -> IniParser a
101109 sectionDef name def (SectionParser thunk) = IniParser $ ExceptT $ \(Ini ini) ->
140148 -- >>> parseIniFile "[MAIN]\nx = 72\n" $ section "MAIN" (fieldOf "x" number)
141149 -- Right 72
142150 -- >>> parseIniFile "[MAIN]\nx = hello\n" $ section "MAIN" (fieldOf "x" number)
143 -- Left "Line 2, in section \"main\": Unable to parse \"hello\" as a value of type Integer"
151 -- Left "Line 2, in section \"MAIN\": Unable to parse \"hello\" as a value of type Integer"
144152 -- >>> parseIniFile "[MAIN]\nx = 72\n" $ section "MAIN" (fieldOf "y" number)
145 -- Left "Missing field \"y\" in section \"main\""
153 -- Left "Missing field \"y\" in section \"MAIN\""
146154 fieldOf :: Text -> (Text -> Either String a) -> SectionParser a
147155 fieldOf name parse = SectionParser $ do
148156 sec <- getSectionName
167175 -- >>> parseIniFile "[MAIN]\nx = 72\n" $ section "MAIN" (fieldMbOf "x" number)
168176 -- Right 72
169177 -- >>> parseIniFile "[MAIN]\nx = hello\n" $ section "MAIN" (fieldMbOf "x" number)
170 -- Left "Line 2, in section \"main\": Unable to parse \"hello\" as a value of type Integer"
178 -- Left "Line 2, in section \"MAIN\": Unable to parse \"hello\" as a value of type Integer"
171179 -- >>> parseIniFile "[MAIN]\nx = 72\n" $ section "MAIN" (fieldMbOf "y" number)
172180 -- Right Nothing
173181 fieldMbOf :: Text -> (Text -> Either String a) -> SectionParser (Maybe a)
199207 -- >>> parseIniFile "[MAIN]\nx = 72\n" $ section "MAIN" (fieldDefOf "x" number 99)
200208 -- Right 72
201209 -- >>> parseIniFile "[MAIN]\nx = hello\n" $ section "MAIN" (fieldDefOf "x" number 99)
202 -- Left "Line 2, in section \"main\": Unable to parse \"hello\" as a value of type Integer"
210 -- Left "Line 2, in section \"MAIN\": Unable to parse \"hello\" as a value of type Integer"
203211 -- >>> parseIniFile "[MAIN]\nx = 72\n" $ section "MAIN" (fieldDefOf "y" number 99)
204212 -- Right 99
205213 fieldDefOf :: Text -> (Text -> Either String a) -> a -> SectionParser a
228236 -- >>> parseIniFile "[MAIN]\nx = yes\n" $ section "MAIN" (fieldFlagDef "x" False)
229237 -- Right True
230238 -- >>> parseIniFile "[MAIN]\nx = hello\n" $ section "MAIN" (fieldFlagDef "x" False)
231 -- Left "Line 2, in section \"main\": Unable to parse \"hello\" as a boolean"
239 -- Left "Line 2, in section \"MAIN\": Unable to parse \"hello\" as a boolean"
232240 -- >>> parseIniFile "[MAIN]\nx = yes\n" $ section "MAIN" (fieldFlagDef "y" False)
233241 -- Right False
234242 fieldFlagDef :: Text -> Bool -> SectionParser Bool