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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use quote::ToTokens;
use syn::{parse::Parse, parse2, spanned::Spanned as _, visit_mut::VisitMut as _, Attribute, Path};

use crate::visitor;

pub struct Options {
    pub containing_path: Path,
}

impl Options {
    pub fn from_attrs(attrs: &[Attribute]) -> syn::Result<Self> {
        let mut args = None;
        for attr in attrs {
            let mut segments_iter = attr.path().segments.iter();
            if let (Some(attr_name), None) = (segments_iter.next(), segments_iter.next()) {
                if attr_name.ident == "telety"
                    && args
                        .replace(parse2(attr.meta.require_list()?.tokens.clone())?)
                        .is_some()
                {
                    return Err(syn::Error::new(
                        attr.span(),
                        "Only one 'telety' attribute is allowed",
                    ));
                }
            }
        }

        args.ok_or_else(|| {
            syn::Error::new(
                attrs.first().span(),
                "'telety' attribute not found (aliasing the attribute is not supported)",
            )
        })
    }

    pub fn converted_containing_path(&self) -> Path {
        let mut containing_path = self.containing_path.clone();
        visitor::Crateify::new().visit_path_mut(&mut containing_path);
        
        containing_path
    }
}

impl Parse for Options {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        let mut containing_path: Path = input.parse()?;
        visitor::Decrateify::new().visit_path_mut(&mut containing_path);

        Ok(Self { containing_path })
    }
}

impl ToTokens for Options {
    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
        let Self {
            containing_path,
        } = self;

        containing_path.to_tokens(tokens);
    }
}