checked_rs_macro_impl/params/
min_or_max.rs

1use proc_macro2::TokenStream;
2use quote::ToTokens;
3use syn::parse::Parse;
4
5use super::kw;
6
7/// Represents the `MIN` or `MAX` keyword.
8#[derive(Clone)]
9pub enum MinOrMax {
10    Min(kw::MIN),
11    Max(kw::MAX),
12}
13
14impl Parse for MinOrMax {
15    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
16        if input.peek(kw::MIN) {
17            Ok(Self::Min(input.parse()?))
18        } else if input.peek(kw::MAX) {
19            Ok(Self::Max(input.parse()?))
20        } else {
21            Err(input.error("expected `MIN` or `MAX`"))
22        }
23    }
24}
25
26impl ToTokens for MinOrMax {
27    fn to_tokens(&self, tokens: &mut TokenStream) {
28        match self {
29            Self::Min(kw) => kw.to_tokens(tokens),
30            Self::Max(kw) => kw.to_tokens(tokens),
31        }
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38    use crate::{assert_parse, snapshot};
39
40    #[test]
41    fn parse_min() {
42        assert_parse!(MinOrMax => { MIN } => { MinOrMax::Min(..) });
43    }
44
45    #[test]
46    fn parse_max() {
47        assert_parse!(MinOrMax => { MAX } => { MinOrMax::Max(..) });
48    }
49
50    #[test]
51    fn snapshot_min() {
52        snapshot!(MinOrMax => { MIN });
53    }
54
55    #[test]
56    fn snapshot_max() {
57        snapshot!(MinOrMax => { MAX });
58    }
59}