NumberRangeRules

Struct NumberRangeRules 

Source
pub struct NumberRangeRules<T>{
    pub min: Option<T>,
    pub max: Option<T>,
}
Expand description

A struct that represents rules for defining a range of numeric values with optional minimum and maximum bounds.

This struct is generic and can work with any type T that meets the following trait bounds:

  • Clone: The type can be cloned.
  • Into<LocaleValue>: The type can be converted into a LocaleValue. This allows for localization support.
  • Default: The type has a default value.
  • PartialOrd: The type supports partial ordering, enabling comparisons like less than or greater than.
  • Display: The type can be formatted as a string for display purposes.

§Fields

  • min (Option<T>): The optional lower bound of the range. If None, there is no restriction on the minimum value.
  • max (Option<T>): The optional upper bound of the range. If None, there is no restriction on the maximum value.

Fields§

§min: Option<T>§max: Option<T>

Implementations§

Source§

impl<T> NumberRangeRules<T>

Source

pub fn check(&self, messages: &mut ValidateErrorCollector, subject: Option<T>)

Validates a given subject against optional minimum and maximum value constraints.

§Parameters
  • &self: A reference to the current instance of the object containing validation constraints.
  • messages: A mutable reference to a ValidateErrorCollector, where validation error messages will be stored if the subject does not meet the constraints.
  • subject: An optional value of type T to be validated against the constraints.
§Behavior
  • If the subject is Some:
    • It checks whether the value is less than the optional minimum value (self.min).
      • If the value is less, an error message is added to messages stating that the value must be at least the specified minimum.
    • It checks whether the value is greater than the optional maximum value (self.max).
      • If the value is greater, an error message is added to messages stating that the value must be at most the specified maximum.
  • If the subject is None, the default value is used during validation (T::default()).
  • Does nothing if both self.min and self.max are None.
§Usage

This function is intended to validate numerical ranges or similar constraints. The errors detected during validation are collected into the ValidateErrorCollector provided in the messages parameter.

§Examples
use cjtoolkit_structured_validator::common::locale::ValidateErrorCollector;
use cjtoolkit_structured_validator::base::number_rules::NumberRangeRules;
let mut error_collector = ValidateErrorCollector::new();
let validator = NumberRangeRules::<usize> {
    min: Some(10),
    max: Some(100),
};

validator.check(&mut error_collector, Some(5));   // Value too small, error is added.
validator.check(&mut error_collector, Some(105)); // Value too large, error is added.
validator.check(&mut error_collector, Some(50));  // Valid value, no error.
§Note
  • It is assumed that T implements the Default, PartialOrd, and Clone traits.
  • The ValidateErrorCollector and NumberRangeLocale types are expected to support the operations shown above.

Auto Trait Implementations§

§

impl<T> Freeze for NumberRangeRules<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for NumberRangeRules<T>
where T: RefUnwindSafe,

§

impl<T> Send for NumberRangeRules<T>
where T: Send,

§

impl<T> Sync for NumberRangeRules<T>
where T: Sync,

§

impl<T> Unpin for NumberRangeRules<T>
where T: Unpin,

§

impl<T> UnwindSafe for NumberRangeRules<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.