gdritter repos config-ini / fabac67
Respect policy settings in new sections (fixes #5) Getty Ritter 7 years ago
1 changed file(s) with 32 addition(s) and 17 deletion(s). Collapse all Expand all
1212
1313 -- * Parsing, Serializing, and Updating Files
1414 -- $using
15 -- parseIniFile
16 emitIniFile
15 Ini
16 , ini
17 , getIniValue
18 -- ** Parsing INI files
19 , parseIni
20 -- ** Serializing INI files
21 , getIniText
22 -- ** Updating INI Files
23 , updateIni
24 , setIniUpdatePolicy
1725 , UpdatePolicy(..)
1826 , UpdateCommentPolicy(..)
1927 , defaultUpdatePolicy
20 , Ini
21 , ini
22 , parseIni
23 , getIniText
24 , getIniValue
25 , updateIni
26 , setIniUpdatePolicy
2728
2829 -- * Bidirectional Parser Types
2930 -- $types
556557 -- ^ Add a consistent comment to all new fields added or modified
557558 -- by an 'updateIniFile' call.
558559 deriving (Eq, Show)
560
561 getComments :: FieldDescription s -> UpdateCommentPolicy -> (Seq BlankLine)
562 getComments _ CommentPolicyNone = Seq.empty
563 getComments f CommentPolicyAddFieldComment =
564 mkComments (fdComment f)
565 getComments _ (CommentPolicyAddDefaultComment cs) =
566 mkComments cs
559567
560568 -- | Given a value, an 'IniSpec', and a 'Text' form of an INI file,
561569 -- parse 'Text' as INI and then selectively modify the file whenever
608616 \ (Section nm spec _) ->
609617 if | nm `elem` existingSectionNames -> return mempty
610618 | otherwise ->
611 let rs = emitNewFields s def spec
619 let rs = emitNewFields s def spec pol
612620 in if Seq.null rs
613621 then return mempty
614622 else return $ Seq.singleton
619627
620628 -- We won't emit a section if everything in the section is also
621629 -- missing
622 emitNewFields :: s -> s -> Seq (Field s) -> Seq (NormalizedText, IniValue)
623 emitNewFields s def fields = go (Seq.viewl fields) where
630 emitNewFields
631 :: s -> s
632 -> Seq (Field s)
633 -> UpdatePolicy ->
634 Seq (NormalizedText, IniValue)
635 emitNewFields s def fields pol = go (Seq.viewl fields) where
624636 go EmptyL = Seq.empty
625637 go (Field l d :< fs)
626638 -- If a field is not present but is also the same as the default,
627639 -- then we can safely omit it
628 | get l s == get l def = go (Seq.viewl fs)
640 | get l s == get l def && not (updateAddOptionalFields pol) =
641 go (Seq.viewl fs)
629642 -- otherwise, we should add it to the result
630643 | otherwise =
631 let new = ( fdName d
644 let cs = getComments d (updateGeneratedCommentPolicy pol)
645 new = ( fdName d
632646 , IniValue
633647 { vLineNo = 0
634648 , vName = actualText (fdName d)
635649 , vValue = fvEmit (fdValue d) (get l s)
636 , vComments = mempty
650 , vComments = cs
637651 , vCommentedOut = False
638652 , vDelimiter = '='
639653 }
643657 case get l s of
644658 Nothing -> go (Seq.viewl fs)
645659 Just v ->
646 let new = ( fdName d
660 let cs = getComments d (updateGeneratedCommentPolicy pol)
661 new = ( fdName d
647662 , IniValue
648663 { vLineNo = 0
649664 , vName = actualText (fdName d)
650665 , vValue = fvEmit (fdValue d) v
651 , vComments = mempty
666 , vComments = cs
652667 , vCommentedOut = False
653668 , vDelimiter = '='
654669 }