From 7640e50df90bde409d41ba3006ffbcbf118556ba Mon Sep 17 00:00:00 2001 From: marvin-j97 Date: Thu, 29 Aug 2024 02:58:04 +0200 Subject: [PATCH] change version format --- src/version.rs | 28 +++++++++++----------------- test_fixture/v1_vlog/.vlog | Bin 5 -> 4 bytes test_fixture/v1_vlog_corrupt/.vlog | Bin 5 -> 4 bytes 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/version.rs b/src/version.rs index d0e9108..a9cdf2d 100644 --- a/src/version.rs +++ b/src/version.rs @@ -2,7 +2,7 @@ // This source code is licensed under both the Apache 2.0 and MIT License // (found in the LICENSE-* files in the repository) -use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}; +use byteorder::WriteBytesExt; /// Disk format version #[derive(Copy, Clone, Debug, Eq, PartialEq)] @@ -13,11 +13,11 @@ pub enum Version { impl std::fmt::Display for Version { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", u16::from(*self)) + write!(f, "{}", u8::from(*self)) } } -impl From for u16 { +impl From for u8 { fn from(value: Version) -> Self { match value { Version::V1 => 1, @@ -25,9 +25,9 @@ impl From for u16 { } } -impl TryFrom for Version { +impl TryFrom for Version { type Error = (); - fn try_from(value: u16) -> Result { + fn try_from(value: u8) -> Result { match value { 1 => Ok(Self::V1), _ => Err(()), @@ -48,14 +48,8 @@ impl Version { let first_three = bytes.get(0..3)?; if first_three == MAGIC_BYTES { - let next_two = bytes.get(3..5)?; - - let mut bytes = [0; 2]; - bytes.copy_from_slice(next_two); - let mut bytes: &[u8] = &bytes; - - let value = bytes.read_u16::().ok()?; - let version = Self::try_from(value).ok()?; + let version = *bytes.get(3)?; + let version = Self::try_from(version).ok()?; Some(version) } else { @@ -68,7 +62,7 @@ impl Version { writer: &mut W, ) -> std::io::Result { writer.write_all(&MAGIC_BYTES)?; - writer.write_u16::(u16::from(self))?; + writer.write_u8(u8::from(self))?; Ok(5) } } @@ -83,21 +77,21 @@ mod tests { pub fn version_serialize() -> crate::Result<()> { let mut bytes = vec![]; Version::V1.write_file_header(&mut bytes)?; - assert_eq!(bytes, &[b'V', b'L', b'G', 0, 1]); + assert_eq!(bytes, &[b'V', b'L', b'G', 1]); Ok(()) } #[test] #[allow(clippy::expect_used)] pub fn version_deserialize_success() { - let version = Version::parse_file_header(&[b'V', b'L', b'G', 0, 1]); + let version = Version::parse_file_header(&[b'V', b'L', b'G', 1]); assert_eq!(version, Some(Version::V1)); } #[test] #[allow(clippy::expect_used)] pub fn version_deserialize_fail() { - let version = Version::parse_file_header(&[b'F', b'J', b'X', 0, 1]); + let version = Version::parse_file_header(&[b'F', b'J', b'X', 1]); assert!(version.is_none()); } diff --git a/test_fixture/v1_vlog/.vlog b/test_fixture/v1_vlog/.vlog index b0f7a741276772f45882c152370d4320f3115d18..43b82265428cf4fbc4b8732d2853412fbc8578bd 100644 GIT binary patch literal 4 LcmWIZac2Yo0?z>J literal 5 McmWIZac5uz00X%I>i_@% diff --git a/test_fixture/v1_vlog_corrupt/.vlog b/test_fixture/v1_vlog_corrupt/.vlog index b0f7a741276772f45882c152370d4320f3115d18..43b82265428cf4fbc4b8732d2853412fbc8578bd 100644 GIT binary patch literal 4 LcmWIZac2Yo0?z>J literal 5 McmWIZac5uz00X%I>i_@%