bon_macros/util/
meta_list.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
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
63
64
65
66
use crate::util::prelude::*;

pub(crate) trait MetaListExt {
    fn require_parens_delim(&self) -> Result<()>;
    fn require_curly_braces_delim(&self) -> Result<()>;
}

impl MetaListExt for syn::MetaList {
    fn require_parens_delim(&self) -> Result<()> {
        require_delim(self, MacroDelimKind::Paren)
    }

    fn require_curly_braces_delim(&self) -> Result<()> {
        require_delim(self, MacroDelimKind::Brace)
    }
}

fn require_delim(meta: &syn::MetaList, expected: MacroDelimKind) -> Result<()> {
    let actual = MacroDelimKind::from_syn(&meta.delimiter);
    if actual == expected {
        return Ok(());
    }

    let path = darling::util::path_to_string(&meta.path);
    bail!(
        meta,
        "wrong delimiter, expected {} e.g. `{path}{}`, but got {}: `{path}{}`",
        expected.name(),
        expected.example(),
        actual.name(),
        actual.example(),
    );
}

#[derive(PartialEq, Eq, Clone, Copy)]
enum MacroDelimKind {
    Paren,
    Brace,
    Bracket,
}

impl MacroDelimKind {
    fn from_syn(delim: &syn::MacroDelimiter) -> Self {
        match delim {
            syn::MacroDelimiter::Paren(_) => Self::Paren,
            syn::MacroDelimiter::Brace(_) => Self::Brace,
            syn::MacroDelimiter::Bracket(_) => Self::Bracket,
        }
    }

    fn name(self) -> &'static str {
        match self {
            Self::Paren => "parentheses",
            Self::Brace => "curly braces",
            Self::Bracket => "square brackets",
        }
    }

    fn example(self) -> &'static str {
        match self {
            Self::Paren => "(...)",
            Self::Brace => "{...}",
            Self::Bracket => "[...]",
        }
    }
}