patch rust

This commit is contained in:
2025-11-14 19:28:56 +00:00
parent c1eef314a9
commit 30d3a1568c
2 changed files with 76 additions and 2 deletions
+42 -1
View File
@@ -23,6 +23,7 @@ struct RustDecl {
methods: Vec<String>,
return_type: Option<String>,
parameters: Vec<String>,
docstring: Option<String>,
}
fn main() {
@@ -292,8 +293,18 @@ fn parse_impl(item_impl: &ItemImpl) -> Option<RustDecl> {
.into_iter()
.collect::<Vec<_>>();
// Prefer the first trait name (if any) as the identifier; otherwise fall back to the target type
let name = if !implements.is_empty() {
implements[0].clone()
} else {
target
};
// Extract any docstring attached to the impl block
let docstring = extract_docstring(&item_impl.attrs);
Some(RustDecl {
name: format!("impl_{}", target),
name,
type_: "impl".to_string(),
start_line,
end_line,
@@ -311,9 +322,39 @@ fn parse_impl(item_impl: &ItemImpl) -> Option<RustDecl> {
methods,
return_type: None,
parameters: Vec::new(),
docstring,
})
}
/// Pull a concatenated docstring from a list of attributes.
/// Handles both `///` comments (converted to `#[doc = "..."]` by syn)
/// and explicit `#[doc = "..."]` attributes.
fn extract_docstring(attrs: &[syn::Attribute]) -> Option<String> {
let mut docs = Vec::new();
for attr in attrs {
if attr.path.is_ident("doc") {
// #[doc = "…"]
if let Ok(meta) = attr.parse_meta() {
if let syn::Meta::NameValue(nv) = meta {
if let syn::Lit::Str(lit) = nv.lit {
docs.push(lit.value());
}
}
}
} else if attr.path.is_ident("doc") {
// Just in case `#[doc]` appears without a value (unlikely but harmless)
continue;
}
}
if docs.is_empty() {
None
} else {
Some(docs.join("\n"))
}
}
fn parse_mod(item_mod: &ItemMod) -> Option<RustDecl> {
let (start_line, end_line) = span_start_end(item_mod);