From 48312e1ff0374146b990d303208a941b18587efc Mon Sep 17 00:00:00 2001 From: Richard Newman Date: Thu, 23 Feb 2017 21:13:04 -0800 Subject: [PATCH] Rebased conversion of mentat_query_parser to use error-chain. r=nalexander This is a tiny bit simpler and more consistent. --- query-parser/src/errors.rs | 35 -------------------------- query-parser/src/find.rs | 26 ++++++++----------- query-parser/src/lib.rs | 14 +++++------ query-parser/src/parse.rs | 51 ++++++++++++++++++++++++++++++++++++-- 4 files changed, 66 insertions(+), 60 deletions(-) delete mode 100644 query-parser/src/errors.rs diff --git a/query-parser/src/errors.rs b/query-parser/src/errors.rs deleted file mode 100644 index 0ef8b53a..00000000 --- a/query-parser/src/errors.rs +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright 2016 Mozilla -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may not use -// this file except in compliance with the License. You may obtain a copy of the -// License at http://www.apache.org/licenses/LICENSE-2.0 -// Unless required by applicable law or agreed to in writing, software distributed -// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -// CONDITIONS OF ANY KIND, either express or implied. See the License for the -// specific language governing permissions and limitations under the License. - -#![allow(dead_code)] - -use edn; -use mentat_parser_utils::ValueParseError; - -error_chain! { - types { - Error, ErrorKind, ResultExt, Result; - } - - foreign_links { - EdnParseError(edn::ParseError); - } - - links { - } - - errors { - NotAVariableError(value: edn::Value) {} - InvalidInput(value: edn::Value) {} - FindParseError(value_parse_error: ValueParseError) {} - WhereParseError(value_parse_error: ValueParseError) {} - MissingField(field: edn::Keyword) {} - } -} diff --git a/query-parser/src/find.rs b/query-parser/src/find.rs index 2946919d..1cb8c8d8 100644 --- a/query-parser/src/find.rs +++ b/query-parser/src/find.rs @@ -47,13 +47,9 @@ use self::mentat_query::{ }; use self::mentat_parser_utils::ValueParseError; -use super::errors::{ - Error, - ErrorKind, - Result, -}; - use super::parse::{ + Result, + ErrorKind, QueryParseResult, clause_seq_to_patterns, }; @@ -127,15 +123,15 @@ fn parse_find_map(map: BTreeMap>) -> QueryParseRes // Oh, if only we had `guard`. if let Some(find) = map.get(&kw_find) { if let Some(wheres) = map.get(&kw_where) { - return parse_find_parts(find, - map.get(&kw_in).map(|x| x.as_slice()), - map.get(&kw_with).map(|x| x.as_slice()), - wheres); + parse_find_parts(find, + map.get(&kw_in).map(|x| x.as_slice()), + map.get(&kw_with).map(|x| x.as_slice()), + wheres) } else { - bail!(ErrorKind::MissingField(kw_where)); + bail!(ErrorKind::MissingFieldError(kw_where)) } } else { - bail!(ErrorKind::MissingField(kw_find)); + bail!(ErrorKind::MissingFieldError(kw_find)) } } @@ -153,10 +149,10 @@ fn parse_find_edn_map(map: BTreeMap) -> QueryParseResult m.insert(kw, vec); continue; } else { - bail!(ErrorKind::InvalidInput(v)); + bail!(ErrorKind::InvalidInputError(v)) } } else { - bail!(ErrorKind::InvalidInput(k)); + bail!(ErrorKind::InvalidInputError(k)) } } @@ -178,7 +174,7 @@ pub fn parse_find(expr: edn::Value) -> QueryParseResult { return parse_find_map(m); } } - bail!(ErrorKind::InvalidInput(expr)); + bail!(ErrorKind::InvalidInputError(expr)) } #[cfg(test)] diff --git a/query-parser/src/lib.rs b/query-parser/src/lib.rs index a44f6973..4a24843a 100644 --- a/query-parser/src/lib.rs +++ b/query-parser/src/lib.rs @@ -12,30 +12,28 @@ #[macro_use] extern crate error_chain; + #[macro_use] extern crate matches; extern crate edn; + #[macro_use] extern crate mentat_parser_utils; mod util; mod parse; -pub mod errors; pub mod find; -pub use errors::{ - Error, - ErrorKind, - ResultExt, - Result, -}; - pub use find::{ parse_find, parse_find_string, }; pub use parse::{ + Error, + ErrorKind, QueryParseResult, + Result, + ResultExt, }; diff --git a/query-parser/src/parse.rs b/query-parser/src/parse.rs index c0258198..34d0621d 100644 --- a/query-parser/src/parse.rs +++ b/query-parser/src/parse.rs @@ -13,9 +13,14 @@ extern crate edn; extern crate mentat_parser_utils; extern crate mentat_query; -use self::mentat_parser_utils::{ResultParser, ValueParseError}; use self::combine::{eof, many1, optional, parser, satisfy_map, Parser, ParseResult, Stream}; use self::combine::combinator::{choice, try}; + +use self::mentat_parser_utils::{ + ResultParser, + ValueParseError, +}; + use self::mentat_query::{ Element, FindQuery, @@ -29,7 +34,49 @@ use self::mentat_query::{ WhereClause, }; -use errors::{Error, ErrorKind, ResultExt, Result}; +error_chain! { + types { + Error, ErrorKind, ResultExt, Result; + } + + foreign_links { + EdnParseError(edn::parse::ParseError); + } + + errors { + NotAVariableError(value: edn::Value) { + description("not a variable") + display("not a variable: '{}'", value) + } + + FindParseError(e: ValueParseError) { + description(":find parse error") + display(":find parse error") + } + + WhereParseError(e: ValueParseError) { + description(":where parse error") + display(":where parse error") + } + + // Not yet used. + WithParseError { + description(":with parse error") + display(":with parse error") + } + + InvalidInputError(value: edn::Value) { + description("invalid input") + display("invalid input: '{}'", value) + } + + MissingFieldError(value: edn::Keyword) { + description("missing field") + display("missing field: '{}'", value) + } + } +} + pub type WhereParseResult = Result>; pub type FindParseResult = Result; pub type QueryParseResult = Result;