Skip to content

Commit

Permalink
Implement Constraint for SingleValueSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
TimDiekmann committed Oct 17, 2024
1 parent a63dac2 commit afe817b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;

use crate::schema::{
ConstraintError, SingleValueSchema, ValueLabel,
ConstraintError, SingleValueSchema,
data_type::{
closed::ResolveClosedDataTypeError,
constraint::{Constraint, ConstraintValidator, ValueConstraints},
Expand Down Expand Up @@ -50,32 +50,22 @@ impl Constraint for AnyOfConstraints {
.into_iter()
.cartesian_product(other.any_of.clone())
{
let (constraints, remainder) = match lhs.constraints.combine(rhs.constraints) {
let (constraints, remainder) = match lhs.intersection(rhs) {
Ok((constraints, remainder)) => (constraints, remainder),
Err(error) => {
errors.push(Err(error));
continue;
}
};

let (description, label) = if lhs.description.is_none() && lhs.label.is_empty() {
(rhs.description, rhs.label)
} else {
(lhs.description, lhs.label)
};
combined_constraints.push(SingleValueSchema {
description,
label,
constraints,
});
combined_constraints.push(constraints);
if let Some(remainder) = remainder {
remainders.push(remainder);
}
}

match combined_constraints.len() {
0 => {
// We now properly capture errors to return it to the caller.
let _: Vec<()> = errors
.into_iter()
.try_collect_reports()
Expand All @@ -86,12 +76,8 @@ impl Constraint for AnyOfConstraints {
Self {
any_of: combined_constraints,
},
remainders.pop().map(|constraints| Self {
any_of: vec![SingleValueSchema {
constraints,
description: None,
label: ValueLabel::default(),
}],
remainders.pop().map(|schema| Self {
any_of: vec![schema],
}),
)),
_ => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl Constraint for ValueConstraints {
label: ValueLabel::default(),
}],
};
lhs.combine(rhs)
lhs.intersection(rhs)
.map(|(lhs, rhs)| (Self::from(lhs), rhs.map(Self::from)))
}
(Self::Typed(lhs), Self::AnyOf(rhs)) => {
Expand All @@ -150,7 +150,7 @@ impl Constraint for ValueConstraints {
label: ValueLabel::default(),
}],
};
lhs.combine(rhs)
lhs.intersection(rhs)
.map(|(lhs, rhs)| (Self::from(lhs), rhs.map(Self::from)))
}
(Self::AnyOf(lhs), Self::AnyOf(rhs)) => lhs
Expand Down Expand Up @@ -256,6 +256,33 @@ pub struct SingleValueSchema {
pub constraints: SingleValueConstraints,
}

impl Constraint for SingleValueSchema {
fn intersection(
self,
other: Self,
) -> Result<(Self, Option<Self>), Report<ResolveClosedDataTypeError>> {
let (combined, remainder) = self.constraints.intersection(other.constraints)?;
let (description, label) = if self.description.is_none() && self.label.is_empty() {
(other.description, other.label)
} else {
(self.description, self.label)
};

Ok((
Self {
description,
label,
constraints: combined,
},
remainder.map(|remainder| Self {
constraints: remainder,
description: None,
label: ValueLabel::default(),
}),
))
}
}

#[cfg(target_arch = "wasm32")]
#[expect(
dead_code,
Expand Down

0 comments on commit afe817b

Please sign in to comment.