codama_attributes/codama_directives/
name_directive.rs1use crate::{utils::FromMeta, Attribute, CodamaAttribute, CodamaDirective};
2use codama_errors::CodamaError;
3use codama_nodes::CamelCaseString;
4use codama_syn_helpers::Meta;
5
6#[derive(Debug, PartialEq)]
7pub struct NameDirective {
8 pub name: CamelCaseString,
9}
10
11impl NameDirective {
12 pub fn parse(meta: &Meta) -> syn::Result<Self> {
13 meta.assert_directive("name")?;
14 Ok(Self {
15 name: String::from_meta(meta)?.into(),
16 })
17 }
18}
19
20impl<'a> TryFrom<&'a CodamaAttribute<'a>> for &'a NameDirective {
21 type Error = CodamaError;
22
23 fn try_from(attribute: &'a CodamaAttribute) -> Result<Self, Self::Error> {
24 match attribute.directive {
25 CodamaDirective::Name(ref a) => Ok(a),
26 _ => Err(CodamaError::InvalidCodamaDirective {
27 expected: "name".to_string(),
28 actual: attribute.directive.name().to_string(),
29 }),
30 }
31 }
32}
33
34impl<'a> TryFrom<&'a Attribute<'a>> for &'a NameDirective {
35 type Error = CodamaError;
36
37 fn try_from(attribute: &'a Attribute) -> Result<Self, Self::Error> {
38 <&CodamaAttribute>::try_from(attribute)?.try_into()
39 }
40}
41
42#[cfg(test)]
43mod tests {
44 use super::*;
45
46 #[test]
47 fn ok() {
48 let meta: Meta = syn::parse_quote! { name = "banana" };
49 let directive = NameDirective::parse(&meta).unwrap();
50 assert_eq!(
51 directive,
52 NameDirective {
53 name: CamelCaseString::from("banana"),
54 }
55 );
56 }
57}