Document value type

Implements #6 partially
This commit is contained in:
Sebastian Schmidt 2019-03-19 20:30:59 +02:00
parent 6fe5a8cdef
commit cdcd24e7b6

View File

@ -5,16 +5,24 @@ mod display;
pub type IntType = i64;
pub type FloatType = f64;
/// The value type used by the parser.
/// Values can be of different subtypes that are the variants of this enum.
#[derive(Clone, Debug, PartialEq)]
pub enum Value {
/// A string value.
String(String),
/// A float value.
Float(FloatType),
/// An integer value.
Int(IntType),
/// A boolean value.
Boolean(bool),
/// A tuple value.
Tuple(Vec<Value>),
}
impl Value {
/// Returns true if `self` is a `Value::Int`.
pub fn is_int(&self) -> bool {
match self {
Value::Int(_) => true,
@ -22,6 +30,7 @@ impl Value {
}
}
/// Returns true if `self` is a `Value::Float`.
pub fn is_float(&self) -> bool {
match self {
Value::Float(_) => true,
@ -29,6 +38,7 @@ impl Value {
}
}
/// Returns `self` as a `IntType`, or `Err` if `self` is not a `Value::Int`.
pub fn as_int(&self) -> Result<IntType, Error> {
match self {
Value::Int(i) => Ok(*i),
@ -36,6 +46,7 @@ impl Value {
}
}
/// Returns `self` as a `FloatType`, or `Err` if `self` is not a `Value::Float` or `Value::Int`.
pub fn as_float(&self) -> Result<FloatType, Error> {
match self {
Value::Float(f) => Ok(*f),