Join adjacent lines in descriptions from #[doc]s
This commit is contained in:
		
							parent
							
								
									feefd418d4
								
							
						
					
					
						commit
						a1c3daaed8
					
				
					 2 changed files with 33 additions and 22 deletions
				
			
		|  | @ -19,13 +19,14 @@ pub struct MyStruct { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /// # A Unit
 | /// # A Unit
 | ||||||
|  | ///
 | ||||||
| #[derive(Debug, JsonSchema)] | #[derive(Debug, JsonSchema)] | ||||||
| pub struct MyUnitStruct; | pub struct MyUnitStruct; | ||||||
| 
 | 
 | ||||||
| #[doc = " # This is the enum's title "] | #[doc = " # This is the enum's title "] | ||||||
| #[doc = " This is..."] | #[doc = " This is "] | ||||||
| #[derive(Debug, JsonSchema)] | #[derive(Debug, JsonSchema)] | ||||||
| #[doc = "...the enum's description. "] | #[doc = " the enum's description."] | ||||||
| pub enum MyEnum { | pub enum MyEnum { | ||||||
|     UndocumentedUnit, |     UndocumentedUnit, | ||||||
|     /// This comment is not included in the generated schema :(
 |     /// This comment is not included in the generated schema :(
 | ||||||
|  | @ -37,7 +38,15 @@ pub enum MyEnum { | ||||||
|         ///
 |         ///
 | ||||||
|         /// This field is a nullable string.
 |         /// This field is a nullable string.
 | ||||||
|         ///
 |         ///
 | ||||||
|         /// This is another line!
 |         /// This
 | ||||||
|  |         ///is
 | ||||||
|  |         ///   the second
 | ||||||
|  |         ///  line!
 | ||||||
|  |         ///
 | ||||||
|  |         ///
 | ||||||
|  |         ///
 | ||||||
|  |         ///
 | ||||||
|  |         /// And this is the third!
 | ||||||
|         my_nullable_string: Option<String>, |         my_nullable_string: Option<String>, | ||||||
|     }, |     }, | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -14,23 +14,25 @@ pub fn get_title_and_desc_from_docs(attrs: &[Attribute]) -> (Option<String>, Opt | ||||||
|             .trim_start_matches('#') |             .trim_start_matches('#') | ||||||
|             .trim() |             .trim() | ||||||
|             .to_owned(); |             .to_owned(); | ||||||
|         let maybe_desc = split.next().map(|s| s.trim().to_owned()); |         let maybe_desc = split.next().and_then(get_description); | ||||||
|         (none_if_empty(title), maybe_desc) |         (none_if_empty(title), maybe_desc) | ||||||
|     } else { |     } else { | ||||||
|         (None, none_if_empty(docs)) |         (None, get_description(&docs)) | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn none_if_empty(s: String) -> Option<String> { | fn get_description(docs: &str) -> Option<String> { | ||||||
|     if s.is_empty() { |     let desc = docs | ||||||
|         None |         .trim() | ||||||
|     } else { |         .split("\n\n") | ||||||
|         Some(s) |         .filter_map(|line| none_if_empty(line.trim().replace('\n', " "))) | ||||||
|     } |         .collect::<Vec<_>>() | ||||||
|  |         .join("\n\n"); | ||||||
|  |     none_if_empty(desc) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn get_docs(attrs: &[Attribute]) -> Option<String> { | fn get_docs(attrs: &[Attribute]) -> Option<String> { | ||||||
|     let doc_attrs = attrs |     let docs = attrs | ||||||
|         .iter() |         .iter() | ||||||
|         .filter_map(|attr| { |         .filter_map(|attr| { | ||||||
|             if !attr.path.is_ident("doc") { |             if !attr.path.is_ident("doc") { | ||||||
|  | @ -44,20 +46,20 @@ fn get_docs(attrs: &[Attribute]) -> Option<String> { | ||||||
| 
 | 
 | ||||||
|             None |             None | ||||||
|         }) |         }) | ||||||
|         .collect::<Vec<_>>(); |         .collect::<Vec<_>>() | ||||||
| 
 |  | ||||||
|     if doc_attrs.is_empty() { |  | ||||||
|         return None; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     let mut docs = doc_attrs |  | ||||||
|         .iter() |         .iter() | ||||||
|         .flat_map(|a| a.split('\n')) |         .flat_map(|a| a.split('\n')) | ||||||
|         .map(str::trim) |         .map(str::trim) | ||||||
|         .skip_while(|s| *s == "") |         .skip_while(|s| *s == "") | ||||||
|         .collect::<Vec<_>>() |         .collect::<Vec<_>>() | ||||||
|         .join("\n"); |         .join("\n"); | ||||||
| 
 |     none_if_empty(docs) | ||||||
|     docs.truncate(docs.trim_end().len()); | } | ||||||
|     Some(docs) | 
 | ||||||
|  | fn none_if_empty(s: String) -> Option<String> { | ||||||
|  |     if s.is_empty() { | ||||||
|  |         None | ||||||
|  |     } else { | ||||||
|  |         Some(s) | ||||||
|  |     } | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Graham Esau
						Graham Esau