diff --git a/src/bindgen/ir/annotation.rs b/src/bindgen/ir/annotation.rs index 05507c6e..e09fa663 100644 --- a/src/bindgen/ir/annotation.rs +++ b/src/bindgen/ir/annotation.rs @@ -199,10 +199,16 @@ impl AnnotationSet { T: Default + FromStr, { match self.annotations.get(name) { - Some(AnnotationValue::Atom(x)) => Some( - x.as_ref() - .map_or(T::default(), |y| y.parse::().ok().unwrap()), - ), + Some(AnnotationValue::Atom(None)) => Some(T::default()), + Some(AnnotationValue::Atom(Some(x))) => match x.parse::() { + Ok(value) => Some(value), + Err(_) => { + warn!( + "Couldn't parse value {x:?} of annotation \"cbindgen:{name}\", ignoring it." + ); + None + } + }, _ => None, } } @@ -218,3 +224,29 @@ fn parse_list(list: &str) -> Option> { .collect(); Some(parsed) } + +#[cfg(test)] +mod tests { + use super::*; + use crate::bindgen::rename::RenameRule; + + fn load(line: &str) -> AnnotationSet { + let attr: syn::Attribute = parse_quote!(#[doc = #line]); + AnnotationSet::load(&[attr]).unwrap() + } + + #[test] + fn parse_atom_valid() { + let annotations = load(" cbindgen:rename-all=SnakeCase"); + assert!(matches!( + annotations.parse_atom::("rename-all"), + Some(RenameRule::SnakeCase) + )); + } + + #[test] + fn parse_atom_invalid() { + let annotations = load(" cbindgen:rename-all=not-a-valid-rename-rule"); + assert!(annotations.parse_atom::("rename-all").is_none()); + } +}