use codec::{Decode, Encode}; #[cfg(feature = "std")] use serde::{Deserialize, Serialize}; /// Length constraint for input validation #[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))] #[derive(Encode, Decode, Default, Clone, PartialEq, Eq, Copy)] pub struct InputValidationLengthConstraint { /// Minimum length pub min: u16, /// Difference between minimum length and max length. /// While having max would have been more direct, this /// way makes max < min unrepresentable semantically, /// which is safer. pub max_min_diff: u16, } impl InputValidationLengthConstraint { /// Helper for computing max pub fn max(self) -> u16 { self.min + self.max_min_diff } pub fn ensure_valid( self, len: usize, too_short_msg: &'static str, too_long_msg: &'static str, ) -> Result<(), &'static str> { let length = len as u16; if length < self.min { Err(too_short_msg) } else if length > self.max() { Err(too_long_msg) } else { Ok(()) } } /// Create default input text constraints. pub fn new(min: u16, max_min_diff: u16) -> Self { InputValidationLengthConstraint { min, max_min_diff } } }