Implement error type for library
This commit is contained in:
parent
7bd14931a5
commit
568fd08dcb
21
Cargo.lock
generated
21
Cargo.lock
generated
@ -405,6 +405,7 @@ dependencies = [
|
||||
"binrw",
|
||||
"chrono",
|
||||
"substring",
|
||||
"thiserror",
|
||||
"urlencoding",
|
||||
]
|
||||
|
||||
@ -697,6 +698,26 @@ dependencies = [
|
||||
"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]]
|
||||
name = "time"
|
||||
version = "0.1.45"
|
||||
|
@ -10,6 +10,7 @@ crate-type = ["cdylib", "rlib"]
|
||||
|
||||
|
||||
[dependencies]
|
||||
thiserror = "1.0"
|
||||
binrw = "0.11"
|
||||
chrono = "0.4"
|
||||
urlencoding = "2.1"
|
||||
|
@ -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)]
|
||||
pub enum RobeColor {
|
||||
Red,
|
||||
@ -111,19 +124,18 @@ pub struct 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
|
||||
R: Read + BinReaderExt,
|
||||
{
|
||||
// TODO: implement error type
|
||||
Ok(reader.read_le()?)
|
||||
Ok(reader.read_le().map_err(Error::DeserializationFailed)?)
|
||||
}
|
||||
|
||||
pub fn write<W>(&self, mut writer: W) -> binrw::BinResult<()>
|
||||
pub fn write<W>(&self, mut writer: W) -> Result<()>
|
||||
where
|
||||
W: Write + BinWriterExt,
|
||||
{
|
||||
writer.write_le(self)?;
|
||||
writer.write_le(self).map_err(Error::SerializationFailed)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user