From d3e114c7b6a3607b1e05bff9e9217d0dc67d33f3 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Thu, 25 Jul 2024 17:47:00 -0700 Subject: [PATCH] implement error traits --- src/bot.rs | 3 ++- src/demo.rs | 3 ++- src/file.rs | 12 ++++++++++-- src/lib.rs | 7 +++++++ src/map.rs | 7 +++++++ 5 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/bot.rs b/src/bot.rs index d531015..ffdaee8 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -1,5 +1,6 @@ use binrw::{BinReaderExt, binrw}; +#[derive(Debug)] pub enum Error{ InvalidHeader, InvalidSegment(binrw::Error), @@ -64,7 +65,7 @@ mod simulation{ #[binrw] #[brw(little)] -#[derive(Clone,Copy,id::Id)] +#[derive(Clone,Copy,Debug,id::Id)] pub struct SegmentId(u32); #[binrw] diff --git a/src/demo.rs b/src/demo.rs index 302ea5a..119290a 100644 --- a/src/demo.rs +++ b/src/demo.rs @@ -1,5 +1,6 @@ use binrw::BinReaderExt; +#[derive(Debug)] pub enum Error{ InvalidHeader, } @@ -27,4 +28,4 @@ impl StreamableDemo{ pub(crate) fn new(file:crate::file::File)->Result{ Err(Error::InvalidHeader) } -} \ No newline at end of file +} diff --git a/src/file.rs b/src/file.rs index d1fd674..91c7229 100644 --- a/src/file.rs +++ b/src/file.rs @@ -2,12 +2,19 @@ use binrw::{binrw, BinReaderExt, io::TakeSeekExt}; +#[derive(Debug)] pub enum Error{ InvalidHeader(binrw::Error), UnexpectedEOF, InvalidBlockId(BlockId), Seek(std::io::Error), } +impl std::fmt::Display for Error{ + fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{ + write!(f,"{self:?}") + } +} +impl std::error::Error for Error{} /* spec @@ -40,7 +47,7 @@ for block_id in 1..num_blocks{ */ #[binrw] #[brw(little)] -#[derive(Clone,Copy)] +#[derive(Clone,Copy,Debug)] pub(crate) enum FourCC{ #[brw(magic=b"SNFM")] Map, @@ -51,6 +58,7 @@ pub(crate) enum FourCC{ } #[binrw] #[brw(little)] +#[derive(Debug)] pub struct Header{ /// Type of file pub fourcc:FourCC, @@ -68,7 +76,7 @@ pub struct Header{ #[binrw] #[brw(little)] -#[derive(Clone,Copy,Hash,id::Id,Eq,PartialEq)] +#[derive(Clone,Copy,Debug,Hash,id::Id,Eq,PartialEq)] pub struct BlockId(u32); pub(crate) struct File{ diff --git a/src/lib.rs b/src/lib.rs index 90594ff..675dfdb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,7 @@ pub mod map; pub mod bot; pub mod demo; +#[derive(Debug)] pub enum Error{ UnexpectedFourCC, Header(file::Error), @@ -14,6 +15,12 @@ pub enum Error{ Bot(bot::Error), Demo(demo::Error), } +impl std::fmt::Display for Error{ + fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{ + write!(f,"{self:?}") + } +} +impl std::error::Error for Error{} pub enum SNF{ Map(map::StreamableMap), diff --git a/src/map.rs b/src/map.rs index 12fd577..305cabe 100644 --- a/src/map.rs +++ b/src/map.rs @@ -9,6 +9,7 @@ use strafesnet_common::aabb::Aabb; use strafesnet_common::bvh::BvhNode; use strafesnet_common::gameplay_modes; +#[derive(Debug)] pub enum Error{ InvalidHeader(binrw::Error), InvalidBlockId(BlockId), @@ -18,6 +19,12 @@ pub enum Error{ IO(std::io::Error), File(crate::file::Error), } +impl std::fmt::Display for Error{ + fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{ + write!(f,"{self:?}") + } +} +impl std::error::Error for Error{} /* block types