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