use std::collections::BTreeMap;
use std::fmt;
use std::str::FromStr;
use dsl_kit_core::{IdGen, NodeId};
use dsl_kit_macros::{DslBuild, DslNode, DslSchema};
use dsl_kit_parse::DslBuild as _;
use dsl_kit_parse::serde_bridge;
use dsl_kit_schema::DslSchema as _;
use evalhub_schema::error::{ErrorCode, ErrorEntry};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value, json};
#[derive(Debug, Clone, PartialEq, DslNode, DslSchema, DslBuild)]
pub enum Filter {
And {
id: NodeId,
children: Vec<Filter>,
},
Or {
id: NodeId,
children: Vec<Filter>,
},
Not {
id: NodeId,
child: Box<Filter>,
},
Compare {
id: NodeId,
path: String,
op: ScalarOp,
value: Value,
},
Exists {
id: NodeId,
path: String,
},
Any {
id: NodeId,
path: String,
terms: BTreeMap<String, MatchTerm>,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ScalarOp {
Eq,
Ne,
Gt,
Gte,
Lt,
Lte,
In,
Prefix,
Contains,
}
impl ScalarOp {
pub const ALL: [ScalarOp; 9] = [
ScalarOp::Eq,
ScalarOp::Ne,
ScalarOp::Gt,
ScalarOp::Gte,
ScalarOp::Lt,
ScalarOp::Lte,
ScalarOp::In,
ScalarOp::Prefix,
ScalarOp::Contains,
];
pub const fn as_str(self) -> &'static str {
match self {
ScalarOp::Eq => "eq",
ScalarOp::Ne => "ne",
ScalarOp::Gt => "gt",
ScalarOp::Gte => "gte",
ScalarOp::Lt => "lt",
ScalarOp::Lte => "lte",
ScalarOp::In => "in",
ScalarOp::Prefix => "prefix",
ScalarOp::Contains => "contains",
}
}
}
impl fmt::Display for ScalarOp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for ScalarOp {
type Err = UnknownOperator;
fn from_str(s: &str) -> Result<Self, Self::Err> {
ScalarOp::ALL
.into_iter()
.find(|op| op.as_str() == s)
.ok_or_else(|| UnknownOperator(s.to_string()))
}
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("unknown operator `{0}`")]
pub struct UnknownOperator(pub String);
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct MatchTerm {
#[serde(default = "MatchTerm::default_op")]
pub op: ScalarOp,
pub value: Value,
}
impl MatchTerm {
const fn default_op() -> ScalarOp {
ScalarOp::Eq
}
}
impl FromStr for MatchTerm {
type Err = serde_json::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
serde_json::from_str(s)
}
}
const EXISTS_OP: &str = "exists";
const ANY_OP: &str = "any";
pub fn parse(value: &Value) -> Result<Filter, Vec<ErrorEntry>> {
let mut errors = Vec::new();
let tagged = lower(value, "/where", &mut errors);
if !errors.is_empty() {
return Err(errors);
}
let schema = Filter::schema();
let tree = serde_bridge::from_json_value(&tagged, &schema)
.map_err(|e| diagnostics_to_entries(&e, "/where"))?;
Filter::from_parse_tree(&tree, &IdGen::new()).map_err(|e| diagnostics_to_entries(&e, "/where"))
}
fn diagnostics_to_entries(error: &dsl_kit_parse::BuildError, pointer: &str) -> Vec<ErrorEntry> {
error
.diagnostics
.iter()
.map(|d| ErrorEntry {
path: pointer.to_string(),
code: ErrorCode::Schema,
hint: Some(d.message.clone()),
})
.collect()
}
fn shape_error(pointer: &str, hint: impl Into<String>) -> ErrorEntry {
ErrorEntry {
path: pointer.to_string(),
code: ErrorCode::Schema,
hint: Some(hint.into()),
}
}
fn lower(value: &Value, pointer: &str, errors: &mut Vec<ErrorEntry>) -> Value {
let Some(object) = value.as_object() else {
errors.push(shape_error(
pointer,
"a filter is an object: {\"and\": [...]}, {\"or\": [...]}, {\"not\": {...}} \
or {\"path\": ..., \"op\": ..., \"value\": ...}",
));
return Value::Null;
};
for (key, variant) in [("and", "And"), ("or", "Or")] {
if let Some(children) = object.get(key) {
if object.len() > 1 {
errors.push(shape_error(
pointer,
format!("`{key}` is the only key a `{key}` filter may carry"),
));
return Value::Null;
}
let Some(items) = children.as_array() else {
errors.push(shape_error(
&format!("{pointer}/{key}"),
format!("`{key}` takes an array of filters"),
));
return Value::Null;
};
let lowered: Vec<Value> = items
.iter()
.enumerate()
.map(|(i, child)| lower(child, &format!("{pointer}/{key}/{i}"), errors))
.collect();
return json!({ "type": variant, "children": lowered });
}
}
if let Some(child) = object.get("not") {
if object.len() > 1 {
errors.push(shape_error(
pointer,
"`not` is the only key a `not` filter may carry",
));
return Value::Null;
}
let lowered = lower(child, &format!("{pointer}/not"), errors);
return json!({ "type": "Not", "child": lowered });
}
lower_leaf(object, pointer, errors)
}
fn lower_leaf(object: &Map<String, Value>, pointer: &str, errors: &mut Vec<ErrorEntry>) -> Value {
let Some(path) = object.get("path") else {
errors.push(shape_error(
pointer,
"a leaf filter needs a `path`; a connective needs `and`, `or` or `not`",
));
return Value::Null;
};
let Some(path) = path.as_str() else {
errors.push(shape_error(
&format!("{pointer}/path"),
"`path` is a string",
));
return Value::Null;
};
let Some(op) = object.get("op") else {
errors.push(shape_error(
pointer,
format!(
"a leaf filter needs an `op`: one of {}, `{EXISTS_OP}` or `{ANY_OP}`",
operator_list()
),
));
return Value::Null;
};
let Some(op) = op.as_str() else {
errors.push(shape_error(&format!("{pointer}/op"), "`op` is a string"));
return Value::Null;
};
match op {
EXISTS_OP => {
reject_extra_keys(object, &["path", "op"], pointer, errors);
json!({ "type": "Exists", "path": path })
}
ANY_OP => {
reject_extra_keys(object, &["path", "op", "match"], pointer, errors);
let Some(terms) = object.get("match") else {
errors.push(shape_error(
pointer,
"`any` needs a `match` object naming the element fields to constrain",
));
return Value::Null;
};
let Some(terms) = terms.as_object() else {
errors.push(shape_error(
&format!("{pointer}/match"),
"`match` is an object",
));
return Value::Null;
};
let lowered: Map<String, Value> = terms
.iter()
.map(|(key, term)| {
(
key.clone(),
lower_term(term, &format!("{pointer}/match/{key}"), errors),
)
})
.collect();
json!({ "type": "Any", "path": path, "terms": lowered })
}
_ => {
reject_extra_keys(object, &["path", "op", "value"], pointer, errors);
let Some(value) = object.get("value") else {
errors.push(shape_error(
pointer,
format!("`{op}` needs a `value`; only `{EXISTS_OP}` stands alone"),
));
return Value::Null;
};
json!({ "type": "Compare", "path": path, "op": op, "value": value })
}
}
}
fn lower_term(term: &Value, pointer: &str, errors: &mut Vec<ErrorEntry>) -> Value {
match term.as_object() {
Some(object) if object.contains_key("op") || object.contains_key("value") => {
reject_extra_keys(object, &["op", "value"], pointer, errors);
if !object.contains_key("value") {
errors.push(shape_error(
pointer,
"a match term with an `op` also needs a `value`",
));
return Value::Null;
}
json!({
"op": object.get("op").cloned().unwrap_or_else(|| json!("eq")),
"value": object.get("value"),
})
}
_ => json!({ "op": "eq", "value": term }),
}
}
fn reject_extra_keys(
object: &Map<String, Value>,
allowed: &[&str],
pointer: &str,
errors: &mut Vec<ErrorEntry>,
) {
for key in object.keys() {
if !allowed.contains(&key.as_str()) {
errors.push(shape_error(
&format!("{pointer}/{key}"),
format!("unknown key `{key}`; this filter takes {}", list(allowed)),
));
}
}
}
fn list(items: &[&str]) -> String {
items
.iter()
.map(|i| format!("`{i}`"))
.collect::<Vec<_>>()
.join(", ")
}
fn operator_list() -> String {
ScalarOp::ALL
.iter()
.map(|op| format!("`{op}`"))
.collect::<Vec<_>>()
.join(", ")
}
pub(crate) fn wire_key(filter: &Filter) -> &'static str {
match filter {
Filter::And { .. } => "and",
Filter::Or { .. } => "or",
Filter::Not { .. } => "not",
Filter::Compare { .. } | Filter::Exists { .. } | Filter::Any { .. } => "",
}
}
pub fn request_schema() -> schemars::Schema {
let node = Filter::schema();
let compare = node
.variant("Compare")
.map(|v| v.fields.iter().map(|f| f.name.clone()).collect::<Vec<_>>())
.unwrap_or_default();
let path_key = compare
.iter()
.find(|f| *f == "path")
.cloned()
.unwrap_or_else(|| "path".to_string());
let value_key = compare
.iter()
.find(|f| *f == "value")
.cloned()
.unwrap_or_else(|| "value".to_string());
let ops: Vec<Value> = ScalarOp::ALL.iter().map(|op| json!(op.as_str())).collect();
let filter_ref = json!({ "$ref": "#/$defs/filter" });
let term = json!({
"description": "A literal (meaning `eq`), or an operator and a literal.",
"oneOf": [
{ "type": ["string", "number", "boolean", "null"] },
{
"type": "object",
"properties": {
"op": { "enum": ops.clone() },
&value_key: true,
},
"required": [&value_key],
"additionalProperties": false,
},
],
});
let document = json!({
"$ref": "#/$defs/filter",
"$defs": {
"filter": {
"description": "A filter: a connective or a leaf.",
"oneOf": [
{
"type": "object",
"properties": { "and": { "type": "array", "items": filter_ref } },
"required": ["and"],
"additionalProperties": false,
},
{
"type": "object",
"properties": { "or": { "type": "array", "items": filter_ref } },
"required": ["or"],
"additionalProperties": false,
},
{
"type": "object",
"properties": { "not": filter_ref },
"required": ["not"],
"additionalProperties": false,
},
{
"description": "A scalar comparison.",
"type": "object",
"properties": {
&path_key: { "type": "string" },
"op": { "enum": ops },
&value_key: true,
},
"required": [&path_key, "op", &value_key],
"additionalProperties": false,
},
{
"description": "The path has a value, `null` included.",
"type": "object",
"properties": {
&path_key: { "type": "string" },
"op": { "const": EXISTS_OP },
},
"required": [&path_key, "op"],
"additionalProperties": false,
},
{
"description": "At least one element of an array path satisfies every term.",
"type": "object",
"properties": {
&path_key: { "type": "string" },
"op": { "const": ANY_OP },
"match": { "type": "object", "additionalProperties": term },
},
"required": [&path_key, "op", "match"],
"additionalProperties": false,
},
],
},
},
});
match document {
Value::Object(map) => schemars::Schema::from(map),
_ => schemars::Schema::default(),
}
}