diff --git a/lib/src/sql/bytes.rs b/lib/src/sql/bytes.rs new file mode 100644 index 00000000..d39d7888 --- /dev/null +++ b/lib/src/sql/bytes.rs @@ -0,0 +1,19 @@ +use crate::sql::serde::is_internal_serialization; +use serde::Deserialize; +use serde::Serialize; + +#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Deserialize, Hash)] +pub struct Bytes(pub(super) Vec); + +impl Serialize for Bytes { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + if is_internal_serialization() { + serializer.serialize_bytes(&self.0) + } else { + serializer.serialize_none() + } + } +} diff --git a/lib/src/sql/mod.rs b/lib/src/sql/mod.rs index 8944db2f..4a22dcd1 100644 --- a/lib/src/sql/mod.rs +++ b/lib/src/sql/mod.rs @@ -4,6 +4,7 @@ pub(crate) mod algorithm; pub(crate) mod array; pub(crate) mod base; pub(crate) mod block; +pub(crate) mod bytes; pub(crate) mod comment; pub(crate) mod common; pub(crate) mod cond; @@ -72,6 +73,7 @@ pub use self::algorithm::Algorithm; pub use self::array::Array; pub use self::base::Base; pub use self::block::Block; +pub use self::bytes::Bytes; pub use self::cond::Cond; pub use self::data::Data; pub use self::datetime::Datetime; diff --git a/lib/src/sql/value/serde/ser/value/mod.rs b/lib/src/sql/value/serde/ser/value/mod.rs index d52091f7..5f30b605 100644 --- a/lib/src/sql/value/serde/ser/value/mod.rs +++ b/lib/src/sql/value/serde/ser/value/mod.rs @@ -10,6 +10,7 @@ use crate::sql::serde::serialize_internal; use crate::sql::value::serde::ser; use crate::sql::value::Value; use crate::sql::Block; +use crate::sql::Bytes; use crate::sql::Datetime; use crate::sql::Duration; use crate::sql::Future; @@ -155,12 +156,8 @@ impl ser::Serializer for Serializer { Ok(value.into()) } - fn serialize_bytes(self, values: &[u8]) -> Result { - let mut vec = Vec::with_capacity(values.len()); - for value in values { - vec.push(Value::from(*value)); - } - Ok(vec.into()) + fn serialize_bytes(self, value: &[u8]) -> Result { + Ok(Value::Bytes(Bytes(value.to_owned()))) } #[inline] @@ -690,6 +687,15 @@ mod tests { assert_eq!(expected, to_value(&expected).unwrap()); } + #[test] + fn bytes() { + let bytes = Bytes("foobar".as_bytes().to_owned()); + let value = to_value(&bytes).unwrap(); + let expected = Value::Bytes(bytes); + assert_eq!(value, expected); + assert_eq!(expected, to_value(&expected).unwrap()); + } + #[test] fn param() { let param = Param::default(); diff --git a/lib/src/sql/value/value.rs b/lib/src/sql/value/value.rs index dd171c28..4fee79c4 100644 --- a/lib/src/sql/value/value.rs +++ b/lib/src/sql/value/value.rs @@ -6,6 +6,7 @@ use crate::dbs::Transaction; use crate::err::Error; use crate::sql::array::{array, Array}; use crate::sql::block::{block, Block}; +use crate::sql::bytes::Bytes; use crate::sql::common::commas; use crate::sql::constant::{constant, Constant}; use crate::sql::datetime::{datetime, Datetime}; @@ -114,7 +115,7 @@ pub enum Value { Array(Array), Object(Object), Geometry(Geometry), - Bytes(Vec), + Bytes(Bytes), // --- Param(Param), Idiom(Idiom),