Implement error type for library

This commit is contained in:
Patrick Auernig 2023-08-12 18:39:04 +02:00
parent 7bd14931a5
commit 568fd08dcb
3 changed files with 39 additions and 5 deletions

21
Cargo.lock generated
View File

@ -405,6 +405,7 @@ dependencies = [
"binrw", "binrw",
"chrono", "chrono",
"substring", "substring",
"thiserror",
"urlencoding", "urlencoding",
] ]
@ -697,6 +698,26 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "thiserror"
version = "1.0.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "611040a08a0439f8248d1990b111c95baa9c704c805fa1f62104b39655fd7f90"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "090198534930841fab3a5d1bb637cde49e339654e606195f8d9c76eeb081dc96"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.27",
]
[[package]] [[package]]
name = "time" name = "time"
version = "0.1.45" version = "0.1.45"

View File

@ -10,6 +10,7 @@ crate-type = ["cdylib", "rlib"]
[dependencies] [dependencies]
thiserror = "1.0"
binrw = "0.11" binrw = "0.11"
chrono = "0.4" chrono = "0.4"
urlencoding = "2.1" urlencoding = "2.1"

View File

@ -33,6 +33,19 @@ pub const LEVEL_NAMES: [&str; 12] = [
]; ];
pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Failed to deserialize savefile")]
DeserializationFailed(binrw::Error),
#[error("Failed to serialize savefile")]
SerializationFailed(binrw::Error),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RobeColor { pub enum RobeColor {
Red, Red,
@ -111,19 +124,18 @@ pub struct Savefile {
} }
impl Savefile { impl Savefile {
pub fn from_reader<R>(mut reader: R) -> binrw::BinResult<Self> pub fn from_reader<R>(mut reader: R) -> Result<Self>
where where
R: Read + BinReaderExt, R: Read + BinReaderExt,
{ {
// TODO: implement error type Ok(reader.read_le().map_err(Error::DeserializationFailed)?)
Ok(reader.read_le()?)
} }
pub fn write<W>(&self, mut writer: W) -> binrw::BinResult<()> pub fn write<W>(&self, mut writer: W) -> Result<()>
where where
W: Write + BinWriterExt, W: Write + BinWriterExt,
{ {
writer.write_le(self)?; writer.write_le(self).map_err(Error::SerializationFailed)?;
Ok(()) Ok(())
} }