use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ErrorKind {
Input,
Io,
Unsupported,
Limit,
Security,
Usage,
State,
Cancelled,
Internal,
}
impl ErrorKind {
#[must_use]
pub const fn is_skippable(self) -> bool {
matches!(self, Self::Input | Self::Unsupported)
}
pub const ALL: [Self; 9] = [
Self::Input,
Self::Io,
Self::Unsupported,
Self::Limit,
Self::Security,
Self::Usage,
Self::State,
Self::Cancelled,
Self::Internal,
];
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Input => "input",
Self::Io => "io",
Self::Unsupported => "unsupported",
Self::Limit => "limit",
Self::Security => "security",
Self::Usage => "usage",
Self::State => "state",
Self::Cancelled => "cancelled",
Self::Internal => "internal",
}
}
}
impl fmt::Display for ErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Position {
FileOffset(u64),
Object(u32),
InStream {
object: u32,
offset: u64,
},
}
impl Position {
#[must_use]
pub fn offset(offset: usize) -> Self {
Self::FileOffset(u64::try_from(offset).unwrap_or(u64::MAX))
}
#[must_use]
pub const fn shifted(self, base: u64) -> Self {
match self {
Self::FileOffset(offset) => Self::FileOffset(offset.saturating_add(base)),
other => other,
}
}
#[must_use]
pub const fn in_stream(self, object: u32) -> Self {
match self {
Self::FileOffset(offset) => Self::InStream { object, offset },
other => other,
}
}
}
impl fmt::Display for Position {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Self::FileOffset(offset) => write!(f, "byte {offset}"),
Self::Object(object) => write!(f, "object {object}"),
Self::InStream { object, offset } => write!(f, "byte {offset} of object {object}'s stream data"),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Location {
pub offset: Option<u64>,
pub object: Option<u32>,
pub stream_offset: Option<u64>,
pub page_index: Option<u64>,
pub id: Option<String>,
}
impl Location {
pub const NONE: Self = Self {
offset: None,
object: None,
stream_offset: None,
page_index: None,
id: None,
};
#[must_use]
pub const fn object(object: Option<u32>) -> Self {
Self { object, ..Self::NONE }
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.offset.is_none()
&& self.object.is_none()
&& self.stream_offset.is_none()
&& self.page_index.is_none()
&& self.id.is_none()
}
}
impl Default for Location {
fn default() -> Self {
Self::NONE
}
}
impl From<Position> for Location {
fn from(position: Position) -> Self {
match position {
Position::FileOffset(offset) => Self {
offset: Some(offset),
..Self::NONE
},
Position::Object(object) => Self::object(Some(object)),
Position::InStream { object, offset } => Self {
object: Some(object),
stream_offset: Some(offset),
..Self::NONE
},
}
}
}
impl From<Option<Position>> for Location {
fn from(position: Option<Position>) -> Self {
position.map_or(Self::NONE, Self::from)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn kind_names_round_trip_through_the_table_order() {
let names: Vec<_> = ErrorKind::ALL.iter().map(|k| k.as_str()).collect();
assert_eq!(
names,
[
"input",
"io",
"unsupported",
"limit",
"security",
"usage",
"state",
"cancelled",
"internal"
]
);
}
#[test]
fn shifted_moves_only_file_offsets() {
let moved = [Position::FileOffset(3), Position::Object(3)].map(|p| p.shifted(10));
assert_eq!(moved, [Position::FileOffset(13), Position::Object(3)]);
}
#[test]
fn in_stream_retags_a_file_offset() {
assert_eq!(
Position::FileOffset(4).in_stream(7),
Position::InStream { object: 7, offset: 4 }
);
}
#[test]
fn location_from_in_stream_names_the_object_and_the_stream_offset() {
let location = Location::from(Position::InStream { object: 7, offset: 4 });
assert_eq!(
(location.object, location.stream_offset, location.offset),
(Some(7), Some(4), None)
);
}
}