From 92454870c89914c0e5eb9ab5681f576cd2391ef5 Mon Sep 17 00:00:00 2001 From: jiacai2050 Date: Wed, 18 Dec 2024 11:02:12 +0800 Subject: [PATCH] remove unwrap --- src/metric_engine/src/manifest/encoding.rs | 34 +++++++++++++++------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/metric_engine/src/manifest/encoding.rs b/src/metric_engine/src/manifest/encoding.rs index 5946b27466..72d43d6e96 100644 --- a/src/metric_engine/src/manifest/encoding.rs +++ b/src/metric_engine/src/manifest/encoding.rs @@ -107,14 +107,18 @@ impl TryFrom<&[u8]> for SnapshotHeader { ); let mut cursor = Cursor::new(bytes); - let magic = cursor.read_u32::().unwrap(); + let magic = cursor + .read_u32::() + .context("read snapshot header magic")?; ensure!( magic == SnapshotHeader::MAGIC, "invalid bytes to convert to header." ); - let version = cursor.read_u8().unwrap(); - let flag = cursor.read_u8().unwrap(); - let length = cursor.read_u64::().unwrap(); + let version = cursor.read_u8().context("read snapshot header version")?; + let flag = cursor.read_u8().context("read snapshot header flag")?; + let length = cursor + .read_u64::() + .context("read snapshot header length")?; Ok(Self { magic, version, @@ -126,7 +130,7 @@ impl TryFrom<&[u8]> for SnapshotHeader { impl SnapshotHeader { pub const LENGTH: usize = 4 /*magic*/ + 1 /*version*/ + 1 /*flag*/ + 8 /*length*/; - pub const MAGIC: u32 = 0xCAFE_6666; + pub const MAGIC: u32 = 0xCAFE_1234; pub fn new(length: u64) -> Self { Self { @@ -223,11 +227,21 @@ impl TryFrom<&[u8]> for SnapshotRecord { ); let mut cursor = Cursor::new(value); - let id = cursor.read_u64::().unwrap(); - let start = cursor.read_i64::().unwrap(); - let end = cursor.read_i64::().unwrap(); - let size = cursor.read_u32::().unwrap(); - let num_rows = cursor.read_u32::().unwrap(); + let id = cursor + .read_u64::() + .context("read record id")?; + let start = cursor + .read_i64::() + .context("read record start")?; + let end = cursor + .read_i64::() + .context("read record end")?; + let size = cursor + .read_u32::() + .context("read record size")?; + let num_rows = cursor + .read_u32::() + .context("read record num_rows")?; Ok(SnapshotRecord { id, time_range: (start..end).into(),