1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//! The functions an expression can call (spec §8.2), each with its signatures, what it gives and examples: the `md.`
//! functions, the functions and methods the profile takes from CEL, and its macros. One catalog, for an editor's
//! completion and hover, a reference page and a language server; the processor's own hints read it too.
//!
//! ```
//! let functions = compmd::functions::all();
//! let currency = functions.iter().find(|f| f.name == "md.currency").unwrap();
//! assert_eq!(currency.signatures[0].written, "md.currency(n: number, code: string) → string");
//! assert_eq!(currency.examples[0].expression, r#"md.currency(price * 10, "USD")"#);
//! assert_eq!(currency.examples[0].result, serde_json::json!("$120.00"));
//! ```
mod by_type;
#[cfg(test)]
pub(crate) mod examples;
mod macros;
#[cfg(test)]
mod tests;
pub use by_type::of_type;
use crate::cel::functions::Function as CelFunction;
use crate::cel::library::{self, Library};
use crate::cel::optionals::OptionalFunction;
use crate::md::MdFunction;
use serde::Serialize;
use serde_json::{Map, Value as Json};
/// A function an expression can call.
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct Function {
/// How it is called: `md.currency`, `size`, `contains` (a method, `s.contains(…)`), `map` (a macro).
pub name: String,
/// Where it comes from.
pub kind: Kind,
/// Each way to call it.
pub signatures: Vec<Signature>,
/// What it gives, in one line.
pub description: String,
/// Calls of it, and what each gives at a render at 2026-10-21T10:00:00Z, in UTC, in English (`md.lang` `en`).
pub examples: Vec<Example>,
/// The spec's section that defines it: `8.2`.
pub section: String,
}
/// Where a function comes from.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Kind {
/// One of CompMD's, called `md.<name>(…)`.
Md,
/// One the profile takes from CEL's standard definitions or its strings extension, called `name(…)` or on a
/// value, `value.name(…)`.
Cel,
/// A macro: `has(…)`, and the comprehensions on a list or a map (`list.map(x, …)`), whose first argument names
/// each element.
Macro,
}
/// One way to call a function.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct Signature {
/// The type of the value it is called on, for a method: `string`, `list(string)`; `None` for a function.
#[serde(skip_serializing_if = "Option::is_none")]
pub receiver: Option<String>,
/// Its params, in order.
pub params: Vec<Param>,
/// The type it gives: `string`, or `dyn` where it depends on what it is given.
pub result: String,
/// The signature as an editor shows it: `string.indexOf(substring: string, start: int) → int`.
pub written: String,
}
/// A param of a signature.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct Param {
/// Its name: the spec's, for the `md.` functions.
pub name: String,
/// Its type, as CEL writes it (`number` is an `int`, a `uint` or a `double`; `name` is a macro's element).
#[serde(rename = "type")]
pub ty: String,
}
/// A call of a function, and what it gives at a render at 2026-10-21T10:00:00Z, in UTC, in English.
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct Example {
/// The expression.
pub expression: String,
/// The variables it reads, by name (`price`, `md.doc`), with their values.
#[serde(skip_serializing_if = "Map::is_empty")]
pub given: Map<String, Json>,
/// What it gives, as JSON: a date or a timestamp as the string it is written as.
pub result: Json,
}
/// The spec's section of every function and macro.
const SECTION: &str = "8.2";
/// Every function an expression can call: the `md.` functions, then CEL's, then the macros, each in the order the
/// spec lists it.
#[must_use]
pub fn all() -> Vec<Function> {
let md = MdFunction::ALL
.iter()
.map(|function| of_library(*function, Kind::Md));
let cel = CelFunction::ALL
.iter()
.map(|function| of_library(*function, Kind::Cel));
let optional = OptionalFunction::ALL
.iter()
.map(|function| of_library(*function, Kind::Cel));
md.chain(cel).chain(optional).chain(macros::all()).collect()
}
/// The function written `name`, `md.` and all (`md.currency`, `trim`, `map`).
pub(crate) fn named(name: &str) -> Option<Function> {
if let Some(name) = name.strip_prefix("md.") {
return MdFunction::from_name(name).map(|function| of_library(function, Kind::Md));
}
if let Some(name) = name.strip_prefix("optional.") {
return OptionalFunction::from_name(name).map(|function| of_library(function, Kind::Cel));
}
CelFunction::from_name(name)
.map(|function| of_library(function, Kind::Cel))
.or_else(|| macros::named(name))
}
/// `function` of a [`Library`], from its definition.
fn of_library<L: Library>(function: L, kind: Kind) -> Function {
let definition = function.definition();
let written_name = format!("{}{}", L::PREFIX, definition.name);
let signatures = definition
.overloads
.iter()
.map(|overload| {
let params = overload
.names
.iter()
.zip(overload.params)
.map(|(name, param)| Param {
name: (*name).to_string(),
ty: param.written().to_string(),
})
.collect();
signature(
&written_name,
overload
.receiver
.map(|receiver| receiver.written().to_string()),
params,
overload.returns_written(),
)
})
.collect();
Function {
name: written_name,
kind,
signatures,
description: definition.description.to_string(),
examples: definition.examples.iter().map(example).collect(),
section: SECTION.to_string(),
}
}
/// A signature, written as an editor shows it; the macros' are written so too.
pub(crate) fn signature(
name: &str,
receiver: Option<String>,
params: Vec<Param>,
result: &str,
) -> Signature {
let on = receiver
.as_ref()
.map(|receiver| format!("{receiver}."))
.unwrap_or_default();
let list: Vec<String> = params
.iter()
.map(|param| format!("{}: {}", param.name, param.ty))
.collect();
Signature {
written: format!("{on}{name}({}) → {result}", list.join(", ")),
receiver,
params,
result: result.to_string(),
}
}
/// An entry's example, its JSON read.
pub(crate) fn example(example: &library::Example) -> Example {
let json = |text: &str| serde_json::from_str(text).unwrap_or(Json::Null);
Example {
expression: example.expression.to_string(),
given: example
.given
.iter()
.map(|(name, value)| ((*name).to_string(), json(value)))
.collect(),
result: json(example.result),
}
}