2019-03-15 15:40:38 +00:00
|
|
|
use error::Error;
|
|
|
|
|
2019-03-19 14:54:52 +00:00
|
|
|
mod display;
|
|
|
|
|
2019-03-20 14:18:52 +00:00
|
|
|
/// The type used to represent integers in `Value::Int`.
|
2019-03-15 15:40:38 +00:00
|
|
|
pub type IntType = i64;
|
2019-03-20 14:18:52 +00:00
|
|
|
|
|
|
|
/// The type used to represent floats in `Value::Float`.
|
2019-03-15 15:40:38 +00:00
|
|
|
pub type FloatType = f64;
|
|
|
|
|
2019-03-20 14:39:27 +00:00
|
|
|
/// The type used to represent tuples in `Value::Tuple`.
|
|
|
|
pub type TupleType = Vec<Value>;
|
|
|
|
|
2019-03-19 18:30:59 +00:00
|
|
|
/// The value type used by the parser.
|
|
|
|
/// Values can be of different subtypes that are the variants of this enum.
|
2019-03-15 15:11:31 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
|
|
pub enum Value {
|
2019-03-19 18:30:59 +00:00
|
|
|
/// A string value.
|
2019-03-15 15:11:31 +00:00
|
|
|
String(String),
|
2019-03-19 18:30:59 +00:00
|
|
|
/// A float value.
|
2019-03-15 15:40:38 +00:00
|
|
|
Float(FloatType),
|
2019-03-19 18:30:59 +00:00
|
|
|
/// An integer value.
|
2019-03-15 15:40:38 +00:00
|
|
|
Int(IntType),
|
2019-03-19 18:30:59 +00:00
|
|
|
/// A boolean value.
|
2019-03-15 15:11:31 +00:00
|
|
|
Boolean(bool),
|
2019-03-19 18:30:59 +00:00
|
|
|
/// A tuple value.
|
2019-03-20 14:39:27 +00:00
|
|
|
Tuple(TupleType),
|
2019-03-15 15:11:31 +00:00
|
|
|
}
|
2019-03-15 15:40:38 +00:00
|
|
|
|
|
|
|
impl Value {
|
2019-03-19 18:30:59 +00:00
|
|
|
/// Returns true if `self` is a `Value::Int`.
|
2019-03-15 15:40:38 +00:00
|
|
|
pub fn is_int(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
Value::Int(_) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-19 18:30:59 +00:00
|
|
|
/// Returns true if `self` is a `Value::Float`.
|
2019-03-15 15:40:38 +00:00
|
|
|
pub fn is_float(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
Value::Float(_) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-19 18:30:59 +00:00
|
|
|
/// Returns `self` as a `IntType`, or `Err` if `self` is not a `Value::Int`.
|
2019-03-15 15:40:38 +00:00
|
|
|
pub fn as_int(&self) -> Result<IntType, Error> {
|
|
|
|
match self {
|
|
|
|
Value::Int(i) => Ok(*i),
|
2019-03-20 09:42:17 +00:00
|
|
|
value => Err(Error::expected_int(value.clone())),
|
2019-03-15 15:40:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-19 18:30:59 +00:00
|
|
|
/// Returns `self` as a `FloatType`, or `Err` if `self` is not a `Value::Float` or `Value::Int`.
|
2019-03-15 15:40:38 +00:00
|
|
|
pub fn as_float(&self) -> Result<FloatType, Error> {
|
|
|
|
match self {
|
|
|
|
Value::Float(f) => Ok(*f),
|
|
|
|
Value::Int(i) => Ok(*i as FloatType),
|
2019-03-20 09:42:17 +00:00
|
|
|
value => Err(Error::expected_number(value.clone())),
|
2019-03-15 15:40:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-19 10:01:06 +00:00
|
|
|
|
|
|
|
impl From<String> for Value {
|
|
|
|
fn from(string: String) -> Self {
|
|
|
|
Value::String(string)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&str> for Value {
|
|
|
|
fn from(string: &str) -> Self {
|
|
|
|
Value::String(string.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<FloatType> for Value {
|
|
|
|
fn from(float: FloatType) -> Self {
|
|
|
|
Value::Float(float)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<IntType> for Value {
|
|
|
|
fn from(int: IntType) -> Self {
|
|
|
|
Value::Int(int)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<bool> for Value {
|
|
|
|
fn from(boolean: bool) -> Self {
|
|
|
|
Value::Boolean(boolean)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 14:39:27 +00:00
|
|
|
impl From<TupleType> for Value {
|
|
|
|
fn from(tuple: TupleType) -> Self {
|
2019-03-19 14:54:52 +00:00
|
|
|
Value::Tuple(tuple)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-19 10:01:06 +00:00
|
|
|
impl From<Value> for Result<Value, Error> {
|
|
|
|
fn from(value: Value) -> Self {
|
|
|
|
Ok(value)
|
|
|
|
}
|
2019-03-19 14:54:52 +00:00
|
|
|
}
|