From e1d7edb7c1845e6df6f5e23e3baf7bc88159eade Mon Sep 17 00:00:00 2001 From: Laurenz Date: Fri, 8 Apr 2022 14:48:02 +0200 Subject: Property resolving --- src/eval/value.rs | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) (limited to 'src/eval/value.rs') diff --git a/src/eval/value.rs b/src/eval/value.rs index 44df89e2..12948d72 100644 --- a/src/eval/value.rs +++ b/src/eval/value.rs @@ -590,6 +590,19 @@ impl Cast> for Spanned { } } +impl Cast for Option { + fn is(value: &Value) -> bool { + matches!(value, Value::None) || T::is(value) + } + + fn cast(value: Value) -> StrResult { + match value { + Value::None => Ok(None), + v => T::cast(v).map(Some).map_err(|msg| with_alternative(msg, "none")), + } + } +} + /// A value that can be automatically determined. #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] pub enum Smart { @@ -601,6 +614,17 @@ pub enum Smart { } impl Smart { + /// Map the contained custom value with `f`. + pub fn map(self, f: F) -> Smart + where + F: FnOnce(T) -> U, + { + match self { + Self::Auto => Smart::Auto, + Self::Custom(x) => Smart::Custom(f(x)), + } + } + /// Returns the contained custom value or a provided default value. pub fn unwrap_or(self, default: T) -> T { match self { @@ -627,19 +651,6 @@ impl Default for Smart { } } -impl Cast for Option { - fn is(value: &Value) -> bool { - matches!(value, Value::None) || T::is(value) - } - - fn cast(value: Value) -> StrResult { - match value { - Value::None => Ok(None), - v => T::cast(v).map(Some).map_err(|msg| with_alternative(msg, "none")), - } - } -} - impl Cast for Smart { fn is(value: &Value) -> bool { matches!(value, Value::Auto) || T::is(value) -- cgit v1.2.3