prost_validate/errors/
number.rs

1#[macro_export]
2macro_rules! make_error {
3    ($name:ident, $typ:ident, $enum_value:ident) => {
4        pub mod $name {
5            use thiserror::Error;
6
7            #[derive(Debug, Clone, Error)]
8            pub enum Error {
9                #[error("must be equal to {0}")]
10                Const($typ),
11                #[error("must be less than {0}")]
12                Lt($typ),
13                #[error("must be less than or equal to {0}")]
14                Lte($typ),
15                #[error("must be greater than {0}")]
16                Gt($typ),
17                #[error("must be greater than or equal to {0:?}")]
18                Gte($typ),
19                #[error("must be in range {0}{1}, {2}{3}")]
20                InRange(String, $typ, $typ, String),
21                #[error("must not be in range {0}{1}, {2}{3}")]
22                NotInRange(String, $typ, $typ, String),
23                #[error("must be in {0:?}")]
24                In(Vec<$typ>),
25                #[error("must not be in {0:?}")]
26                NotIn(Vec<$typ>),
27            }
28
29            impl Error {
30                pub fn in_range(
31                    start_inclusive: bool,
32                    start: $typ,
33                    end: $typ,
34                    end_inclusive: bool,
35                ) -> Self {
36                    Self::InRange(
37                        if start_inclusive { "[" } else { "(" }.to_string(),
38                        start,
39                        end,
40                        if end_inclusive { "]" } else { ")" }.to_string(),
41                    )
42                }
43                pub fn not_in_range(
44                    start_inclusive: bool,
45                    start: $typ,
46                    end: $typ,
47                    end_inclusive: bool,
48                ) -> Self {
49                    Self::NotInRange(
50                        if start_inclusive { "[" } else { "(" }.to_string(),
51                        start,
52                        end,
53                        if end_inclusive { "]" } else { ")" }.to_string(),
54                    )
55                }
56            }
57            impl From<Error> for $crate::errors::Error {
58                fn from(value: Error) -> Self {
59                    Self::$enum_value(value)
60                }
61            }
62        }
63    };
64}