@@ -28,9 +28,26 @@ pub struct SectionMut<'a, 'event> {
28
28
29
29
/// Mutating methods.
30
30
impl < ' a , ' event > SectionMut < ' a , ' event > {
31
- /// Adds an entry to the end of this section name `key` and `value`. If `value` is None`, no equal sign will be written leaving
31
+ /// Adds an entry to the end of this section name `key` and `value`. If `value` is ` None`, no equal sign will be written leaving
32
32
/// just the key. This is useful for boolean values which are true if merely the key exists.
33
33
pub fn push < ' b > ( & mut self , key : Key < ' event > , value : Option < & ' b BStr > ) {
34
+ self . push_with_comment_inner ( key, value, None )
35
+ }
36
+
37
+ /// Adds an entry to the end of this section name `key` and `value`. If `value` is `None`, no equal sign will be written leaving
38
+ /// just the key. This is useful for boolean values which are true if merely the key exists.
39
+ /// `comment` has to be the text to put right after the value and behind a `#` character. Note that newlines are silently transformed
40
+ /// into spaces.
41
+ pub fn push_with_comment < ' b , ' c > (
42
+ & mut self ,
43
+ key : Key < ' event > ,
44
+ value : Option < & ' b BStr > ,
45
+ comment : impl Into < & ' c BStr > ,
46
+ ) {
47
+ self . push_with_comment_inner ( key, value, comment. into ( ) . into ( ) )
48
+ }
49
+
50
+ fn push_with_comment_inner ( & mut self , key : Key < ' event > , value : Option < & BStr > , comment : Option < & BStr > ) {
34
51
let body = & mut self . section . body . 0 ;
35
52
if let Some ( ws) = & self . whitespace . pre_key {
36
53
body. push ( Event :: Whitespace ( ws. clone ( ) ) ) ;
@@ -44,6 +61,21 @@ impl<'a, 'event> SectionMut<'a, 'event> {
44
61
}
45
62
None => body. push ( Event :: Value ( Cow :: Borrowed ( "" . into ( ) ) ) ) ,
46
63
}
64
+ if let Some ( comment) = comment {
65
+ body. push ( Event :: Whitespace ( Cow :: Borrowed ( " " . into ( ) ) ) ) ;
66
+ body. push ( Event :: Comment ( parse:: Comment {
67
+ tag : b'#' ,
68
+ text : Cow :: Owned ( {
69
+ let mut c = Vec :: with_capacity ( comment. len ( ) ) ;
70
+ let mut bytes = comment. iter ( ) . peekable ( ) ;
71
+ if !bytes. peek ( ) . map_or ( true , |b| b. is_ascii_whitespace ( ) ) {
72
+ c. insert ( 0 , b' ' ) ;
73
+ }
74
+ c. extend ( bytes. map ( |b| ( * b == b'\n' ) . then ( || b' ' ) . unwrap_or ( * b) ) ) ;
75
+ c. into ( )
76
+ } ) ,
77
+ } ) ) ;
78
+ }
47
79
if self . implicit_newline {
48
80
body. push ( Event :: Newline ( BString :: from ( self . newline . to_vec ( ) ) . into ( ) ) ) ;
49
81
}
0 commit comments