backbone-core 3.6.0

Backbone Framework Core - Foundation for generic CRUD system
Documentation
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
//! The values a compiled row rule computes with, and its operators.
//!
//! The schema generator compiles each row rule a module declares (a condition
//! over one row's own fields) into a Rust expression over [`Val`], built from
//! the functions here, and evaluates it against the row the write would store,
//! read through the row's JSON form so enums, decimals and dates arrive as the
//! text and numbers their columns hold.
//!
//! The semantics are SQL's, because the same rule also compiles to a `CHECK`
//! constraint and both must give one answer:
//!
//! - A missing or null field is [`Val::Null`], and an operator over a null is
//!   null: `name.length >= 1` with no name is neither true nor false.
//! - `&&`, `||` and `!` are three-valued: `false && null` is false,
//!   `true || null` is true, anything else with a null is null.
//! - A rule [`holds`] unless it is definitely false, as a `CHECK` passes on
//!   null. `x == null` and `x != null` test for null itself and are never null.
//! - A comparison between a number and a text parses the text, the way a
//!   decimal column serialized as `"12.50"` compares to `0`; a comparison
//!   between a time and a text parses the text as a timestamp or a date.
//!
//! A rule may also read the row before the write (`$old.x`, through [`field`]
//! on that row), the writer ([`actor`]) and the time of the write ([`at`]).

use std::collections::HashMap;
use std::sync::{Mutex, OnceLock};

use chrono::{DateTime, NaiveDate, Utc};
use serde_json::Value;

/// One value inside a rule.
#[derive(Debug, Clone, PartialEq)]
pub enum Val {
    Null,
    Bool(bool),
    Num(f64),
    Text(String),
    List(Vec<Val>),
    /// A point in time: the write's `now()`, or a stored timestamp or date read as one.
    Time(DateTime<Utc>),
}

impl Val {
    fn from_json(v: &Value) -> Val {
        match v {
            Value::Null | Value::Object(_) => Val::Null,
            Value::Bool(b) => Val::Bool(*b),
            Value::Number(n) => n.as_f64().map(Val::Num).unwrap_or(Val::Null),
            Value::String(s) => Val::Text(s.clone()),
            Value::Array(items) => Val::List(items.iter().map(Val::from_json).collect()),
        }
    }

    fn num(&self) -> Option<f64> {
        match self {
            Val::Num(n) => Some(*n),
            Val::Text(s) => s.trim().parse().ok(),
            _ => None,
        }
    }

    fn time(&self) -> Option<DateTime<Utc>> {
        match self {
            Val::Time(t) => Some(*t),
            Val::Text(s) => DateTime::parse_from_rfc3339(s)
                .map(|t| t.with_timezone(&Utc))
                .ok()
                .or_else(|| {
                    let d = NaiveDate::parse_from_str(s, "%Y-%m-%d").ok()?;
                    Some(d.and_hms_opt(0, 0, 0)?.and_utc())
                }),
            _ => None,
        }
    }

    fn text(&self) -> Option<&str> {
        match self {
            Val::Text(s) => Some(s),
            _ => None,
        }
    }

    fn truth(&self) -> Option<bool> {
        match self {
            Val::Bool(b) => Some(*b),
            _ => None,
        }
    }
}

/// The row's field `name`; [`Val::Null`] when it is absent or null.
pub fn field(row: &Value, name: &str) -> Val {
    row.get(name).map(Val::from_json).unwrap_or(Val::Null)
}

/// The writer, as `$actor.id` reads it; null for a job, a seed or an unscoped caller.
pub fn actor(actor: Option<&str>) -> Val {
    actor.map(|a| Val::Text(a.to_string())).unwrap_or(Val::Null)
}

/// The time of the write, as `now()` reads it.
pub fn at(at: DateTime<Utc>) -> Val {
    Val::Time(at)
}

/// Whether any of `fields` differs between the row before and the row after a write. A rule
/// about the time of the write checks an update only when it changes what the rule reads, so a
/// later edit of a row whose deadline has passed is not a break.
pub fn changed(before: Option<&Value>, after: &Value, fields: &[&str]) -> bool {
    match before {
        None => true,
        Some(b) => fields.iter().any(|f| b.get(*f) != after.get(*f)),
    }
}

/// `$actor.has_role(role)`: null without a writer, without an installed roles source
/// ([`crate::actor_roles`]), or when the source cannot tell; the rule then holds.
pub async fn has_role(actor: Option<&str>, role: &str) -> Val {
    static WARNED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
    let Some(actor) = actor else { return Val::Null };
    let Some(roles) = crate::actor_roles::installed() else {
        if !WARNED.swap(true, std::sync::atomic::Ordering::Relaxed) {
            tracing::warn!(
                target: "backbone::write_guard",
                "a role rule ran with no roles source installed (backbone_core::actor_roles::install); role rules check nothing"
            );
        }
        return Val::Null;
    };
    roles.holds(actor, role).await.map(Val::Bool).unwrap_or(Val::Null)
}

/// How many rows still reference the row being deleted: `sql` counts them with the row's id as
/// `$1`, run on the request's own connection. Null outside a request or without an id.
pub async fn referencing(sql: &str, id: Val) -> Val {
    let Val::Text(id) = id else { return Val::Null };
    backbone_orm::company_scope::count_on_request_conn(sql, &[id.as_str()])
        .await
        .map(|n| Val::Num(n as f64))
        .unwrap_or(Val::Null)
}

pub fn text(s: &str) -> Val {
    Val::Text(s.to_string())
}

/// Whether a rule's value lets the write through: everything but a definite `false`.
pub fn holds(v: &Val) -> bool {
    !matches!(v, Val::Bool(false))
}

// ── Null tests ───────────────────────────────────────────────────────────

pub fn is_null(a: Val) -> Val {
    Val::Bool(matches!(a, Val::Null))
}

pub fn is_not_null(a: Val) -> Val {
    Val::Bool(!matches!(a, Val::Null))
}

// ── Logic ────────────────────────────────────────────────────────────────

pub fn and(a: Val, b: Val) -> Val {
    match (a.truth(), b.truth()) {
        (Some(false), _) | (_, Some(false)) => Val::Bool(false),
        (Some(true), Some(true)) => Val::Bool(true),
        _ => Val::Null,
    }
}

pub fn or(a: Val, b: Val) -> Val {
    match (a.truth(), b.truth()) {
        (Some(true), _) | (_, Some(true)) => Val::Bool(true),
        (Some(false), Some(false)) => Val::Bool(false),
        _ => Val::Null,
    }
}

pub fn not(a: Val) -> Val {
    a.truth().map(|b| Val::Bool(!b)).unwrap_or(Val::Null)
}

/// `c ? t : e`, as SQL's `CASE WHEN c THEN t ELSE e END`: a null condition takes `e`.
pub fn cond(c: Val, t: Val, e: Val) -> Val {
    if c.truth() == Some(true) {
        t
    } else {
        e
    }
}

// ── Comparison ───────────────────────────────────────────────────────────

fn order(a: &Val, b: &Val) -> Option<std::cmp::Ordering> {
    match (a, b) {
        (Val::Null, _) | (_, Val::Null) => None,
        (Val::Text(x), Val::Text(y)) => Some(x.cmp(y)),
        (Val::Bool(x), Val::Bool(y)) => Some(x.cmp(y)),
        (Val::Time(_), _) | (_, Val::Time(_)) => Some(a.time()?.cmp(&b.time()?)),
        _ => a.num()?.partial_cmp(&b.num()?),
    }
}

fn compare(a: Val, b: Val, test: fn(std::cmp::Ordering) -> bool) -> Val {
    order(&a, &b).map(|o| Val::Bool(test(o))).unwrap_or(Val::Null)
}

pub fn eq(a: Val, b: Val) -> Val {
    compare(a, b, |o| o.is_eq())
}

pub fn ne(a: Val, b: Val) -> Val {
    compare(a, b, |o| o.is_ne())
}

pub fn lt(a: Val, b: Val) -> Val {
    compare(a, b, |o| o.is_lt())
}

pub fn le(a: Val, b: Val) -> Val {
    compare(a, b, |o| o.is_le())
}

pub fn gt(a: Val, b: Val) -> Val {
    compare(a, b, |o| o.is_gt())
}

pub fn ge(a: Val, b: Val) -> Val {
    compare(a, b, |o| o.is_ge())
}

/// `a in [..]`, as SQL's `IN`: true on a match, null when `a` is null or no
/// item matched but one was null, false otherwise.
pub fn in_list(a: Val, list: Val) -> Val {
    let Val::List(items) = list else { return Val::Null };
    if matches!(a, Val::Null) {
        return Val::Null;
    }
    let mut unknown = false;
    for item in items {
        match eq(a.clone(), item) {
            Val::Bool(true) => return Val::Bool(true),
            Val::Null => unknown = true,
            _ => {}
        }
    }
    if unknown {
        Val::Null
    } else {
        Val::Bool(false)
    }
}

pub fn not_in(a: Val, list: Val) -> Val {
    not(in_list(a, list))
}

/// `a matches 'pattern'`: whether the text contains a match of the pattern.
/// The generator only emits patterns that compile.
pub fn matches(a: Val, pattern: &str) -> Val {
    static CACHE: OnceLock<Mutex<HashMap<String, Option<regex::Regex>>>> = OnceLock::new();
    let Some(s) = a.text() else { return Val::Null };
    let mut cache = CACHE.get_or_init(Default::default).lock().unwrap_or_else(|e| e.into_inner());
    let re = cache.entry(pattern.to_string()).or_insert_with(|| regex::Regex::new(pattern).ok());
    match re {
        Some(re) => Val::Bool(re.is_match(s)),
        None => Val::Null,
    }
}

// ── Arithmetic ───────────────────────────────────────────────────────────

fn arith(a: Val, b: Val, f: fn(f64, f64) -> Option<f64>) -> Val {
    match (a.num(), b.num()) {
        (Some(x), Some(y)) => f(x, y).map(Val::Num).unwrap_or(Val::Null),
        _ => Val::Null,
    }
}

pub fn add(a: Val, b: Val) -> Val {
    arith(a, b, |x, y| Some(x + y))
}

pub fn sub(a: Val, b: Val) -> Val {
    arith(a, b, |x, y| Some(x - y))
}

pub fn mul(a: Val, b: Val) -> Val {
    arith(a, b, |x, y| Some(x * y))
}

/// Division by zero is null rather than an error: a rule never panics a write.
pub fn div(a: Val, b: Val) -> Val {
    arith(a, b, |x, y| (y != 0.0).then(|| x / y))
}

pub fn rem(a: Val, b: Val) -> Val {
    arith(a, b, |x, y| (y != 0.0).then(|| x % y))
}

pub fn neg(a: Val) -> Val {
    a.num().map(|n| Val::Num(-n)).unwrap_or(Val::Null)
}

fn num_fn(a: Val, f: fn(f64) -> f64) -> Val {
    a.num().map(|n| Val::Num(f(n))).unwrap_or(Val::Null)
}

pub fn abs(a: Val) -> Val {
    num_fn(a, f64::abs)
}

pub fn ceil(a: Val) -> Val {
    num_fn(a, f64::ceil)
}

pub fn floor(a: Val) -> Val {
    num_fn(a, f64::floor)
}

pub fn round(a: Val) -> Val {
    num_fn(a, f64::round)
}

/// The first value that is not null.
pub fn coalesce(values: Vec<Val>) -> Val {
    values.into_iter().find(|v| !matches!(v, Val::Null)).unwrap_or(Val::Null)
}

// ── Text and lists ───────────────────────────────────────────────────────

/// A text's length in characters (code points, as on every target), or a list's.
pub fn length(a: Val) -> Val {
    match a {
        Val::Text(s) => Val::Num(s.chars().count() as f64),
        Val::List(items) => Val::Num(items.len() as f64),
        _ => Val::Null,
    }
}

pub fn is_empty(a: Val) -> Val {
    match length(a) {
        Val::Num(n) => Val::Bool(n == 0.0),
        _ => Val::Null,
    }
}

fn text_fn(a: Val, f: fn(&str) -> String) -> Val {
    a.text().map(|s| Val::Text(f(s))).unwrap_or(Val::Null)
}

pub fn trim(a: Val) -> Val {
    text_fn(a, |s| s.trim().to_string())
}

pub fn lower(a: Val) -> Val {
    text_fn(a, str::to_lowercase)
}

pub fn upper(a: Val) -> Val {
    text_fn(a, str::to_uppercase)
}

/// `a.contains(b)`: a text containing a text, or a list containing an item.
pub fn contains(a: Val, b: Val) -> Val {
    match (a, b) {
        (Val::Text(s), Val::Text(t)) => Val::Bool(s.contains(t.as_str())),
        (list @ Val::List(_), item) => in_list(item, list),
        _ => Val::Null,
    }
}

pub fn starts_with(a: Val, b: Val) -> Val {
    match (a.text(), b.text()) {
        (Some(s), Some(t)) => Val::Bool(s.starts_with(t)),
        _ => Val::Null,
    }
}

pub fn ends_with(a: Val, b: Val) -> Val {
    match (a.text(), b.text()) {
        (Some(s), Some(t)) => Val::Bool(s.ends_with(t)),
        _ => Val::Null,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn a_rule_over_a_null_field_holds_as_a_check_constraint_does() {
        let row = json!({ "name": null });
        let rule = ge(length(field(&row, "name")), Val::Num(1.0));
        assert_eq!(rule, Val::Null);
        assert!(holds(&rule));
        // A missing field reads the same as a null one.
        assert!(holds(&ge(length(field(&json!({}), "name")), Val::Num(1.0))));
    }

    #[test]
    fn a_null_test_is_never_null_itself() {
        let row = json!({ "email": null });
        assert_eq!(is_not_null(field(&row, "email")), Val::Bool(false));
        assert!(!holds(&and(is_not_null(field(&row, "email")), matches(field(&row, "email"), "@"))));
    }

    #[test]
    fn logic_is_three_valued() {
        assert_eq!(and(Val::Bool(false), Val::Null), Val::Bool(false));
        assert_eq!(and(Val::Bool(true), Val::Null), Val::Null);
        assert_eq!(or(Val::Bool(true), Val::Null), Val::Bool(true));
        assert_eq!(or(Val::Bool(false), Val::Null), Val::Null);
        assert_eq!(not(Val::Null), Val::Null);
        assert_eq!(cond(Val::Null, Val::Bool(true), Val::Bool(false)), Val::Bool(false));
    }

    #[test]
    fn a_decimal_serialized_as_text_compares_as_a_number() {
        let row = json!({ "risk_score": "0.75", "count": 3 });
        assert_eq!(le(field(&row, "risk_score"), Val::Num(1.0)), Val::Bool(true));
        assert_eq!(gt(field(&row, "risk_score"), Val::Num(1.0)), Val::Bool(false));
        assert_eq!(ge(field(&row, "count"), Val::Num(1.0)), Val::Bool(true));
    }

    #[test]
    fn in_follows_sql_in() {
        let list = Val::List(vec![text("json"), text("csv")]);
        assert_eq!(in_list(text("csv"), list.clone()), Val::Bool(true));
        assert_eq!(in_list(text("pdf"), list.clone()), Val::Bool(false));
        assert_eq!(in_list(Val::Null, list.clone()), Val::Null);
        assert_eq!(not_in(text("pdf"), list), Val::Bool(true));
    }

    #[test]
    fn text_helpers_count_characters_not_bytes() {
        assert_eq!(length(text("héllo")), Val::Num(5.0));
        assert_eq!(length(trim(text("  a "))), Val::Num(1.0));
        assert_eq!(contains(text("a/../b"), text("..")), Val::Bool(true));
        assert_eq!(matches(text("my-bucket_1"), "^[a-zA-Z0-9_-]+$"), Val::Bool(true));
        assert_eq!(matches(text("no spaces"), "^[a-zA-Z0-9_-]+$"), Val::Bool(false));
    }

    #[test]
    fn a_stored_timestamp_or_date_compares_with_the_time_of_the_write() {
        let at = DateTime::parse_from_rfc3339("2026-10-11T10:00:00Z").unwrap().with_timezone(&Utc);
        let row = json!({ "expires_at": "2026-10-12T00:00:00Z", "dob": "1990-05-01", "gone": "2026-01-01T00:00:00+07:00" });
        assert_eq!(gt(field(&row, "expires_at"), super::at(at)), Val::Bool(true));
        assert_eq!(lt(field(&row, "dob"), super::at(at)), Val::Bool(true));
        assert_eq!(gt(field(&row, "gone"), super::at(at)), Val::Bool(false));
        assert_eq!(gt(field(&row, "missing"), super::at(at)), Val::Null);
    }

    #[test]
    fn the_writer_and_the_row_before_the_write_are_readable() {
        assert_eq!(eq(actor(Some("u1")), text("u1")), Val::Bool(true));
        assert_eq!(eq(actor(None), text("u1")), Val::Null, "no writer: the rule holds");
        let (before, after) = (json!({ "progress": 40, "x": 1 }), json!({ "progress": 30, "x": 1 }));
        assert_eq!(ge(field(&after, "progress"), field(&before, "progress")), Val::Bool(false));
        assert!(changed(Some(&before), &after, &["progress"]));
        assert!(!changed(Some(&before), &after, &["x"]));
        assert!(changed(None, &after, &["x"]), "a create changes everything");
    }

    #[tokio::test]
    async fn a_role_rule_asks_the_installed_source_and_holds_without_one() {
        struct Fixed;
        #[async_trait::async_trait]
        impl crate::actor_roles::ActorRoles for Fixed {
            async fn holds(&self, actor: &str, role: &str) -> Option<bool> {
                (actor == "u1").then(|| role.eq_ignore_ascii_case("ADMIN"))
            }
        }
        assert_eq!(has_role(None, "admin").await, Val::Null, "no writer");
        crate::actor_roles::install(std::sync::Arc::new(Fixed));
        assert_eq!(has_role(Some("u1"), "admin").await, Val::Bool(true));
        assert_eq!(has_role(Some("u1"), "auditor").await, Val::Bool(false));
        assert_eq!(has_role(Some("u2"), "admin").await, Val::Null, "the source cannot tell");
        // Outside a request there is no connection to count on: the delete guard holds.
        assert_eq!(referencing("SELECT count(*) FROM t WHERE x = $1::uuid", text("id")).await, Val::Null);
        assert_eq!(referencing("SELECT 1", Val::Null).await, Val::Null);
    }

    #[test]
    fn division_by_zero_is_null_not_a_panic() {
        assert_eq!(div(Val::Num(1.0), Val::Num(0.0)), Val::Null);
        assert_eq!(rem(Val::Num(1.0), Val::Num(0.0)), Val::Null);
    }
}