diff --git a/db-traits/errors.rs b/db-traits/errors.rs index 94ccc8fd..15612f44 100644 --- a/db-traits/errors.rs +++ b/db-traits/errors.rs @@ -118,10 +118,10 @@ impl ::std::fmt::Display for InputError { match self { BadDbId => { writeln!(f, ":db/id in map notation must either not be present or be an entid, an ident, or a tempid") - }, + } BadEntityPlace => { writeln!(f, "cannot convert value place into entity place") - }, + } } } } diff --git a/db/src/cache.rs b/db/src/cache.rs index c4ac794a..2fb05695 100644 --- a/db/src/cache.rs +++ b/db/src/cache.rs @@ -61,7 +61,7 @@ use std::iter::Peekable; use failure::ResultExt; use rusqlite; -use rusqlite::{params_from_iter}; +use rusqlite::params_from_iter; use core_traits::{Binding, Entid, TypedValue}; @@ -1072,7 +1072,9 @@ impl AttributeCaches { replacing: bool, ) -> Result<()> { let mut aev_factory = AevFactory::new(); - let rows = statement.query_map(params_from_iter(&args), |row| Ok(aev_factory.row_to_aev(row)))?; + let rows = statement.query_map(params_from_iter(&args), |row| { + Ok(aev_factory.row_to_aev(row)) + })?; let aevs = AevRows { rows }; self.accumulate_into_cache( None, diff --git a/db/src/db.rs b/db/src/db.rs index 5d2747cd..cbb05b41 100644 --- a/db/src/db.rs +++ b/db/src/db.rs @@ -22,9 +22,9 @@ use itertools; use itertools::Itertools; use rusqlite; use rusqlite::limits::Limit; +use rusqlite::params_from_iter; use rusqlite::types::{ToSql, ToSqlOutput}; use rusqlite::TransactionBehavior; -use rusqlite::{params_from_iter}; use crate::bootstrap; use crate::{repeat_values, to_namespaced_keyword}; diff --git a/db/src/internal_types.rs b/db/src/internal_types.rs index d7aa5929..1b41fcc3 100644 --- a/db/src/internal_types.rs +++ b/db/src/internal_types.rs @@ -75,7 +75,7 @@ impl TransactableValue for ValueAndSpan { } } Nil | Boolean(_) | Instant(_) | BigInteger(_) | Float(_) | Uuid(_) | PlainSymbol(_) - | NamespacedSymbol(_) | Vector(_) | Set(_) | Map(_) => { + | NamespacedSymbol(_) | Vector(_) | Set(_) | Map(_) | Bytes(_) => { bail!(DbErrorKind::InputError(errors::InputError::BadEntityPlace)) } } diff --git a/db/src/timelines.rs b/db/src/timelines.rs index ae14e047..cbb35eac 100644 --- a/db/src/timelines.rs +++ b/db/src/timelines.rs @@ -81,7 +81,7 @@ fn move_transactions_to( new_timeline, crate::repeat_values(tx_ids.len(), 1) ), - params_from_iter(tx_ids.iter()) + params_from_iter(tx_ids.iter()), )?; Ok(()) } diff --git a/edn/Cargo.toml b/edn/Cargo.toml index d65dc4cf..51ce50d1 100644 --- a/edn/Cargo.toml +++ b/edn/Cargo.toml @@ -19,6 +19,8 @@ uuid = { version = "~0.8", features = ["v4", "serde"] } serde = { version = "~1.0", optional = true } serde_derive = { version = "~1.0", optional = true } peg = "~0.7" +bytes = "1.0.1" +hex = "0.4.3" [dev-dependencies] serde_test = "~1.0" diff --git a/edn/src/lib.rs b/edn/src/lib.rs index 2cbb21e2..1d1cfec2 100644 --- a/edn/src/lib.rs +++ b/edn/src/lib.rs @@ -8,7 +8,9 @@ // CONDITIONS OF ANY KIND, either express or implied. See the License for the // specific language governing permissions and limitations under the License. +extern crate bytes; extern crate chrono; +extern crate hex; extern crate itertools; extern crate num; extern crate ordered_float; @@ -38,7 +40,9 @@ pub mod value_rc; pub use crate::value_rc::{Cloned, FromRc, ValueRc}; // Re-export the types we use. +use bytes::Bytes; pub use chrono::{DateTime, Utc}; +use hex::decode; pub use num::BigInt; pub use ordered_float::OrderedFloat; pub use uuid::Uuid; @@ -172,6 +176,13 @@ peg::parser!(pub grammar parse() for str { pub rule uuid() -> SpannedValue = "#uuid" whitespace()+ u:uuid_string() { SpannedValue::Uuid(u) } + rule byte_buffer() -> Bytes = + u:$( ['a'..='f' | 'A'..='F' | '0'..='9']* ) { + let b = decode(u).expect("this is a valid hex byte string"); + Bytes::copy_from_slice(&b) + } + pub rule bytes() -> SpannedValue = "#bytes" whitespace()+ u:byte_buffer() + { SpannedValue::Bytes(u) } rule namespace_divider() = "." rule namespace_separator() = "/" @@ -219,7 +230,7 @@ peg::parser!(pub grammar parse() for str { // Note: It's important that float comes before integer or the parser assumes that floats are integers and fails to parse. pub rule value() -> ValueAndSpan = - __ start:position!() v:(nil() / nan() / infinity() / boolean() / number() / inst() / uuid() / text() / keyword() / symbol() / list() / vector() / map() / set()) end:position!() __ { + __ start:position!() v:(nil() / nan() / infinity() / boolean() / number() / inst() / uuid() / text() / keyword() / symbol() / list() / vector() / map() / set() / bytes() ) end:position!() __ { ValueAndSpan { inner: v, span: Span::new(start, end) diff --git a/edn/src/pretty_print.rs b/edn/src/pretty_print.rs index 2e92a192..acaa579c 100644 --- a/edn/src/pretty_print.rs +++ b/edn/src/pretty_print.rs @@ -58,9 +58,7 @@ impl Value { let open = open.into(); let n = open.len() as isize; let i = { - let this = vs - .into_iter() - .map(|v| v.as_doc(allocator)); + let this = vs.into_iter().map(|v| v.as_doc(allocator)); let element = allocator.line(); Itertools::intersperse(this, element) }; @@ -86,9 +84,9 @@ impl Value { Value::Map(ref vs) => { let xs = { let this = vs - .iter() - .rev() - .map(|(k, v)| k.as_doc(pp).append(pp.line()).append(v.as_doc(pp)).group()); + .iter() + .rev() + .map(|(k, v)| k.as_doc(pp).append(pp.line()).append(v.as_doc(pp)).group()); let element = pp.line(); Itertools::intersperse(this, element) }; diff --git a/edn/src/query.rs b/edn/src/query.rs index 7cc6bcd3..a788a8f0 100644 --- a/edn/src/query.rs +++ b/edn/src/query.rs @@ -233,7 +233,7 @@ impl FromValue for FnArg { { Some(FnArg::Constant(x.clone().into())) } - Nil | NamespacedSymbol(_) | Vector(_) | List(_) | Set(_) | Map(_) => None, + Nil | NamespacedSymbol(_) | Vector(_) | List(_) | Set(_) | Map(_) | Bytes(_) => None, } } } @@ -410,6 +410,7 @@ impl FromValue for PatternValuePlace { crate::SpannedValue::List(_) => None, crate::SpannedValue::Set(_) => None, crate::SpannedValue::Vector(_) => None, + crate::SpannedValue::Bytes(_) => None, } } } diff --git a/edn/src/types.rs b/edn/src/types.rs index c1f4b078..37d4158a 100644 --- a/edn/src/types.rs +++ b/edn/src/types.rs @@ -27,6 +27,8 @@ use uuid::Uuid; use crate::symbols; +use bytes::Bytes; +use hex::encode; /// Value represents one of the allowed values in an EDN string. #[derive(PartialEq, Eq, Hash, Clone, Debug)] pub enum Value { @@ -52,6 +54,7 @@ pub enum Value { // See https://internals.rust-lang.org/t/implementing-hash-for-hashset-hashmap/3817/1 Set(BTreeSet), Map(BTreeMap), + Bytes(Bytes), } /// `SpannedValue` is the parallel to `Value` but used in `ValueAndSpan`. @@ -73,6 +76,7 @@ pub enum SpannedValue { List(LinkedList), Set(BTreeSet), Map(BTreeMap), + Bytes(Bytes), } /// Span represents the current offset (start, end) into the input string. @@ -172,6 +176,7 @@ impl From for Value { .map(|(x, y)| (x.without_spans(), y.without_spans())) .collect(), ), + SpannedValue::Bytes(b) => Value::Bytes(b), } } } @@ -328,6 +333,7 @@ macro_rules! def_common_value_methods { def_is!(is_list, $t::List(_)); def_is!(is_set, $t::Set(_)); def_is!(is_map, $t::Map(_)); + def_is!(is_bytes, $t::Bytes(_)); pub fn is_keyword(&self) -> bool { match self { @@ -360,6 +366,7 @@ macro_rules! def_common_value_methods { def_as_ref!(as_uuid, $t::Uuid, Uuid); def_as_ref!(as_symbol, $t::PlainSymbol, symbols::PlainSymbol); def_as_ref!(as_namespaced_symbol, $t::NamespacedSymbol, symbols::NamespacedSymbol); + def_as_ref!(as_bytes, $t::Bytes, Bytes); pub fn as_keyword(&self) -> Option<&symbols::Keyword> { match self { @@ -397,6 +404,7 @@ macro_rules! def_common_value_methods { def_into!(into_uuid, $t::Uuid, Uuid,); def_into!(into_symbol, $t::PlainSymbol, symbols::PlainSymbol,); def_into!(into_namespaced_symbol, $t::NamespacedSymbol, symbols::NamespacedSymbol,); + def_into!(into_bytes, $t::Bytes, Bytes,); pub fn into_keyword(self) -> Option { match self { @@ -467,6 +475,7 @@ macro_rules! def_common_value_methods { $t::List(_) => 13, $t::Set(_) => 14, $t::Map(_) => 15, + $t::Bytes(_) => 16, } } @@ -487,6 +496,7 @@ macro_rules! def_common_value_methods { $t::List(_) => true, $t::Set(_) => true, $t::Map(_) => true, + $t::Bytes(_) => true, } } @@ -524,6 +534,7 @@ macro_rules! def_common_value_ord { (&$t::List(ref a), &$t::List(ref b)) => b.cmp(a), (&$t::Set(ref a), &$t::Set(ref b)) => b.cmp(a), (&$t::Map(ref a), &$t::Map(ref b)) => b.cmp(a), + (&$t::Bytes(ref a), &$t::Bytes(ref b)) => b.cmp(a), _ => $value.precedence().cmp(&$other.precedence()), } }; @@ -590,6 +601,10 @@ macro_rules! def_common_value_display { } write!($f, " }}") } + $t::Bytes(ref v) => { + let s = encode(v); + write!($f, "#bytes \"{}\"", s) + } } }; } diff --git a/edn/tests/tests.rs b/edn/tests/tests.rs index 82316c29..077f3265 100644 --- a/edn/tests/tests.rs +++ b/edn/tests/tests.rs @@ -82,6 +82,7 @@ fn_parse_into_value!(vector); fn_parse_into_value!(set); fn_parse_into_value!(map); fn_parse_into_value!(value); +fn_parse_into_value!(bytes); #[test] fn test_nil() { @@ -316,6 +317,27 @@ fn test_uuid() { assert_eq!(value.to_pretty(100).unwrap(), s); } +#[test] +fn test_bytes() { + assert!(parse::bytes("#bytes01 ").is_err()); // No whitespace. + assert!(parse::bytes("#bytes _ZZ").is_err()); // No whitespace. + assert!(parse::bytes("#bytes 01 ").is_err()); // No whitespace. + assert!(parse::bytes("#01 ").is_err()); // No whitespace. + + let expected = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + let s = format!("{} {}", "#bytes", hex::encode(expected.clone())); + let actual: Value = parse::bytes(&s).expect("parse success").into(); + assert!(actual.is_bytes()); + assert_eq!(expected, actual.as_bytes().unwrap().to_vec()); + + assert_eq!( + self::bytes("#bytes 010203050403022a").unwrap(), + Value::Bytes(bytes::Bytes::copy_from_slice(&vec!( + 1, 2, 3, 5, 4, 3, 2, 42 + ))) + ); +} + #[test] fn test_inst() { assert!(parse::value("#inst\"2016-01-01T11:00:00.000Z\"").is_err()); // No whitespace. @@ -584,6 +606,12 @@ fn test_value() { value("#inst \"2017-04-28T20:23:05.187Z\"").unwrap(), Instant(Utc.timestamp(1493410985, 187000000)) ); + assert_eq!( + value("#bytes 010203050403022a").unwrap(), + Bytes(bytes::Bytes::copy_from_slice(&vec!( + 1, 2, 3, 5, 4, 3, 2, 42 + ))) + ); } #[test] diff --git a/tolstoy/src/remote_client.rs b/tolstoy/src/remote_client.rs index cfee9335..9c620716 100644 --- a/tolstoy/src/remote_client.rs +++ b/tolstoy/src/remote_client.rs @@ -10,7 +10,7 @@ #![allow(dead_code)] -use hyper::{body, header, Client, Body, Method, Request, StatusCode}; +use hyper::{body, header, Body, Client, Method, Request, StatusCode}; use hyper_tls::HttpsConnector; // TODO: https://github.com/mozilla/mentat/issues/570 // use serde_cbor; diff --git a/tolstoy/src/syncer.rs b/tolstoy/src/syncer.rs index 4ad34e96..b2c61b77 100644 --- a/tolstoy/src/syncer.rs +++ b/tolstoy/src/syncer.rs @@ -741,13 +741,14 @@ impl Syncer { // Since we've "merged" with the remote bootstrap, the "no-op" and // "local fast-forward" cases are reported as merges. match Syncer::what_do(remote_state, local_state) { - SyncAction::NoOp => { - Ok(SyncReport::Merge(SyncFollowup::None)) - } + SyncAction::NoOp => Ok(SyncReport::Merge(SyncFollowup::None)), SyncAction::PopulateRemote => { // This is a programming error. - bail!(TolstoyError::UnexpectedState("Remote state can't be empty on first sync against non-empty remote".to_string())) + bail!(TolstoyError::UnexpectedState( + "Remote state can't be empty on first sync against non-empty remote" + .to_string() + )) } SyncAction::RemoteFastForward => { @@ -761,12 +762,11 @@ impl Syncer { SyncAction::CombineChanges => { let local_txs = Processor::process( - &ip.transaction, Some(local_metadata.root), LocalTxSet::new())?; - Syncer::merge( - ip, - incoming_txs[1..].to_vec(), - local_txs, - ) + &ip.transaction, + Some(local_metadata.root), + LocalTxSet::new(), + )?; + Syncer::merge(ip, incoming_txs[1..].to_vec(), local_txs) } } }