@@ -4,7 +4,7 @@ use documented::{Documented, DocumentedFields};
44use toml_edit:: { ArrayOfTables , Decor , Item , RawString , Table } ;
55use tracing:: warn;
66
7- use crate :: error:: ConfigError ;
7+ use crate :: error:: { ConfigError , Result } ;
88
99/// Appends documentation lines as TOML comments to the given `Decor`.
1010///
@@ -52,7 +52,7 @@ pub fn append_docs_as_toml_comments(decor: &mut Decor, docs: &str) {
5252///
5353/// # Returns
5454/// Returns `Ok(())` if successful, or a `ConfigError` if a TOML item is unexpectedly `None`.
55- pub fn annotate_toml_table < T > ( table : & mut Table , is_root : bool ) -> Result < ( ) , ConfigError >
55+ pub fn annotate_toml_table < T > ( table : & mut Table , is_root : bool ) -> Result < ( ) >
5656where
5757 T : Documented + DocumentedFields ,
5858{
6666 Ok ( docs) => {
6767 match value_item {
6868 Item :: None => {
69- return Err ( ConfigError :: Custom ( format ! (
70- "Encountered TomlEditItem::None for key '{key_str}' unexpectedly" ,
71- ) ) )
69+ return Err ( ConfigError :: UnexpectedTomlItem ( key_str. into ( ) ) ) ;
7270 }
7371 Item :: Value ( _) => append_docs_as_toml_comments ( key_mut. leaf_decor_mut ( ) , docs) ,
7472 Item :: Table ( sub_table) => {
@@ -105,14 +103,60 @@ where
105103///
106104/// # Returns
107105/// Returns `Ok(())` if annotation succeeds, or a `ConfigError` if annotation fails on the first table.
108- pub fn annotate_toml_array_of_tables < T > ( array : & mut ArrayOfTables ) -> Result < ( ) , ConfigError >
106+ pub fn annotate_toml_array_of_tables < T > ( array : & mut ArrayOfTables ) -> Result < ( ) >
109107where
110108 T : Documented + DocumentedFields ,
111109{
112110 if let Some ( first_table) = array. iter_mut ( ) . next ( ) {
113- annotate_toml_table :: < T > ( first_table, false ) . map_err ( |err| {
114- ConfigError :: Custom ( format ! ( "Failed to annotate first table in array: {err}" ) )
115- } ) ?;
111+ annotate_toml_table :: < T > ( first_table, false )
112+ . map_err ( |err| ConfigError :: AnnotateFirstTable ( err. to_string ( ) ) ) ?;
116113 }
117114 Ok ( ( ) )
118115}
116+
117+ #[ cfg( test) ]
118+ mod tests {
119+ use toml_edit:: Decor ;
120+
121+ use super :: * ;
122+ use crate :: config:: Config ;
123+
124+ #[ test]
125+ fn test_append_docs_as_toml_comments ( ) {
126+ let mut decor = Decor :: new ( "" , "" ) ;
127+ append_docs_as_toml_comments ( & mut decor, "Test documentation" ) ;
128+
129+ let prefix = decor. prefix ( ) . and_then ( |p| p. as_str ( ) ) . unwrap ( ) ;
130+ assert ! ( prefix. contains( "# Test documentation" ) ) ;
131+ }
132+
133+ #[ test]
134+ fn test_append_docs_multiline ( ) {
135+ let mut decor = Decor :: new ( "" , "" ) ;
136+ append_docs_as_toml_comments ( & mut decor, "Line 1\n Line 2\n Line 3" ) ;
137+
138+ let prefix = decor. prefix ( ) . and_then ( |p| p. as_str ( ) ) . unwrap ( ) ;
139+ assert ! ( prefix. contains( "# Line 1" ) ) ;
140+ assert ! ( prefix. contains( "# Line 2" ) ) ;
141+ assert ! ( prefix. contains( "# Line 3" ) ) ;
142+ }
143+
144+ #[ test]
145+ fn test_append_docs_empty_lines ( ) {
146+ let mut decor = Decor :: new ( "" , "" ) ;
147+ append_docs_as_toml_comments ( & mut decor, "Line 1\n \n Line 2" ) ;
148+
149+ let prefix = decor. prefix ( ) . and_then ( |p| p. as_str ( ) ) . unwrap ( ) ;
150+ assert ! ( prefix. contains( "#\n " ) ) ;
151+ }
152+
153+ #[ test]
154+ fn test_annotate_toml_document ( ) {
155+ let config = Config :: default_config :: < & str > ( false , & [ ] ) ;
156+ let doc = config. to_annotated_document ( ) ;
157+
158+ assert ! ( doc. is_ok( ) ) ;
159+ let doc = doc. unwrap ( ) ;
160+ assert ! ( doc. to_string( ) . contains( "#" ) ) ;
161+ }
162+ }
0 commit comments