Trait wagon_value::Valueable
source · pub trait Valueable: Debug + Display + Clone + Eq + PartialEq + PartialOrd + Hash + Add + Sub + Mul + Div + Rem + Pow<Self> {
// Required methods
fn is_truthy(&self) -> ValueResult<bool, Self>;
fn to_int(&self) -> ValueResult<i32, Self>;
fn to_float(&self) -> ValueResult<f32, Self>;
fn display_numerical(&self) -> ValueResult<String, Self>;
}
Expand description
A trait to allow “extension” of the Value
enum.
Sometimes, the basic types supported by Value
are not enough and the newtype pattern is required to extend it.
Registering this newtype as Valueable
means that it supports all common operations associated with a Value
.
Besides implementing the required methods. Any type that implements Valueable
is required to implement the following traits:
std::fmt::Debug
std::fmt::Display
Clone
Eq
PartialEq
PartialOrd
std::hash::Hash
std::ops::Add
std::ops::Sub
std::ops::Mul
std::ops::Div
std::ops::Rem
num_traits::Pow<Self>
This is required such that we can expect anyValueable
type to work more or less the same.
Most of the required traits can be automatically derived. You can use wagon_macros::ValueOps
(or it’s associated methods) to
automatically derive implementations for all the operative traits (Add
, Sub
, etc).
Required Methods§
sourcefn is_truthy(&self) -> ValueResult<bool, Self>
fn is_truthy(&self) -> ValueResult<bool, Self>
Is this value seen as true
or false
?
§Errors
Should return an error if this value can not be converted into a bool
.
sourcefn to_int(&self) -> ValueResult<i32, Self>
fn to_int(&self) -> ValueResult<i32, Self>
sourcefn to_float(&self) -> ValueResult<f32, Self>
fn to_float(&self) -> ValueResult<f32, Self>
sourcefn display_numerical(&self) -> ValueResult<String, Self>
fn display_numerical(&self) -> ValueResult<String, Self>
Get a string representation of the value, as if it were a number.
§Errors
Should return an error if this value can not be displayed as a number