Skip to main content

Module filter

Module filter 

Source
Expand description

Input filters: idempotent, per-cell domain constraints.

A Filter conforms or rejects a value written externally to its cell, applied live by crate::sheet::Sheet::propagate against a source cell’s own current value (see crate::sheet::Sheet::add_filter), and is separately re-evaluated as a non-gating diagnostic against a value a relationship’s method derives for that cell (also in crate::sheet::Sheet::propagate).

use adam_rs::{Filter, Sheet};
use std::any::TypeId;
let mut sheet = Sheet::new();
let bound = sheet.add_source(10_i32);
let low = sheet.add_source(0_i32);
let raw = sheet.add_cell(5_i32);
let zero = sheet.add_cell(-5_i32);
let one = sheet.add_cell(20_i32);
let two = sheet.add_cell(30_i32);
sheet.add_filter(raw, Filter::new(TypeId::of::<i32>(), vec![], vec![],
    |v, _| Ok(Box::new(*v.downcast_ref::<i32>().unwrap()))))?;
sheet.add_filter(zero, Filter::from_fn_0(|v: &i32| Ok((*v).max(0))))?;
sheet.add_filter(one, Filter::from_fn_1(bound, |v: &i32, hi: &i32| Ok((*v).min(*hi))))?;
sheet.add_filter(two, Filter::from_fn_2([low, bound],
    |v: &i32, lo: &i32, hi: &i32| Ok((*v).clamp(*lo, *hi))))?;
sheet.propagate()?;
assert_eq!(*sheet.read::<i32>(raw)?, 5);
assert_eq!(*sheet.read::<i32>(zero)?, 0);
assert_eq!(*sheet.read::<i32>(one)?, 10);
assert_eq!(*sheet.read::<i32>(two)?, 10);
assert_eq!(*sheet.source::<i32>(two)?, 30);

Structs§

Filter
An idempotent, per-cell domain constraint with optional dynamic arguments.

Enums§

FilterKind
What shape of validation/derivation a Filter performs, beyond its opaque function — set by adam-lang’s compile phase when a filter’s expression matches a recognized structural form. Opaque carries no extra information; consumers that don’t care about structure treat every kind identically at write/propagate time — FilterKind is purely informational, queried by consumers like begin’s UI that want to render a specialized editor without inspecting the filter’s function.
FilterViolation
The outcome of re-checking a filter against a value a relationship’s method derived, rather than a value written externally.