Skip to main content

openformula_kernel/
context.rs

1use crate::{CalcError, CalcErrorKind, CalcResult, Number};
2
3/// Host-provided capabilities for impure or time-dependent functions.
4///
5/// The initial standard function subset is pure, but the context makes future
6/// clock and random functions injectable rather than ambient.
7pub trait EvalContext {
8    /// Return the current Unix timestamp in milliseconds.
9    fn now_utc_millis(&mut self) -> CalcResult<i64> {
10        Err(CalcError::new(
11            CalcErrorKind::Unsupported,
12            "clock capability is unavailable",
13        ))
14    }
15
16    /// Return a random number in the half-open interval `[0, 1)`.
17    fn random_unit(&mut self) -> CalcResult<Number> {
18        Err(CalcError::new(
19            CalcErrorKind::Unsupported,
20            "random capability is unavailable",
21        ))
22    }
23}
24
25/// A context that rejects every impure capability.
26#[derive(Clone, Copy, Debug, Default)]
27pub struct PureContext;
28
29impl EvalContext for PureContext {}