Skip to main content

wolfram_expr/
association.rs

1//! [`Association`][ref/Association]<sub>WL</sub> data type — `<|k -> v, ...|>`.
2//!
3//! `Association` is a plain type alias for `Vec<RuleEntry>`. Use the
4//! ordinary `Vec` API (`push`, `iter`, `len`, …); there is no map-style
5//! lookup — iterate to find an entry by key.
6//!
7//! # Example
8//!
9//! ```
10//! use wolfram_expr::{Association, Expr, RuleEntry};
11//!
12//! let mut a: Association = Association::new();
13//! a.push(RuleEntry::rule(Expr::from("eager"), Expr::from(1)));
14//! a.push(RuleEntry::rule_delayed(Expr::from("lazy"), Expr::from(2)));
15//! ```
16//!
17//! [ref/Association]: https://reference.wolfram.com/language/ref/Association.html
18
19use crate::Expr;
20
21/// Single association entry — key, value, and a flag indicating
22/// `Rule` (`->`, immediate) vs `RuleDelayed` (`:>`, held).
23#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
24pub struct RuleEntry {
25    /// The left-hand side expression.
26    pub key: Expr,
27    /// The right-hand side expression.
28    pub value: Expr,
29    /// `false` for `Rule` (`->`, immediate), `true` for `RuleDelayed` (`:>`, held).
30    pub delayed: bool,
31}
32
33impl RuleEntry {
34    /// Construct a `Rule` (`->`, immediate) entry.
35    pub fn rule(key: Expr, value: Expr) -> Self {
36        RuleEntry {
37            key,
38            value,
39            delayed: false,
40        }
41    }
42
43    /// Construct a `RuleDelayed` (`:>`, held) entry.
44    pub fn rule_delayed(key: Expr, value: Expr) -> Self {
45        RuleEntry {
46            key,
47            value,
48            delayed: true,
49        }
50    }
51}
52
53/// Wolfram Language `<|...|>` — an ordered list of [`RuleEntry`].
54///
55/// A plain type alias for `Vec<RuleEntry>`. Insertion order is preserved.
56/// No map-style lookup is exposed; iterate to find an entry by key.
57pub type Association = Vec<RuleEntry>;