Skip to content

Commit

Permalink
variable: Add DummyVariable to make unvariable expression easy to make
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerollmops committed Jan 7, 2017
1 parent 90cc812 commit f80b3fe
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 46 deletions.
3 changes: 2 additions & 1 deletion examples/custom_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use ripin::{Stack, pop_two_operands};
use ripin::convert_ref::TryFromRef;
use ripin::expression::Expression;
use ripin::evaluate::Evaluate;
use ripin::variable::DummyVariable;

// Implementing Expression for a new specific type
// is not a difficult thing to do:
Expand Down Expand Up @@ -112,7 +113,7 @@ impl Evaluate<MyOperand> for MyEvaluator<MyOperand> {
}
}

type MyExpression = Expression<MyOperand, (), MyEvaluator<MyOperand>>;
type MyExpression = Expression<MyOperand, DummyVariable, MyEvaluator<MyOperand>>;

// Once you implement the TryFromRef trait on your “custom” types,
// make an iterator of it and give it to the Expression struct.
Expand Down
21 changes: 11 additions & 10 deletions src/evaluate/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,13 +353,13 @@ mod tests {
assert_eq!(expr.evaluate(), Ok(3.0));
}

// #[test]
// fn to_string() {
// let expr_str = "3.3 3 + round neg 4 +";
// let tokens = expr_str.split_whitespace();
// let expr = FloatExpression::<f32>::from_iter(tokens).unwrap();
// assert_eq!(&expr.to_string(), expr_str);
// }
#[test]
fn to_string() {
let expr_str = "3.3 3 + round neg 4 +";
let tokens = expr_str.split_whitespace();
let expr = FloatExpr::<f32>::from_iter(tokens).unwrap();
assert_eq!(&expr.to_string(), expr_str);
}

use std::ops::Index;
use std::str::FromStr;
Expand All @@ -380,9 +380,10 @@ mod tests {
fn try_from_ref(s: &&'a str) -> Result<Self, Self::Err> {
match s.chars().next() {
Some('$') => {
FromStr::from_str(&s[1..])
.map(|n| VarIdx(n))
.map_err(|err| VarIdxErr::ConvertErr(err))
match FromStr::from_str(&s[1..]) {
Ok(n) => Ok(VarIdx(n)),
Err(err) => Err(VarIdxErr::ConvertErr(err)),
}
},
_ => Err(VarIdxErr::InvalidVariableName(s)),
}
Expand Down
14 changes: 7 additions & 7 deletions src/evaluate/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,11 +346,11 @@ mod tests {
assert_eq!(expr.evaluate(), Ok(1));
}

// #[test]
// fn to_string() {
// let expr_str = "3 3 + neg neg 4 +";
// let tokens = expr_str.split_whitespace();
// let expr = IntExpression::<i32>::from_iter(tokens).unwrap();
// assert_eq!(&expr.to_string(), expr_str);
// }
#[test]
fn to_string() {
let expr_str = "3 3 + neg neg 4 +";
let tokens = expr_str.split_whitespace();
let expr = IntExpr::<i32>::from_iter(tokens).unwrap();
assert_eq!(&expr.to_string(), expr_str);
}
}
1 change: 1 addition & 0 deletions src/evaluate/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use stack::Stack;
use expression::Expression;
use variable::DummyVariable;

mod float;
mod integer;
Expand Down
28 changes: 0 additions & 28 deletions src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,6 @@ impl<T, V, E: Evaluate<T>> Expression<T, V, E> {
}
}

impl<T> TryFromRef<T> for () {
type Err = ();

fn try_from_ref(_: &T) -> Result<Self, Self::Err> {
Err(())
}
}

/// Used to specify the error during the conversion.
#[derive(Debug, PartialEq)]
pub enum ExprResult<A, B, C> {
Expand Down Expand Up @@ -187,23 +179,3 @@ impl<T, V, E> fmt::Display for Expression<T, V, E>
Ok(())
}
}

// impl<T, V, E> fmt::Display for Expression<T, V, E>
// where T: fmt::Display,
// E: fmt::Display + Evaluate<T>
// {
// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// let len = self.expr.len();
// for (i, arithm) in self.expr.iter().enumerate() {
// match *arithm {
// Arithm::Operand(ref operand) => operand.fmt(f)?,
// Arithm::Variable(_) => panic!("Cannot display variable!"),
// Arithm::Evaluator(ref evaluator) => evaluator.fmt(f)?,
// }
// if i != len - 1 {
// f.write_str(" ")?
// }
// }
// Ok(())
// }
// }
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ pub mod convert_ref;
/// Operation on expressions and `Expression` construction methods.
pub mod expression;

pub mod variable;

/// `Evaluate Trait` and default `Evaluators`.
pub mod evaluate;

Expand Down
26 changes: 26 additions & 0 deletions src/variable/dummy_variable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use std::fmt;
use convert_ref::TryFromRef;

/// Struct that implement [`TryFromRef`] and always returns `Err`,
/// used to fake variables when don't needed in expressions.
///
/// Prefer using this type with the [`DummyVariables`] fake container.
///
/// [`TryFromRef`]: ../convert_ref/trait.TryFromRef.html
/// [`DummyVariables`]: ../variable/struct.DummyVariables.html
#[derive(Debug, Copy, Clone)]
pub struct DummyVariable;

impl<T> TryFromRef<T> for DummyVariable {
type Err = ();

fn try_from_ref(_: &T) -> Result<Self, Self::Err> {
Err(())
}
}

impl fmt::Display for DummyVariable {
fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
Err(fmt::Error)
}
}
24 changes: 24 additions & 0 deletions src/variable/dummy_variables.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use std::ops::Index;
use std::marker::PhantomData;

/// Struct that implement [`Index`],
/// used to fake variables when don't needed in expressions.
///
/// Prefer using this container with the [`DummyVariable`] fake type.
///
/// # Panics
/// Panics if you call the [`index()`] method.
///
/// [`Index`]: https://doc.rust-lang.org/std/ops/trait.Index.html
/// [`DummyVariable`]: ../variable/struct.DummyVariable.html
/// [`index()`]: https://doc.rust-lang.org/std/ops/trait.Index.html#tymethod.index
#[derive(Debug, Copy, Clone)]
pub struct DummyVariables<T>(PhantomData<T>);

impl<I, T> Index<I> for DummyVariables<T> {
type Output = T;

fn index(&self, _: I) -> &Self::Output {
panic!("DummyVariables cannot return variable!")
}
}
5 changes: 5 additions & 0 deletions src/variable/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod dummy_variables;
mod dummy_variable;

pub use self::dummy_variables::DummyVariables;
pub use self::dummy_variable::DummyVariable;

0 comments on commit f80b3fe

Please sign in to comment.