Change to_namespaced_keyword(s) to return a Result rather than Option to (#333)

reduce error handling throughout db code. Fixes #331. r=nalexander
This commit is contained in:
Jordan Santell 2017-02-17 16:10:34 -08:00 committed by GitHub
parent 6f67f8563b
commit bc2b2ec4c8
3 changed files with 13 additions and 18 deletions

View file

@ -346,8 +346,6 @@ impl TypedSQLValue for TypedValue {
(10, rusqlite::types::Value::Text(x)) => Ok(TypedValue::String(x)), (10, rusqlite::types::Value::Text(x)) => Ok(TypedValue::String(x)),
(13, rusqlite::types::Value::Text(x)) => { (13, rusqlite::types::Value::Text(x)) => {
to_namespaced_keyword(&x).map(|k| TypedValue::Keyword(k)) to_namespaced_keyword(&x).map(|k| TypedValue::Keyword(k))
// TODO Use custom ErrorKind https://github.com/brson/error-chain/issues/117
.ok_or(ErrorKind::NotYetImplemented(format!("InvalidNamespacedKeyword: {}", x)).into())
}, },
(_, value) => bail!(ErrorKind::BadSQLValuePair(value, value_type_tag)), (_, value) => bail!(ErrorKind::BadSQLValuePair(value, value_type_tag)),
} }
@ -402,10 +400,7 @@ pub fn read_ident_map(conn: &rusqlite::Connection) -> Result<IdentMap> {
let mut stmt: rusqlite::Statement = conn.prepare("SELECT ident, entid FROM idents")?; let mut stmt: rusqlite::Statement = conn.prepare("SELECT ident, entid FROM idents")?;
let m = stmt.query_and_then(&[], |row| -> Result<(symbols::NamespacedKeyword, Entid)> { let m = stmt.query_and_then(&[], |row| -> Result<(symbols::NamespacedKeyword, Entid)> {
let ident: String = row.get(0); let ident: String = row.get(0);
to_namespaced_keyword(&ident) to_namespaced_keyword(&ident).map(|i| (i, row.get(1)))
.map(|i| (i, row.get(1)))
// TODO Use custom ErrorKind https://github.com/brson/error-chain/issues/117
.ok_or(ErrorKind::NotYetImplemented(format!("InvalidNamespacedKeyword: {}", ident.clone())).into())
})?.collect(); })?.collect();
m m
} }
@ -435,13 +430,9 @@ pub fn read_schema(conn: &rusqlite::Connection, ident_map: &IdentMap) -> Result<
let ident = to_namespaced_keyword(&symbolic_ident); let ident = to_namespaced_keyword(&symbolic_ident);
let attr = to_namespaced_keyword(&symbolic_attr); let attr = to_namespaced_keyword(&symbolic_attr);
match (ident, attr, typed_value) { match (ident, attr, typed_value) {
(Some(ident), Some(attr), typed_value) => Ok((ident, attr, typed_value)), (Ok(ident), Ok(attr), typed_value) => Ok((ident, attr, typed_value)),
(None, _, _) => (Err(e), _, _) => Err(e),
// TODO Use custom ErrorKind https://github.com/brson/error-chain/issues/117 (_, Err(e), _) => Err(e),
Err(ErrorKind::NotYetImplemented(format!("InvalidNamespacedKeyword: {}", &symbolic_ident)).into()),
(_, None, _) =>
// TODO Use custom ErrorKind https://github.com/brson/error-chain/issues/117
Err(ErrorKind::NotYetImplemented(format!("InvalidNamespacedKeyword: {}", &symbolic_attr)).into()),
} }
})?.collect(); })?.collect();

View file

@ -26,6 +26,7 @@ extern crate mentat_tx_parser;
use itertools::Itertools; use itertools::Itertools;
use std::iter::repeat; use std::iter::repeat;
use errors::{ErrorKind, Result};
pub mod db; pub mod db;
mod bootstrap; mod bootstrap;
@ -46,13 +47,16 @@ use edn::symbols;
// TODO: replace with sqlite3_limit. #288. // TODO: replace with sqlite3_limit. #288.
pub const SQLITE_MAX_VARIABLE_NUMBER: usize = 999; pub const SQLITE_MAX_VARIABLE_NUMBER: usize = 999;
pub fn to_namespaced_keyword(s: &str) -> Option<symbols::NamespacedKeyword> { pub fn to_namespaced_keyword(s: &str) -> Result<symbols::NamespacedKeyword> {
let splits = [':', '/']; let splits = [':', '/'];
let mut i = s.split(&splits[..]); let mut i = s.split(&splits[..]);
match (i.next(), i.next(), i.next(), i.next()) { let nsk = match (i.next(), i.next(), i.next(), i.next()) {
(Some(""), Some(namespace), Some(name), None) => Some(symbols::NamespacedKeyword::new(namespace, name)), (Some(""), Some(namespace), Some(name), None) => Some(symbols::NamespacedKeyword::new(namespace, name)),
_ => None _ => None,
} };
// TODO Use custom ErrorKind https://github.com/brson/error-chain/issues/117
nsk.ok_or(ErrorKind::NotYetImplemented(format!("InvalidNamespacedKeyword: {}", s)).into())
} }
/// Prepare an SQL `VALUES` block, like (?, ?, ?), (?, ?, ?). /// Prepare an SQL `VALUES` block, like (?, ?, ?), (?, ?, ?).

View file

@ -11,7 +11,7 @@
#![allow(dead_code)] #![allow(dead_code)]
use entids; use entids;
use errors::*; use errors::{ErrorKind, Result};
use edn::symbols; use edn::symbols;
use mentat_core::{ use mentat_core::{
Attribute, Attribute,