gdritter repos config-ini / a0a643b
Fix whitespace-stripping regression in standard API Getty Ritter 7 years ago
1 changed file(s) with 10 addition(s) and 7 deletion(s). Collapse all Expand all
203203 " in section " ++ show sec)
204204 Just x -> return x
205205
206 getVal :: IniValue -> Text
207 getVal = T.strip . vValue
208
206209 -- | Retrieve a field, failing if it doesn't exist, and return its raw value.
207210 --
208211 -- >>> parseIniFile "[MAIN]\nx = hello\n" $ section "MAIN" (field "x")
210213 -- >>> parseIniFile "[MAIN]\nx = hello\n" $ section "MAIN" (field "y")
211214 -- Left "Missing field \"y\" in section \"MAIN\""
212215 field :: Text -> SectionParser Text
213 field name = SectionParser $ vValue `fmap` rawField name
216 field name = SectionParser $ getVal `fmap` rawField name
214217
215218 -- | Retrieve a field and use the supplied parser to parse it as a value,
216219 -- failing if the field does not exist, or if the parser fails to
226229 fieldOf name parse = SectionParser $ do
227230 sec <- getSectionName
228231 val <- rawField name
229 case parse (vValue val) of
232 case parse (getVal val) of
230233 Left err -> addLineInformation (vLineNo val) sec (throw err)
231234 Right x -> return x
232235
237240 -- >>> parseIniFile "[MAIN]\nx = hello\n" $ section "MAIN" (fieldMb "y")
238241 -- Right Nothing
239242 fieldMb :: Text -> SectionParser (Maybe Text)
240 fieldMb name = SectionParser $ fmap vValue `fmap` rawFieldMb name
243 fieldMb name = SectionParser $ fmap getVal `fmap` rawFieldMb name
241244
242245 -- | Retrieve a field and parse it according to the given parser, returning
243246 -- @Nothing@ if it does not exist. If the parser fails, then this will
255258 mb <- rawFieldMb name
256259 case mb of
257260 Nothing -> return Nothing
258 Just v -> case parse (vValue v) of
261 Just v -> case parse (getVal v) of
259262 Left err -> addLineInformation (vLineNo v) sec (throw err)
260263 Right x -> return (Just x)
261264
269272 fieldDef name def = SectionParser $ ExceptT $ \m ->
270273 case lkp (normalize name) (isVals m) of
271274 Nothing -> return def
272 Just x -> return (vValue x)
275 Just x -> return (getVal x)
273276
274277 -- | Retrieve a field, parsing it according to the given parser, and returning
275278 -- a default value if it does not exist. If the parser fails, then this will
287290 mb <- rawFieldMb name
288291 case mb of
289292 Nothing -> return def
290 Just v -> case parse (vValue v) of
293 Just v -> case parse (getVal v) of
291294 Left err -> addLineInformation (vLineNo v) sec (throw err)
292295 Right x -> return x
293296
395398 -- >>> listWithSeparator ":" string "/bin:/usr/bin" :: Either String [FilePath]
396399 -- Right ["/bin","/usr/bin"]
397400 -- >>> listWithSeparator "," number "7 8 9" :: Either String [Int]
398 -- Left "Unable to parse \"2 3 4\" as a value of type Int"
401 -- Left "Unable to parse \"7 8 9\" as a value of type Int"
399402 listWithSeparator :: (IsList l)
400403 => Text
401404 -> (Text -> Either String (Item l))