@@ -28,9 +28,26 @@ pub struct SectionMut<'a, 'event> {
2828
2929/// Mutating methods.
3030impl < ' 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
3232 /// just the key. This is useful for boolean values which are true if merely the key exists.
3333 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 > ) {
3451 let body = & mut self . section . body . 0 ;
3552 if let Some ( ws) = & self . whitespace . pre_key {
3653 body. push ( Event :: Whitespace ( ws. clone ( ) ) ) ;
@@ -44,6 +61,21 @@ impl<'a, 'event> SectionMut<'a, 'event> {
4461 }
4562 None => body. push ( Event :: Value ( Cow :: Borrowed ( "" . into ( ) ) ) ) ,
4663 }
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+ }
4779 if self . implicit_newline {
4880 body. push ( Event :: Newline ( BString :: from ( self . newline . to_vec ( ) ) . into ( ) ) ) ;
4981 }
0 commit comments