Implement Error for PathError
This commit is contained in:
parent
eb89155365
commit
68f70707b0
30
src/lib.rs
30
src/lib.rs
|
@ -17,7 +17,11 @@ pub struct Path {
|
|||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct PathError;
|
||||
pub enum PathError {
|
||||
Format,
|
||||
Length,
|
||||
Range,
|
||||
}
|
||||
|
||||
impl Inner {
|
||||
fn next(&mut self) -> Vec<u16> {
|
||||
|
@ -66,7 +70,7 @@ impl Path {
|
|||
let inner = bytes
|
||||
.chunks(2)
|
||||
.map(|chunk| {
|
||||
let be_bytes: [u8; 2] = chunk.try_into().map_err(|_| PathError)?;
|
||||
let be_bytes: [u8; 2] = chunk.try_into().map_err(|_| PathError::Length)?;
|
||||
Ok(u16::from_be_bytes(be_bytes))
|
||||
})
|
||||
.collect::<Result<_, PathError>>()?;
|
||||
|
@ -91,12 +95,12 @@ impl Path {
|
|||
}
|
||||
|
||||
if inner.len().saturating_sub(1) != inner[0].into() {
|
||||
return Err(PathError);
|
||||
return Err(PathError::Format);
|
||||
}
|
||||
|
||||
for elem in inner.iter() {
|
||||
if *elem > 999 {
|
||||
return Err(PathError);
|
||||
return Err(PathError::Range);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -118,3 +122,21 @@ impl Path {
|
|||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for PathError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
PathError::Format => {
|
||||
write!(f, "Invalid path format")
|
||||
}
|
||||
PathError::Length => {
|
||||
write!(f, "Invalid segment length")
|
||||
}
|
||||
PathError::Range => {
|
||||
write!(f, "Invalid segment format")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for PathError {}
|
||||
|
|
Loading…
Reference in a new issue