From b3e27d86a9e2dcfaecb9dfd6c25b111e0142a8b3 Mon Sep 17 00:00:00 2001 From: Emily Toop Date: Thu, 5 Apr 2018 11:08:20 +0100 Subject: [PATCH] Add converter functions from TypedValue to underlying type --- core/src/lib.rs | 72 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/core/src/lib.rs b/core/src/lib.rs index b70ec7e8..e87ccaf1 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -329,6 +329,78 @@ impl From for TypedValue { } } +impl TypedValue { + pub fn into_entid(self) -> Option { + match self { + TypedValue::Ref(v) => Some(v), + _ => None, + } + } + + pub fn into_kw(self) -> Option> { + match self { + TypedValue::Keyword(v) => Some(v), + _ => None, + } + } + + pub fn into_boolean(self) -> Option { + match self { + TypedValue::Boolean(v) => Some(v), + _ => None, + } + } + + pub fn into_long(self) -> Option { + match self { + TypedValue::Long(v) => Some(v), + _ => None, + } + } + + pub fn into_double(self) -> Option { + match self { + TypedValue::Double(v) => Some(v.into_inner()), + _ => None, + } + } + + pub fn into_instant(self) -> Option> { + match self { + TypedValue::Instant(v) => Some(v), + _ => None, + } + } + + pub fn into_timestamp(self) -> Option { + match self { + TypedValue::Instant(v) => Some(v.timestamp()), + _ => None, + } + } + + pub fn into_string(self) -> Option> { + match self { + TypedValue::String(v) => Some(v), + _ => None, + } + } + + pub fn into_uuid(self) -> Option { + match self { + TypedValue::Uuid(v) => Some(v), + _ => None, + } + } + + pub fn into_uuid_string(self) -> Option { + match self { + TypedValue::Uuid(v) => Some(v.hyphenated().to_string()), + _ => None, + } + } +} + /// Type safe representation of the possible return values from SQLite's `typeof` #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialOrd, PartialEq)] pub enum SQLTypeAffinity {