Respect policy settings in new sections (fixes #5)
Getty Ritter
7 years ago
12 | 12 | |
13 | 13 | -- * Parsing, Serializing, and Updating Files |
14 | 14 | -- $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 | |
17 | 25 | , UpdatePolicy(..) |
18 | 26 | , UpdateCommentPolicy(..) |
19 | 27 | , defaultUpdatePolicy |
20 | , Ini | |
21 | , ini | |
22 | , parseIni | |
23 | , getIniText | |
24 | , getIniValue | |
25 | , updateIni | |
26 | , setIniUpdatePolicy | |
27 | 28 | |
28 | 29 | -- * Bidirectional Parser Types |
29 | 30 | -- $types |
556 | 557 | -- ^ Add a consistent comment to all new fields added or modified |
557 | 558 | -- by an 'updateIniFile' call. |
558 | 559 | 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 | |
559 | 567 | |
560 | 568 | -- | Given a value, an 'IniSpec', and a 'Text' form of an INI file, |
561 | 569 | -- parse 'Text' as INI and then selectively modify the file whenever |
608 | 616 | \ (Section nm spec _) -> |
609 | 617 | if | nm `elem` existingSectionNames -> return mempty |
610 | 618 | | otherwise -> |
611 |
let rs = emitNewFields s def spec |
|
619 | let rs = emitNewFields s def spec pol | |
612 | 620 | in if Seq.null rs |
613 | 621 | then return mempty |
614 | 622 | else return $ Seq.singleton |
619 | 627 | |
620 | 628 | -- We won't emit a section if everything in the section is also |
621 | 629 | -- 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 | |
624 | 636 | go EmptyL = Seq.empty |
625 | 637 | go (Field l d :< fs) |
626 | 638 | -- If a field is not present but is also the same as the default, |
627 | 639 | -- then we can safely omit it |
628 |
| get l s == get l def |
|
640 | | get l s == get l def && not (updateAddOptionalFields pol) = | |
641 | go (Seq.viewl fs) | |
629 | 642 | -- otherwise, we should add it to the result |
630 | 643 | | otherwise = |
631 |
let |
|
644 | let cs = getComments d (updateGeneratedCommentPolicy pol) | |
645 | new = ( fdName d | |
632 | 646 | , IniValue |
633 | 647 | { vLineNo = 0 |
634 | 648 | , vName = actualText (fdName d) |
635 | 649 | , vValue = fvEmit (fdValue d) (get l s) |
636 |
, vComments = |
|
650 | , vComments = cs | |
637 | 651 | , vCommentedOut = False |
638 | 652 | , vDelimiter = '=' |
639 | 653 | } |
643 | 657 | case get l s of |
644 | 658 | Nothing -> go (Seq.viewl fs) |
645 | 659 | Just v -> |
646 |
let |
|
660 | let cs = getComments d (updateGeneratedCommentPolicy pol) | |
661 | new = ( fdName d | |
647 | 662 | , IniValue |
648 | 663 | { vLineNo = 0 |
649 | 664 | , vName = actualText (fdName d) |
650 | 665 | , vValue = fvEmit (fdValue d) v |
651 |
, vComments = |
|
666 | , vComments = cs | |
652 | 667 | , vCommentedOut = False |
653 | 668 | , vDelimiter = '=' |
654 | 669 | } |