Struct Context

Source
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

Source

pub fn new() -> Context

Creates a new, empty context

Source

pub fn set_time(&mut self, time: DateTime<Local>)

Source

pub fn update_time(&mut self)

Source

pub fn load_dates(&mut self, dates: Vec<Vec<DatePattern>>)

Source

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.

Source

pub fn canonicalize(&self, name: &str) -> Option<String>

Given a unit name, try to return a canonical name (expanding aliases and such)

Source

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).

Source

pub fn typo_dym<'a>(&'a self, what: &str) -> Option<&'a str>

Source

pub fn unknown_unit_err(&self, name: &str) -> NotFoundError

Source

pub fn humanize<Tz: TimeZone>(&self, date: DateTime<Tz>) -> Option<String>

Source

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.

Source

pub fn eval(&self, expr: &Expr) -> Result<Value, QueryError>

Evaluates an expression to compute its value, excluding -> conversions.

Source

pub fn eval_outer(&self, query: &Query) -> Result<QueryReply, QueryError>

👎Deprecated since 0.7.0: renamed to eval_query()
Source

pub fn eval_query(&self, query: &Query) -> Result<QueryReply, QueryError>

Evaluates an expression, include -> conversions.

Source

pub fn show( &self, raw: &Number, bottom: &Number, bottom_name: BTreeMap<String, isize>, bottom_const: Numeric, base: u8, digits: Digits, ) -> ConversionReply

Trait Implementations§

Source§

impl Debug for Context

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Context

Source§

fn default() -> Self

Equivalent to Context::new()

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.