From 460fdac25265ad851f07e7167b0e4f2751c19353 Mon Sep 17 00:00:00 2001 From: Richard Newman Date: Wed, 29 Mar 2017 14:11:00 -0700 Subject: [PATCH] Pre: add Variable::from_valid_name, TypedValue::{typed_string,typed_ns_keyword}. --- core/src/lib.rs | 20 +++++++++++++++++--- query/src/lib.rs | 23 ++++++++++++++++++++++- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/core/src/lib.rs b/core/src/lib.rs index ad78de81..0cbc365f 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -93,6 +93,20 @@ impl TypedValue { &TypedValue::Keyword(_) => ValueType::Keyword, } } + + /// Construct a new `TypedValue::Keyword` instance by cloning the provided + /// values. This is expensive, so this might + /// be best limited to tests. + pub fn typed_ns_keyword(ns: &str, name: &str) -> TypedValue { + TypedValue::Keyword(NamespacedKeyword::new(ns, name)) + } + + /// Construct a new `TypedValue::String` instance by cloning the provided + /// value. This is expensive, so this might + /// be best limited to tests. + pub fn typed_string(s: &str) -> TypedValue { + TypedValue::String(s.to_string()) + } } // Put this here rather than in `db` simply because it's widely needed. @@ -141,9 +155,9 @@ impl SQLValueType for ValueType { fn test_typed_value() { assert!(TypedValue::Boolean(false).is_congruent_with(None)); assert!(TypedValue::Boolean(false).is_congruent_with(ValueType::Boolean)); - assert!(!TypedValue::String("foo".to_string()).is_congruent_with(ValueType::Boolean)); - assert!(TypedValue::String("foo".to_string()).is_congruent_with(ValueType::String)); - assert!(TypedValue::String("foo".to_string()).is_congruent_with(None)); + assert!(!TypedValue::typed_string("foo").is_congruent_with(ValueType::Boolean)); + assert!(TypedValue::typed_string("foo").is_congruent_with(ValueType::String)); + assert!(TypedValue::typed_string("foo").is_congruent_with(None)); } /// Bit flags used in `flags0` column in temporary tables created during search, diff --git a/query/src/lib.rs b/query/src/lib.rs index bc2735a7..6fbff8f0 100644 --- a/query/src/lib.rs +++ b/query/src/lib.rs @@ -44,6 +44,27 @@ pub type SrcVarName = String; // Do not include the required syntactic #[derive(Clone, PartialEq, Eq, PartialOrd, Ord)] pub struct Variable(pub PlainSymbol); +impl Variable { + pub fn as_str(&self) -> &str { + (self.0).0.as_str() + } + + pub fn to_string(&self) -> String { + (self.0).0.clone() + } + + pub fn name(&self) -> PlainSymbol { + self.0.clone() + } + + /// Return a new `Variable`, assuming that the provided string is a valid name. + pub fn from_valid_name(name: &str) -> Variable { + let s = PlainSymbol::new(name); + assert!(s.is_var_symbol()); + Variable(s) + } +} + pub trait FromValue { fn from_value(v: &edn::Value) -> Option; } @@ -633,4 +654,4 @@ impl ContainsVariables for Pattern { acc_ref(acc, v) } } -} \ No newline at end of file +}