Some doc changes
Getty Ritter
9 years ago
| 65 | 65 | runExceptT mote ini |
| 66 | 66 | |
| 67 | 67 | -- | 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__. | |
| 69 | 71 | -- |
| 70 | 72 | -- >>> parseIniFile "[ONE]\nx = hello\n" $ section "ONE" (field "x") |
| 71 | 73 | -- Right "hello" |
| 72 |
-- >>> parseIniFile "[ONE]\nx = hello\n" $ section "TWO" (field " |
|
| 74 | -- >>> parseIniFile "[ONE]\nx = hello\n" $ section "TWO" (field "x") | |
| 73 | 75 | -- Left "No top-level section named \"TWO\"" |
| 74 | 76 | section :: Text -> SectionParser a -> IniParser a |
| 75 | 77 | section name (SectionParser thunk) = IniParser $ ExceptT $ \(Ini ini) -> |
| 79 | 81 | |
| 80 | 82 | -- | Find a named section in the INI file and parse it with the provided |
| 81 | 83 | -- 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__. | |
| 82 | 87 | -- |
| 83 | 88 | -- >>> parseIniFile "[ONE]\nx = hello\n" $ sectionMb "ONE" (field "x") |
| 84 | 89 | -- Right (Just "hello") |
| 85 |
-- >>> parseIniFile "[ONE]\nx = hello\n" $ sectionMb "TWO" (field " |
|
| 90 | -- >>> parseIniFile "[ONE]\nx = hello\n" $ sectionMb "TWO" (field "x") | |
| 86 | 91 | -- Right Nothing |
| 87 | 92 | sectionMb :: Text -> SectionParser a -> IniParser (Maybe a) |
| 88 | 93 | sectionMb name (SectionParser thunk) = IniParser $ ExceptT $ \(Ini ini) -> |
| 92 | 97 | |
| 93 | 98 | -- | Find a named section in the INI file and parse it with the provided |
| 94 | 99 | -- 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__. | |
| 95 | 103 | -- |
| 96 | 104 | -- >>> parseIniFile "[ONE]\nx = hello\n" $ sectionDef "ONE" "def" (field "x") |
| 97 | 105 | -- Right "hello" |
| 98 |
-- >>> parseIniFile "[ONE]\nx = hello\n" $ sectionDef "TWO" "def" (field " |
|
| 106 | -- >>> parseIniFile "[ONE]\nx = hello\n" $ sectionDef "TWO" "def" (field "x") | |
| 99 | 107 | -- Right "def" |
| 100 | 108 | sectionDef :: Text -> a -> SectionParser a -> IniParser a |
| 101 | 109 | sectionDef name def (SectionParser thunk) = IniParser $ ExceptT $ \(Ini ini) -> |
| 140 | 148 | -- >>> parseIniFile "[MAIN]\nx = 72\n" $ section "MAIN" (fieldOf "x" number) |
| 141 | 149 | -- Right 72 |
| 142 | 150 | -- >>> parseIniFile "[MAIN]\nx = hello\n" $ section "MAIN" (fieldOf "x" number) |
| 143 |
-- Left "Line 2, in section \" |
|
| 151 | -- Left "Line 2, in section \"MAIN\": Unable to parse \"hello\" as a value of type Integer" | |
| 144 | 152 | -- >>> parseIniFile "[MAIN]\nx = 72\n" $ section "MAIN" (fieldOf "y" number) |
| 145 |
-- Left "Missing field \"y\" in section \" |
|
| 153 | -- Left "Missing field \"y\" in section \"MAIN\"" | |
| 146 | 154 | fieldOf :: Text -> (Text -> Either String a) -> SectionParser a |
| 147 | 155 | fieldOf name parse = SectionParser $ do |
| 148 | 156 | sec <- getSectionName |
| 167 | 175 | -- >>> parseIniFile "[MAIN]\nx = 72\n" $ section "MAIN" (fieldMbOf "x" number) |
| 168 | 176 | -- Right 72 |
| 169 | 177 | -- >>> parseIniFile "[MAIN]\nx = hello\n" $ section "MAIN" (fieldMbOf "x" number) |
| 170 |
-- Left "Line 2, in section \" |
|
| 178 | -- Left "Line 2, in section \"MAIN\": Unable to parse \"hello\" as a value of type Integer" | |
| 171 | 179 | -- >>> parseIniFile "[MAIN]\nx = 72\n" $ section "MAIN" (fieldMbOf "y" number) |
| 172 | 180 | -- Right Nothing |
| 173 | 181 | fieldMbOf :: Text -> (Text -> Either String a) -> SectionParser (Maybe a) |
| 199 | 207 | -- >>> parseIniFile "[MAIN]\nx = 72\n" $ section "MAIN" (fieldDefOf "x" number 99) |
| 200 | 208 | -- Right 72 |
| 201 | 209 | -- >>> parseIniFile "[MAIN]\nx = hello\n" $ section "MAIN" (fieldDefOf "x" number 99) |
| 202 |
-- Left "Line 2, in section \" |
|
| 210 | -- Left "Line 2, in section \"MAIN\": Unable to parse \"hello\" as a value of type Integer" | |
| 203 | 211 | -- >>> parseIniFile "[MAIN]\nx = 72\n" $ section "MAIN" (fieldDefOf "y" number 99) |
| 204 | 212 | -- Right 99 |
| 205 | 213 | fieldDefOf :: Text -> (Text -> Either String a) -> a -> SectionParser a |
| 228 | 236 | -- >>> parseIniFile "[MAIN]\nx = yes\n" $ section "MAIN" (fieldFlagDef "x" False) |
| 229 | 237 | -- Right True |
| 230 | 238 | -- >>> parseIniFile "[MAIN]\nx = hello\n" $ section "MAIN" (fieldFlagDef "x" False) |
| 231 |
-- Left "Line 2, in section \" |
|
| 239 | -- Left "Line 2, in section \"MAIN\": Unable to parse \"hello\" as a boolean" | |
| 232 | 240 | -- >>> parseIniFile "[MAIN]\nx = yes\n" $ section "MAIN" (fieldFlagDef "y" False) |
| 233 | 241 | -- Right False |
| 234 | 242 | fieldFlagDef :: Text -> Bool -> SectionParser Bool |