codama_attributes/codama_directives/
program_directive.rs1use 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 other => other,
79 }
80 }
81}
82
83impl<'a> TryFrom<&'a CodamaAttribute<'a>> for &'a ProgramDirective {
84 type Error = CodamaError;
85
86 fn try_from(attribute: &'a CodamaAttribute) -> Result<Self, Self::Error> {
87 match attribute.directive.as_ref() {
88 CodamaDirective::Program(ref a) => Ok(a),
89 _ => Err(CodamaError::InvalidCodamaDirective {
90 expected: "program".to_string(),
91 actual: attribute.directive.name().to_string(),
92 }),
93 }
94 }
95}
96
97impl<'a> TryFrom<&'a Attribute<'a>> for &'a ProgramDirective {
98 type Error = CodamaError;
99
100 fn try_from(attribute: &'a Attribute) -> Result<Self, Self::Error> {
101 <&CodamaAttribute>::try_from(attribute)?.try_into()
102 }
103}
104
105#[cfg(test)]
106mod tests {
107 use super::*;
108
109 #[test]
110 fn ok() {
111 let meta: Meta = syn::parse_quote! { program(name = "associatedToken", address = "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL") };
112 let directive = ProgramDirective::parse(&meta).unwrap();
113 assert_eq!(
114 directive,
115 ProgramDirective {
116 name: CamelCaseString::from("associatedToken"),
117 address: "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL".to_string(),
118 }
119 );
120 }
121
122 #[test]
123 fn name_missing() {
124 let meta: Meta =
125 syn::parse_quote! { program(address = "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL") };
126 let error = ProgramDirective::parse(&meta).unwrap_err();
127 assert_eq!(error.to_string(), "name is missing");
128 }
129
130 #[test]
131 fn address_missing() {
132 let meta: Meta = syn::parse_quote! { program(name = "associatedToken") };
133 let error = ProgramDirective::parse(&meta).unwrap_err();
134 assert_eq!(error.to_string(), "address is missing");
135 }
136}