checked_rs_macro_impl/params/
panic_or_panicking.rs

1use proc_macro2::TokenStream;
2use quote::ToTokens;
3use syn::parse::Parse;
4
5use super::kw;
6
7/// Represents the `Saturate` or `Saturating` keyword.
8#[derive(Clone)]
9pub enum PanicOrPanicking {
10    Panic(kw::Panic),
11    Panicking(kw::Panicking),
12}
13
14impl Parse for PanicOrPanicking {
15    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
16        if input.peek(kw::Panic) {
17            Ok(Self::Panic(input.parse()?))
18        } else if input.peek(kw::Panicking) {
19            Ok(Self::Panicking(input.parse()?))
20        } else {
21            Err(input.error("expected `Panic` or `Panicking`"))
22        }
23    }
24}
25
26impl ToTokens for PanicOrPanicking {
27    fn to_tokens(&self, tokens: &mut TokenStream) {
28        match self {
29            Self::Panic(kw) => kw.to_tokens(tokens),
30            Self::Panicking(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_panic() {
42        assert_parse!(PanicOrPanicking => { Panic } => { PanicOrPanicking::Panic(..) });
43    }
44
45    #[test]
46    fn parse_panicking() {
47        assert_parse!(PanicOrPanicking => { Panicking } => { PanicOrPanicking::Panicking(..) });
48    }
49
50    #[test]
51    fn snapshot_panic() {
52        snapshot!(PanicOrPanicking => { Panic });
53    }
54
55    #[test]
56    fn snapshot_panicking() {
57        snapshot!(PanicOrPanicking => { Panicking });
58    }
59}