-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Infrastructure: trace! macro and bug!(), .bug(), .bug_msg() errors
- Loading branch information
1 parent
433c3e7
commit 707a742
Showing
3 changed files
with
102 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,16 @@ | ||
[package] | ||
name = "synless" | ||
version = "0.0.1" | ||
edition = "2021" | ||
authors = ["justinpombrio <[email protected]>", "e-matteson <[email protected]>"] | ||
|
||
[workspace] | ||
members = [ | ||
"forest", | ||
"language", | ||
"frontends", | ||
"editor", | ||
"demo"] | ||
[dependencies] | ||
generational-arena = "0.2" | ||
[dependencies.no-nonsense-flamegraphs] | ||
version = "0.2.*" | ||
git = "https://github.com/justinpombrio/no-nonsense-flamegraphs" | ||
optional = true | ||
|
||
[features] | ||
default = [] | ||
profile = ["no-nonsense-flamegraphs"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use crate::bug; | ||
use std::fmt; | ||
|
||
#[doc(hidden)] | ||
#[macro_export] | ||
/// You can add this to the top of the body of a function to include it in | ||
/// flamegraphs when `--features profile` is used. | ||
macro_rules! trace { | ||
($name:expr) => { | ||
#[cfg(feature = "profile")] | ||
no_nonsense_flamegraphs::span!($name); | ||
}; | ||
} | ||
|
||
/// Versions of `.unwrap()` and `.expect()` that print a Synless-specific | ||
/// error message, and format errors with Display instead of Debug. | ||
pub trait SynlessBug<T>: Sized { | ||
/// Like `.unwrap()`, but with a better error message. | ||
fn bug(self) -> T; | ||
/// Like `.expect()`, but with a better error message. | ||
fn bug_msg(self, msg: &str) -> T; | ||
} | ||
|
||
impl<T> SynlessBug<T> for Option<T> { | ||
fn bug(self) -> T { | ||
match self { | ||
Some(val) => val, | ||
None => bug!("Tried to unwrap a `None` value"), | ||
} | ||
} | ||
|
||
fn bug_msg(self, msg: &str) -> T { | ||
match self { | ||
Some(val) => val, | ||
None => bug!("{}", msg), | ||
} | ||
} | ||
} | ||
|
||
impl<T, E: fmt::Display> SynlessBug<T> for Result<T, E> { | ||
fn bug(self) -> T { | ||
match self { | ||
Ok(ok) => ok, | ||
Err(err) => bug!("{}", err), | ||
} | ||
} | ||
|
||
fn bug_msg(self, msg: &str) -> T { | ||
match self { | ||
Ok(ok) => ok, | ||
Err(err) => bug!("{}\n{}", msg, err), | ||
} | ||
} | ||
} | ||
|
||
#[track_caller] | ||
pub(crate) fn format_bug(location: String, message: String) -> String { | ||
let mut output = "\n*** Bug in Synless.".to_owned(); | ||
output | ||
.push_str("\n*** Please open an issue at https://github.com/justinpombrio/synless/issues."); | ||
output.push_str("\n*** Location:"); | ||
output.push_str("\n*** "); | ||
output.push_str(&location); | ||
output.push_str("\n*** Error message:"); | ||
for line in message.lines() { | ||
output.push_str("\n*** "); | ||
output.push_str(line); | ||
} | ||
output.push('\n'); | ||
output | ||
} | ||
|
||
#[doc(hidden)] | ||
#[macro_export] | ||
/// Like `panic!()`, but with a better error message. | ||
macro_rules! bug { | ||
($message:literal) => { | ||
bug!($message,) | ||
}; | ||
($message:literal, $( $arg:expr ),*) => { | ||
panic!("{}", | ||
$crate::infra::format_bug( | ||
format!("{}:{}:{}", file!(), line!(), column!()), | ||
format!($message, $( $arg ),*) | ||
) | ||
) | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
mod infra; | ||
|
||
fn main() { | ||
unimplemented!(); | ||
bug!("not yet implemented"); | ||
} |