abstract hidden line related in config generator macro
This commit is contained in:
parent
f9e76d6239
commit
afdf5a07b5
1 changed files with 34 additions and 36 deletions
|
@ -15,6 +15,8 @@ use crate::{
|
||||||
|
|
||||||
const UNDOCUMENTED: &str = "# This item is undocumented. Please contribute documentation for it.";
|
const UNDOCUMENTED: &str = "# This item is undocumented. Please contribute documentation for it.";
|
||||||
|
|
||||||
|
const HIDDEN: &[&str] = &["default"];
|
||||||
|
|
||||||
#[allow(clippy::needless_pass_by_value)]
|
#[allow(clippy::needless_pass_by_value)]
|
||||||
pub(super) fn example_generator(input: ItemStruct, args: &[Meta]) -> Result<TokenStream> {
|
pub(super) fn example_generator(input: ItemStruct, args: &[Meta]) -> Result<TokenStream> {
|
||||||
if is_cargo_build() && !is_cargo_test() {
|
if is_cargo_build() && !is_cargo_test() {
|
||||||
|
@ -93,7 +95,7 @@ fn generate_example(input: &ItemStruct, args: &[Meta]) -> Result<()> {
|
||||||
format!("{doc}\n#\n")
|
format!("{doc}\n#\n")
|
||||||
};
|
};
|
||||||
|
|
||||||
let default = get_doc_default(field)
|
let default = get_doc_comment_line(field, "default")
|
||||||
.or_else(|| get_default(field))
|
.or_else(|| get_default(field))
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
@ -163,40 +165,40 @@ fn get_default(field: &Field) -> Option<String> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_doc_default(field: &Field) -> Option<String> {
|
fn get_doc_comment(field: &Field) -> Option<String> {
|
||||||
for attr in &field.attrs {
|
let comment = get_doc_comment_full(field)?;
|
||||||
let Meta::NameValue(MetaNameValue { path, value, .. }) = &attr.meta else {
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
|
|
||||||
if path.segments.iter().next().is_none_or(|s| s.ident != "doc") {
|
let out = comment
|
||||||
continue;
|
.lines()
|
||||||
}
|
.filter(|line| {
|
||||||
|
!HIDDEN.iter().any(|key| {
|
||||||
|
line.trim().starts_with(key) && line.trim().chars().nth(key.len()) == Some(':')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.fold(String::new(), |full, line| full + "#" + line + "\n");
|
||||||
|
|
||||||
let Expr::Lit(ExprLit { lit, .. }) = &value else {
|
(!out.is_empty()).then_some(out)
|
||||||
continue;
|
|
||||||
};
|
|
||||||
|
|
||||||
let Lit::Str(token) = &lit else {
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
|
|
||||||
let value = token.value();
|
|
||||||
if !value.trim().starts_with("default:") {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
return value
|
|
||||||
.split_once(':')
|
|
||||||
.map(|(_, v)| v)
|
|
||||||
.map(str::trim)
|
|
||||||
.map(ToOwned::to_owned);
|
|
||||||
}
|
|
||||||
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_doc_comment(field: &Field) -> Option<String> {
|
fn get_doc_comment_line(field: &Field, label: &str) -> Option<String> {
|
||||||
|
let comment = get_doc_comment_full(field)?;
|
||||||
|
|
||||||
|
comment
|
||||||
|
.lines()
|
||||||
|
.map(str::trim)
|
||||||
|
.filter(|line| line.starts_with(label))
|
||||||
|
.filter(|line| line.chars().nth(label.len()) == Some(':'))
|
||||||
|
.map(|line| {
|
||||||
|
line.split_once(':')
|
||||||
|
.map(|(_, v)| v)
|
||||||
|
.map(str::trim)
|
||||||
|
.map(ToOwned::to_owned)
|
||||||
|
})
|
||||||
|
.next()
|
||||||
|
.flatten()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_doc_comment_full(field: &Field) -> Option<String> {
|
||||||
let mut out = String::new();
|
let mut out = String::new();
|
||||||
for attr in &field.attrs {
|
for attr in &field.attrs {
|
||||||
let Meta::NameValue(MetaNameValue { path, value, .. }) = &attr.meta else {
|
let Meta::NameValue(MetaNameValue { path, value, .. }) = &attr.meta else {
|
||||||
|
@ -216,11 +218,7 @@ fn get_doc_comment(field: &Field) -> Option<String> {
|
||||||
};
|
};
|
||||||
|
|
||||||
let value = token.value();
|
let value = token.value();
|
||||||
if value.trim().starts_with("default:") {
|
writeln!(&mut out, "{value}").expect("wrote to output string buffer");
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
writeln!(&mut out, "#{value}").expect("wrote to output string buffer");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
(!out.is_empty()).then_some(out)
|
(!out.is_empty()).then_some(out)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue