oar_ocr/core/config/
derive.rs1#[macro_export]
32macro_rules! impl_config_validator {
33 ($type_name:ident { $($field:ident: $validation:tt),* $(,)? }) => {
34 impl $crate::core::config::ConfigValidator for $type_name {
35 fn validate(&self) -> Result<(), $crate::core::config::ConfigError> {
36 $(
37 $crate::validate_field!(self, $field, $validation);
38 )*
39 Ok(())
40 }
41
42 fn get_defaults() -> Self
43 where
44 Self: Sized,
45 {
46 Self::default()
47 }
48 }
49 };
50}
51
52#[macro_export]
54macro_rules! validate_field {
55 ($self:expr, $field:ident, range($min:expr, $max:expr)) => {
56 if !($min..=$max).contains(&$self.$field) {
57 return Err($crate::core::config::ConfigError::InvalidConfig {
58 message: format!(
59 "{} must be between {} and {}",
60 stringify!($field),
61 $min,
62 $max
63 ),
64 });
65 }
66 };
67
68 ($self:expr, $field:ident, min($min_val:expr)) => {
69 if $self.$field < $min_val {
70 return Err($crate::core::config::ConfigError::InvalidConfig {
71 message: format!("{} must be at least {}", stringify!($field), $min_val),
72 });
73 }
74 };
75
76 ($self:expr, $field:ident, max($max_val:expr)) => {
77 if $self.$field > $max_val {
78 return Err($crate::core::config::ConfigError::InvalidConfig {
79 message: format!("{} must be at most {}", stringify!($field), $max_val),
80 });
81 }
82 };
83
84 ($self:expr, $field:ident, optional_range($min:expr, $max:expr)) => {
85 if let Some(value) = $self.$field {
86 if !($min..=$max).contains(&value) {
87 return Err($crate::core::config::ConfigError::InvalidConfig {
88 message: format!(
89 "{} must be between {} and {}",
90 stringify!($field),
91 $min,
92 $max
93 ),
94 });
95 }
96 }
97 };
98
99 ($self:expr, $field:ident, path) => {
100 $self.validate_model_path(&$self.$field)?;
101 };
102
103 ($self:expr, $field:ident, optional_path) => {
104 if let Some(ref path) = $self.$field {
105 $self.validate_model_path(path)?;
106 }
107 };
108}