use super::{Example, Function, Kind, Param, SECTION, signature};
use crate::cel::ast::Macro;
use serde_json::{Map, Value as Json};
pub(super) fn all() -> Vec<Function> {
std::iter::once(has())
.chain(Macro::ALL.iter().copied().map(comprehension))
.collect()
}
pub(super) fn comprehensions(optional: bool) -> Vec<Function> {
Macro::ALL
.iter()
.copied()
.filter(|found| found.on_optional() == optional)
.map(comprehension)
.collect()
}
pub(super) fn named(name: &str) -> Option<Function> {
if name == "has" {
return Some(has());
}
Macro::from_name(name).map(comprehension)
}
fn has() -> Function {
Function {
name: "has".to_string(),
kind: Kind::Macro,
signatures: vec![signature(
"has",
None,
vec![param("x.field", "field")],
"bool",
)],
description: "true when the map or record has the field".to_string(),
examples: vec![example(
"has(md.doc.title)",
Some(("md.doc", serde_json::json!({ "title": "Notes" }))),
Json::Bool(true),
)],
section: SECTION.to_string(),
}
}
fn comprehension(found: Macro) -> Function {
let (body, result, description, expression, gives) = match found {
Macro::All => (
"predicate",
"bool",
"true when every element satisfies the predicate",
"[1, 2, 3].all(x, x > 0)",
Json::Bool(true),
),
Macro::Exists => (
"predicate",
"bool",
"true when any element satisfies the predicate",
"[1, 2, 3].exists(x, x > 2)",
Json::Bool(true),
),
Macro::ExistsOne => (
"predicate",
"bool",
"true when exactly one element satisfies the predicate",
"[1, 2, 3].exists_one(x, x > 2)",
Json::Bool(true),
),
Macro::Filter => (
"predicate",
"list",
"the elements that satisfy the predicate",
"[1, 2, 3].filter(x, x > 1)",
serde_json::json!([2, 3]),
),
Macro::Map => (
"expression",
"list",
"each element made into another; `map(x, filter, expression)` keeps those the filter keeps",
"[1, 2, 3].map(x, x * 2)",
serde_json::json!([2, 4, 6]),
),
Macro::OptMap => (
"expression",
"optional",
"the optional's value made into another, as an optional; none stays none",
"optional.of(2).optMap(x, x * 3).value()",
serde_json::json!(6),
),
Macro::OptFlatMap => (
"expression",
"optional",
"the optional the expression gives for the optional's value; none stays none",
"optional.of(2).optFlatMap(x, x > 1 ? optional.of(x) : optional.none()).hasValue()",
Json::Bool(true),
),
};
let ty = if body == "predicate" { "bool" } else { "dyn" };
let receiver = if found.on_optional() {
"optional"
} else {
"list | map"
};
Function {
name: found.name().to_string(),
kind: Kind::Macro,
signatures: vec![signature(
found.name(),
Some(receiver.to_string()),
vec![param("x", "name"), param(body, ty)],
result,
)],
description: description.to_string(),
examples: vec![example(expression, None, gives)],
section: SECTION.to_string(),
}
}
fn param(name: &str, ty: &str) -> Param {
Param {
name: name.to_string(),
ty: ty.to_string(),
}
}
fn example(expression: &str, given: Option<(&str, Json)>, result: Json) -> Example {
Example {
expression: expression.to_string(),
given: given
.into_iter()
.map(|(name, value)| (name.to_string(), value))
.collect::<Map<_, _>>(),
result,
}
}