predicator_rs/lib.rs
1//! A Rust port of the predicator condition language, which compiles a
2//! condition to a small instruction set and evaluates it on a stack machine.
3//!
4//! The crate holds its value model: [`Value`] and the types it is
5//! built from ([`Map`] and its [`Key`], [`Date`], [`DateTime`], [`Duration`]), the
6//! [`Error`] every entry point answers, and the [`tagged`] codec for the
7//! conformance corpus's tagged-value encoding.
8//!
9//! The evaluator runs every tier of the instruction set:
10//! [`evaluate`] runs [`Instructions`] (read with
11//! [`Instructions::from_json`]) over a [`Context`] with the [`Options`]
12//! given per call, answering a [`Value`]. It runs `lit`, `load`, `compare`,
13//! `not`, `unary_bang`, `unary_minus`, `jump_if_falsy_or_pop` and
14//! `jump_if_true_or_pop`; `add`, `subtract`, `multiply`, `divide` and
15//! `modulo`; `access`, `bracket_access`, `in`, `contains` and `make_list`;
16//! `object_new`, `object_set`, `duration` and `relative_date`, which
17//! reads the current time from the clock the [`Options`] name; and `call`,
18//! which calls a function the [`Options`] pass in by name
19//! ([`Options::function`]) or one of the reference's library functions
20//! (the string, list, math, date and JSON functions); `store` and `pop`;
21//! `cast`; `jump` and `pop_jump_if_falsy`; `jump_backward`, each back edge
22//! spending one of the run's loop budget ([`Options::loop_budget`]); and
23//! `presence` and `intersects`. Every other opcode is refused as `unknown_instruction`, and the retired `and`
24//! and `or` as `retired_opcode`.
25//!
26//! The compiler turns a source into the same instructions the reference's
27//! compiler does: [`compile`] reads one expression and [`compile_program`]
28//! a statement program, each answering [`Instructions`] or the
29//! [`Error::Parse`] that refused the source, and [`Instructions::to_json`]
30//! writes the list back in the tagged encoding. [`evaluate`] also takes a
31//! source as its program (a `&str`), compiled as an expression, and
32//! [`execute`] and [`execute_value`] one compiled as a statement program.
33//!
34//! [`execute`] and [`execute_value`] run the same instructions as a
35//! statement program: the answer is an [`Execution`], the context the run
36//! leaves (a new value; the one passed in is never changed) with the roots
37//! it stored into, or a [`Stopped`], the error a run stopped on with the
38//! context as of its last completed statement. A store into a root the
39//! [`Options`] protect ([`Options::protected_roots`]) is refused.
40//!
41//! [`parse_duration`] reads a duration from its literal spelling (`"1h30m"`),
42//! and [`ISA_VERSION`] is the version of the instruction set the crate runs.
43//!
44//! # The public surface
45//!
46//! The crate root holds the functions [`compile`], [`compile_program`],
47//! [`evaluate`], [`execute`], [`execute_value`] and [`parse_duration`]; the
48//! constant [`ISA_VERSION`]; the module [`tagged`]; and the types [`Value`]
49//! (with its parts [`Map`] and [`Key`]), [`Context`], [`Instructions`],
50//! [`Duration`], [`Date`], [`DateTime`], [`Options`] (with its part
51//! [`OnUnbound`]), [`Execution`], [`Stopped`], [`Position`], and [`Error`]
52//! with the four error types it holds. Every other module is private.
53//! Every entry point answers a `Result`, and nothing is configured
54//! globally: host functions, the clock and the random source are passed per
55//! call in an [`Options`], and may borrow from the caller for the length of
56//! the call.
57//!
58//! No source, instruction list or context makes the crate panic or
59//! overflow its stack. A panic inside a function the caller passes in (a
60//! host function, a clock or a random source) is not caught: it unwinds
61//! through the run to the caller, as any panic in the caller's own code
62//! does.
63//!
64//! With the `serde` feature (off by default), [`Value`] implements
65//! `Serialize` and `Deserialize` in the tagged encoding, and [`Date`],
66//! [`DateTime`] and [`Duration`] in the text and the unit map that encoding
67//! carries them in.
68
69#![forbid(unsafe_code)]
70#![warn(missing_docs)]
71
72mod boundary;
73mod civil;
74mod compare;
75mod compiler;
76mod context;
77mod error;
78mod evaluator;
79mod execution;
80mod instructions;
81mod json;
82mod lexer;
83mod map;
84mod options;
85mod parser;
86#[cfg(feature = "serde")]
87mod serde_impl;
88pub mod tagged;
89mod value;
90
91pub use civil::{Date, DateTime, Duration, parse_duration};
92pub use compiler::{compile, compile_program};
93pub use context::Context;
94pub use error::{
95 Error, EvaluationError, ParseError, Position, TypeMismatchError, UndefinedVariableError,
96};
97pub use evaluator::{evaluate, execute, execute_value};
98pub use execution::{Execution, Stopped};
99pub use instructions::Instructions;
100pub use map::{Key, Map};
101pub use options::{OnUnbound, Options};
102pub use value::Value;
103
104/// The version of the reference's instruction set this crate runs: every
105/// opcode of every tier up to it, the version the vendored conformance
106/// corpus names.
107///
108/// ```
109/// assert_eq!(predicator_rs::ISA_VERSION, 7);
110/// ```
111pub const ISA_VERSION: u32 = 7;
112
113/// The README's Rust examples, collected only by `cargo test --doc` so that
114/// they compile and run as doctests.
115#[cfg(doctest)]
116#[doc = include_str!("../README.md")]
117struct ReadmeDoctests;
118
119#[cfg(test)]
120mod tests {
121 use unicode_segmentation::UnicodeSegmentation;
122
123 // The crate's one required dependency resolves and links.
124 // Sabotage: with the expected count changed from 2 to 3, this test fails.
125 #[test]
126 fn links_the_segmentation_dependency() {
127 assert_eq!("e\u{301}x".graphemes(true).count(), 2);
128 }
129}