Gen7ScriptsRs/pkmn_lib_interface/src/result.rs

50 lines
1.0 KiB
Rust

use alloc::string::{String, ToString};
use core::fmt;
use core::fmt::{Debug, Display, Formatter};
use cstr_core::CString;
pub type PkmnResult<T> = Result<T, PkmnErr>;
pub struct PkmnErr {
pub file: &'static str,
pub line: u32,
pub message: String,
}
impl PkmnErr {
pub fn new(file: &'static str, line: u32, message: String) -> Self {
Self {
file,
line,
message,
}
}
}
impl Debug for PkmnErr {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "PkmnErr: {}:{}: {}", self.file, self.line, self.message)
}
}
impl Display for PkmnErr {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "PkmnErr: {}:{}: {}", self.file, self.line, self.message)
}
}
impl From<PkmnErr> for CString {
fn from(val: PkmnErr) -> Self {
CString::new(val.to_string()).unwrap()
}
}
#[macro_export]
macro_rules! pkmn_err {
($message: expr) => {
PkmnErr::new(file!(), line!(), $message.to_string())
};
}
pub use pkmn_err;