Add tags to Rust std lib functions (#6701)
Signed-off-by: Nick Cameron <nrc@ncameron.org>
This commit is contained in:
@ -232,30 +232,16 @@ fn do_stdlib_inner(
|
||||
quote! { "" }
|
||||
};
|
||||
|
||||
let cb = doc_info.code_blocks.clone();
|
||||
let code_blocks = if !cb.is_empty() {
|
||||
quote! {
|
||||
let code_blocks = vec![#(#cb),*];
|
||||
code_blocks.iter().map(|cb| {
|
||||
let program = crate::Program::parse_no_errs(cb).unwrap();
|
||||
|
||||
let mut options: crate::parsing::ast::types::FormatOptions = Default::default();
|
||||
options.insert_final_newline = false;
|
||||
program.ast.recast(&options, 0)
|
||||
}).collect::<Vec<String>>()
|
||||
}
|
||||
} else {
|
||||
if doc_info.code_blocks.is_empty() {
|
||||
errors.push(Error::new_spanned(
|
||||
&ast.sig,
|
||||
"stdlib functions must have at least one code block",
|
||||
));
|
||||
|
||||
quote! { vec![] }
|
||||
};
|
||||
}
|
||||
|
||||
// Make sure the function name is in all the code blocks.
|
||||
for code_block in doc_info.code_blocks.iter() {
|
||||
if !code_block.contains(&name) {
|
||||
if !code_block.0.contains(&name) {
|
||||
errors.push(Error::new_spanned(
|
||||
&ast.sig,
|
||||
format!(
|
||||
@ -270,9 +256,28 @@ fn do_stdlib_inner(
|
||||
.code_blocks
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, code_block)| generate_code_block_test(&fn_name_str, code_block, index))
|
||||
.map(|(index, (code_block, norun))| {
|
||||
if !norun {
|
||||
generate_code_block_test(&fn_name_str, code_block, index)
|
||||
} else {
|
||||
quote! {}
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let (cb, norun): (Vec<_>, Vec<_>) = doc_info.code_blocks.into_iter().unzip();
|
||||
let code_blocks = quote! {
|
||||
let code_blocks = vec![#(#cb),*];
|
||||
let norun = vec![#(#norun),*];
|
||||
code_blocks.iter().zip(norun).map(|(cb, norun)| {
|
||||
let program = crate::Program::parse_no_errs(cb).unwrap();
|
||||
|
||||
let mut options: crate::parsing::ast::types::FormatOptions = Default::default();
|
||||
options.insert_final_newline = false;
|
||||
(program.ast.recast(&options, 0), norun)
|
||||
}).collect::<Vec<(String, bool)>>()
|
||||
};
|
||||
|
||||
let tags = metadata
|
||||
.tags
|
||||
.iter()
|
||||
@ -534,7 +539,7 @@ fn do_stdlib_inner(
|
||||
#feature_tree_operation
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<String> {
|
||||
fn examples(&self) -> Vec<(String, bool)> {
|
||||
#code_blocks
|
||||
}
|
||||
|
||||
@ -577,7 +582,7 @@ fn get_crate(var: Option<String>) -> proc_macro2::TokenStream {
|
||||
struct DocInfo {
|
||||
pub summary: Option<String>,
|
||||
pub description: Option<String>,
|
||||
pub code_blocks: Vec<String>,
|
||||
pub code_blocks: Vec<(String, bool)>,
|
||||
}
|
||||
|
||||
fn extract_doc_from_attrs(attrs: &[syn::Attribute]) -> DocInfo {
|
||||
@ -597,21 +602,22 @@ fn extract_doc_from_attrs(attrs: &[syn::Attribute]) -> DocInfo {
|
||||
});
|
||||
|
||||
// Parse any code blocks from the doc string.
|
||||
let mut code_blocks: Vec<String> = Vec::new();
|
||||
let mut code_block: Option<String> = None;
|
||||
let mut code_blocks: Vec<(String, bool)> = Vec::new();
|
||||
let mut code_block: Option<(String, bool)> = None;
|
||||
let mut parsed_lines = Vec::new();
|
||||
for line in raw_lines {
|
||||
if line.starts_with("```") {
|
||||
if let Some(ref inner_code_block) = code_block {
|
||||
code_blocks.push(inner_code_block.trim().to_string());
|
||||
if let Some((inner_code_block, norun)) = code_block {
|
||||
code_blocks.push((inner_code_block.trim().to_owned(), norun));
|
||||
code_block = None;
|
||||
} else {
|
||||
code_block = Some(String::new());
|
||||
let norun = line.contains("kcl,norun") || line.contains("kcl,no_run");
|
||||
code_block = Some((String::new(), norun));
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
if let Some(ref mut code_block) = code_block {
|
||||
if let Some((code_block, _)) = &mut code_block {
|
||||
code_block.push_str(&line);
|
||||
code_block.push('\n');
|
||||
} else {
|
||||
@ -619,8 +625,8 @@ fn extract_doc_from_attrs(attrs: &[syn::Attribute]) -> DocInfo {
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(code_block) = code_block {
|
||||
code_blocks.push(code_block.trim().to_string());
|
||||
if let Some((code_block, norun)) = code_block {
|
||||
code_blocks.push((code_block.trim().to_string(), norun));
|
||||
}
|
||||
|
||||
let mut summary = None;
|
||||
|
Reference in New Issue
Block a user