From bc2b2ec4c81b27a2ee70846d3002f116a7b1183e Mon Sep 17 00:00:00 2001 From: Jordan Santell Date: Fri, 17 Feb 2017 16:10:34 -0800 Subject: [PATCH] Change to_namespaced_keyword(s) to return a Result rather than Option to (#333) reduce error handling throughout db code. Fixes #331. r=nalexander --- db/src/db.rs | 17 ++++------------- db/src/lib.rs | 12 ++++++++---- db/src/schema.rs | 2 +- 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/db/src/db.rs b/db/src/db.rs index db3b7b2b..bde4619c 100644 --- a/db/src/db.rs +++ b/db/src/db.rs @@ -346,8 +346,6 @@ impl TypedSQLValue for TypedValue { (10, rusqlite::types::Value::Text(x)) => Ok(TypedValue::String(x)), (13, rusqlite::types::Value::Text(x)) => { 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)), } @@ -402,10 +400,7 @@ pub fn read_ident_map(conn: &rusqlite::Connection) -> Result { let mut stmt: rusqlite::Statement = conn.prepare("SELECT ident, entid FROM idents")?; let m = stmt.query_and_then(&[], |row| -> Result<(symbols::NamespacedKeyword, Entid)> { let ident: String = row.get(0); - to_namespaced_keyword(&ident) - .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()) + to_namespaced_keyword(&ident).map(|i| (i, row.get(1))) })?.collect(); m } @@ -435,13 +430,9 @@ pub fn read_schema(conn: &rusqlite::Connection, ident_map: &IdentMap) -> Result< let ident = to_namespaced_keyword(&symbolic_ident); let attr = to_namespaced_keyword(&symbolic_attr); match (ident, attr, typed_value) { - (Some(ident), Some(attr), typed_value) => Ok((ident, attr, typed_value)), - (None, _, _) => - // TODO Use custom ErrorKind https://github.com/brson/error-chain/issues/117 - 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()), + (Ok(ident), Ok(attr), typed_value) => Ok((ident, attr, typed_value)), + (Err(e), _, _) => Err(e), + (_, Err(e), _) => Err(e), } })?.collect(); diff --git a/db/src/lib.rs b/db/src/lib.rs index b53b3615..3e7c0417 100644 --- a/db/src/lib.rs +++ b/db/src/lib.rs @@ -26,6 +26,7 @@ extern crate mentat_tx_parser; use itertools::Itertools; use std::iter::repeat; +use errors::{ErrorKind, Result}; pub mod db; mod bootstrap; @@ -46,13 +47,16 @@ use edn::symbols; // TODO: replace with sqlite3_limit. #288. pub const SQLITE_MAX_VARIABLE_NUMBER: usize = 999; -pub fn to_namespaced_keyword(s: &str) -> Option { +pub fn to_namespaced_keyword(s: &str) -> Result { let 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)), - _ => 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 (?, ?, ?), (?, ?, ?). diff --git a/db/src/schema.rs b/db/src/schema.rs index c7e5077e..30ba81af 100644 --- a/db/src/schema.rs +++ b/db/src/schema.rs @@ -11,7 +11,7 @@ #![allow(dead_code)] use entids; -use errors::*; +use errors::{ErrorKind, Result}; use edn::symbols; use mentat_core::{ Attribute,