diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-07-10 20:42:28 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-07-10 23:14:46 +0200 |
| commit | 891e0c5fa6cd9200c24011c33b6f2115d84d4d74 (patch) | |
| tree | 3ea29dff87350d3d2020cdbb48b7acd01a33a1a2 /src/eval | |
| parent | 982ce85976913463eed6c95d3599868c5e1a79dd (diff) | |
Remove warnings from parsing and casting
Diffstat (limited to 'src/eval')
| -rw-r--r-- | src/eval/function.rs | 34 | ||||
| -rw-r--r-- | src/eval/mod.rs | 8 | ||||
| -rw-r--r-- | src/eval/value.rs | 63 |
3 files changed, 31 insertions, 74 deletions
diff --git a/src/eval/function.rs b/src/eval/function.rs index c986d71a..28a62873 100644 --- a/src/eval/function.rs +++ b/src/eval/function.rs @@ -2,7 +2,7 @@ use std::fmt::{self, Debug, Formatter}; use std::ops::Deref; use std::rc::Rc; -use super::{Cast, CastResult, EvalContext, Value}; +use super::{Cast, EvalContext, Value}; use crate::eco::EcoString; use crate::syntax::{Span, Spanned}; @@ -76,7 +76,7 @@ pub struct FuncArg { impl FuncArgs { /// Find and consume the first castable positional argument. - pub fn eat<T>(&mut self, ctx: &mut EvalContext) -> Option<T> + pub fn eat<T>(&mut self) -> Option<T> where T: Cast<Spanned<Value>>, { @@ -87,19 +87,12 @@ impl FuncArgs { } let value = std::mem::replace(&mut slot.value, Spanned::zero(Value::None)); - let span = value.span; - match T::cast(value) { - CastResult::Ok(t) => { - self.items.remove(index); - Some(t) - } - CastResult::Warn(t, m) => { + Ok(t) => { self.items.remove(index); - ctx.diag(warning!(span, "{}", m)); Some(t) } - CastResult::Err(value) => { + Err(value) => { slot.value = value; None } @@ -113,7 +106,7 @@ impl FuncArgs { where T: Cast<Spanned<Value>>, { - let found = self.eat(ctx); + let found = self.eat(); if found.is_none() { ctx.diag(error!(self.span, "missing argument: {}", what)); } @@ -121,16 +114,11 @@ impl FuncArgs { } /// Find, consume and collect all castable positional arguments. - /// - /// This function returns a vector instead of an iterator because the - /// iterator would require unique access to the context, rendering it rather - /// unusable. If you need to process arguments one-by-one, you probably want - /// to use a while-let loop together with [`eat()`](Self::eat). - pub fn all<T>(&mut self, ctx: &mut EvalContext) -> Vec<T> + pub fn all<T>(&mut self) -> impl Iterator<Item = T> + '_ where T: Cast<Spanned<Value>>, { - std::iter::from_fn(|| self.eat(ctx)).collect() + std::iter::from_fn(move || self.eat()) } /// Cast and remove the value for the given named argument, producing an @@ -148,12 +136,8 @@ impl FuncArgs { let span = value.span; match T::cast(value) { - CastResult::Ok(t) => Some(t), - CastResult::Warn(t, m) => { - ctx.diag(warning!(span, "{}", m)); - Some(t) - } - CastResult::Err(value) => { + Ok(t) => Some(t), + Err(value) => { ctx.diag(error!( span, "expected {}, found {}", diff --git a/src/eval/mod.rs b/src/eval/mod.rs index 689234bd..fd4417ec 100644 --- a/src/eval/mod.rs +++ b/src/eval/mod.rs @@ -195,12 +195,8 @@ impl<'a> EvalContext<'a> { } match T::cast(value) { - CastResult::Ok(t) => Some(t), - CastResult::Warn(t, m) => { - self.diag(warning!(span, "{}", m)); - Some(t) - } - CastResult::Err(value) => { + Ok(t) => Some(t), + Err(value) => { self.diag(error!( span, "expected {}, found {}", diff --git a/src/eval/value.rs b/src/eval/value.rs index 7c35fdbd..8e3a1c61 100644 --- a/src/eval/value.rs +++ b/src/eval/value.rs @@ -84,7 +84,7 @@ impl Value { } /// Try to cast the value into a specific type. - pub fn cast<T>(self) -> CastResult<T, Self> + pub fn cast<T>(self) -> Result<T, Self> where T: Cast<Value>, { @@ -236,28 +236,7 @@ where /// Cast from a value to a specific type. pub trait Cast<V>: Type + Sized { /// Try to cast the value into an instance of `Self`. - fn cast(value: V) -> CastResult<Self, V>; -} - -/// The result of casting a value to a specific type. -#[derive(Debug, Clone, Eq, PartialEq)] -pub enum CastResult<T, V> { - /// The value was cast successfully. - Ok(T), - /// The value was cast successfully, but with a warning message. - Warn(T, String), - /// The value could not be cast into the specified type. - Err(V), -} - -impl<T, V> CastResult<T, V> { - /// Access the conversion result, discarding a possibly existing warning. - pub fn ok(self) -> Option<T> { - match self { - CastResult::Ok(t) | CastResult::Warn(t, _) => Some(t), - CastResult::Err(_) => None, - } - } + fn cast(value: V) -> Result<Self, V>; } impl Type for Value { @@ -265,8 +244,8 @@ impl Type for Value { } impl Cast<Value> for Value { - fn cast(value: Value) -> CastResult<Self, Value> { - CastResult::Ok(value) + fn cast(value: Value) -> Result<Self, Value> { + Ok(value) } } @@ -274,12 +253,11 @@ impl<T> Cast<Spanned<Value>> for T where T: Cast<Value>, { - fn cast(value: Spanned<Value>) -> CastResult<Self, Spanned<Value>> { + fn cast(value: Spanned<Value>) -> Result<Self, Spanned<Value>> { let span = value.span; match T::cast(value.v) { - CastResult::Ok(t) => CastResult::Ok(t), - CastResult::Warn(t, m) => CastResult::Warn(t, m), - CastResult::Err(v) => CastResult::Err(Spanned::new(v, span)), + Ok(t) => Ok(t), + Err(v) => Err(Spanned::new(v, span)), } } } @@ -288,12 +266,11 @@ impl<T> Cast<Spanned<Value>> for Spanned<T> where T: Cast<Value>, { - fn cast(value: Spanned<Value>) -> CastResult<Self, Spanned<Value>> { + fn cast(value: Spanned<Value>) -> Result<Self, Spanned<Value>> { let span = value.span; match T::cast(value.v) { - CastResult::Ok(t) => CastResult::Ok(Spanned::new(t, span)), - CastResult::Warn(t, m) => CastResult::Warn(Spanned::new(t, span), m), - CastResult::Err(v) => CastResult::Err(Spanned::new(v, span)), + Ok(t) => Ok(Spanned::new(t, span)), + Err(v) => Err(Spanned::new(v, span)), } } } @@ -315,11 +292,11 @@ macro_rules! primitive { } impl Cast<Value> for $type { - fn cast(value: Value) -> CastResult<Self, Value> { + fn cast(value: Value) -> Result<Self, Value> { match value { - $variant(v) => CastResult::Ok(v), - $($pattern => CastResult::Ok($out),)* - v => CastResult::Err(v), + $variant(v) => Ok(v), + $($pattern => Ok($out),)* + v => Err(v), } } } @@ -426,26 +403,26 @@ macro_rules! castable { impl $crate::eval::Cast<$crate::eval::Value> for $type { fn cast( value: $crate::eval::Value, - ) -> $crate::eval::CastResult<Self, $crate::eval::Value> { + ) -> Result<Self, $crate::eval::Value> { use $crate::eval::*; #[allow(unreachable_code)] match value { - $($pattern => CastResult::Ok($out),)* + $($pattern => Ok($out),)* Value::Any(mut any) => { any = match any.downcast::<Self>() { - Ok(t) => return CastResult::Ok(t), + Ok(t) => return Ok(t), Err(any) => any, }; $(any = match any.downcast::<$anytype>() { - Ok($anyvar) => return CastResult::Ok($anyout), + Ok($anyvar) => return Ok($anyout), Err(any) => any, };)* - CastResult::Err(Value::Any(any)) + Err(Value::Any(any)) }, - v => CastResult::Err(v), + v => Err(v), } } } |
