From 592dec72419e64d7e66f387c8226d32f76c1952e Mon Sep 17 00:00:00 2001 From: Richard Newman Date: Wed, 1 Feb 2017 14:51:02 -0800 Subject: [PATCH] Implement a FromValue trait for SrcVar and Variable. (#227) r=nalexander --- query-parser/src/parse.rs | 4 ++-- query-parser/src/util.rs | 16 ++----------- query/src/lib.rs | 48 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 51 insertions(+), 17 deletions(-) diff --git a/query-parser/src/parse.rs b/query-parser/src/parse.rs index b634b883..583db04d 100644 --- a/query-parser/src/parse.rs +++ b/query-parser/src/parse.rs @@ -15,7 +15,7 @@ extern crate mentat_query; use self::combine::{eof, many1, parser, satisfy_map, Parser, ParseResult, Stream}; use self::combine::combinator::{Expected, FnParser, choice, try}; use self::edn::Value::PlainSymbol; -use self::mentat_query::{Element, FindSpec, Variable}; +use self::mentat_query::{Element, FromValue, FindSpec, Variable}; use super::error::{FindParseError, FindParseResult}; @@ -45,7 +45,7 @@ impl FindSp } fn variable_(input: I) -> ParseResult { - satisfy_map(|x: edn::Value| super::util::value_to_variable(&x)).parse_stream(input) + satisfy_map(|x: edn::Value| Variable::from_value(&x)).parse_stream(input) } fn period() -> FindSpParser<(), I> { diff --git a/query-parser/src/util.rs b/query-parser/src/util.rs index e3f24657..a3226688 100644 --- a/query-parser/src/util.rs +++ b/query-parser/src/util.rs @@ -13,28 +13,16 @@ extern crate mentat_query; use std::collections::BTreeMap; -use self::edn::Value::PlainSymbol; -use self::mentat_query::Variable; +use self::mentat_query::{FromValue, Variable}; use super::error::NotAVariableError; -/// If the provided EDN value is a PlainSymbol beginning with '?', return -/// it wrapped in a Variable. If not, return None. -pub fn value_to_variable(v: &edn::Value) -> Option { - if let PlainSymbol(ref sym) = *v { - if sym.0.starts_with('?') { - return Some(Variable(sym.clone())); - } - } - return None; -} - /// If the provided slice of EDN values are all variables as /// defined by `value_to_variable`, return a Vec of Variables. /// Otherwise, return the unrecognized Value. pub fn values_to_variables(vals: &[edn::Value]) -> Result, NotAVariableError> { let mut out: Vec = Vec::with_capacity(vals.len()); for v in vals { - if let Some(var) = value_to_variable(v) { + if let Some(var) = Variable::from_value(v) { out.push(var); continue; } diff --git a/query/src/lib.rs b/query/src/lib.rs index c1b3cf14..3ecc8120 100644 --- a/query/src/lib.rs +++ b/query/src/lib.rs @@ -43,12 +43,58 @@ pub type SrcVarName = String; // Do not include the required syntactic #[derive(Clone, Debug, PartialEq, Eq)] pub struct Variable(pub PlainSymbol); -#[derive(Clone,Debug,Eq,PartialEq)] +pub trait FromValue { + fn from_value(v: &edn::Value) -> Option; +} + +/// If the provided EDN value is a PlainSymbol beginning with '?', return +/// it wrapped in a Variable. If not, return None. +impl FromValue for Variable { + fn from_value(v: &edn::Value) -> Option { + if let edn::Value::PlainSymbol(ref s) = *v { + Variable::from_symbol(s) + } else { + None + } + } +} + +impl Variable { + fn from_symbol(sym: &PlainSymbol) -> Option { + if sym.is_var_symbol() { + Some(Variable(sym.clone())) + } else { + None + } + } +} + +#[derive(Clone, Debug, Eq, PartialEq)] pub enum SrcVar { DefaultSrc, NamedSrc(SrcVarName), } +impl FromValue for SrcVar { + fn from_value(v: &edn::Value) -> Option { + if let edn::Value::PlainSymbol(ref s) = *v { + SrcVar::from_symbol(s) + } else { + None + } + } +} + +impl SrcVar { + pub fn from_symbol(sym: &PlainSymbol) -> Option { + if sym.is_src_symbol() { + Some(SrcVar::NamedSrc(sym.plain_name().to_string())) + } else { + None + } + } +} + /// These are the scalar values representable in EDN. #[derive(Clone,Debug,Eq,PartialEq)] pub enum NonIntegerConstant {