Skip to main content

openformula_kernel/
value.rs

1use alloc::string::String;
2use alloc::vec::Vec;
3
4use crate::{CalcError, Number};
5
6/// A scalar value understood by the calculation kernel.
7#[derive(Clone, Debug, PartialEq)]
8pub enum Value {
9    /// A finite calculation number.
10    Number(Number),
11    /// UTF-8 text.
12    Text(String),
13    /// A distinct logical value.
14    Boolean(bool),
15    /// An empty cell or absent scalar value.
16    Empty,
17    /// A typed calculation error carried as a value for propagation.
18    Error(CalcError),
19}
20
21impl From<Number> for Value {
22    fn from(value: Number) -> Self {
23        Self::Number(value)
24    }
25}
26
27impl From<i64> for Value {
28    fn from(value: i64) -> Self {
29        Self::Number(Number::from_i64(value))
30    }
31}
32
33impl From<bool> for Value {
34    fn from(value: bool) -> Self {
35        Self::Boolean(value)
36    }
37}
38
39impl From<String> for Value {
40    fn from(value: String) -> Self {
41        Self::Text(value)
42    }
43}
44
45impl From<&str> for Value {
46    fn from(value: &str) -> Self {
47        Self::Text(value.into())
48    }
49}
50
51impl From<CalcError> for Value {
52    fn from(value: CalcError) -> Self {
53        Self::Error(value)
54    }
55}
56
57/// An evaluated function argument retaining whether values came from a range.
58#[derive(Clone, Debug, PartialEq)]
59pub enum Argument {
60    /// One directly supplied scalar.
61    Scalar(Value),
62    /// Values supplied through a host-resolved range or reference list.
63    Range(Vec<Value>),
64    /// An explicitly omitted optional parameter.
65    Missing,
66}
67
68impl Argument {
69    /// Construct a scalar argument.
70    #[must_use]
71    pub fn scalar(value: impl Into<Value>) -> Self {
72        Self::Scalar(value.into())
73    }
74
75    /// Construct a range argument in deterministic source order.
76    #[must_use]
77    pub fn range(values: impl IntoIterator<Item = Value>) -> Self {
78        Self::Range(values.into_iter().collect())
79    }
80}
81
82impl From<Value> for Argument {
83    fn from(value: Value) -> Self {
84        Self::Scalar(value)
85    }
86}