pub struct Context {
pub registry: Registry,
pub now: DateTime<Local>,
pub use_humanize: bool,
pub save_previous_result: bool,
pub previous_result: Option<Number>,
/* private fields */
}
Expand description
The evaluation context that contains unit definitions.
Fields§
§registry: Registry
Contains all the information about units.
now: DateTime<Local>
The current time, as set by the caller.
This is used instead of directly asking the OS for the time
since it allows determinism in unit tests, and prevents edge
cases like now - now
being non-zero.
use_humanize: bool
Enables the use of chrono-humanize. It can be disabled for unit tests, as well as in wasm builds where the time API panics.
save_previous_result: bool
Whether to save the previous query result and make it available
as the ans
variable.
previous_result: Option<Number>
The previous query result.
Implementations§
Source§impl Context
impl Context
pub fn set_time(&mut self, time: DateTime<Local>)
pub fn update_time(&mut self)
pub fn load_dates(&mut self, dates: Vec<Vec<DatePattern>>)
Sourcepub fn lookup(&self, name: &str) -> Option<Number>
pub fn lookup(&self, name: &str) -> Option<Number>
Given a unit name, returns its value if it exists. Supports SI prefixes, plurals, bare dimensions like length, and quantities.
Sourcepub fn canonicalize(&self, name: &str) -> Option<String>
pub fn canonicalize(&self, name: &str) -> Option<String>
Given a unit name, try to return a canonical name (expanding aliases and such)
Sourcepub fn describe_unit(&self, value: &Number) -> (bool, String)
pub fn describe_unit(&self, value: &Number) -> (bool, String)
Describes a value’s unit, gives true if the unit is reciprocal (e.g. you should prefix “1.0 / “ or replace “multiply” with “divide” when rendering it).
pub fn typo_dym<'a>(&'a self, what: &str) -> Option<&'a str>
pub fn unknown_unit_err(&self, name: &str) -> NotFoundError
pub fn humanize<Tz: TimeZone>(&self, date: DateTime<Tz>) -> Option<String>
Sourcepub fn load(&mut self, defs: Defs) -> Result<(), String>
pub fn load(&mut self, defs: Defs) -> Result<(), String>
Takes a parsed definitions.units from
gnu_units::parse()
. Returns a list of errors, if there were any.
Sourcepub fn eval(&self, expr: &Expr) -> Result<Value, QueryError>
pub fn eval(&self, expr: &Expr) -> Result<Value, QueryError>
Evaluates an expression to compute its value, excluding ->
conversions.
pub fn eval_outer(&self, query: &Query) -> Result<QueryReply, QueryError>
Sourcepub fn eval_query(&self, query: &Query) -> Result<QueryReply, QueryError>
pub fn eval_query(&self, query: &Query) -> Result<QueryReply, QueryError>
Evaluates an expression, include ->
conversions.