use crate::base::string_rules::{StringLengthRules, StringMandatoryRules};
use crate::common::locale::{ValidateErrorCollector, ValidateErrorStore};
use crate::common::string_validator::{StrValidationExtension, StringValidator};
use crate::common::validation_check::ValidationCheck;
use thiserror::Error;
pub struct DescriptionRules {
pub is_mandatory: bool,
pub min_length: Option<usize>,
pub max_length: Option<usize>,
}
impl Default for DescriptionRules {
fn default() -> Self {
Self {
is_mandatory: true,
min_length: None,
max_length: Some(40),
}
}
}
impl Into<(StringMandatoryRules, StringLengthRules)> for &DescriptionRules {
fn into(self) -> (StringMandatoryRules, StringLengthRules) {
(
StringMandatoryRules {
is_mandatory: self.is_mandatory,
},
StringLengthRules {
min_length: self.min_length,
max_length: self.max_length,
},
)
}
}
impl DescriptionRules {
fn rules(&self) -> (StringMandatoryRules, StringLengthRules) {
self.into()
}
fn check(
&self,
messages: &mut ValidateErrorCollector,
subject: &StringValidator,
is_none: bool,
) {
if !self.is_mandatory && is_none {
return;
}
let (mandatory_rule, length_rule) = self.rules();
mandatory_rule.check(messages, subject);
if !messages.is_empty() {
return;
}
length_rule.check(messages, subject);
}
}
#[derive(Debug, Error, PartialEq, Clone, Default)]
#[error("Description Validation Error")]
pub struct DescriptionError(pub ValidateErrorStore);
impl ValidationCheck for DescriptionError {
fn validate_new(messages: ValidateErrorStore) -> Self {
Self(messages)
}
}
impl Into<ValidateErrorStore> for &DescriptionError {
fn into(self) -> ValidateErrorStore {
self.0.clone()
}
}
#[derive(Debug, PartialEq, Clone)]
pub struct Description(String, bool);
#[cfg(any(feature = "allow-default-value", test))]
impl Default for Description {
fn default() -> Self {
Self(String::new(), true)
}
}
impl Description {
pub fn parse_custom(
s: Option<&str>,
rules: DescriptionRules,
) -> Result<Self, DescriptionError> {
let is_none = s.is_none();
let s = s.unwrap_or_default();
let subject = s.as_string_validator();
let mut messages = ValidateErrorCollector::new();
rules.check(&mut messages, &subject, is_none);
DescriptionError::validate_check(messages)?;
Ok(Self(s.to_string(), is_none))
}
pub fn parse(s: Option<&str>) -> Result<Self, DescriptionError> {
Self::parse_custom(s, DescriptionRules::default())
}
pub fn as_str(&self) -> &str {
&self.0
}
pub fn into_option(self) -> Option<Description> {
if self.1 { None } else { Some(self) }
}
}
impl Into<String> for &Description {
fn into(self) -> String {
self.as_str().to_string()
}
}
pub mod description_alias {
use super::*;
pub type TextRules = DescriptionRules;
pub type TextError = DescriptionError;
pub type Text = Description;
pub type BodyRules = DescriptionRules;
pub type BodyError = DescriptionError;
pub type Body = Description;
pub type SummaryRules = DescriptionRules;
pub type SummaryError = DescriptionError;
pub type Summary = Description;
pub type IngredientsRules = DescriptionRules;
pub type IngredientsError = DescriptionError;
pub type Ingredients = Description;
}