gdritter repos config-ini / 1c13d05
Raw: add functions to look up section and key information Jonathan Daugherty 6 years ago
1 changed file(s) with 47 addition(s) and 0 deletion(s). Collapse all Expand all
99 -- * serializing and deserializing
1010 , parseIni
1111 , printIni
12 -- * inspection
13 , lookupInSection
14 , lookupSection
15 , lookupValue
1216 ) where
1317
1418 import Control.Monad (void)
195199 Builder.fromText (vValue val) <>
196200 Builder.singleton '\n'
197201
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
198245 {- $main
199246
200247 __Warning!__ This module is subject to change in the future, and therefore should