Skip to content

Commit

Permalink
Infrastructure: trace! macro and bug!(), .bug(), .bug_msg() errors
Browse files Browse the repository at this point in the history
  • Loading branch information
justinpombrio committed Mar 7, 2024
1 parent 433c3e7 commit 707a742
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 8 deletions.
18 changes: 11 additions & 7 deletions Cargo.toml
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"]
88 changes: 88 additions & 0 deletions src/infra.rs
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;

Check failure on line 19 in src/infra.rs

View workflow job for this annotation

GitHub Actions / build

methods `bug` and `bug_msg` are never used
/// 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 ),*)
)
)
};
}
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod infra;

fn main() {
unimplemented!();
bug!("not yet implemented");
}

0 comments on commit 707a742

Please sign in to comment.