Introduce TupleType type alias for representing tuples

This commit is contained in:
Sebastian Schmidt 2019-03-20 16:39:27 +02:00
parent 98ca788910
commit e337520805
4 changed files with 15 additions and 9 deletions

View File

@ -7,16 +7,17 @@
* Internal alias `IntType` and `FloatType` used by the `Value` enum are now public * Internal alias `IntType` and `FloatType` used by the `Value` enum are now public
* Error types for expecting each value type * Error types for expecting each value type
* Shortcut functions like `eval_int` to evaluate directly into a value type * Shortcut functions like `eval_int` to evaluate directly into a value type
* Type alias `TupleType` used to represent tuples was added
### Removed ### Removed
* Removed integration tests from shipped crate * Integration tests were removed from shipped crate
### Changed ### Changed
### Fixed ### Fixed
* Wording of some documentation items * Wording of some documentation items was changed to improve readability
### Deprecated ### Deprecated

View File

@ -7,6 +7,7 @@
use crate::value::Value; use crate::value::Value;
use token::PartialToken; use token::PartialToken;
use value::TupleType;
/// Errors used in this crate. /// Errors used in this crate.
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
@ -88,7 +89,7 @@ pub enum Error {
/// Only use this if there is no other error that describes the expected and provided types in more detail. /// Only use this if there is no other error that describes the expected and provided types in more detail.
TypeError { TypeError {
/// The expected types. /// The expected types.
expected: Vec<Value>, expected: TupleType,
/// The actual value. /// The actual value.
actual: Value, actual: Value,
}, },
@ -166,7 +167,7 @@ impl Error {
} }
/// Constructs `Error::TypeError{actual, expected}`. /// Constructs `Error::TypeError{actual, expected}`.
pub fn type_error(actual: Value, expected: Vec<Value>) -> Self { pub fn type_error(actual: Value, expected: TupleType) -> Self {
Error::TypeError { actual, expected } Error::TypeError { actual, expected }
} }

View File

@ -1,5 +1,6 @@
use token; use token;
use tree; use tree;
use value::TupleType;
use Configuration; use Configuration;
use EmptyConfiguration; use EmptyConfiguration;
use Error; use Error;
@ -120,7 +121,7 @@ pub fn eval_boolean(string: &str) -> Result<bool, Error> {
/// Evaluate the given expression string into a tuple. /// Evaluate the given expression string into a tuple.
/// ///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.* /// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
pub fn eval_tuple(string: &str) -> Result<Vec<Value>, Error> { pub fn eval_tuple(string: &str) -> Result<TupleType, Error> {
match eval(string) { match eval(string) {
Ok(Value::Tuple(tuple)) => Ok(tuple), Ok(Value::Tuple(tuple)) => Ok(tuple),
Ok(value) => Err(Error::expected_tuple(value)), Ok(value) => Err(Error::expected_tuple(value)),
@ -190,7 +191,7 @@ pub fn eval_boolean_with_configuration(
pub fn eval_tuple_with_configuration( pub fn eval_tuple_with_configuration(
string: &str, string: &str,
configuration: &Configuration, configuration: &Configuration,
) -> Result<Vec<Value>, Error> { ) -> Result<TupleType, Error> {
match eval_with_configuration(string, configuration) { match eval_with_configuration(string, configuration) {
Ok(Value::Tuple(tuple)) => Ok(tuple), Ok(Value::Tuple(tuple)) => Ok(tuple),
Ok(value) => Err(Error::expected_tuple(value)), Ok(value) => Err(Error::expected_tuple(value)),

View File

@ -8,6 +8,9 @@ pub type IntType = i64;
/// The type used to represent floats in `Value::Float`. /// The type used to represent floats in `Value::Float`.
pub type FloatType = f64; pub type FloatType = f64;
/// The type used to represent tuples in `Value::Tuple`.
pub type TupleType = Vec<Value>;
/// The value type used by the parser. /// The value type used by the parser.
/// Values can be of different subtypes that are the variants of this enum. /// Values can be of different subtypes that are the variants of this enum.
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
@ -21,7 +24,7 @@ pub enum Value {
/// A boolean value. /// A boolean value.
Boolean(bool), Boolean(bool),
/// A tuple value. /// A tuple value.
Tuple(Vec<Value>), Tuple(TupleType),
} }
impl Value { impl Value {
@ -89,8 +92,8 @@ impl From<bool> for Value {
} }
} }
impl From<Vec<Value>> for Value { impl From<TupleType> for Value {
fn from(tuple: Vec<Value>) -> Self { fn from(tuple: TupleType) -> Self {
Value::Tuple(tuple) Value::Tuple(tuple)
} }
} }