diff options
| author | Thomas Voss <mail@thomasvoss.com> | 2026-02-24 11:08:42 +0100 |
|---|---|---|
| committer | Thomas Voss <mail@thomasvoss.com> | 2026-02-24 11:08:42 +0100 |
| commit | 7d42170c8625de0fe44b98f47e8b9a603a9de794 (patch) | |
| tree | 86623e0e0809d23ee3dd861ad7006ff21672e455 /oryxc/src/errors.rs | |
Genesis commit
Diffstat (limited to 'oryxc/src/errors.rs')
| -rw-r--r-- | oryxc/src/errors.rs | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/oryxc/src/errors.rs b/oryxc/src/errors.rs new file mode 100644 index 0000000..b3e6013 --- /dev/null +++ b/oryxc/src/errors.rs @@ -0,0 +1,67 @@ +use std::ffi::{ + OsStr, + OsString, +}; +use std::fmt::Display; +use std::ops::Deref; +use std::path::Path; +use std::sync::OnceLock; +use std::{ + env, + process, +}; + +pub fn progname() -> &'static OsString { + static ARGV0: OnceLock<OsString> = OnceLock::new(); + return ARGV0.get_or_init(|| { + let default = OsStr::new("oryxc"); + let s = env::args_os().next().unwrap_or(default.into()); + return Path::new(&s).file_name().unwrap_or(default).to_os_string(); + }); +} + +#[macro_export] +macro_rules! warn { + ($err:expr, $fmt:literal, $($arg:tt)*) => {{ + use crate::errors::progname; + let _ = eprintln!("{}: {}: {}", progname().display(), + format_args!($fmt, $($arg)*), $err); + }}; + + ($err:expr, $fmt:literal) => {{ + warn!($err, $fmt,); + }}; + + ($err:expr) => {{ + use crate::errors::progname; + let _ = eprintln!("{}: {}", progname().display(), $err); + }}; +} + +#[macro_export] +macro_rules! err { + ($err:expr, $fmt:literal, $($arg:tt)*) => {{ + use crate::warn; + warn!($err, $fmt, $($arg)*); + std::process::exit(1); + }}; + + ($err:expr, $fmt:literal) => {{ + err!($err, $fmt,); + }}; + + ($err:expr) => {{ + use crate::warn; + warn!($err); + std::process::exit(1); + }}; +} + +pub fn err_at_position<T, S>(filename: T, s: S) -> ! +where + T: Deref<Target = OsStr>, + S: Display, +{ + eprintln!("{}: \x1b[31;1mError:\x1b[0m {}", filename.display(), s); + process::exit(1); +} |