codama_syn_helpers/extensions/
parse_buffer.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use proc_macro2::{TokenStream, TokenTree};
use syn::{parse::ParseBuffer, Token};

pub trait ParseBufferExtension<'a> {
    fn get_self(&self) -> &ParseBuffer<'a>;

    /// Advance the buffer until we reach a comma or the end of the buffer.
    /// Returns the consumed tokens as a `TokenStream`.
    fn parse_arg(&self) -> syn::Result<TokenStream> {
        self.get_self().step(|cursor| {
            let mut tts = Vec::new();
            let mut rest = *cursor;
            while let Some((tt, next)) = rest.token_tree() {
                match &tt {
                    TokenTree::Punct(punct) if punct.as_char() == ',' => {
                        return Ok((tts.into_iter().collect(), rest));
                    }
                    _ => {
                        tts.push(tt);
                        rest = next
                    }
                }
            }
            Ok((tts.into_iter().collect(), rest))
        })
    }

    /// Fork the current buffer and move the original buffer to the end of the argument.
    fn fork_arg(&self) -> syn::Result<ParseBuffer<'a>> {
        let this = self.get_self();
        let fork = this.fork();
        this.parse_arg()?;
        Ok(fork)
    }

    // Check if the buffer is empty or the next token is a comma.
    fn is_end_of_arg(&self) -> bool {
        let this = self.get_self();
        this.is_empty() || this.peek(Token![,])
    }

    /// Check if the next token is an empty group.
    fn is_empty_group(&self) -> bool {
        match self.get_self().fork().parse::<proc_macro2::Group>() {
            Ok(group) => group.stream().is_empty(),
            Err(_) => false,
        }
    }
}

impl<'a> ParseBufferExtension<'a> for ParseBuffer<'a> {
    fn get_self(&self) -> &ParseBuffer<'a> {
        self
    }
}

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

    macro_rules! test_buffer {
        ($ty:ty, $callback:expr) => {{
            struct TestBuffer($ty);
            impl syn::parse::Parse for TestBuffer {
                fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
                    let result = ($callback)(input)?; // Call the provided callback
                    input.parse::<proc_macro2::TokenStream>()?; // Consume the rest of the input
                    Ok(Self(result))
                }
            }
            move |input: &str| -> syn::Result<$ty> {
                syn::parse_str::<TestBuffer>(input).map(|x| x.0)
            }
        }};
    }

    #[test]
    fn parse_arg() {
        let test = test_buffer!(
            (String, String),
            |input: syn::parse::ParseStream| -> syn::Result<(String, String)> {
                let arg = input.parse_arg()?;
                Ok((arg.to_string(), input.to_string()))
            }
        );

        assert_eq!(
            test("foo , bar , baz").unwrap(),
            ("foo".into(), ", bar , baz".into())
        );
        assert_eq!(
            test(", bar , baz").unwrap(),
            ("".into(), ", bar , baz".into())
        );
        assert_eq!(
            test("(foo , bar), baz").unwrap(),
            ("(foo , bar)".into(), ", baz".into())
        );
        assert_eq!(
            test("foo bar baz").unwrap(),
            ("foo bar baz".into(), "".into())
        );
        assert_eq!(test("").unwrap(), ("".into(), "".into()));
    }

    #[test]
    fn fork_arg() {
        let test = test_buffer!(
            (String, String),
            |input: syn::parse::ParseStream| -> syn::Result<(String, String)> {
                let arg = input.fork_arg()?;
                Ok((arg.to_string(), input.to_string()))
            }
        );

        assert_eq!(
            test("foo , bar , baz").unwrap(),
            ("foo , bar , baz".into(), ", bar , baz".into())
        );
        assert_eq!(
            test("foo bar baz").unwrap(),
            ("foo bar baz".into(), "".into())
        );
    }

    #[test]
    fn is_end_of_arg() {
        let test = test_buffer!(
            bool,
            |input: syn::parse::ParseStream| -> syn::Result<bool> { Ok(input.is_end_of_arg()) }
        );

        assert!(test("").unwrap());
        assert!(test(", bar").unwrap());
        assert!(!test("foo").unwrap());
    }

    #[test]
    fn is_empty_group() {
        let test = test_buffer!(
            bool,
            |input: syn::parse::ParseStream| -> syn::Result<bool> { Ok(input.is_empty_group()) }
        );

        assert!(test("()").unwrap());
        assert!(test("[]").unwrap());
        assert!(test("{}").unwrap());
        assert!(!test("").unwrap());
        assert!(!test("foo").unwrap());
        assert!(!test("(foo)").unwrap());
    }
}