Raw: add functions to look up section and key information
Jonathan Daugherty
7 years ago
9 | 9 | -- * serializing and deserializing |
10 | 10 | , parseIni |
11 | 11 | , printIni |
12 | -- * inspection | |
13 | , lookupInSection | |
14 | , lookupSection | |
15 | , lookupValue | |
12 | 16 | ) where |
13 | 17 | |
14 | 18 | import Control.Monad (void) |
195 | 199 | Builder.fromText (vValue val) <> |
196 | 200 | Builder.singleton '\n' |
197 | 201 | |
202 | -- | Normalize a section name. | |
203 | normalizeSectionName :: Text -> Text | |
204 | normalizeSectionName = T.toLower . T.strip | |
205 | ||
206 | -- | Normalize a key. | |
207 | normalizeKey :: Text -> Text | |
208 | normalizeKey = normalizeSectionName | |
209 | ||
210 | -- | Look up an Ini value by section name and key. Returns Nothing if | |
211 | -- either the section or key could not be found. | |
212 | lookupInSection :: Text | |
213 | -- ^ The section name. Will be normalized prior to | |
214 | -- comparison. | |
215 | -> Text | |
216 | -- ^ The key. Will be normalized prior to comparison. | |
217 | -> Ini | |
218 | -- ^ The Ini to search. | |
219 | -> Seq.Seq Text | |
220 | lookupInSection sec opt ini = | |
221 | vValue <$> snd <$> (F.asum (lookupValue opt <$> snd <$> lookupSection sec ini)) | |
222 | ||
223 | -- | Look up an Ini section by name. Returns a sequence of all matching | |
224 | -- section records along with their normalized names. | |
225 | lookupSection :: Text | |
226 | -- ^ The section name. Will be normalized prior to | |
227 | -- comparison. | |
228 | -> Ini | |
229 | -- ^ The Ini to search. | |
230 | -> Seq.Seq (Text, IniSection) | |
231 | lookupSection name ini = | |
232 | Seq.filter ((== normalizeSectionName name) . fst) $ fromIni ini | |
233 | ||
234 | -- | Look up an Ini key's value in a given section by the key. Returns | |
235 | -- Nothing if the key could not be found. Otherwise returns the IniValue | |
236 | -- and its normalized name. | |
237 | lookupValue :: Text | |
238 | -- ^ The key. Will be normalized prior to comparison. | |
239 | -> IniSection | |
240 | -- ^ The section to search. | |
241 | -> Seq.Seq (Text, IniValue) | |
242 | lookupValue name section = | |
243 | Seq.filter ((== normalizeKey name) . fst) (isVals section) | |
244 | ||
198 | 245 | {- $main |
199 | 246 | |
200 | 247 | __Warning!__ This module is subject to change in the future, and therefore should |