fieldwork/
method.rs

1use proc_macro2::Span;
2use syn::{Error, Path, spanned::Spanned};
3
4#[derive(PartialEq, Eq, Hash, Copy, Clone, Debug)]
5pub(crate) enum Method {
6    Get,
7    Set,
8    With,
9    GetMut,
10}
11
12impl Method {
13    pub(crate) const fn all() -> &'static [Method] {
14        &[Self::Get, Self::GetMut, Self::Set, Self::With]
15    }
16
17    pub(crate) fn from_str_with_span(s: &str, span: Span) -> syn::Result<Self> {
18        match s {
19            "get" => Ok(Self::Get),
20            "set" => Ok(Self::Set),
21            "with" => Ok(Self::With),
22            "get_mut" => Ok(Self::GetMut),
23            _ => Err(Error::new(span, "unrecognized method")),
24        }
25    }
26}
27
28impl TryFrom<&Path> for Method {
29    type Error = Error;
30
31    fn try_from(path: &Path) -> Result<Self, Self::Error> {
32        if path.is_ident("get") {
33            Ok(Self::Get)
34        } else if path.is_ident("set") {
35            Ok(Self::Set)
36        } else if path.is_ident("with") {
37            Ok(Self::With)
38        } else if path.is_ident("get_mut") {
39            Ok(Self::GetMut)
40        } else {
41            Err(Error::new(path.span(), "unrecognized method"))
42        }
43    }
44}