1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
mod ty; pub use crate::ty::Lovm2ErrorTy; pub type Lovm2Result<T> = Result<T, Lovm2Error>; pub type Lovm2CompileResult<T> = Result<T, Lovm2CompileError>; #[derive(Debug)] pub struct Lovm2Error { pub ty: Lovm2ErrorTy, pub msg: String, pub trace: backtrace::Backtrace, } impl From<Lovm2ErrorTy> for Lovm2Error { fn from(ty: Lovm2ErrorTy) -> Self { Self { ty, ..Self::default() } } } impl <T> From<(Lovm2ErrorTy, T)> for Lovm2Error where T: AsRef<str> { fn from(descr: (Lovm2ErrorTy, T)) -> Self { Self { ty: descr.0, msg: descr.1.as_ref().to_string(), ..Self::default() } } } impl From<(String, String)> for Lovm2Error { fn from(f: (String, String)) -> Self { Self { ty: Lovm2ErrorTy::Custom(f.0), msg: f.1, ..Self::default() } } } impl From<String> for Lovm2Error { fn from(f: String) -> Self { Self { msg: f, ..Self::default() } } } impl From<&str> for Lovm2Error { fn from(f: &str) -> Self { Self { msg: f.to_string(), ..Self::default() } } } impl std::fmt::Display for Lovm2Error { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { writeln!(f, "{}: {}", self.ty, self.msg)?; writeln!(f, "{:?}", self.trace)?; Ok(()) } } impl Default for Lovm2Error { fn default() -> Self { Self { ty: Lovm2ErrorTy::Basic, msg: String::new(), trace: backtrace::Backtrace::new(), } } } #[derive(Debug)] pub enum Lovm2CompileError { Msg(Option<String>, String), } impl From<String> for Lovm2CompileError { fn from(f: String) -> Self { Self::Msg(None, f) } } impl From<&str> for Lovm2CompileError { fn from(f: &str) -> Self { Self::Msg(None, f.to_string()) } } impl std::fmt::Display for Lovm2CompileError { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { Self::Msg(Some(ty), msg) => write!(f, "{}: {}", ty, msg), Self::Msg(None, msg) => write!(f, "{}", msg), } } }