Skip to main content

codama_attributes/codama_directives/
program_directive.rs

1use crate::{utils::SetOnce, Attribute, CodamaAttribute, CodamaDirective, TryFromFilter};
2use codama_errors::CodamaError;
3use codama_nodes::{CamelCaseString, Node, ProgramNode};
4use codama_syn_helpers::{extensions::*, Meta};
5
6#[derive(Debug, PartialEq)]
7pub struct ProgramDirective {
8    pub name: CamelCaseString,
9    pub address: String,
10}
11
12impl ProgramDirective {
13    pub fn parse(meta: &Meta) -> syn::Result<Self> {
14        let pl = meta.assert_directive("program")?.as_path_list()?;
15
16        let mut name = SetOnce::<CamelCaseString>::new("name");
17        let mut address = SetOnce::<String>::new("address");
18
19        pl.each(|ref meta| match meta.path_str().as_str() {
20            "name" => name.set(meta.as_value()?.as_expr()?.as_string()?.into(), meta),
21            "address" => address.set(meta.as_value()?.as_expr()?.as_string()?, meta),
22            _ => Err(meta.error("unrecognized attribute")),
23        })?;
24
25        Ok(Self {
26            name: name.take(meta)?,
27            address: address.take(meta)?,
28        })
29    }
30
31    pub fn apply(attributes: &crate::Attributes, node: Node) -> Node {
32        match attributes.get_last(Self::filter) {
33            Some(pd) => pd.update_or_wrap_program_node(node),
34            None => node,
35        }
36    }
37
38    pub fn update_or_wrap_program_node(&self, node: Node) -> Node {
39        match node {
40            Node::Root(mut root) => {
41                root.program.name = self.name.clone();
42                root.program.public_key = self.address.clone();
43                root.into()
44            }
45            Node::Program(mut program) => {
46                program.name = self.name.clone();
47                program.public_key = self.address.clone();
48                program.into()
49            }
50            Node::Account(account) => ProgramNode {
51                name: self.name.clone(),
52                public_key: self.address.clone(),
53                accounts: vec![account],
54                ..ProgramNode::default()
55            }
56            .into(),
57            Node::Instruction(instruction) => ProgramNode {
58                name: self.name.clone(),
59                public_key: self.address.clone(),
60                instructions: vec![instruction],
61                ..ProgramNode::default()
62            }
63            .into(),
64            Node::Error(error) => ProgramNode {
65                name: self.name.clone(),
66                public_key: self.address.clone(),
67                errors: vec![error],
68                ..ProgramNode::default()
69            }
70            .into(),
71            Node::Pda(pda) => ProgramNode {
72                name: self.name.clone(),
73                public_key: self.address.clone(),
74                pdas: vec![pda],
75                ..ProgramNode::default()
76            }
77            .into(),
78            Node::Event(event) => ProgramNode {
79                name: self.name.clone(),
80                public_key: self.address.clone(),
81                events: vec![event],
82                ..ProgramNode::default()
83            }
84            .into(),
85            other => other,
86        }
87    }
88}
89
90impl<'a> TryFrom<&'a CodamaAttribute<'a>> for &'a ProgramDirective {
91    type Error = CodamaError;
92
93    fn try_from(attribute: &'a CodamaAttribute) -> Result<Self, Self::Error> {
94        match attribute.directive.as_ref() {
95            CodamaDirective::Program(ref a) => Ok(a),
96            _ => Err(CodamaError::InvalidCodamaDirective {
97                expected: "program".to_string(),
98                actual: attribute.directive.name().to_string(),
99            }),
100        }
101    }
102}
103
104impl<'a> TryFrom<&'a Attribute<'a>> for &'a ProgramDirective {
105    type Error = CodamaError;
106
107    fn try_from(attribute: &'a Attribute) -> Result<Self, Self::Error> {
108        <&CodamaAttribute>::try_from(attribute)?.try_into()
109    }
110}
111
112#[cfg(test)]
113mod tests {
114    use super::*;
115
116    #[test]
117    fn ok() {
118        let meta: Meta = syn::parse_quote! { program(name = "associatedToken", address = "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL") };
119        let directive = ProgramDirective::parse(&meta).unwrap();
120        assert_eq!(
121            directive,
122            ProgramDirective {
123                name: CamelCaseString::from("associatedToken"),
124                address: "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL".to_string(),
125            }
126        );
127    }
128
129    #[test]
130    fn name_missing() {
131        let meta: Meta =
132            syn::parse_quote! { program(address = "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL") };
133        let error = ProgramDirective::parse(&meta).unwrap_err();
134        assert_eq!(error.to_string(), "name is missing");
135    }
136
137    #[test]
138    fn address_missing() {
139        let meta: Meta = syn::parse_quote! { program(name = "associatedToken") };
140        let error = ProgramDirective::parse(&meta).unwrap_err();
141        assert_eq!(error.to_string(), "address is missing");
142    }
143}