Rebased conversion of mentat_query_parser to use error-chain. r=nalexander

This is a tiny bit simpler and more consistent.
This commit is contained in:
Richard Newman 2017-02-23 21:13:04 -08:00
parent b2f22952c1
commit 48312e1ff0
4 changed files with 66 additions and 60 deletions

View file

@ -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) {}
}
}

View file

@ -47,13 +47,9 @@ use self::mentat_query::{
}; };
use self::mentat_parser_utils::ValueParseError; use self::mentat_parser_utils::ValueParseError;
use super::errors::{
Error,
ErrorKind,
Result,
};
use super::parse::{ use super::parse::{
Result,
ErrorKind,
QueryParseResult, QueryParseResult,
clause_seq_to_patterns, clause_seq_to_patterns,
}; };
@ -127,15 +123,15 @@ fn parse_find_map(map: BTreeMap<edn::Keyword, Vec<edn::Value>>) -> QueryParseRes
// Oh, if only we had `guard`. // Oh, if only we had `guard`.
if let Some(find) = map.get(&kw_find) { if let Some(find) = map.get(&kw_find) {
if let Some(wheres) = map.get(&kw_where) { if let Some(wheres) = map.get(&kw_where) {
return parse_find_parts(find, parse_find_parts(find,
map.get(&kw_in).map(|x| x.as_slice()), map.get(&kw_in).map(|x| x.as_slice()),
map.get(&kw_with).map(|x| x.as_slice()), map.get(&kw_with).map(|x| x.as_slice()),
wheres); wheres)
} else { } else {
bail!(ErrorKind::MissingField(kw_where)); bail!(ErrorKind::MissingFieldError(kw_where))
} }
} else { } else {
bail!(ErrorKind::MissingField(kw_find)); bail!(ErrorKind::MissingFieldError(kw_find))
} }
} }
@ -153,10 +149,10 @@ fn parse_find_edn_map(map: BTreeMap<edn::Value, edn::Value>) -> QueryParseResult
m.insert(kw, vec); m.insert(kw, vec);
continue; continue;
} else { } else {
bail!(ErrorKind::InvalidInput(v)); bail!(ErrorKind::InvalidInputError(v))
} }
} else { } 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); return parse_find_map(m);
} }
} }
bail!(ErrorKind::InvalidInput(expr)); bail!(ErrorKind::InvalidInputError(expr))
} }
#[cfg(test)] #[cfg(test)]

View file

@ -12,30 +12,28 @@
#[macro_use] #[macro_use]
extern crate error_chain; extern crate error_chain;
#[macro_use] #[macro_use]
extern crate matches; extern crate matches;
extern crate edn; extern crate edn;
#[macro_use] #[macro_use]
extern crate mentat_parser_utils; extern crate mentat_parser_utils;
mod util; mod util;
mod parse; mod parse;
pub mod errors;
pub mod find; pub mod find;
pub use errors::{
Error,
ErrorKind,
ResultExt,
Result,
};
pub use find::{ pub use find::{
parse_find, parse_find,
parse_find_string, parse_find_string,
}; };
pub use parse::{ pub use parse::{
Error,
ErrorKind,
QueryParseResult, QueryParseResult,
Result,
ResultExt,
}; };

View file

@ -13,9 +13,14 @@ extern crate edn;
extern crate mentat_parser_utils; extern crate mentat_parser_utils;
extern crate mentat_query; 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::{eof, many1, optional, parser, satisfy_map, Parser, ParseResult, Stream};
use self::combine::combinator::{choice, try}; use self::combine::combinator::{choice, try};
use self::mentat_parser_utils::{
ResultParser,
ValueParseError,
};
use self::mentat_query::{ use self::mentat_query::{
Element, Element,
FindQuery, FindQuery,
@ -29,7 +34,49 @@ use self::mentat_query::{
WhereClause, 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<Vec<WhereClause>>; pub type WhereParseResult = Result<Vec<WhereClause>>;
pub type FindParseResult = Result<FindSpec>; pub type FindParseResult = Result<FindSpec>;
pub type QueryParseResult = Result<FindQuery>; pub type QueryParseResult = Result<FindQuery>;