diff --git a/crdt-enc-tokio/src/lib.rs b/crdt-enc-tokio/src/lib.rs index 248a628..9a534f2 100644 --- a/crdt-enc-tokio/src/lib.rs +++ b/crdt-enc-tokio/src/lib.rs @@ -7,7 +7,6 @@ use ::futures::{ stream::{self, Stream, StreamExt, TryStreamExt}, }; use ::std::{ - convert::TryFrom, fmt::Debug, path::{Path, PathBuf}, str::FromStr, @@ -55,7 +54,7 @@ impl crdt_enc::storage::Storage for Storage { .with_context(|| format!("failed reading local meta file {}", path.display()))?; bytes .map(|bytes| { - let lm = VersionBytes::try_from(bytes.as_ref()).with_context(|| { + let lm = VersionBytes::deserialize(&bytes).with_context(|| { format!("failed parsing local meta file {}", path.display()) })?; Ok(lm) @@ -103,7 +102,7 @@ impl crdt_enc::storage::Storage for Storage { let bytes = fs::read(&path).await.with_context(|| { format!("failed reading remote meta file {}", path.display()) })?; - let rm = VersionBytes::try_from(bytes.as_ref()).with_context(|| { + let rm = VersionBytes::deserialize(&bytes).with_context(|| { format!("failed parsing remote meta file {}", path.display()) })?; Ok((name, rm)) @@ -163,7 +162,7 @@ impl crdt_enc::storage::Storage for Storage { let block = fs::read(&path) .await .with_context(|| format!("failed reading state file {}", path.display()))?; - let block = VersionBytes::try_from(block.as_ref()) + let block = VersionBytes::deserialize(&block) .with_context(|| format!("failed parsing state file {}", path.display()))?; Ok((name, block)) } @@ -239,7 +238,7 @@ impl crdt_enc::storage::Storage for Storage { return Ok(None); }; - let data = VersionBytes::try_from(bytes.as_ref()) + let data = VersionBytes::deserialize(&bytes) .with_context(|| format!("failed parsing op file {}", path.display()))?; Ok(Some((actor, version, data))) diff --git a/crdt-enc/src/lib.rs b/crdt-enc/src/lib.rs index a0d14f6..80730df 100644 --- a/crdt-enc/src/lib.rs +++ b/crdt-enc/src/lib.rs @@ -465,7 +465,7 @@ where .await .with_context(|| format!("failed decrypting remote state {}", name))?; - let clear_text = VersionBytesRef::from_slice(&clear_text)?; + let clear_text = VersionBytesRef::deserialize(&clear_text)?; clear_text.ensure_versions(&self.supported_data_versions)?; let state_wrapper: StateWrapper = rmp_serde::from_read_ref(&clear_text)?; @@ -524,7 +524,7 @@ where .await .unwrap(); - let clear_text = VersionBytesRef::from_slice(&clear_text)?; + let clear_text = VersionBytesRef::deserialize(&clear_text)?; clear_text.ensure_versions(&self.supported_data_versions)?; let ops: Vec<_> = rmp_serde::from_read_ref(&clear_text)?; @@ -700,7 +700,7 @@ where let data_enc = self .cryptor - .encrypt(key.key(), &clear_text.to_vec()) + .encrypt(key.key(), &clear_text.serialize()) .await .unwrap(); diff --git a/crdt-enc/src/utils.rs b/crdt-enc/src/utils.rs index 5a357a0..36b287c 100644 --- a/crdt-enc/src/utils.rs +++ b/crdt-enc/src/utils.rs @@ -1,6 +1,5 @@ mod version_bytes; -use futures::TryFutureExt; pub use version_bytes::*; use ::anyhow::{Context, Result}; diff --git a/crdt-enc/src/utils/version_bytes.rs b/crdt-enc/src/utils/version_bytes.rs index 91319f2..513bc8e 100644 --- a/crdt-enc/src/utils/version_bytes.rs +++ b/crdt-enc/src/utils/version_bytes.rs @@ -1,6 +1,6 @@ use ::bytes::Buf; use ::serde::{Deserialize, Serialize}; -use ::std::{borrow::Cow, convert::TryFrom, fmt, io::IoSlice}; +use ::std::{borrow::Cow, fmt, io::IoSlice}; use ::uuid::Uuid; #[derive(Debug)] @@ -41,26 +41,16 @@ impl VersionBytes { } pub fn ensure_version(&self, version: Uuid) -> Result<(), VersionError> { - if self.0 != version { - Err(VersionError { - expected: vec![version], - got: self.0, - }) - } else { - Ok(()) - } + self.as_version_bytes_ref().ensure_version(version) } /// `versions` needs to be sorted! pub fn ensure_versions(&self, versions: &[Uuid]) -> Result<(), VersionError> { - if versions.binary_search(&self.0).is_err() { - Err(VersionError { - expected: versions.to_owned(), - got: self.0, - }) - } else { - Ok(()) - } + self.as_version_bytes_ref().ensure_versions(versions) + } + + pub fn into_inner(self) -> Vec { + self.1 } pub fn as_version_bytes_ref(&self) -> VersionBytesRef<'_> { @@ -71,16 +61,12 @@ impl VersionBytes { VersionBytesBuf::new(self.0, &self.1) } - pub fn from_slice(slice: &[u8]) -> Result { - TryFrom::try_from(slice) + pub fn deserialize(slice: &[u8]) -> Result { + Ok(VersionBytesRef::deserialize(slice)?.into()) } - pub fn to_vec(&self) -> Vec { - self.as_version_bytes_ref().to_vec() - } - - pub fn into_inner(self) -> Vec { - self.1 + pub fn serialize(&self) -> Vec { + self.as_version_bytes_ref().serialize() } } @@ -102,14 +88,6 @@ impl AsRef<[u8]> for VersionBytes { } } -impl TryFrom<&[u8]> for VersionBytes { - type Error = ParseError; - - fn try_from(buf: &[u8]) -> Result { - Ok(VersionBytesRef::try_from(buf)?.into()) - } -} - #[derive(Serialize, Deserialize, Debug, Clone)] pub struct VersionBytesRef<'a>( Uuid, @@ -154,11 +132,19 @@ impl<'a> VersionBytesRef<'a> { VersionBytesBuf::new(self.0, &self.1) } - pub fn from_slice(slice: &'a [u8]) -> Result, ParseError> { - TryFrom::try_from(slice) + pub fn deserialize(slice: &'a [u8]) -> Result, ParseError> { + if slice.len() < VERSION_LEN { + return Err(ParseError::InvalidLength); + } + + let mut version = [0; 16]; + version.copy_from_slice(&slice[0..16]); + let version = Uuid::from_bytes(version); + + Ok(VersionBytesRef::new(version, &slice[VERSION_LEN..])) } - pub fn to_vec(&self) -> Vec { + pub fn serialize(&self) -> Vec { let mut buf = self.buf(); let mut vec = Vec::with_capacity(buf.remaining()); while buf.has_remaining() { @@ -183,22 +169,6 @@ impl<'a> From<&'a VersionBytes> for VersionBytesRef<'a> { } } -impl<'a> TryFrom<&'a [u8]> for VersionBytesRef<'a> { - type Error = ParseError; - - fn try_from(buf: &'a [u8]) -> Result, ParseError> { - if buf.len() < VERSION_LEN { - return Err(ParseError::InvalidLength); - } - - let mut version = [0; 16]; - version.copy_from_slice(&buf[0..16]); - let version = Uuid::from_bytes(version); - - Ok(VersionBytesRef::new(version, &buf[VERSION_LEN..])) - } -} - #[derive(Debug)] #[non_exhaustive] pub enum ParseError {