use super::super::{ConfigMetadata, FieldMetadata, ValidationCheck, ValidationValue};
impl ConfigMetadata {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn from_fields<I>(fields: I) -> Self
where
I: IntoIterator<Item = FieldMetadata>,
{
let mut metadata = Self::default();
metadata.extend_fields(fields);
metadata
}
pub fn push(&mut self, field: FieldMetadata) {
self.fields.push(field);
self.normalize();
}
pub fn extend_fields<I>(&mut self, fields: I)
where
I: IntoIterator<Item = FieldMetadata>,
{
self.fields.extend(fields);
self.normalize();
}
pub fn extend(&mut self, other: Self) {
self.fields.extend(other.fields);
self.checks.extend(other.checks);
self.normalize();
}
pub fn push_check(&mut self, check: ValidationCheck) {
self.checks.push(check);
self.normalize();
}
pub fn extend_checks<I>(&mut self, checks: I)
where
I: IntoIterator<Item = ValidationCheck>,
{
self.checks.extend(checks);
self.normalize();
}
#[must_use]
pub fn check(mut self, check: ValidationCheck) -> Self {
self.push_check(check);
self
}
#[must_use]
pub fn at_least_one_of<I, S>(self, paths: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.check(ValidationCheck::AtLeastOneOf {
paths: paths.into_iter().map(Into::into).collect(),
})
}
#[must_use]
pub fn exactly_one_of<I, S>(self, paths: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.check(ValidationCheck::ExactlyOneOf {
paths: paths.into_iter().map(Into::into).collect(),
})
}
#[must_use]
pub fn mutually_exclusive<I, S>(self, paths: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.check(ValidationCheck::MutuallyExclusive {
paths: paths.into_iter().map(Into::into).collect(),
})
}
#[must_use]
pub fn required_with<I, S>(self, path: impl Into<String>, requires: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.check(ValidationCheck::RequiredWith {
path: path.into(),
requires: requires.into_iter().map(Into::into).collect(),
})
}
#[must_use]
pub fn required_if<I, S, V>(self, path: impl Into<String>, equals: V, requires: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
V: Into<ValidationValue>,
{
self.check(ValidationCheck::RequiredIf {
path: path.into(),
equals: equals.into(),
requires: requires.into_iter().map(Into::into).collect(),
})
}
}