use std::borrow::Cow;
use crate::Problem;
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Rules {
fields: Vec<FieldRules>,
}
impl Rules {
pub fn of_fields(fields: impl IntoIterator<Item = FieldRules>) -> Self {
Self {
fields: fields.into_iter().collect(),
}
}
#[must_use]
pub fn fields(&self) -> &[FieldRules] {
&self.fields
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct FieldRules {
name: Cow<'static, str>,
has_default: bool,
rules: ValueRules,
}
impl FieldRules {
pub fn new(name: impl Into<Cow<'static, str>>, rules: ValueRules) -> Self {
Self {
name: name.into(),
has_default: false,
rules,
}
}
#[must_use]
pub fn with_default(self) -> Self {
Self {
has_default: true,
..self
}
}
#[must_use]
pub fn name(&self) -> &str {
&self.name
}
#[must_use]
pub fn has_default(&self) -> bool {
self.has_default
}
#[must_use]
pub fn rules(&self) -> &ValueRules {
&self.rules
}
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct ValueRules {
is_optional: bool,
problems: Vec<Problem>,
elements: Option<Box<ValueRules>>,
}
impl ValueRules {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn optional() -> Self {
Self {
is_optional: true,
..Self::default()
}
}
#[must_use]
pub fn with_problem(mut self, problem: Problem) -> Self {
self.problems.push(problem);
self
}
#[must_use]
pub fn with_elements(self, elements: ValueRules) -> Self {
Self {
elements: Some(Box::new(elements)),
..self
}
}
#[must_use]
pub fn is_optional(&self) -> bool {
self.is_optional
}
#[must_use]
pub fn problems(&self) -> &[Problem] {
&self.problems
}
#[must_use]
pub fn elements(&self) -> Option<&ValueRules> {
self.elements.as_deref()
}
}