Skip to content

Commit 6004a06

Browse files
committed
Return syn::Result from extract_doc_line
Update callers to use Iterator::try_fold and propagate errors with ?. Also switch parse2().ok() to map(Some) and convert early returns to Ok(...) variants.
1 parent ab7b510 commit 6004a06

3 files changed

Lines changed: 28 additions & 28 deletions

File tree

crates/rmcp-macros/src/common.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ pub fn none_expr() -> syn::Result<Expr> {
99
}
1010

1111
/// Extract documentation from doc attributes
12-
pub fn extract_doc_line(existing_docs: Option<Expr>, attr: &Attribute) -> Option<Expr> {
12+
pub fn extract_doc_line(
13+
existing_docs: Option<Expr>,
14+
attr: &Attribute,
15+
) -> syn::Result<Option<Expr>> {
1316
if !attr.path().is_ident("doc") {
14-
return None;
17+
return Ok(None);
1518
}
1619

1720
let syn::Meta::NameValue(name_value) = &attr.meta else {
18-
return None;
21+
return Ok(None);
1922
};
2023

2124
let value = &name_value.value;
@@ -28,23 +31,23 @@ pub fn extract_doc_line(existing_docs: Option<Expr>, attr: &Attribute) -> Option
2831
}) => {
2932
let content = lit_str.value().trim().to_string();
3033
if content.is_empty() {
31-
return existing_docs;
34+
return Ok(existing_docs);
3235
}
3336
Some(Expr::Lit(syn::ExprLit {
3437
attrs: Vec::new(),
3538
lit: syn::Lit::Str(syn::LitStr::new(&content, lit_str.span())),
3639
}))
3740
}
38-
_ => return None,
41+
_ => return Ok(None),
3942
};
4043

4144
match (existing_docs, this_expr) {
4245
(Some(existing), Some(this)) => {
43-
syn::parse2::<Expr>(quote! { concat!(#existing, "\n", #this) }).ok()
46+
syn::parse2::<Expr>(quote! { concat!(#existing, "\n", #this) }).map(Some)
4447
}
45-
(Some(existing), None) => Some(existing),
46-
(None, Some(this)) => Some(this),
47-
_ => None,
48+
(Some(existing), None) => Ok(Some(existing)),
49+
(None, Some(this)) => Ok(Some(this)),
50+
_ => Ok(None),
4851
}
4952
}
5053

crates/rmcp-macros/src/prompt.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,14 @@ pub fn prompt(attr: TokenStream, input: TokenStream) -> syn::Result<TokenStream>
9898
};
9999

100100
let name = attribute.name.unwrap_or_else(|| fn_ident.to_string());
101-
let description = attribute
102-
.description
103-
.map(|s| {
104-
Expr::Lit(syn::ExprLit {
105-
attrs: Vec::new(),
106-
lit: syn::Lit::Str(syn::LitStr::new(&s, Span::call_site())),
107-
})
108-
})
109-
.or_else(|| fn_item.attrs.iter().fold(None, extract_doc_line));
101+
let description = if let Some(s) = attribute.description {
102+
Some(Expr::Lit(syn::ExprLit {
103+
attrs: Vec::new(),
104+
lit: syn::Lit::Str(syn::LitStr::new(&s, Span::call_site())),
105+
}))
106+
} else {
107+
fn_item.attrs.iter().try_fold(None, extract_doc_line)?
108+
};
110109
let arguments = arguments_expr;
111110

112111
let resolved_prompt_attr = ResolvedPromptAttribute {

crates/rmcp-macros/src/tool.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -244,16 +244,14 @@ pub fn tool(attr: TokenStream, input: TokenStream) -> syn::Result<TokenStream> {
244244
}
245245
});
246246

247-
let description_expr = attribute
248-
.description
249-
.map(|s| {
250-
let lit = LitStr::new(&s, Span::call_site());
251-
Expr::Lit(syn::ExprLit {
252-
attrs: Vec::new(),
253-
lit: syn::Lit::Str(lit),
254-
})
255-
})
256-
.or_else(|| fn_item.attrs.iter().fold(None, extract_doc_line));
247+
let description_expr = if let Some(s) = attribute.description {
248+
Some(Expr::Lit(syn::ExprLit {
249+
attrs: Vec::new(),
250+
lit: syn::Lit::Str(LitStr::new(&s, Span::call_site())),
251+
}))
252+
} else {
253+
fn_item.attrs.iter().try_fold(None, extract_doc_line)?
254+
};
257255
let resolved_tool_attr = ResolvedToolAttribute {
258256
name: attribute.name.unwrap_or_else(|| fn_ident.to_string()),
259257
description: description_expr,

0 commit comments

Comments
 (0)