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
use syn::{punctuated::Punctuated, token::Comma, GenericParam, Meta, Path, Type, WherePredicate};

use crate::common::where_predicates_bool::{
    create_where_predicates_from_generic_parameters,
    create_where_predicates_from_generic_parameters_check_types, meta_2_where_predicates,
    WherePredicates, WherePredicatesOrBool,
};

pub(crate) enum Bound {
    Disabled,
    Auto,
    Custom(WherePredicates),
}

impl Bound {
    #[inline]
    pub(crate) fn from_meta(meta: &Meta) -> syn::Result<Self> {
        debug_assert!(meta.path().is_ident("bound"));

        Ok(match meta_2_where_predicates(meta)? {
            WherePredicatesOrBool::WherePredicates(where_predicates) => {
                Self::Custom(where_predicates)
            },
            WherePredicatesOrBool::Bool(b) => {
                if b {
                    Self::Auto
                } else {
                    Self::Disabled
                }
            },
        })
    }
}

impl Bound {
    #[inline]
    pub(crate) fn into_where_predicates_by_generic_parameters(
        self,
        params: &Punctuated<GenericParam, Comma>,
        bound_trait: &Path,
    ) -> Punctuated<WherePredicate, Comma> {
        match self {
            Self::Disabled => Punctuated::new(),
            Self::Auto => create_where_predicates_from_generic_parameters(params, bound_trait),
            Self::Custom(where_predicates) => where_predicates,
        }
    }

    #[inline]
    pub(crate) fn into_where_predicates_by_generic_parameters_check_types(
        self,
        params: &Punctuated<GenericParam, Comma>,
        bound_trait: &Path,
        types: &[&Type],
        recursive: Option<(bool, bool, bool)>,
    ) -> Punctuated<WherePredicate, Comma> {
        match self {
            Self::Disabled => Punctuated::new(),
            Self::Auto => create_where_predicates_from_generic_parameters_check_types(
                params,
                bound_trait,
                types,
                recursive,
            ),
            Self::Custom(where_predicates) => where_predicates,
        }
    }
}