mindustry logic execution, map- and schematic- parsing and rendering
Diffstat (limited to 'src/data/mod.rs')
-rw-r--r--src/data/mod.rs40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/data/mod.rs b/src/data/mod.rs
index 6117cf8..e68200b 100644
--- a/src/data/mod.rs
+++ b/src/data/mod.rs
@@ -1,3 +1,5 @@
+use std::error::Error;
+use std::fmt;
use std::str::Utf8Error;
pub mod base64;
@@ -106,6 +108,30 @@ impl From<Utf8Error> for ReadError
}
}
+impl fmt::Display for ReadError
+{
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
+ {
+ match self
+ {
+ ReadError::Underflow{need, have} => write!(f, "Buffer underflow (expected {need} but got {have})"),
+ ReadError::Utf8(e) => e.fmt(f),
+ }
+ }
+}
+
+impl Error for ReadError
+{
+ fn source(&self) -> Option<&(dyn Error + 'static)>
+ {
+ match self
+ {
+ ReadError::Utf8(e) => Some(e),
+ _ => None,
+ }
+ }
+}
+
enum WriteBuff<'d>
{
// unlike the DataRead want to access the written region after
@@ -229,6 +255,20 @@ pub enum WriteError
TooLong{len: usize},
}
+impl fmt::Display for WriteError
+{
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
+ {
+ match self
+ {
+ WriteError::Overflow{need, have} => write!(f, "Buffer overflow (expected {need} but got {have})"),
+ WriteError::TooLong{len} => write!(f, "String too long ({len} bytes of {})", u16::MAX),
+ }
+ }
+}
+
+impl Error for WriteError {}
+
impl<'d> From<&'d mut [u8]> for DataWrite<'d>
{
fn from(value: &'d mut [u8]) -> Self