2019-03-15 15:40:38 +00:00
|
|
|
use error::Error;
|
|
|
|
|
2019-03-19 14:54:52 +00:00
|
|
|
mod display;
|
|
|
|
|
2019-03-15 15:40:38 +00:00
|
|
|
pub type IntType = i64;
|
|
|
|
pub type FloatType = f64;
|
|
|
|
|
2019-03-15 15:11:31 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
|
|
pub enum Value {
|
|
|
|
String(String),
|
2019-03-15 15:40:38 +00:00
|
|
|
Float(FloatType),
|
|
|
|
Int(IntType),
|
2019-03-15 15:11:31 +00:00
|
|
|
Boolean(bool),
|
2019-03-19 14:54:52 +00:00
|
|
|
Tuple(Vec<Value>),
|
2019-03-15 15:11:31 +00:00
|
|
|
}
|
2019-03-15 15:40:38 +00:00
|
|
|
|
|
|
|
impl Value {
|
|
|
|
pub fn is_int(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
Value::Int(_) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_float(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
Value::Float(_) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_int(&self) -> Result<IntType, Error> {
|
|
|
|
match self {
|
|
|
|
Value::Int(i) => Ok(*i),
|
|
|
|
_ => Err(Error::TypeError),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_float(&self) -> Result<FloatType, Error> {
|
|
|
|
match self {
|
|
|
|
Value::Float(f) => Ok(*f),
|
|
|
|
Value::Int(i) => Ok(*i as FloatType),
|
|
|
|
_ => Err(Error::TypeError),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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-19 14:54:52 +00:00
|
|
|
impl From<Vec<Value>> for Value {
|
|
|
|
fn from(tuple: Vec<Value>) -> Self {
|
|
|
|
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
|
|
|
}
|