Do not collapse newlines in doc comments (#310)
This commit is contained in:
parent
91ee3f915c
commit
ce15380863
5 changed files with 14 additions and 48 deletions
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||||
"title": "My Amazing Struct",
|
"title": "My Amazing Struct",
|
||||||
"description": "This struct shows off generating a schema with a custom title and description.",
|
"description": "This struct shows off generating a schema with\na custom title and description.",
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
"my_bool": {
|
"my_bool": {
|
||||||
|
@ -48,7 +48,7 @@
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"description": "A struct-like enum variant which contains some floats",
|
"description": "A struct-like enum variant which contains\nsome floats",
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
"StructVariant": {
|
"StructVariant": {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||||
"title": "My Amazing Struct",
|
"title": "My Amazing Struct",
|
||||||
"description": "This struct shows off generating a schema with a custom title and description.",
|
"description": "This struct shows off generating a schema with\na custom title and description.",
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
"my_bool": {
|
"my_bool": {
|
||||||
|
@ -48,7 +48,7 @@
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"description": "A struct-like enum variant which contains some floats",
|
"description": "A struct-like enum variant which contains\nsome floats",
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
"StructVariant": {
|
"StructVariant": {
|
||||||
|
|
|
@ -5,11 +5,9 @@ use util::*;
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
#[derive(JsonSchema)]
|
#[derive(JsonSchema)]
|
||||||
/**
|
/**
|
||||||
*
|
# This is the struct's title
|
||||||
* # This is the struct's title
|
|
||||||
*
|
This is the struct's description.
|
||||||
* This is the struct's description.
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
struct MyStruct {
|
struct MyStruct {
|
||||||
/// # An integer
|
/// # An integer
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||||
"title": "This is the enum's title",
|
"title": "This is the enum's title",
|
||||||
"description": "This is the enum's description.",
|
"description": "This is \n the enum's description.",
|
||||||
"oneOf": [
|
"oneOf": [
|
||||||
{
|
{
|
||||||
"type": "string",
|
"type": "string",
|
||||||
|
@ -25,7 +25,7 @@
|
||||||
"properties": {
|
"properties": {
|
||||||
"my_nullable_string": {
|
"my_nullable_string": {
|
||||||
"title": "A nullable string",
|
"title": "A nullable string",
|
||||||
"description": "This field is a nullable string.\n\nThis is the second line!\n\nAnd this is the third!",
|
"description": "This field is a nullable string.\n\n This\nis\n the second\n line!\n\n\n\n\n And this is the third!",
|
||||||
"type": [
|
"type": [
|
||||||
"string",
|
"string",
|
||||||
"null"
|
"null"
|
||||||
|
|
|
@ -14,25 +14,15 @@ pub fn get_title_and_desc_from_doc(attrs: &[Attribute]) -> (Option<String>, Opti
|
||||||
.trim_start_matches('#')
|
.trim_start_matches('#')
|
||||||
.trim()
|
.trim()
|
||||||
.to_owned();
|
.to_owned();
|
||||||
let maybe_desc = split.next().and_then(merge_description_lines);
|
let maybe_desc = split.next().map(|s| s.trim().to_owned());
|
||||||
(none_if_empty(title), maybe_desc)
|
(none_if_empty(title), maybe_desc)
|
||||||
} else {
|
} else {
|
||||||
(None, merge_description_lines(&doc))
|
(None, Some(doc))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn merge_description_lines(doc: &str) -> Option<String> {
|
|
||||||
let desc = doc
|
|
||||||
.trim()
|
|
||||||
.split("\n\n")
|
|
||||||
.filter_map(|line| none_if_empty(line.trim().replace('\n', " ")))
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join("\n\n");
|
|
||||||
none_if_empty(desc)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_doc(attrs: &[Attribute]) -> Option<String> {
|
fn get_doc(attrs: &[Attribute]) -> Option<String> {
|
||||||
let attrs = attrs
|
let lines = attrs
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|attr| {
|
.filter_map(|attr| {
|
||||||
if !attr.path().is_ident("doc") {
|
if !attr.path().is_ident("doc") {
|
||||||
|
@ -52,29 +42,7 @@ fn get_doc(attrs: &[Attribute]) -> Option<String> {
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
let mut lines = attrs
|
none_if_empty(lines.join("\n").trim().to_owned())
|
||||||
.iter()
|
|
||||||
.flat_map(|a| a.split('\n'))
|
|
||||||
.map(str::trim)
|
|
||||||
.skip_while(|s| s.is_empty())
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
if let Some(&"") = lines.last() {
|
|
||||||
lines.pop();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Added for backward-compatibility, but perhaps we shouldn't do this
|
|
||||||
// https://github.com/rust-lang/rust/issues/32088
|
|
||||||
if lines.iter().all(|l| l.starts_with('*')) {
|
|
||||||
for line in lines.iter_mut() {
|
|
||||||
*line = line[1..].trim()
|
|
||||||
}
|
|
||||||
while let Some(&"") = lines.first() {
|
|
||||||
lines.remove(0);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
none_if_empty(lines.join("\n"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn none_if_empty(s: String) -> Option<String> {
|
fn none_if_empty(s: String) -> Option<String> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue