educe/common/
bound.rs

1use syn::{punctuated::Punctuated, token::Comma, GenericParam, Meta, Path, Type, WherePredicate};
2
3use crate::common::where_predicates_bool::{
4    create_where_predicates_from_all_generic_parameters,
5    create_where_predicates_from_generic_parameters_check_types, meta_2_where_predicates,
6    WherePredicates, WherePredicatesOrBool,
7};
8
9pub(crate) enum Bound {
10    Disabled,
11    Auto,
12    Custom(WherePredicates),
13    All,
14}
15
16impl Bound {
17    #[inline]
18    pub(crate) fn from_meta(meta: &Meta) -> syn::Result<Self> {
19        debug_assert!(meta.path().is_ident("bound"));
20
21        Ok(match meta_2_where_predicates(meta)? {
22            WherePredicatesOrBool::WherePredicates(where_predicates) => {
23                Self::Custom(where_predicates)
24            },
25            WherePredicatesOrBool::Bool(b) => {
26                if b {
27                    Self::Auto
28                } else {
29                    Self::Disabled
30                }
31            },
32            WherePredicatesOrBool::All => Self::All,
33        })
34    }
35}
36
37impl Bound {
38    #[inline]
39    pub(crate) fn into_where_predicates_by_generic_parameters_check_types(
40        self,
41        params: &Punctuated<GenericParam, Comma>,
42        bound_trait: &Path,
43        types: &[&Type],
44        supertraits: &[proc_macro2::TokenStream],
45    ) -> Punctuated<WherePredicate, Comma> {
46        match self {
47            Self::Disabled => Punctuated::new(),
48            Self::Auto => create_where_predicates_from_generic_parameters_check_types(
49                bound_trait,
50                types,
51                supertraits,
52            ),
53            Self::Custom(where_predicates) => where_predicates,
54            Self::All => create_where_predicates_from_all_generic_parameters(params, bound_trait),
55        }
56    }
57}