Fix whitespace-stripping regression in standard API
Getty Ritter
7 years ago
203 | 203 | " in section " ++ show sec) |
204 | 204 | Just x -> return x |
205 | 205 | |
206 | getVal :: IniValue -> Text | |
207 | getVal = T.strip . vValue | |
208 | ||
206 | 209 | -- | Retrieve a field, failing if it doesn't exist, and return its raw value. |
207 | 210 | -- |
208 | 211 | -- >>> parseIniFile "[MAIN]\nx = hello\n" $ section "MAIN" (field "x") |
210 | 213 | -- >>> parseIniFile "[MAIN]\nx = hello\n" $ section "MAIN" (field "y") |
211 | 214 | -- Left "Missing field \"y\" in section \"MAIN\"" |
212 | 215 | field :: Text -> SectionParser Text |
213 |
field name = SectionParser $ |
|
216 | field name = SectionParser $ getVal `fmap` rawField name | |
214 | 217 | |
215 | 218 | -- | Retrieve a field and use the supplied parser to parse it as a value, |
216 | 219 | -- failing if the field does not exist, or if the parser fails to |
226 | 229 | fieldOf name parse = SectionParser $ do |
227 | 230 | sec <- getSectionName |
228 | 231 | val <- rawField name |
229 |
case parse ( |
|
232 | case parse (getVal val) of | |
230 | 233 | Left err -> addLineInformation (vLineNo val) sec (throw err) |
231 | 234 | Right x -> return x |
232 | 235 | |
237 | 240 | -- >>> parseIniFile "[MAIN]\nx = hello\n" $ section "MAIN" (fieldMb "y") |
238 | 241 | -- Right Nothing |
239 | 242 | fieldMb :: Text -> SectionParser (Maybe Text) |
240 |
fieldMb name = SectionParser $ fmap |
|
243 | fieldMb name = SectionParser $ fmap getVal `fmap` rawFieldMb name | |
241 | 244 | |
242 | 245 | -- | Retrieve a field and parse it according to the given parser, returning |
243 | 246 | -- @Nothing@ if it does not exist. If the parser fails, then this will |
255 | 258 | mb <- rawFieldMb name |
256 | 259 | case mb of |
257 | 260 | Nothing -> return Nothing |
258 |
Just v -> case parse ( |
|
261 | Just v -> case parse (getVal v) of | |
259 | 262 | Left err -> addLineInformation (vLineNo v) sec (throw err) |
260 | 263 | Right x -> return (Just x) |
261 | 264 | |
269 | 272 | fieldDef name def = SectionParser $ ExceptT $ \m -> |
270 | 273 | case lkp (normalize name) (isVals m) of |
271 | 274 | Nothing -> return def |
272 |
Just x -> return ( |
|
275 | Just x -> return (getVal x) | |
273 | 276 | |
274 | 277 | -- | Retrieve a field, parsing it according to the given parser, and returning |
275 | 278 | -- a default value if it does not exist. If the parser fails, then this will |
287 | 290 | mb <- rawFieldMb name |
288 | 291 | case mb of |
289 | 292 | Nothing -> return def |
290 |
Just v -> case parse ( |
|
293 | Just v -> case parse (getVal v) of | |
291 | 294 | Left err -> addLineInformation (vLineNo v) sec (throw err) |
292 | 295 | Right x -> return x |
293 | 296 | |
395 | 398 | -- >>> listWithSeparator ":" string "/bin:/usr/bin" :: Either String [FilePath] |
396 | 399 | -- Right ["/bin","/usr/bin"] |
397 | 400 | -- >>> listWithSeparator "," number "7 8 9" :: Either String [Int] |
398 |
-- Left "Unable to parse \" |
|
401 | -- Left "Unable to parse \"7 8 9\" as a value of type Int" | |
399 | 402 | listWithSeparator :: (IsList l) |
400 | 403 | => Text |
401 | 404 | -> (Text -> Either String (Item l)) |