Skip to content

Commit

Permalink
Merge pull request #25 from vic1707/fix(typos)-&-update-deps
Browse files Browse the repository at this point in the history
Fix(typos) & update deps
  • Loading branch information
vic1707 committed Mar 9, 2024
2 parents ec3f290 + 3795180 commit 701d05c
Show file tree
Hide file tree
Showing 12 changed files with 186 additions and 163 deletions.
247 changes: 133 additions & 114 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "xprs"
version = "0.0.2"
version = "0.1.0"
edition = "2021"
authors = ["Victor LEFEBVRE <contact@vic1707.xyz>"]
description = "Xprs is a flexible and extensible mathematical expression parser and evaluator for Rust, designed for simplicity and ease of use."
Expand All @@ -24,7 +24,7 @@ rust-version = "1.70.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
miette = { version = "5.10.0", features = ["fancy-no-backtrace"] }
miette = { version = "7.2.0", features = ["fancy-no-backtrace"] }
thiserror = "1.0.56"

[build-dependencies]
Expand Down
File renamed without changes.
16 changes: 8 additions & 8 deletions README.md
Expand Up @@ -14,7 +14,7 @@ Add this to your `Cargo.toml`:

```toml
[dependencies]
xprs = "0.0.2"
xprs = "0.1.0"
```

or run this command in your terminal:
Expand Down Expand Up @@ -51,7 +51,7 @@ Currently, the minimum supported Rust version is `1.70.0`.
- `6/2(2+1)` gets interpreted as `6/2*(2+1)` which gives `9` as a result.
- `1/2x` gets interpreted as `(1/2)*x` which, with `x` being `2`, gives `1` as a result.

Note: `Display` and `Debug` shows additionnal parenthesis to make the order of operations more obvious.
Note: `Display` and `Debug` shows additional parenthesis to make the order of operations more obvious.

<br />

Expand All @@ -65,7 +65,7 @@ Currently, the minimum supported Rust version is `1.70.0`.
- `6/2(2+1)` gets interpreted as `6/(2*(2+1))` which gives `1` as a result.
- `1/2x` gets interpreted as `1/(2*x)` which, with `x` being `2`, gives `0.25` as a result.

Note: `Display` and `Debug` shows additionnal parenthesis to make the order of operations more obvious.
Note: `Display` and `Debug` shows additional parenthesis to make the order of operations more obvious.

## Usage

Expand Down Expand Up @@ -114,8 +114,8 @@ You can use functions `bind`, `bind2` etc up to `bind9` to bind variables to the
If you ever need more, you can use the `bind_n` and `bind_n_runtime` methods which takes an array of size N or a slice respectively.

Notes:
All `bind` function (except `bind_n_runtime`) returns a [`Result`] of a function which is guarenteed to return a [`f64`].
`bind_n_runtime` returns a [`Result`] of a function which also returns a [`Result`] of a [`f64`] since there are no guarentees that the array/slice will be of the correct size.
All `bind` function (except `bind_n_runtime`) returns a [`Result`] of a function which is guaranteed to return a [`f64`].
`bind_n_runtime` returns a [`Result`] of a function which also returns a [`Result`] of a [`f64`] since there are no guarantees that the array/slice will be of the correct size.

### Context and Parser

Expand Down Expand Up @@ -246,7 +246,7 @@ fn main() {

println!("{xprs}"); // (w + (sin((x + (2 * y))) * (3 * z)))

xprs.simplify_for_inplace(("z", 4.0));
xprs.simplify_for_in_place(("z", 4.0));

println!("{xprs}"); // (w + (sin((x + (2 * y))) * 12))

Expand Down Expand Up @@ -282,12 +282,12 @@ These examples and others can be found in the [examples](./examples) directory.

Complete documentation can be found on [docs.rs](https://docs.rs/xprs).

## Licence
## License

Copyright © 2023 [Victor LEFEBVRE](contact@vic1707.xyz)
This work is free. You can redistribute it and/or modify it under the
terms of the Do What The Fuck You Want To Public License, Version 2,
as published by Sam Hocevar. See the [LICENCE](./LICENCE). file for more details.
as published by Sam Hocevar. See the [LICENSE](./LICENSE). file for more details.

## TODOs

Expand Down
2 changes: 1 addition & 1 deletion examples/xprs_simplification.rs
Expand Up @@ -12,7 +12,7 @@ fn main() -> Result<(), Box<dyn Error>> {

println!("{xprs}"); // (w + (sin((x + (2 * y))) * (3 * z)))

xprs.simplify_for_inplace(("z", 4.0));
xprs.simplify_for_in_place(("z", 4.0));

println!("{xprs}"); // (w + (sin((x + (2 * y))) * 12))

Expand Down
18 changes: 9 additions & 9 deletions src/lib.rs
Expand Up @@ -2,7 +2,7 @@
//!
//! [<img alt="github" src="https://img.shields.io/badge/github-vic1707/xprs-8da0cb?style=for-the-badge&labelColor=555555&logo=github" height="20">](https://github.com/vic1707/xprs)
//! [<img alt="crates.io" src="https://img.shields.io/crates/v/xprs.svg?style=for-the-badge&color=fc8d62&logo=rust" height="20">](https://crates.io/crates/xprs)
//! [<img alt="build status" src="https://img.shields.io/github/actions/workflow/status/vic1707/xprs/tests.yml?branch=main&style=for-the-badge" height="20">](https://github.com/vic1707/xprs/actions?query=branch%3Amain)
//! [<img alt="build status" src="https://img.shields.io/github/actions/workflow/status/vic1707/xprs/global_matrix.yml?branch=main&style=for-the-badge" height="20">](https://github.com/vic1707/xprs/actions?query=branch%3Amain)
//! [<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-xprs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs" height="20">](https://docs.rs/xprs)
//! [<img alt="downloads" src="https://img.shields.io/crates/d/xprs.svg?style=for-the-badge&logo=docs.rs" height="20">](https://crates.io/crates/xprs)
//!
Expand All @@ -14,7 +14,7 @@
//!
//! ```toml
//! [dependencies]
//! xprs = "0.0.2"
//! xprs = "0.1.0"
//! ```
//!
//! or run this command in your terminal:
Expand Down Expand Up @@ -51,7 +51,7 @@
//! - `6/2(2+1)` gets interpreted as `6/2*(2+1)` which gives `9` as a result.
//! - `1/2x` gets interpreted as `(1/2)*x` which, with `x` being `2`, gives `1` as a result.
//!
//! Note: `Display` and `Debug` shows additionnal parenthesis to make the order of operations more obvious.
//! Note: `Display` and `Debug` shows additional parenthesis to make the order of operations more obvious.
//!
//! <br />
//!
Expand All @@ -65,7 +65,7 @@
//! - `6/2(2+1)` gets interpreted as `6/(2*(2+1))` which gives `1` as a result.
//! - `1/2x` gets interpreted as `1/(2*x)` which, with `x` being `2`, gives `0.25` as a result.
//!
//! Note: `Display` and `Debug` shows additionnal parenthesis to make the order of operations more obvious.
//! Note: `Display` and `Debug` shows additional parenthesis to make the order of operations more obvious.
//!
//! ## Usage
//!
Expand Down Expand Up @@ -114,8 +114,8 @@
//! If you ever need more, you can use the `bind_n` and `bind_n_runtime` methods which takes an array of size N or a slice respectively.
//!
//! Notes:
//! All `bind` function (except `bind_n_runtime`) returns a [`Result`] of a function which is guarenteed to return a [`f64`].
//! `bind_n_runtime` returns a [`Result`] of a function which also returns a [`Result`] of a [`f64`] since there are no guarentees that the array/slice will be of the correct size.
//! All `bind` function (except `bind_n_runtime`) returns a [`Result`] of a function which is guaranteed to return a [`f64`].
//! `bind_n_runtime` returns a [`Result`] of a function which also returns a [`Result`] of a [`f64`] since there are no guarantees that the array/slice will be of the correct size.
//!
//! ### Context and Parser
//!
Expand Down Expand Up @@ -246,7 +246,7 @@
//!
//! println!("{xprs}"); // (w + (sin((x + (2 * y))) * (3 * z)))
//!
//! xprs.simplify_for_inplace(("z", 4.0));
//! xprs.simplify_for_in_place(("z", 4.0));
//!
//! println!("{xprs}"); // (w + (sin((x + (2 * y))) * 12))
//!
Expand Down Expand Up @@ -282,12 +282,12 @@
//!
//! Complete documentation can be found on [docs.rs](https://docs.rs/xprs).
//!
//! ## Licence
//! ## License
//!
//! Copyright © 2023 [Victor LEFEBVRE](contact@vic1707.xyz)
//! This work is free. You can redistribute it and/or modify it under the
//! terms of the Do What The Fuck You Want To Public License, Version 2,
//! as published by Sam Hocevar. See the [LICENCE](./LICENCE). file for more details.
//! as published by Sam Hocevar. See the [LICENSE](./LICENSE). file for more details.
//!
//! ## TODOs
//!
Expand Down
11 changes: 7 additions & 4 deletions src/parser.rs
Expand Up @@ -477,10 +477,10 @@ impl miette::Diagnostic for ParseError {
ErrorKind::ExpectedToken(tok) => {
format!("Try adding a `{tok}` here.")
},
ErrorKind::VariableNotDeclared(_, ref availables) => {
ErrorKind::VariableNotDeclared(_, ref available_vars) => {
format!(
"Try replacing it with one of the following: `{}`.",
availables.join("`, `")
available_vars.join("`, `")
)
},
ErrorKind::TooManyArguments(expected, got) => {
Expand Down Expand Up @@ -617,10 +617,13 @@ impl ParseError {
fn new_variable_not_declared(
input: &str,
var: &str,
availables: Vec<String>,
available_vars: Vec<String>,
) -> Self {
Self {
kind: ErrorKind::VariableNotDeclared(var.to_owned(), availables),
kind: ErrorKind::VariableNotDeclared(
var.to_owned(),
available_vars,
),
span: (0, input.len()).into(),
src: input.to_owned(),
}
Expand Down
2 changes: 0 additions & 2 deletions src/tests/xprs/eval.rs
Expand Up @@ -4,8 +4,6 @@ use std::collections::HashMap;
use super::super::macros::assert_f64_eq;
use crate::Parser;

// shitty type because of clippy and default numeric fallback
// https://github.com/rust-lang/rust-clippy/issues/11535
type InputVarsResult = (&'static str, &'static [(&'static str, f64)], f64);
/// 2(3)
/// 2 + 3 ^ 2 * 3 + 4
Expand Down
4 changes: 2 additions & 2 deletions src/tests/xprs/simplify.rs
Expand Up @@ -14,9 +14,9 @@ fn test_simplify() {
xprs = xprs.simplify_for(X_VAR);
assert_eq!(xprs, parser.parse("4 + 3y + 8 + 5z").unwrap());
// simplify for y
xprs.simplify_for_inplace(Y_VAR);
xprs.simplify_for_in_place(Y_VAR);
assert_eq!(xprs, parser.parse("21 + 5z").unwrap());
// try simplifying for an unknown variable
xprs.simplify_for_inplace(UNKNOWN_VAR);
xprs.simplify_for_in_place(UNKNOWN_VAR);
assert_eq!(xprs, parser.parse("21 + 5z").unwrap());
}
22 changes: 11 additions & 11 deletions src/token/function.rs
Expand Up @@ -3,7 +3,7 @@ extern crate alloc;
use alloc::sync::Arc;
use core::{cmp::Ordering, fmt, ops::Deref};

/// Represents a mathematical function core informations.
/// Represents a mathematical function core infos.
#[derive(Debug, PartialEq, Clone)]
#[non_exhaustive]
pub struct Function {
Expand Down Expand Up @@ -162,44 +162,44 @@ macro_rules! xprs_fn {
)
};

//// closure wraping ////
//// closure wrapping ////
(wrap $function:expr, 0) => {
move |_| $function()
};
(wrap $function:expr, 1) => {
#[allow(clippy::indexing_slicing)]
#[allow(clippy::indexing_slicing, clippy::missing_asserts_for_indexing)]
move |args| $function(args[0])
};
(wrap $function:expr, 2) => {
#[allow(clippy::indexing_slicing)]
#[allow(clippy::indexing_slicing, clippy::missing_asserts_for_indexing)]
move |args| $function(args[0], args[1])
};
(wrap $function:expr, 3) => {
#[allow(clippy::indexing_slicing)]
#[allow(clippy::indexing_slicing, clippy::missing_asserts_for_indexing)]
move |args| $function(args[0], args[1], args[2])
};
(wrap $function:expr, 4) => {
#[allow(clippy::indexing_slicing)]
#[allow(clippy::indexing_slicing, clippy::missing_asserts_for_indexing)]
move |args| $function(args[0], args[1], args[2], args[3])
};
(wrap $function:expr, 5) => {
#[allow(clippy::indexing_slicing)]
#[allow(clippy::indexing_slicing, clippy::missing_asserts_for_indexing)]
move |args| $function(args[0], args[1], args[2], args[3], args[4])
};
(wrap $function:expr, 6) => {
#[allow(clippy::indexing_slicing)]
#[allow(clippy::indexing_slicing, clippy::missing_asserts_for_indexing)]
move |args| $function(args[0], args[1], args[2], args[3], args[4], args[5])
};
(wrap $function:expr, 7) => {
#[allow(clippy::indexing_slicing)]
#[allow(clippy::indexing_slicing, clippy::missing_asserts_for_indexing)]
move |args| $function(args[0], args[1], args[2], args[3], args[4], args[5], args[6])
};
(wrap $function:expr, 8) => {
#[allow(clippy::indexing_slicing)]
#[allow(clippy::indexing_slicing, clippy::missing_asserts_for_indexing)]
move |args| $function(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7])
};
(wrap $function:expr, 9) => {
#[allow(clippy::indexing_slicing)]
#[allow(clippy::indexing_slicing, clippy::missing_asserts_for_indexing)]
move |args| $function(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8])
};
}
5 changes: 4 additions & 1 deletion src/token/mod.rs
Expand Up @@ -6,6 +6,9 @@ mod identifier;
/// The operator module.
mod operator;
/* Exports */
pub use function::{FnPointer, Function};
pub use function::Function;
pub use identifier::Identifier;
pub use operator::Operator;

#[cfg(test)]
pub use function::FnPointer;
18 changes: 9 additions & 9 deletions src/xprs.rs
Expand Up @@ -133,14 +133,14 @@ impl Xprs<'_> {
/// assert_eq!(format!("{xprs}"), "((2 * x) + y)");
/// assert_eq!(xprs.vars, ["x", "y"].into());
///
/// xprs.simplify_for_inplace(("x", 3.0));
/// xprs.simplify_for_in_place(("x", 3.0));
///
/// assert_eq!(format!("{xprs}"), "(6 + y)");
/// assert_eq!(xprs.vars, ["y"].into());
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn simplify_for_inplace(&mut self, var: (&str, f64)) {
pub fn simplify_for_in_place(&mut self, var: (&str, f64)) {
let mut tmp = trust_me!(ptr::read(&self.root));
tmp = tmp.simplify_for(var);
trust_me!(ptr::write(&mut self.root, tmp););
Expand Down Expand Up @@ -192,7 +192,7 @@ impl Xprs<'_> {
#[inline]
#[must_use]
pub fn simplify_for(mut self, var: (&str, f64)) -> Self {
self.simplify_for_inplace(var);
self.simplify_for_in_place(var);
self
}

Expand All @@ -209,15 +209,15 @@ impl Xprs<'_> {
/// assert_eq!(format!("{xprs}"), "(((2 * x) + y) + (4 * z))");
/// assert_eq!(xprs.vars, ["x", "y", "z"].into());
///
/// xprs.simplify_for_multiple_inplace(&[("x", 3.0), ("z", 2.0)]);
/// 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());
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn simplify_for_multiple_inplace(&mut self, vars: &[(&str, f64)]) {
// rewriting `simplify_for_inplace` to avoid dozens of `ptr::read` and `ptr::write`
pub fn simplify_for_multiple_in_place(&mut self, vars: &[(&str, f64)]) {
// rewriting `simplify_for_in_place` to avoid dozens of `ptr::read` and `ptr::write`
let mut tmp = trust_me!(ptr::read(&self.root));
for &var in vars {
tmp = tmp.simplify_for(var);
Expand Down Expand Up @@ -248,7 +248,7 @@ impl Xprs<'_> {
#[inline]
#[must_use]
pub fn simplify_for_multiple(mut self, vars: &[(&str, f64)]) -> Self {
self.simplify_for_multiple_inplace(vars);
self.simplify_for_multiple_in_place(vars);
self
}
}
Expand Down Expand Up @@ -687,7 +687,7 @@ impl<'a> Xprs<'a> {
if let Some(bind_error) = BindError::from_diff(missing_vars) {
yeet!(bind_error);
}
Ok(move |vals| self.eval_unchecked(&vars.into_iter().zip(vals).collect()))
Ok(move |values| self.eval_unchecked(&vars.into_iter().zip(values).collect()))
}

/// Creates a function of any number of [`f64`] based on this [`Xprs`] instance.
Expand Down Expand Up @@ -724,7 +724,7 @@ impl<'a> Xprs<'a> {
// can't drop the closure from returning a result because we can't use the unchecked version
// because we don't know the length of the slice at compile time
// it could be different from the length of the slice of variables names
Ok(move |vals: &[f64]| self.eval(&vars.iter().copied().zip(vals.iter().copied()).collect()))
Ok(move |values: &[f64]| self.eval(&vars.iter().copied().zip(values.iter().copied()).collect()))
}
}

Expand Down

0 comments on commit 701d05c

Please sign in to comment.