From 021a7bd739ace9abce4426a5c7b29b930c271ad7 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sun, 14 Jun 2020 17:37:51 -0400 Subject: [PATCH] Implement the outstanding type casts for OwnedLiterals. --- src/renderer/parameters_context.rs | 60 ++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/src/renderer/parameters_context.rs b/src/renderer/parameters_context.rs index b5dd3be..8c8255d 100644 --- a/src/renderer/parameters_context.rs +++ b/src/renderer/parameters_context.rs @@ -226,22 +226,60 @@ impl Castable for OwnedLiteral { .map(|num| IceResult::from_owned(OwnedLiteral::LFloat(num))) }) .ok(), - (OwnedLiteral::LBoolean(_), "number") => todo!(), + (OwnedLiteral::LBoolean(boolean), "number") => { + if *boolean { + Some(IceResult::from_owned(OwnedLiteral::LPositiveInteger(1))) + } else { + Some(IceResult::from_owned(OwnedLiteral::LPositiveInteger(0))) + } + } (OwnedLiteral::LPositiveInteger(_), "number") => Some(IceResult::from_borrowed(self)), (OwnedLiteral::LNegativeInteger(_), "number") => Some(IceResult::from_borrowed(self)), (OwnedLiteral::LFloat(_), "number") => Some(IceResult::from_borrowed(self)), - (OwnedLiteral::LString(text), "string") => todo!(), - (OwnedLiteral::LBoolean(_), "string") => todo!(), - (OwnedLiteral::LPositiveInteger(_), "string") => todo!(), - (OwnedLiteral::LNegativeInteger(_), "string") => todo!(), - (OwnedLiteral::LFloat(_), "string") => todo!(), + (OwnedLiteral::LString(_), "string") => Some(IceResult::from_borrowed(self)), + (OwnedLiteral::LBoolean(boolean), "string") => Some(IceResult::from_owned( + OwnedLiteral::LString(boolean.to_string()), + )), + (OwnedLiteral::LPositiveInteger(num), "string") => Some(IceResult::from_owned( + OwnedLiteral::LString(num.to_string()), + )), + (OwnedLiteral::LNegativeInteger(num), "string") => Some(IceResult::from_owned( + OwnedLiteral::LString(num.to_string()), + )), + (OwnedLiteral::LFloat(num), "string") => Some(IceResult::from_owned( + OwnedLiteral::LString(num.to_string()), + )), - (OwnedLiteral::LString(text), "boolean") => todo!(), - (OwnedLiteral::LBoolean(_), "boolean") => todo!(), - (OwnedLiteral::LPositiveInteger(_), "boolean") => todo!(), - (OwnedLiteral::LNegativeInteger(_), "boolean") => todo!(), - (OwnedLiteral::LFloat(_), "boolean") => todo!(), + (OwnedLiteral::LString(text), "boolean") => { + if text.is_empty() { + Some(IceResult::from_owned(OwnedLiteral::LBoolean(false))) + } else { + Some(IceResult::from_owned(OwnedLiteral::LBoolean(true))) + } + } + (OwnedLiteral::LBoolean(_), "boolean") => Some(IceResult::from_borrowed(self)), + (OwnedLiteral::LPositiveInteger(num), "boolean") => { + if *num == 0 { + Some(IceResult::from_owned(OwnedLiteral::LBoolean(false))) + } else { + Some(IceResult::from_owned(OwnedLiteral::LBoolean(true))) + } + } + (OwnedLiteral::LNegativeInteger(num), "boolean") => { + if *num == 0 { + Some(IceResult::from_owned(OwnedLiteral::LBoolean(false))) + } else { + Some(IceResult::from_owned(OwnedLiteral::LBoolean(true))) + } + } + (OwnedLiteral::LFloat(num), "boolean") => { + if *num == 0.0 || num.is_nan() { + Some(IceResult::from_owned(OwnedLiteral::LBoolean(false))) + } else { + Some(IceResult::from_owned(OwnedLiteral::LBoolean(true))) + } + } (_, _) => panic!("Unimplemented cast"), } }