constrained_type/
error.rs1#![deny(missing_docs)]
2
3use std::fmt;
6
7use thiserror::Error;
8
9pub type ConstrainedTypeResult<T> = ::std::result::Result<T, ConstrainedTypeError>;
11
12#[derive(Error, Debug, Eq, PartialEq)]
14pub enum ConstrainedTypeErrorKind {
15 #[error("{field_name:?} must not be greater than {expected:?}, {found:?}")]
17 InvalidMaxVal {
18 field_name: String,
20 expected: String,
22 found: String,
24 },
25 #[error("{field_name:?} must not be less than {expected:?}, {found:?}")]
27 InvalidMinVal {
28 field_name: String,
30 expected: String,
32 found: String,
34 },
35 #[error("{field_name:?} does not match pattern {expected:?} for value {found:?}")]
37 InvalidPattern {
38 field_name: String,
40 expected: String,
42 found: String,
44 },
45 #[error("{field_name:?} must not be empty")]
47 InvalidOption {
48 field_name: String,
50 },
51 #[error("{field_name:?} must not be greater than {expected:?} characters, {found:?}")]
53 InvalidMaxLen {
54 field_name: String,
56 expected: String,
58 found: String,
60 },
61}
62
63#[derive(Error, Debug, Eq, PartialEq)]
65pub struct ConstrainedTypeError {
66 kind: ConstrainedTypeErrorKind,
67}
68
69impl ConstrainedTypeError {
70 pub fn kind(&self) -> &ConstrainedTypeErrorKind {
72 &self.kind
73 }
74}
75
76impl From<ConstrainedTypeErrorKind> for ConstrainedTypeError {
77 fn from(kind: ConstrainedTypeErrorKind) -> ConstrainedTypeError {
78 ConstrainedTypeError { kind }
79 }
80}
81
82impl<T> From<ConstrainedTypeError> for Result<T, ConstrainedTypeError> {
83 fn from(e: ConstrainedTypeError) -> Self {
84 Err(e)
85 }
86}
87
88impl fmt::Display for ConstrainedTypeError {
89 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
90 fmt::Display::fmt(&self.kind, f)
91 }
92}