codama_attributes/codama_directives/
encoding_directive.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use codama_nodes::BytesEncoding;
use codama_syn_helpers::{extensions::*, Meta};

#[derive(Debug, PartialEq)]
pub struct EncodingDirective {
    pub encoding: BytesEncoding,
}

impl EncodingDirective {
    pub fn parse(meta: &Meta) -> syn::Result<Self> {
        let pv = meta.assert_directive("encoding")?.as_path_value()?;
        let value = pv.value.as_path()?;
        match BytesEncoding::try_from(value.to_string()) {
            Ok(encoding) => Ok(Self { encoding }),
            _ => Err(value.error("invalid encoding")),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn ok() {
        let meta: Meta = syn::parse_quote! { encoding = base64 };
        let directive = EncodingDirective::parse(&meta).unwrap();
        assert_eq!(
            directive,
            EncodingDirective {
                encoding: BytesEncoding::Base64
            }
        );
    }
}