Xprs

Struct Xprs 

Source
#[non_exhaustive]
pub struct Xprs<'a> { pub root: Element<'a>, pub vars: HashSet<&'a str>, }
Expand description

Represents a mathematical expression and its variables.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§root: Element<'a>

The root element of the expression.

§vars: HashSet<&'a str>

The set of variables present in the expression.

Implementations§

Source§

impl Xprs<'_>

Source

pub fn eval(&self, variables: &HashMap<&str, f64>) -> Result<f64, EvalError>

Evaluates the expression using the provided variable values. Returns an f64 if the evaluation is successful, or an EvalError if an error occurs.

§Errors

An EvalError is returned if a variable is not provided.

§Example
use xprs::Xprs;
use std::collections::HashMap;

let expression = "2 * x + y";
let xprs = Xprs::try_from(expression)?;

let mut variable_values = HashMap::new();
variable_values.insert("x", 3.0);
variable_values.insert("y", 2.0);

let result = xprs.eval(&variable_values);
assert_eq!(result, Ok(8.0));

// we didn't provide the variables, so this should fail
let failed_eval = xprs.eval(&HashMap::new());
assert!(failed_eval.is_err());
Source

pub fn eval_unchecked(&self, variables: &HashMap<&str, f64>) -> f64

Evaluates the expression using the provided variable values without error handling. Returns an f64 if the evaluation is successful, or panics if an error occurs.

§Panic

Use with caution, as it may panic if variable(s) are missing.

§Example
use xprs::Xprs;
use std::collections::HashMap;

let expression = "2 * x + y";
let xprs = Xprs::try_from(expression)?;

let mut variable_values = HashMap::new();
variable_values.insert("x", 3.0);
variable_values.insert("y", 2.0);

let result = xprs.eval_unchecked(&variable_values);
assert_eq!(result, 8.0);

// we didn't provide the variables, so this should panic
assert_panic!(xprs.eval_unchecked(&HashMap::new()));
Source

pub fn simplify_for_in_place(&mut self, var: (&str, f64))

Simplifies the expression in-place for a single variable.

§Example
use xprs::Xprs;

let expression = "2 * x + y";
let mut xprs = Xprs::try_from(expression)?;

assert_eq!(format!("{xprs}"), "((2 * x) + y)");
assert_eq!(xprs.vars, ["x", "y"].into());

xprs.simplify_for_in_place(("x", 3.0));

assert_eq!(format!("{xprs}"), "(6 + y)");
assert_eq!(xprs.vars, ["y"].into());
Source

pub fn eval_no_vars(&self) -> Result<f64, EvalError>

Simple wrapper around Xprs::eval that doesn’t require any variables. This will obviously fail if the expression contains variables.

§Errors

An EvalError is returned if the expression contains variables.

Source

pub fn eval_no_vars_unchecked(&self) -> f64

Simple wrapper around Xprs::eval_unchecked that doesn’t require any variables. This will obviously fail if the expression contains variables.

§Panic

Use with caution, as it may panic if the expression contains variables.

Source

pub fn simplify_for(self, var: (&str, f64)) -> Self

Simplifies the expression in-place for a single variable and returns the expression.

§Example
use xprs::Xprs;

let expression = "2 * x + y";
let xprs = Xprs::try_from(expression)?;

assert_eq!(format!("{xprs}"), "((2 * x) + y)");
assert_eq!(xprs.vars, ["x", "y"].into());

let simplified_xprs = xprs.simplify_for(("x", 3.0));

assert_eq!(format!("{simplified_xprs}"), "(6 + y)");
assert_eq!(simplified_xprs.vars, ["y"].into());
Source

pub fn simplify_for_multiple_in_place(&mut self, vars: &[(&str, f64)])

Simplifies the expression in-place for multiple variables.

§Example
use xprs::Xprs;

let expression = "2 * x + y + 4z";
let mut xprs = Xprs::try_from(expression)?;

assert_eq!(format!("{xprs}"), "(((2 * x) + y) + (4 * z))");
assert_eq!(xprs.vars, ["x", "y", "z"].into());

xprs.simplify_for_multiple_in_place(&[("x", 3.0), ("z", 2.0)]);

assert_eq!(format!("{xprs}"), "((6 + y) + 8)");
assert_eq!(xprs.vars, ["y"].into());
Source

pub fn simplify_for_multiple(self, vars: &[(&str, f64)]) -> Self

Simplifies the expression in-place for multiple variables and returns the expression.

§Example
use xprs::Xprs;

let expression = "2 * x + y + 4z";
let xprs = Xprs::try_from(expression)?;

assert_eq!(format!("{xprs}"), "(((2 * x) + y) + (4 * z))");
assert_eq!(xprs.vars, ["x", "y", "z"].into());

let simplified_xprs = xprs.simplify_for_multiple(&[("x", 3.0), ("z", 2.0)]);

assert_eq!(format!("{simplified_xprs}"), "((6 + y) + 8)");
assert_eq!(simplified_xprs.vars, ["y"].into());
Source§

impl<'a> Xprs<'a>

Source

pub fn bind(self, var: &'a str) -> Result<impl Fn(f64) -> f64 + 'a, BindError>

Creates a function of one variable based on this Xprs instance.

§Errors

A BindError is returned if one or more required variables were not provided.

§Example
use xprs::Parser;

let expression = Parser::default().parse("x + 2")?;
let func = expression.bind("x");

assert!(func.is_ok());
let func = func?;

let result = func(3.0);
assert_eq!(result, 5.0);
Source

pub fn bind2( self, var1: &'a str, var2: &'a str, ) -> Result<impl Fn(f64, f64) -> f64 + 'a, BindError>

Creates a function of two f64 based on this Xprs instance.

§Errors

A BindError is returned if one or more required variables were not provided.

§Example
use xprs::Parser;

let expression = Parser::default().parse("x + y")?;
let func = expression.bind2("x", "y");

assert!(func.is_ok());
let func = func?;

let result = func(3.0, 2.0);
assert_eq!(result, 5.0);
Source

pub fn bind3( self, var1: &'a str, var2: &'a str, var3: &'a str, ) -> Result<impl Fn(f64, f64, f64) -> f64 + 'a, BindError>

Creates a function of three f64 based on this Xprs instance.

§Errors

A BindError is returned if one or more required variables were not provided.

§Example
use xprs::Parser;

let expression = Parser::default().parse("x + y + z")?;
let func = expression.bind3("x", "y", "z");

assert!(func.is_ok());
let func = func?;

let result = func(3.0, 2.0, 1.0);
assert_eq!(result, 6.0);
Source

pub fn bind4( self, var1: &'a str, var2: &'a str, var3: &'a str, var4: &'a str, ) -> Result<impl Fn(f64, f64, f64, f64) -> f64 + 'a, BindError>

Creates a function of four f64 based on this Xprs instance.

§Errors

A BindError is returned if one or more required variables were not provided.

§Example
use xprs::Parser;

let expression = Parser::default().parse("w + x + y + z")?;
let func = expression.bind4("w", "x", "y", "z");

assert!(func.is_ok());
let func = func?;

let result = func(1.0, 2.0, 3.0, 4.0);
assert_eq!(result, 10.0);
Source

pub fn bind5( self, var1: &'a str, var2: &'a str, var3: &'a str, var4: &'a str, var5: &'a str, ) -> Result<impl Fn(f64, f64, f64, f64, f64) -> f64 + 'a, BindError>

Creates a function of five f64 based on this Xprs instance.

§Errors

A BindError is returned if one or more required variables were not provided.

§Example
use xprs::Parser;

let expression = Parser::default().parse("v + w + x + y + z")?;
let func = expression.bind5("v", "w", "x", "y", "z");

assert!(func.is_ok());
let func = func?;

let result = func(1.0, 2.0, 3.0, 4.0, 5.0);
assert_eq!(result, 15.0);
Source

pub fn bind6( self, var1: &'a str, var2: &'a str, var3: &'a str, var4: &'a str, var5: &'a str, var6: &'a str, ) -> Result<impl Fn(f64, f64, f64, f64, f64, f64) -> f64 + 'a, BindError>

Creates a function of six f64 based on this Xprs instance.

§Errors

A BindError is returned if one or more required variables were not provided.

§Example
use xprs::Parser;

let expression = Parser::default().parse("u + v + w + x + y + z")?;
let func = expression.bind6("u", "v", "w", "x", "y", "z");

assert!(func.is_ok());
let func = func?;

let result = func(1.0, 2.0, 3.0, 4.0, 5.0, 6.0);
assert_eq!(result, 21.0);
Source

pub fn bind7( self, var1: &'a str, var2: &'a str, var3: &'a str, var4: &'a str, var5: &'a str, var6: &'a str, var7: &'a str, ) -> Result<impl Fn(f64, f64, f64, f64, f64, f64, f64) -> f64 + 'a, BindError>

Creates a function of seven f64 based on this Xprs instance.

§Errors

A BindError is returned if one or more required variables were not provided.

§Example
use xprs::Parser;

let expression = Parser::default().parse("t + u + v + w + x + y + z")?;
let func = expression.bind7("t", "u", "v", "w", "x", "y", "z");

assert!(func.is_ok());
let func = func?;

let result = func(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0);
assert_eq!(result, 28.0);
Source

pub fn bind8( self, var1: &'a str, var2: &'a str, var3: &'a str, var4: &'a str, var5: &'a str, var6: &'a str, var7: &'a str, var8: &'a str, ) -> Result<impl Fn(f64, f64, f64, f64, f64, f64, f64, f64) -> f64 + 'a, BindError>

Creates a function of eight f64 based on this Xprs instance.

§Errors

A BindError is returned if one or more required variables were not provided.

§Example
use xprs::Parser;

let expression = Parser::default().parse("s + t + u + v + w + x + y + z")?;
let func = expression.bind8("s", "t", "u", "v", "w", "x", "y", "z");

assert!(func.is_ok());
let func = func?;

let result = func(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);
assert_eq!(result, 36.0);
Source

pub fn bind9( self, var1: &'a str, var2: &'a str, var3: &'a str, var4: &'a str, var5: &'a str, var6: &'a str, var7: &'a str, var8: &'a str, var9: &'a str, ) -> Result<impl Fn(f64, f64, f64, f64, f64, f64, f64, f64, f64) -> f64 + 'a, BindError>

Creates a function of nine f64 based on this Xprs instance.

§Errors

A BindError is returned if one or more required variables were not provided.

§Example
use xprs::Parser;

let expression = Parser::default().parse("r + s + t + u + v + w + x + y + z")?;
let func = expression.bind9("r", "s", "t", "u", "v", "w", "x", "y", "z");

assert!(func.is_ok());
let func = func?;

let result = func(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0);
assert_eq!(result, 45.0);
Source

pub fn bind_n<const T: usize>( self, vars: [&'a str; T], ) -> Result<impl Fn([f64; T]) -> f64 + 'a, BindError>

Creates a function of any number* of f64 based on this Xprs instance. *The number of variables must be known at compile time.

The returned closure takes an array of f64 as input and returns an f64.

§Errors

A BindError is returned if one or more required variables were not provided.

§Example
use xprs::Parser;

let expression = Parser::default().parse("a + b + c + d")?;
let func = expression.bind_n(["a", "b", "c", "d"]);

assert!(func.is_ok());
let func = func?;

let result = func([1.0, 2.0, 3.0, 4.0]);
assert_eq!(result, 10.0);
Source

pub fn bind_n_runtime( self, vars: &'a [&'a str], ) -> Result<impl Fn(&[f64]) -> Result<f64, EvalError> + 'a, BindError>

Creates a function of any number of f64 based on this Xprs instance.

The returned closure takes a slice of f64 as input and returns a Result containing an f64 if the evaluation is successful, or an EvalError if an error occurs.

§Errors

A BindError is returned if one or more required variables were not provided.

§Example
use xprs::Parser;

let expression = Parser::default().parse("a + b + c + d")?;
let func = expression.bind_n_runtime(&["a", "b", "c", "d"]);

assert!(func.is_ok());
let func = func?;

let result = func(&[1.0, 2.0, 3.0, 4.0])?;
assert_eq!(result, 10.0);

Trait Implementations§

Source§

impl<'a> Clone for Xprs<'a>

Source§

fn clone(&self) -> Xprs<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Xprs<'a>

Source§

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

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

impl Display for Xprs<'_>

Source§

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

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

impl<'a> PartialEq for Xprs<'a>

Source§

fn eq(&self, other: &Xprs<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'input> TryFrom<&'input str> for Xprs<'input>

Source§

type Error = ParseError

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

fn try_from(value: &'input str) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> StructuralPartialEq for Xprs<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Xprs<'a>

§

impl<'a> !RefUnwindSafe for Xprs<'a>

§

impl<'a> Send for Xprs<'a>

§

impl<'a> Sync for Xprs<'a>

§

impl<'a> Unpin for Xprs<'a>

§

impl<'a> !UnwindSafe for Xprs<'a>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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<D> OwoColorize for D

Source§

fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>
where C: Color,

Set the foreground color generically Read more
Source§

fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>
where C: Color,

Set the background color generically. Read more
Source§

fn black(&self) -> FgColorDisplay<'_, Black, Self>

Change the foreground color to black
Source§

fn on_black(&self) -> BgColorDisplay<'_, Black, Self>

Change the background color to black
Source§

fn red(&self) -> FgColorDisplay<'_, Red, Self>

Change the foreground color to red
Source§

fn on_red(&self) -> BgColorDisplay<'_, Red, Self>

Change the background color to red
Source§

fn green(&self) -> FgColorDisplay<'_, Green, Self>

Change the foreground color to green
Source§

fn on_green(&self) -> BgColorDisplay<'_, Green, Self>

Change the background color to green
Source§

fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>

Change the foreground color to yellow
Source§

fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>

Change the background color to yellow
Source§

fn blue(&self) -> FgColorDisplay<'_, Blue, Self>

Change the foreground color to blue
Source§

fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>

Change the background color to blue
Source§

fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>

Change the foreground color to magenta
Source§

fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>

Change the background color to magenta
Source§

fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>

Change the foreground color to purple
Source§

fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>

Change the background color to purple
Source§

fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>

Change the foreground color to cyan
Source§

fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>

Change the background color to cyan
Source§

fn white(&self) -> FgColorDisplay<'_, White, Self>

Change the foreground color to white
Source§

fn on_white(&self) -> BgColorDisplay<'_, White, Self>

Change the background color to white
Source§

fn default_color(&self) -> FgColorDisplay<'_, Default, Self>

Change the foreground color to the terminal default
Source§

fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>

Change the background color to the terminal default
Source§

fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>

Change the foreground color to bright black
Source§

fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>

Change the background color to bright black
Source§

fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>

Change the foreground color to bright red
Source§

fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>

Change the background color to bright red
Source§

fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>

Change the foreground color to bright green
Source§

fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>

Change the background color to bright green
Source§

fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>

Change the foreground color to bright yellow
Source§

fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>

Change the background color to bright yellow
Source§

fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>

Change the foreground color to bright blue
Source§

fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>

Change the background color to bright blue
Source§

fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>

Change the foreground color to bright magenta
Source§

fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>

Change the background color to bright magenta
Source§

fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>

Change the foreground color to bright purple
Source§

fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>

Change the background color to bright purple
Source§

fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>

Change the foreground color to bright cyan
Source§

fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>

Change the background color to bright cyan
Source§

fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>

Change the foreground color to bright white
Source§

fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>

Change the background color to bright white
Source§

fn bold(&self) -> BoldDisplay<'_, Self>

Make the text bold
Source§

fn dimmed(&self) -> DimDisplay<'_, Self>

Make the text dim
Source§

fn italic(&self) -> ItalicDisplay<'_, Self>

Make the text italicized
Source§

fn underline(&self) -> UnderlineDisplay<'_, Self>

Make the text underlined
Make the text blink
Make the text blink (but fast!)
Source§

fn reversed(&self) -> ReversedDisplay<'_, Self>

Swap the foreground and background colors
Source§

fn hidden(&self) -> HiddenDisplay<'_, Self>

Hide the text
Source§

fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>

Cross out the text
Source§

fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>
where Color: DynColor,

Set the foreground color at runtime. Only use if you do not know which color will be used at compile-time. If the color is constant, use either OwoColorize::fg or a color-specific method, such as OwoColorize::green, Read more
Source§

fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>
where Color: DynColor,

Set the background color at runtime. Only use if you do not know what color to use at compile-time. If the color is constant, use either OwoColorize::bg or a color-specific method, such as OwoColorize::on_yellow, Read more
Source§

fn fg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> FgColorDisplay<'_, CustomColor<R, G, B>, Self>

Set the foreground color to a specific RGB value.
Source§

fn bg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> BgColorDisplay<'_, CustomColor<R, G, B>, Self>

Set the background color to a specific RGB value.
Source§

fn truecolor(&self, r: u8, g: u8, b: u8) -> FgDynColorDisplay<'_, Rgb, Self>

Sets the foreground color to an RGB value.
Source§

fn on_truecolor(&self, r: u8, g: u8, b: u8) -> BgDynColorDisplay<'_, Rgb, Self>

Sets the background color to an RGB value.
Source§

fn style(&self, style: Style) -> Styled<&Self>

Apply a runtime-determined style
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.