Remove the panics and unwraps

This commit is contained in:
Clément Renault 2023-11-23 14:47:56 +01:00
parent 0dbf1a16ff
commit 58dac8af42
No known key found for this signature in database
GPG Key ID: F250A4C4E3AE5F5F
7 changed files with 41 additions and 26 deletions

View File

@ -63,7 +63,7 @@ where
v.extend_from_slice(&value.field_id.to_be_bytes());
v.extend_from_slice(&[value.level]);
let bound = T::bytes_encode(&value.left_bound).unwrap();
let bound = T::bytes_encode(&value.left_bound)?;
v.extend_from_slice(&bound);
Ok(Cow::Owned(v))

View File

@ -12,10 +12,10 @@ impl<'a> BytesDecode<'a> for OrderedF64Codec {
fn bytes_decode(bytes: &'a [u8]) -> Result<Self::DItem, BoxedError> {
if bytes.len() < 16 {
panic!() // TODO don't panic
Err(BoxedError::from("invalid slice length"))
} else {
bytes[8..].try_into().map(f64::from_be_bytes).map_err(Into::into)
}
let f = bytes[8..].try_into().ok().map(f64::from_be_bytes).unwrap();
Ok(f)
}
}
@ -26,7 +26,8 @@ impl heed::BytesEncode<'_> for OrderedF64Codec {
let mut buffer = [0u8; 16];
// write the globally ordered float
let bytes = f64_into_bytes(*f).unwrap();
let bytes = f64_into_bytes(*f)
.ok_or_else(|| BoxedError::from("cannot generate a globally ordered float"))?;
buffer[..8].copy_from_slice(&bytes[..]);
// Then the f64 value just to be able to read it back
let bytes = f.to_be_bytes();

View File

@ -10,9 +10,11 @@ impl<'a> heed::BytesDecode<'a> for FieldIdWordCountCodec {
type DItem = (FieldId, u8);
fn bytes_decode(bytes: &'a [u8]) -> Result<Self::DItem, BoxedError> {
let (field_id_bytes, bytes) = try_split_array_at(bytes).unwrap();
let (field_id_bytes, bytes) =
try_split_array_at(bytes).ok_or("invalid slice length").map_err(BoxedError::from)?;
let field_id = u16::from_be_bytes(field_id_bytes);
let ([word_count], _nothing) = try_split_array_at(bytes).unwrap();
let ([word_count], _nothing) =
try_split_array_at(bytes).ok_or("invalid slice length").map_err(BoxedError::from)?;
Ok((field_id, word_count))
}
}

View File

@ -10,7 +10,11 @@ impl<'a> heed::BytesDecode<'a> for ScriptLanguageCodec {
type DItem = (Script, Language);
fn bytes_decode(bytes: &'a [u8]) -> Result<Self::DItem, BoxedError> {
let sep = bytes.iter().position(|b| *b == 0).unwrap();
let sep = bytes
.iter()
.position(|b| *b == 0)
.ok_or("cannot find nul byte")
.map_err(BoxedError::from)?;
let (s_bytes, l_bytes) = bytes.split_at(sep);
let script = str::from_utf8(s_bytes)?;
let script_name = Script::from_name(script);

View File

@ -14,7 +14,7 @@ impl<'a> heed::BytesDecode<'a> for StrBEU32Codec {
let footer_len = size_of::<u32>();
if bytes.len() < footer_len {
panic!() // TODO Do not panic
return Err(BoxedError::from("cannot extract footer from bytes"));
}
let (word, bytes) = bytes.split_at(bytes.len() - footer_len);
@ -48,13 +48,13 @@ impl<'a> heed::BytesDecode<'a> for StrBEU16Codec {
let footer_len = size_of::<u16>();
if bytes.len() < footer_len + 1 {
panic!() // TODO do not panic
return Err(BoxedError::from("cannot extract footer from bytes"));
}
let (word_plus_nul_byte, bytes) = bytes.split_at(bytes.len() - footer_len);
let (_, word) = word_plus_nul_byte.split_last().unwrap();
let word = str::from_utf8(word).ok().unwrap();
let pos = bytes.try_into().map(u16::from_be_bytes).ok().unwrap();
let word = str::from_utf8(word)?;
let pos = bytes.try_into().map(u16::from_be_bytes)?;
Ok((word, pos))
}

View File

@ -9,12 +9,16 @@ impl<'a> heed::BytesDecode<'a> for U8StrStrCodec {
type DItem = (u8, &'a str, &'a str);
fn bytes_decode(bytes: &'a [u8]) -> Result<Self::DItem, BoxedError> {
let (n, bytes) = bytes.split_first().unwrap();
let s1_end = bytes.iter().position(|b| *b == 0).unwrap();
let (n, bytes) = bytes.split_first().ok_or("not enough bytes").map_err(BoxedError::from)?;
let s1_end = bytes
.iter()
.position(|b| *b == 0)
.ok_or("cannot find nul byte")
.map_err(BoxedError::from)?;
let (s1_bytes, rest) = bytes.split_at(s1_end);
let s2_bytes = &rest[1..];
let s1 = str::from_utf8(s1_bytes).ok().unwrap();
let s2 = str::from_utf8(s2_bytes).ok().unwrap();
let s1 = str::from_utf8(s1_bytes)?;
let s2 = str::from_utf8(s2_bytes)?;
Ok((*n, s1, s2))
}
}
@ -37,8 +41,12 @@ impl<'a> heed::BytesDecode<'a> for UncheckedU8StrStrCodec {
type DItem = (u8, &'a [u8], &'a [u8]);
fn bytes_decode(bytes: &'a [u8]) -> Result<Self::DItem, BoxedError> {
let (n, bytes) = bytes.split_first().unwrap();
let s1_end = bytes.iter().position(|b| *b == 0).unwrap();
let (n, bytes) = bytes.split_first().ok_or("not enough bytes").map_err(BoxedError::from)?;
let s1_end = bytes
.iter()
.position(|b| *b == 0)
.ok_or("cannot find nul byte")
.map_err(BoxedError::from)?;
let (s1_bytes, rest) = bytes.split_at(s1_end);
let s2_bytes = &rest[1..];
Ok((*n, s1_bytes, s2_bytes))

View File

@ -64,13 +64,13 @@ impl<'ctx> DatabaseCache<'ctx> {
match cache.get(&cache_key).unwrap() {
Some(Cow::Borrowed(bytes)) => DC::bytes_decode_owned(bytes)
.map(Some)
.map_err(heed::Error::Decoding)
.map_err(Into::into)
.map(Some),
.map_err(Into::into),
Some(Cow::Owned(bytes)) => DC::bytes_decode_owned(bytes)
.map(Some)
.map_err(heed::Error::Decoding)
.map_err(Into::into)
.map(Some),
.map_err(Into::into),
None => Ok(None),
}
}
@ -113,13 +113,13 @@ impl<'ctx> DatabaseCache<'ctx> {
match cache.get(&cache_key).unwrap() {
Some(Cow::Borrowed(bytes)) => DC::bytes_decode_owned(bytes)
.map(Some)
.map_err(heed::Error::Decoding)
.map_err(Into::into)
.map(Some),
.map_err(Into::into),
Some(Cow::Owned(bytes)) => DC::bytes_decode_owned(bytes)
.map(Some)
.map_err(heed::Error::Decoding)
.map_err(Into::into)
.map(Some),
.map_err(Into::into),
None => Ok(None),
}
}