use serde_json::Value;
use std::ops::RangeInclusive;
use crate::error::{IssueCode, PathSegment, VldError, absent_field_error};
use crate::schema::VldSchema;
#[derive(Clone)]
pub struct ZRange<S> {
item: S,
start: String,
end: String,
messages: crate::primitives::Messages,
}
impl<S: VldSchema> ZRange<S> {
pub fn new(item: S) -> Self {
Self {
item,
start: "start".to_string(),
end: "end".to_string(),
messages: None,
}
}
pub fn keys(mut self, start: impl Into<String>, end: impl Into<String>) -> Self {
self.start = start.into();
self.end = end.into();
self
}
pub fn with_messages<F>(mut self, f: F) -> Self
where
F: Fn(&str) -> Option<String> + Send + Sync + 'static,
{
self.messages = Some(std::sync::Arc::new(f));
self
}
fn field(
&self,
obj: &serde_json::Map<String, Value>,
key: &str,
) -> Result<S::Output, VldError> {
match obj.get(key) {
Some(v) => self.item.parse_value(v),
None => self.item.parse_missing().map_err(absent_field_error),
}
.map_err(|e| e.with_prefix(PathSegment::Field(key.to_string())))
}
}
impl<S: VldSchema> VldSchema for ZRange<S>
where
S::Output: PartialOrd,
{
type Output = RangeInclusive<S::Output>;
fn parse_value(&self, value: &Value) -> Result<Self::Output, VldError> {
let obj = crate::error::expect_object(value)?;
match (self.field(obj, &self.start), self.field(obj, &self.end)) {
(Ok(start), Ok(end)) if end < start => {
let code = IssueCode::RangeOrder {
start: self.start.clone(),
end: self.end.clone(),
};
let msg = self.messages.as_ref().and_then(|f| f("range_order"));
let received = obj.get(&self.end).unwrap_or(&Value::Null);
Err(crate::error::issue_error(code, msg.as_ref(), received)
.with_prefix(PathSegment::Field(self.end.clone())))
}
(Ok(start), Ok(end)) => Ok(start..=end),
(a, b) => Err(a
.err()
.unwrap_or_default()
.merge(b.err().unwrap_or_default())),
}
}
}
#[cfg(feature = "openapi")]
impl<S: crate::openapi::JsonSchema> crate::openapi::JsonSchema for ZRange<S> {
fn json_schema(&self) -> Value {
let item = self.item.json_schema();
let mut props = serde_json::Map::new();
props.insert(self.start.clone(), item.clone());
props.insert(self.end.clone(), item);
serde_json::json!({
"type": "object",
"properties": props,
"required": [self.start, self.end],
"x-rangeOrder": [self.start, self.end],
})
}
}
#[cfg(feature = "openapi")]
impl<S: crate::openapi::CollectNestedSchemas> crate::openapi::CollectNestedSchemas for ZRange<S> {
fn collect_nested_schemas(&self, out: &mut Vec<crate::schema::NestedSchemaEntry>) {
self.item.collect_nested_schemas(out)
}
}