Skip to main content

codama_attributes/codama_directives/
account_directive.rs

1use crate::{
2    utils::{FromMeta, SetOnce},
3    Attribute, AttributeContext, CodamaAttribute, CodamaDirective, Resolvable,
4};
5use codama_errors::{CodamaError, CodamaResult};
6use codama_nodes::{
7    CamelCaseString, Docs, InstructionAccountNode, InstructionInputValueNode, IsAccountSigner,
8};
9use codama_syn_helpers::{extensions::*, Meta};
10
11#[derive(Debug, PartialEq)]
12pub struct AccountDirective {
13    pub name: CamelCaseString,
14    pub is_writable: bool,
15    pub is_signer: IsAccountSigner,
16    pub is_optional: bool,
17    pub docs: Docs,
18    pub default_value: Option<Resolvable<InstructionInputValueNode>>,
19}
20
21impl AccountDirective {
22    pub fn parse(meta: &Meta, ctx: &AttributeContext) -> syn::Result<Self> {
23        meta.assert_directive("account")?;
24        let mut name = SetOnce::<CamelCaseString>::new("name");
25        if let AttributeContext::Field(syn::Field {
26            ident: Some(ident), ..
27        }) = ctx
28        {
29            name = name.initial_value(ident.to_string().into())
30        }
31        let mut is_writable = SetOnce::<bool>::new("writable").initial_value(false);
32        let mut is_signer = SetOnce::<IsAccountSigner>::new("signer").initial_value(false.into());
33        let mut is_optional = SetOnce::<bool>::new("optional").initial_value(false);
34        let mut default_value =
35            SetOnce::<Resolvable<InstructionInputValueNode>>::new("default_value");
36        let mut docs = SetOnce::<Docs>::new("docs");
37        match meta.is_path_or_empty_list() {
38            true => (),
39            false => meta
40                .as_path_list()?
41                .each(|ref meta| match meta.path_str().as_str() {
42                    "name" => name.set(meta.as_value()?.as_expr()?.as_string()?.into(), meta),
43                    "writable" => is_writable.set(bool::from_meta(meta)?, meta),
44                    "signer" => is_signer.set(IsAccountSigner::from_meta(meta)?, meta),
45                    "optional" => is_optional.set(bool::from_meta(meta)?, meta),
46                    "default_value" => default_value.set(
47                        Resolvable::<InstructionInputValueNode>::from_meta(meta.as_value()?)?,
48                        meta,
49                    ),
50                    "docs" => docs.set(Docs::from_meta(meta)?, meta),
51                    _ => Err(meta.error("unrecognized attribute")),
52                })?,
53        }
54        Ok(AccountDirective {
55            name: name.take(meta)?,
56            is_writable: is_writable.take(meta)?,
57            is_signer: is_signer.take(meta)?,
58            is_optional: is_optional.take(meta)?,
59            docs: docs.option().unwrap_or_default(),
60            default_value: default_value.option(),
61        })
62    }
63
64    /// Construct an `InstructionAccountNode` from this directive.
65    /// Returns an error if any unresolved directives remain.
66    pub fn to_instruction_account_node(&self) -> CodamaResult<InstructionAccountNode> {
67        Ok(InstructionAccountNode {
68            name: self.name.clone(),
69            is_writable: self.is_writable,
70            is_signer: self.is_signer,
71            is_optional: self.is_optional,
72            docs: self.docs.clone(),
73            default_value: self
74                .default_value
75                .as_ref()
76                .map(|r| r.try_resolved().cloned())
77                .transpose()?,
78        })
79    }
80}
81
82impl<'a> TryFrom<&'a CodamaAttribute<'a>> for &'a AccountDirective {
83    type Error = CodamaError;
84
85    fn try_from(attribute: &'a CodamaAttribute) -> Result<Self, Self::Error> {
86        match attribute.directive.as_ref() {
87            CodamaDirective::Account(ref a) => Ok(a),
88            _ => Err(CodamaError::InvalidCodamaDirective {
89                expected: "account".to_string(),
90                actual: attribute.directive.name().to_string(),
91            }),
92        }
93    }
94}
95
96impl<'a> TryFrom<&'a Attribute<'a>> for &'a AccountDirective {
97    type Error = CodamaError;
98
99    fn try_from(attribute: &'a Attribute) -> Result<Self, Self::Error> {
100        <&CodamaAttribute>::try_from(attribute)?.try_into()
101    }
102}
103
104#[cfg(test)]
105mod tests {
106    use super::*;
107    use codama_nodes::PayerValueNode;
108
109    #[test]
110    fn fully_set() {
111        let meta: Meta = syn::parse_quote! { account(name = "payer", writable, signer, optional, default_value = payer) };
112        let item = syn::parse_quote! { struct Foo; };
113        let ctx = AttributeContext::Item(&item);
114        let directive = AccountDirective::parse(&meta, &ctx).unwrap();
115        assert_eq!(
116            directive,
117            AccountDirective {
118                name: "payer".into(),
119                is_writable: true,
120                is_signer: IsAccountSigner::True,
121                is_optional: true,
122                default_value: Some(Resolvable::Resolved(PayerValueNode::new().into())),
123                docs: Docs::default(),
124            }
125        );
126    }
127
128    #[test]
129    fn fully_set_with_explicit_values() {
130        let meta: Meta = syn::parse_quote! { account(
131            name = "payer",
132            writable = true,
133            signer = "either",
134            optional = false,
135            default_value = payer
136        ) };
137        let item = syn::parse_quote! { struct Foo; };
138        let ctx = AttributeContext::Item(&item);
139        let directive = AccountDirective::parse(&meta, &ctx).unwrap();
140        assert_eq!(
141            directive,
142            AccountDirective {
143                name: "payer".into(),
144                is_writable: true,
145                is_signer: IsAccountSigner::Either,
146                is_optional: false,
147                default_value: Some(Resolvable::Resolved(PayerValueNode::new().into())),
148                docs: Docs::default(),
149            }
150        );
151    }
152
153    #[test]
154    fn empty_on_named_field() {
155        let meta: Meta = syn::parse_quote! { account };
156        let field = syn::parse_quote! { authority: AccountMeta };
157        let ctx = AttributeContext::Field(&field);
158        let directive = AccountDirective::parse(&meta, &ctx).unwrap();
159        assert_eq!(
160            directive,
161            AccountDirective {
162                name: "authority".into(),
163                is_writable: false,
164                is_signer: IsAccountSigner::False,
165                is_optional: false,
166                default_value: None,
167                docs: Docs::default(),
168            }
169        );
170    }
171
172    #[test]
173    fn empty_on_struct() {
174        let meta: Meta = syn::parse_quote! { account };
175        let item = syn::parse_quote! { struct Foo; };
176        let ctx = AttributeContext::Item(&item);
177        let error = AccountDirective::parse(&meta, &ctx).unwrap_err();
178        assert_eq!(error.to_string(), "name is missing");
179    }
180
181    #[test]
182    fn with_docs() {
183        let meta: Meta = syn::parse_quote! { account(name = "stake", writable, docs = "what this account is for") };
184        let item = syn::parse_quote! { struct Foo; };
185        let ctx = AttributeContext::Item(&item);
186        let directive = AccountDirective::parse(&meta, &ctx).unwrap();
187        assert_eq!(
188            directive,
189            AccountDirective {
190                name: "stake".into(),
191                is_writable: true,
192                is_signer: IsAccountSigner::False,
193                is_optional: false,
194                default_value: None,
195                docs: vec!["what this account is for".to_string()].into(),
196            }
197        );
198    }
199
200    #[test]
201    fn with_docs_array() {
202        let meta: Meta = syn::parse_quote! { account(name = "authority", signer, docs = ["Line 1", "Line 2", "Line 3"]) };
203        let item = syn::parse_quote! { struct Foo; };
204        let ctx = AttributeContext::Item(&item);
205        let directive = AccountDirective::parse(&meta, &ctx).unwrap();
206        assert_eq!(
207            directive,
208            AccountDirective {
209                name: "authority".into(),
210                is_writable: false,
211                is_signer: IsAccountSigner::True,
212                is_optional: false,
213                default_value: None,
214                docs: vec![
215                    "Line 1".to_string(),
216                    "Line 2".to_string(),
217                    "Line 3".to_string()
218                ]
219                .into(),
220            }
221        );
222    }
223}