ctb_formats_eite/
util.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program.  If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! General utility modules and helper functions for the EITE runtime.
21
22pub mod array;
23pub mod ascii;
24pub mod bitwise;
25pub mod logic;
26pub mod math;
27pub mod pred;
28pub mod string;
29
30/// Representation of the “generic” StageL values referenced by the JS.
31/// We only add the variants actually needed by the converted functions here.
32#[derive(Clone, Debug, PartialEq)]
33pub enum Value {
34    Bool(bool),
35    Int(i32),
36    Str(String),
37    Bytes(Vec<u8>),
38    Array(Vec<Value>),
39}
40
41impl Value {
42    pub fn as_bool(&self) -> Option<bool> {
43        if let Value::Bool(b) = self {
44            Some(*b)
45        } else {
46            None
47        }
48    }
49    pub fn as_int(&self) -> Option<i32> {
50        if let Value::Int(i) = self {
51            Some(*i)
52        } else {
53            None
54        }
55    }
56    pub fn as_str(&self) -> Option<&str> {
57        if let Value::Str(s) = self {
58            Some(s)
59        } else {
60            None
61        }
62    }
63    pub fn is_generic_primitive(&self) -> bool {
64        matches!(self, Value::Bool(_) | Value::Int(_) | Value::Str(_))
65    }
66}
67
68pub fn ne_values(a: &Value, b: &Value) -> bool {
69    !eq_values(a, b)
70}
71
72/// Comparison helpers (implEq / implGt / implLt) simplified.
73/// Generic equality only covers primitive subset implemented in Value above.
74/// (The original JS allowed direct === which included string/int/bool.)
75pub fn eq_values(a: &Value, b: &Value) -> bool {
76    a == b
77}
78pub fn gt_int(a: i32, b: i32) -> bool {
79    a > b
80}
81pub fn lt_int(a: i32, b: i32) -> bool {
82    a < b
83}