needed.rs
1 use std::fmt; 2 3 #[macro_export] 4 macro_rules! panic_red { 5 ($($arg:tt)*) => {{ 6 eprintln!("\x1b[31;1m{}\x1b[0m", format!($($arg)*)); 7 std::process::exit(1); 8 }}; 9 } 10 11 #[macro_export] 12 macro_rules! eprintln_red { 13 ($($arg:tt)*) => {{ 14 eprintln!("\x1b[31;1m{}\x1b[0m", format!($($arg)*)); 15 }}; 16 } 17 18 #[macro_export] 19 macro_rules! println_green { 20 ($($arg:tt)*) => {{ 21 println!("\x1b[32m{}\x1b[0m", format!($($arg)*)); 22 }}; 23 } 24 25 #[macro_export] 26 macro_rules! println_cyan { 27 ($($arg:tt)*) => {{ 28 println!("\x1b[36;1m{}\x1b[0m", format!($($arg)*)); 29 }}; 30 } 31 32 33 // error types here, might make it an individual file if I need more 34 35 // Implement da error type 36 #[derive(Debug)] 37 pub(crate) enum Polar<T> { 38 Some(T), 39 Silly(u16), 40 } 41 42 // Implement a display for printing 43 impl<T: fmt::Debug> fmt::Display for Polar<T> { 44 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 45 match self { 46 Polar::Some(value) => write!(f, "Some({:?})", value), 47 Polar::Silly(value) => write!(f, "Silly({})", value), 48 } 49 } 50 } 51 52 // Implement the functions 53 impl<T: Default> Polar<T> { 54 pub(crate) fn is_some(&self) -> bool { 55 matches!(self, Polar::Some(_)) 56 } 57 58 pub(crate) fn is_silly(&self) -> bool { 59 matches!(self, Polar::Silly(_)) 60 } 61 62 // normal unwrap is low-key silly 63 pub(crate) fn unwrap_or(self, default: T) -> T { 64 match self { 65 Polar::Some(value) => value, 66 Polar::Silly(_) => default, 67 } 68 } 69 70 pub(crate) fn unwrap_or_default(self) -> T { 71 match self { 72 Polar::Some(value) => value, 73 Polar::Silly(_) => T::default(), 74 } 75 } 76 } 77 78 79 //pub(crate) trait PolarUnwrap<T> { 80 // fn unwrap_or_return(self, error_code: u16) -> Polar<T>; 81 //} 82 // 83 //impl<T> PolarUnwrap<T> for Option<T> { 84 // fn unwrap_or_return(self, error_code: u16) -> Polar<T> { 85 // match self { 86 // Some(value) => Polar::Some(value), 87 // None => return Polar::Silly(error_code), 88 // } 89 // } 90 //} 91 92 //impl<T, E> From<Result<T, E>> for Polar<T> 93 //{ 94 // fn from(result: Result<T, E>) -> Self { 95 // match result { 96 // Ok(value) => Polar::Some(value), 97 // Err(e) => Polar::Silly(16), // Convert error to u16 98 // } 99 // } 100 //}