codama_attributes/codama_directives/
codama_directive.rs

1use crate::{
2    AttributeContext, EncodingDirective, FixedSizeDirective, SizePrefixDirective, TypeDirective,
3};
4use codama_syn_helpers::{extensions::*, Meta};
5use derive_more::derive::From;
6
7use super::AccountDirective;
8
9#[derive(Debug, PartialEq, From)]
10pub enum CodamaDirective {
11    // Type directives.
12    Type(TypeDirective),
13    Encoding(EncodingDirective),
14    FixedSize(FixedSizeDirective),
15    SizePrefix(SizePrefixDirective),
16
17    // Instruction directives.
18    Account(AccountDirective),
19}
20
21impl CodamaDirective {
22    pub fn parse(meta: &Meta, ctx: &AttributeContext) -> syn::Result<Self> {
23        let path = meta.path()?;
24        match path.to_string().as_str() {
25            // Type directives.
26            "type" => Ok(TypeDirective::parse(meta)?.into()),
27            "encoding" => Ok(EncodingDirective::parse(meta)?.into()),
28            "fixed_size" => Ok(FixedSizeDirective::parse(meta)?.into()),
29            "size_prefix" => Ok(SizePrefixDirective::parse(meta)?.into()),
30
31            // Instruction directives.
32            "account" => Ok(AccountDirective::parse(meta, ctx)?.into()),
33
34            _ => Err(path.error("unrecognized codama directive")),
35        }
36    }
37
38    pub fn name(&self) -> &'static str {
39        match self {
40            // Type directives.
41            CodamaDirective::Type(_) => "type",
42            CodamaDirective::Encoding(_) => "encoding",
43            CodamaDirective::FixedSize(_) => "fixed_size",
44            CodamaDirective::SizePrefix(_) => "size_prefix",
45
46            // Instruction directives.
47            CodamaDirective::Account(_) => "account",
48        }
49    }
50}