Skip to main content

lemma/
lib.rs

1//! # Lemma Engine
2//!
3//! **Rules for man and machine**
4//!
5//! Lemma is a declarative programming language for expressing rules, data, and business logic
6//! in a way that is both human-readable and machine-executable.
7//!
8//! ## Quick Start
9//!
10//! ```rust,no_run
11//! use lemma::{Engine, SourceType};
12//!
13//! let mut engine = Engine::new();
14//!
15//! // Load Lemma code
16//! engine.load(r#"
17//!     spec example
18//!     data price: 100
19//!     data quantity: 5
20//!     rule total: price * quantity
21//! "#, SourceType::Path(std::sync::Arc::new(std::path::PathBuf::from("example.lemma")))).expect("failed to load");
22//!
23//! // Evaluate the spec (all rules, no data values)
24//! let now = lemma::DateTimeValue::now();
25//! let response = engine.run(None, "example", Some(&now), std::collections::HashMap::new(), false, None).unwrap();
26//! ```
27//!
28//! ## Core Concepts
29//!
30//! ### Specs
31//! A spec is a collection of data and rules. Specs can reference
32//! other Specs to build composable logic.
33//!
34//! ### Data
35//! Data are named values: numbers, text, dates, booleans, or typed units
36//! like `50 kilograms` or `100`.
37//!
38//! ### Rules
39//! Rules compute values based on data and other rules. They support
40//! conditional logic through "unless" clauses.
41//!
42//! ### Types
43//! Lemma has a rich type system including units (mass, length, time, money)
44//! with automatic conversions.
45
46#[cfg(test)]
47mod tests;
48
49pub(crate) mod computation;
50pub(crate) mod engine;
51pub(crate) mod error;
52pub(crate) mod evaluation;
53pub(crate) mod formatting;
54pub(crate) mod limits;
55pub(crate) mod literals;
56pub(crate) mod parsing;
57pub(crate) mod planning;
58pub(crate) mod registry;
59pub(crate) mod spec_set_id;
60pub(crate) mod stdlib;
61
62#[cfg(not(target_arch = "wasm32"))]
63pub mod deps;
64
65#[cfg(target_arch = "wasm32")]
66pub mod wasm;
67
68// === Construction + orchestration ===
69#[cfg(not(target_arch = "wasm32"))]
70pub use engine::{
71    collect_lemma_sources, Context, Engine, Errors, ResolvedRepository, EMBEDDED_STDLIB_REPOSITORY,
72};
73#[cfg(target_arch = "wasm32")]
74pub use engine::{Context, Engine, Errors, ResolvedRepository, EMBEDDED_STDLIB_REPOSITORY};
75
76// === Errors ===
77pub use error::{Error, ErrorDetails, ErrorKind, RequestErrorKind};
78
79// === Limits ===
80pub use limits::{
81    ResourceLimits, MAX_DATA_NAME_LENGTH, MAX_RULE_NAME_LENGTH, MAX_SPEC_NAME_LENGTH,
82};
83
84// === Source + parsing ===
85pub use parsing::ast::{
86    DataValue, DateGranularity, DateTimeValue, EffectiveDate, LemmaRepository, LemmaSpec, Span,
87    SpecRef, TimezoneValue,
88};
89pub use parsing::source::{Source, SourceType};
90// Planning [`semantics::DataValue`] (bindings) vs parse [`ast::DataValue`]; only the parse enum is `DataValue` at the root.
91pub use parsing::lexer::{Lexer, TokenKind};
92pub use parsing::{parse, ParseResult};
93pub use planning::semantics::DataValue as BindingDataValue;
94
95// === Data input ===
96pub use planning::data_input::DataValueInput;
97
98// === Execution plan + schema ===
99pub use planning::execution_plan::{
100    type_detail_lines, validate_instruction_jumps, validate_instructions, DataEntry, DataOverlay,
101    ExecutionPlan, ExecutionPlanSerialized, Instruction, Instructions, SpecSchema,
102    INSTRUCTIONS_VERSION,
103};
104pub use planning::plan;
105pub use planning::semantics::{
106    DataDefinition, DataPath, LemmaType, LiteralValue, QuantityUnit, QuantityUnits, RatioUnit,
107    RatioUnits, RulePath, Source as PlanningSource, TypeSpecification, ValueKind,
108};
109pub use planning::spec_set::LemmaSpecSet;
110
111// === Evaluation output ===
112pub use evaluation::explanations::{
113    format_explanation, format_expression, Cause, ConversionTraceRole, ConversionTraceStep,
114    Explanation, ExplanationNode,
115};
116pub use evaluation::operations::{ComputationKind, OperationResult, VetoType};
117pub use evaluation::response::{DataGroup, Response, RuleResult};
118
119// === Formatting ===
120pub use formatting::{format_parse_result, format_source, format_specs};
121
122// === Registry ===
123#[cfg(all(feature = "registry", not(target_arch = "wasm32")))]
124pub use registry::resolve_registry_references;
125pub use registry::{LemmaBase, Registry, RegistryBundle, RegistryError, RegistryErrorKind};
126
127// === Spec set ID ===
128pub use spec_set_id::parse_spec_set_id;
129
130// === Stdlib ===
131pub use stdlib::UNITS_LEMMA;