tamer: diagnose::panic::DiagnosticOptionPanic: New panic
This is just intended to simplify the job of panicing when something is expected to be `None`. In my case, `Lookahead`; see upcoming commits. This is intended to be generalized to more than just `Option`, but I have no use for it elsewhere yet; I primarily just needed to implement a method on `Option` so that I could have the ergonomics of the dot notation. DEV-13156main
parent
42618c5add
commit
1ce36225f6
|
@ -258,3 +258,44 @@ impl<T> DiagnosticPanic for Option<T> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Convenience methods for [`Option`]-like data.
|
||||
///
|
||||
/// See also [`DiagnosticPanic`].
|
||||
pub trait DiagnosticOptionPanic<T> {
|
||||
/// Expect that a value is [`None`]-like,
|
||||
/// otherwise panic with diagnostic information generated from inner
|
||||
/// [`Some`]-like value.
|
||||
fn diagnostic_expect_none<'a>(
|
||||
self,
|
||||
fdesc: impl FnOnce(T) -> Vec<AnnotatedSpan<'a>>,
|
||||
msg: &str,
|
||||
);
|
||||
}
|
||||
|
||||
impl<T> DiagnosticOptionPanic<T> for Option<T> {
|
||||
fn diagnostic_expect_none<'a>(
|
||||
self,
|
||||
fdesc: impl FnOnce(T) -> Vec<AnnotatedSpan<'a>>,
|
||||
msg: &str,
|
||||
) {
|
||||
match self {
|
||||
None => (),
|
||||
Some(x) => diagnostic_panic!(fdesc(x), "{}", msg),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::DiagnosticOptionPanic;
|
||||
|
||||
#[should_panic = "__expected_panic__"]
|
||||
#[test]
|
||||
fn panic_on_none() {
|
||||
struct UniqFoo;
|
||||
|
||||
Some(UniqFoo)
|
||||
.diagnostic_expect_none(|_: UniqFoo| vec![], "__expected_panic__")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue