use serde_json::{Map, Value};
use crate::error::{IssueCode, PathSegment, SizeOrigin, VldError};
use crate::primitives::ZNumber;
use crate::schema::VldSchema;
#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct GeoPoint {
pub lat: f64,
pub lng: f64,
}
fn field(key: &str) -> PathSegment {
PathSegment::Field(key.to_string())
}
fn issue(code: IssueCode, value: &Value) -> VldError {
VldError::from_code_with_value(code, value)
}
fn missing(key: &str) -> VldError {
VldError::from_code(IssueCode::MissingField).with_prefix(field(key))
}
fn wrong_type(expected: &str, value: &Value) -> VldError {
crate::primitives::type_error(&None, expected, value)
}
#[derive(Clone, Default)]
pub struct ZGeoPoint {
custom_type_error: Option<String>,
}
impl ZGeoPoint {
pub fn new() -> Self {
Self::default()
}
pub fn type_error(mut self, msg: impl Into<String>) -> Self {
self.custom_type_error = Some(msg.into());
self
}
}
fn coord(schema: ZNumber, value: &Value, seg: PathSegment) -> Result<f64, VldError> {
schema.parse_value(value).map_err(|e| e.with_prefix(seg))
}
fn both(lat: Result<f64, VldError>, lng: Result<f64, VldError>) -> Result<GeoPoint, VldError> {
match (lat, lng) {
(Ok(lat), Ok(lng)) => Ok(GeoPoint { lat, lng }),
(a, b) => Err(a
.err()
.unwrap_or_default()
.merge(b.err().unwrap_or_default())),
}
}
impl VldSchema for ZGeoPoint {
type Output = GeoPoint;
fn parse_value(&self, value: &Value) -> Result<GeoPoint, VldError> {
match value {
Value::Object(o) => {
let lat = match o.get("lat") {
Some(v) => coord(crate::latitude(), v, field("lat")),
None => Err(missing("lat")),
};
let lng = match o
.get("lng")
.map(|v| (v, "lng"))
.or(o.get("lon").map(|v| (v, "lon")))
{
Some((v, key)) => coord(crate::longitude(), v, field(key)),
None => Err(missing("lng")),
};
both(lat, lng)
}
Value::Array(a) if a.len() == 2 => {
let lng = coord(crate::longitude(), &a[0], PathSegment::Index(0));
let lat = coord(crate::latitude(), &a[1], PathSegment::Index(1));
both(lat, lng)
}
Value::Array(_) => Err(issue(
IssueCode::InvalidLength {
origin: SizeOrigin::Tuple,
expected: 2,
},
value,
)),
_ => Err(crate::primitives::type_error(
&self.custom_type_error,
"geo_point",
value,
)),
}
}
}
#[cfg(feature = "openapi")]
impl crate::openapi::JsonSchema for ZGeoPoint {
fn json_schema(&self) -> Value {
let (lat, lng) = (
crate::latitude().json_schema(),
crate::longitude().json_schema(),
);
serde_json::json!({
"anyOf": [
{
"type": "object",
"properties": {"lat": lat, "lng": lng},
"required": ["lat", "lng"],
},
{
"type": "array",
"prefixItems": [lng, lat],
"items": false,
"minItems": 2,
"maxItems": 2,
},
]
})
}
}
#[cfg(feature = "openapi")]
impl crate::openapi::CollectNestedSchemas for ZGeoPoint {}
const GEOMETRIES: &[&str] = &[
"Point",
"MultiPoint",
"LineString",
"MultiLineString",
"Polygon",
"MultiPolygon",
"GeometryCollection",
];
#[derive(Clone)]
pub struct ZGeoJson {
types: Vec<String>,
}
impl ZGeoJson {
pub fn new() -> Self {
Self {
types: GEOMETRIES.iter().map(|t| t.to_string()).collect(),
}
}
pub fn types(mut self, types: &[&str]) -> Self {
assert!(
!types.is_empty(),
"geojson().types() needs at least one type"
);
for t in types {
assert!(
GEOMETRIES.contains(t) || *t == "Feature" || *t == "FeatureCollection",
"Unknown GeoJSON type: {}",
t
);
}
self.types = types.iter().map(|t| t.to_string()).collect();
self
}
}
impl Default for ZGeoJson {
fn default() -> Self {
Self::new()
}
}
fn position(v: &Value) -> VldError {
let Some(a) = v.as_array() else {
return wrong_type("array", v);
};
let mut err = VldError::new();
for (i, x) in a.iter().enumerate().filter(|(_, x)| !x.is_number()) {
err = err.merge(wrong_type("number", x).with_prefix(PathSegment::Index(i)));
}
if a.len() < 2 {
err = err.merge(too_small(2, v));
}
err
}
fn too_small(min: usize, v: &Value) -> VldError {
issue(
IssueCode::TooSmall {
origin: SizeOrigin::Array,
minimum: min.into(),
inclusive: true,
},
v,
)
}
fn array_of(v: &Value, min: usize, item: impl Fn(&Value) -> VldError) -> VldError {
let Some(a) = v.as_array() else {
return wrong_type("array", v);
};
let mut err = VldError::new();
for (i, x) in a.iter().enumerate() {
err = err.merge(item(x).with_prefix(PathSegment::Index(i)));
}
if a.len() < min {
err = err.merge(too_small(min, v));
}
err
}
fn ring(v: &Value) -> VldError {
let err = array_of(v, 4, position);
let closed = match v.as_array().map(Vec::as_slice) {
Some([first, .., last]) => crate::primitives::json_eq(first, last),
_ => true,
};
if err.is_empty() && !closed {
return issue(IssueCode::RingNotClosed, v);
}
err
}
fn coordinates(ty: &str, v: &Value) -> VldError {
let line = |v: &Value| array_of(v, 2, position);
let polygon = |v: &Value| array_of(v, 0, ring);
match ty {
"Point" => position(v),
"MultiPoint" => array_of(v, 0, position),
"LineString" => line(v),
"MultiLineString" => array_of(v, 0, line),
"Polygon" => polygon(v),
_ => array_of(v, 0, polygon),
}
}
fn bbox(v: &Value) -> VldError {
let Some(a) = v.as_array() else {
return wrong_type("array", v);
};
let mut err = VldError::new();
for (i, x) in a.iter().enumerate().filter(|(_, x)| !x.is_number()) {
err = err.merge(wrong_type("number", x).with_prefix(PathSegment::Index(i)));
}
let code = match a.len() {
4 | 6 => return err,
0..4 => IssueCode::TooSmall {
origin: SizeOrigin::Array,
minimum: 4.into(),
inclusive: true,
},
5 => IssueCode::InvalidLength {
origin: SizeOrigin::Array,
expected: 6,
},
_ => IssueCode::TooBig {
origin: SizeOrigin::Array,
maximum: 6.into(),
inclusive: true,
},
};
err.merge(issue(code, v))
}
fn member(obj: &Map<String, Value>, key: &str, check: impl Fn(&Value) -> VldError) -> VldError {
match obj.get(key) {
Some(v) => check(v).with_prefix(field(key)),
None => missing(key),
}
}
fn object(v: &Value, allowed: &[&str]) -> VldError {
let Some(obj) = v.as_object() else {
return wrong_type("object", v);
};
let ty = match obj.get("type") {
None => return missing("type"),
Some(t) => match t.as_str() {
Some(s) if allowed.contains(&s) => s,
Some(_) => {
let options = allowed.iter().map(|s| s.to_string()).collect();
return issue(IssueCode::InvalidEnumValue { options }, t)
.with_prefix(field("type"));
}
None => return wrong_type("string", t).with_prefix(field("type")),
},
};
let mut err = match ty {
"GeometryCollection" => member(obj, "geometries", |g| {
array_of(g, 0, |x| object(x, GEOMETRIES))
}),
"Feature" => member(obj, "geometry", |g| match g {
Value::Null => VldError::new(),
g => object(g, GEOMETRIES),
})
.merge(member(obj, "properties", |p| match p {
Value::Null | Value::Object(_) => VldError::new(),
p => wrong_type("object", p),
})),
"FeatureCollection" => member(obj, "features", |f| {
array_of(f, 0, |x| object(x, &["Feature"]))
}),
geometry => member(obj, "coordinates", |c| coordinates(geometry, c)),
};
if let Some(b) = obj.get("bbox") {
err = err.merge(bbox(b).with_prefix(field("bbox")));
}
err
}
impl VldSchema for ZGeoJson {
type Output = Value;
fn parse_value(&self, value: &Value) -> Result<Value, VldError> {
let allowed: Vec<&str> = self.types.iter().map(String::as_str).collect();
let err = object(value, &allowed);
if err.is_empty() {
Ok(value.clone())
} else {
Err(err)
}
}
}
#[cfg(feature = "openapi")]
mod json_schema {
use serde_json::{Value, json};
fn array(items: Value, min: usize) -> Value {
let mut s = json!({"type": "array", "items": items});
if min > 0 {
s["minItems"] = min.into();
}
s
}
fn position() -> Value {
array(json!({"type": "number"}), 2)
}
fn ring() -> Value {
let mut s = array(position(), 4);
s["x-closedRing"] = true.into();
s
}
fn with_type(ty: &str, key: &str, member: Value) -> Value {
json!({
"type": "object",
"properties": {"type": {"const": ty}, key: member},
"required": ["type", key],
})
}
fn geometry(ty: &str) -> Value {
let coordinates = match ty {
"Point" => position(),
"MultiPoint" => array(position(), 0),
"LineString" => array(position(), 2),
"MultiLineString" => array(array(position(), 2), 0),
"Polygon" => array(ring(), 0),
"MultiPolygon" => array(array(ring(), 0), 0),
_ => {
let simple = super::GEOMETRIES[..6].iter().map(|t| geometry(t)).collect();
return with_type(
ty,
"geometries",
array(json!({"anyOf": Value::Array(simple)}), 0),
);
}
};
with_type(ty, "coordinates", coordinates)
}
fn feature() -> Value {
let mut geometries: Vec<Value> = super::GEOMETRIES.iter().map(|t| geometry(t)).collect();
geometries.push(json!({"type": "null"}));
json!({
"type": "object",
"properties": {
"type": {"const": "Feature"},
"geometry": {"anyOf": geometries},
"properties": {"type": ["object", "null"]},
},
"required": ["type", "geometry", "properties"],
})
}
pub(super) fn of(ty: &str) -> Value {
match ty {
"Feature" => feature(),
"FeatureCollection" => with_type(ty, "features", array(feature(), 0)),
_ => geometry(ty),
}
}
}
#[cfg(feature = "openapi")]
impl crate::openapi::JsonSchema for ZGeoJson {
fn json_schema(&self) -> Value {
let mut all: Vec<Value> = self.types.iter().map(|t| json_schema::of(t)).collect();
if all.len() == 1 {
all.remove(0)
} else {
serde_json::json!({ "anyOf": all })
}
}
}
#[cfg(feature = "openapi")]
impl crate::openapi::CollectNestedSchemas for ZGeoJson {}