codama_attributes/codama_directives/
pda_directive.rs1use crate::{Attribute, CodamaAttribute, CodamaDirective};
2use codama_errors::CodamaError;
3use codama_nodes::PdaLinkNode;
4use codama_syn_helpers::{extensions::*, Meta};
5
6#[derive(Debug, PartialEq)]
7pub struct PdaDirective {
8 pub pda: PdaLinkNode,
9}
10
11impl PdaDirective {
12 pub fn parse(meta: &Meta) -> syn::Result<Self> {
13 let name = meta
14 .assert_directive("pda")?
15 .as_value()?
16 .as_expr()?
17 .as_string()?;
18 Ok(Self {
19 pda: PdaLinkNode::new(name),
20 })
21 }
22}
23
24impl<'a> TryFrom<&'a CodamaAttribute<'a>> for &'a PdaDirective {
25 type Error = CodamaError;
26
27 fn try_from(attribute: &'a CodamaAttribute) -> Result<Self, Self::Error> {
28 match attribute.directive {
29 CodamaDirective::Pda(ref a) => Ok(a),
30 _ => Err(CodamaError::InvalidCodamaDirective {
31 expected: "seed".to_string(),
32 actual: attribute.directive.name().to_string(),
33 }),
34 }
35 }
36}
37
38impl<'a> TryFrom<&'a Attribute<'a>> for &'a PdaDirective {
39 type Error = CodamaError;
40
41 fn try_from(attribute: &'a Attribute) -> Result<Self, Self::Error> {
42 <&CodamaAttribute>::try_from(attribute)?.try_into()
43 }
44}
45
46#[cfg(test)]
47mod tests {
48 use super::*;
49
50 #[test]
51 fn ok() {
52 let meta: Meta = syn::parse_quote! { pda = "my_pda" };
53 let directive = PdaDirective::parse(&meta).unwrap();
54 assert_eq!(
55 directive,
56 PdaDirective {
57 pda: PdaLinkNode::new("my_pda"),
58 }
59 );
60 }
61
62 #[test]
63 fn name_missing() {
64 let meta: Meta = syn::parse_quote! { pda };
65 let error = PdaDirective::parse(&meta).unwrap_err();
66 assert_eq!(
67 error.to_string(),
68 "expected a value for that path: `pda = ...`"
69 );
70 }
71}