1
8
Fork
You've already forked miette
0

Is there a reason that Report doesn't implement Error and Diagnostic? #366

Open
opened 2024年04月24日 00:25:00 +02:00 by TheLostLambda · 2 comments
TheLostLambda commented 2024年04月24日 00:25:00 +02:00 (Migrated from github.com)
Copy link

I've maybe not done enough thinking on this yet, but it would seem that https://docs.rs/miette/latest/miette/struct.Error.html can be converted to all sorts of Error or Diagnostic trait objects, but doesn't implement either of those traits itself?

I was wondering if that was done intentionally, or if that might be another helpful PR?

Perhaps in addition to that, is there a reason there isn't a blanket impl for Box<T> where T: Diagnostic?

I've got this kinda hacky code, and I'll be adding an impl to manually deal with Report, but ideally I wouldn't need this intermediate trait?

pubtraitUnwrapDiagnostic<E>{fn unwrap_diagnostic(self)-> E;}impl<T: Debug,E: Diagnostic>UnwrapDiagnostic<E>forResult<T,E>{fn unwrap_diagnostic(self)-> E{self.unwrap_err()}}impl<T: Debug,E: Diagnostic>UnwrapDiagnostic<E>forResult<T,Box<E>>{fn unwrap_diagnostic(self)-> E{*self.unwrap_err()}}macro_rules!assert_miette_snapshot{($diag:expr)=>{{useinsta::{with_settings,assert_snapshot};usemiette::{GraphicalReportHandler,GraphicalTheme};usecrate::testing_tools::UnwrapDiagnostic;letmutout=String::new();GraphicalReportHandler::new_themed(GraphicalTheme::unicode_nocolor()).with_width(80).render_report(&mutout,&$diag.unwrap_diagnostic()).unwrap();with_settings!({description=>stringify!($diag)},{assert_snapshot!(out);});}};}

My manual impl for Report also isn't working, I think because that Box<T> blanket impl is missing... This code:

impl<T: Debug>UnwrapDiagnostic<Box<dynDiagnostic>>forResult<T,Report>{fn unwrap_diagnostic(self)-> Box<dynDiagnostic>{self.unwrap_err().into()}}

returns this error, once I try to pass it to .report_error():

image

Let me know if I'm crazy, or if adding those impls would help ergonomics!

I've maybe not done enough thinking on this yet, but it would seem that https://docs.rs/miette/latest/miette/struct.Error.html can be converted to all sorts of `Error` or `Diagnostic` trait objects, but doesn't implement either of those traits itself? I was wondering if that was done intentionally, or if that might be another helpful PR? Perhaps in addition to that, is there a reason there isn't a blanket impl for `Box<T>` where `T: Diagnostic`? I've got this kinda hacky code, and I'll be adding an impl to manually deal with `Report`, but ideally I wouldn't need this intermediate trait? ```rust pub trait UnwrapDiagnostic<E> { fn unwrap_diagnostic(self) -> E; } impl<T: Debug, E: Diagnostic> UnwrapDiagnostic<E> for Result<T, E> { fn unwrap_diagnostic(self) -> E { self.unwrap_err() } } impl<T: Debug, E: Diagnostic> UnwrapDiagnostic<E> for Result<T, Box<E>> { fn unwrap_diagnostic(self) -> E { *self.unwrap_err() } } macro_rules! assert_miette_snapshot { ($diag:expr) => {{ use insta::{with_settings, assert_snapshot}; use miette::{GraphicalReportHandler, GraphicalTheme}; use crate::testing_tools::UnwrapDiagnostic; let mut out = String::new(); GraphicalReportHandler::new_themed(GraphicalTheme::unicode_nocolor()) .with_width(80) .render_report(&mut out, &$diag.unwrap_diagnostic()) .unwrap(); with_settings!({ description => stringify!($diag) }, { assert_snapshot!(out); }); }}; } ``` My manual impl for `Report` also isn't working, I think because that `Box<T>` blanket impl is missing... This code: ```rust impl<T: Debug> UnwrapDiagnostic<Box<dyn Diagnostic>> for Result<T, Report> { fn unwrap_diagnostic(self) -> Box<dyn Diagnostic> { self.unwrap_err().into() } } ``` returns this error, once I try to pass it to `.report_error()`: ![image](https://github.com/zkat/miette/assets/6251883/0a7dfd32-762e-45ce-a1a8-992a35ed3df2) Let me know if I'm crazy, or if adding those impls would help ergonomics!
Owner
Copy link

Same reason anyhow::Error doesn't implement it: the blanket impl in std for impl From<T> for T

Same reason `anyhow::Error` doesn't implement it: the blanket impl in std for `impl From<T> for T`
TheLostLambda commented 2024年04月24日 04:53:12 +02:00 (Migrated from github.com)
Copy link

Haha, a good answer, and a very very painfully correct one: #368

Even if we give up on having a From impl, there appear to be a lot more dragons lurking...

Haha, a good answer, and a very very painfully correct one: #368 Even if we give up on having a `From` impl, there appear to be a *lot* more dragons lurking...
Sign in to join this conversation.
No Branch/Tag specified
main
zkat/typed-report
v7.6.0
miette-derive-v7.6.0
v7.4.0
miette-derive-v7.4.0
v7.3.0
miette-derive-v7.3.0
v7.2.0
miette-derive-v7.2.0
miette-derive-v7.1.0
v7.1.0
miette-derive-v7.0.0
v7.0.0
miette-derive-v6.0.1
v6.0.1
miette-derive-v6.0.0
v6.0.0
miette-derive-v5.10.0
v5.10.0
miette-derive-v5.9.0
v5.9.0
miette-derive-v5.8.0
v5.8.0
miette-derive-v5.7.0
v5.7.0
miette-derive-v5.6.0
v5.6.0
miette-derive-v5.5.0
v5.5.0
miette-derive-v5.4.1
v5.4.1
miette-derive-v5.4.0
v5.4.0
miette-derive-v5.3.1
v5.3.1
miette-derive-v5.3.0
v5.3.0
v5.2.0
miette-derive-v5.2.0
miette-derive-v5.1.1
v5.1.1
miette-derive-v5.1.0
v5.1.0
miette-derive-v5.0.0
v5.0.0
miette-derive-v4.7.1
v4.7.1
miette-derive-v4.7.0
v4.7.0
miette-derive-v4.6.0
v4.6.0
miette-derive-v4.5.0
v4.5.0
miette-derive-v4.4.0
v4.4.0
miette-derive-v4.3.0
v4.3.0
v4.2.1
v4.2.0
v4.1.0
v4.0.1
miette-derive-v4.0.0
v4.0.0
miette-derive-v3.3.0
v3.3.0
miette-derive-v3.2.0
v3.2.0
v3.1.0
v3.0.1
v3.0.0
v3.0.0-beta.0
v3.0.0-alpha.0
miette-derive-v2.2.0
v2.2.0
miette-derive-v2.1.2
v2.1.2
v2.1.1
v2.1.0
v2.0.0
miette-derive-v2.0.0
v1.1.0
1.0.1
1.0.0
1.0.0-beta.1
0.13.0
0.12.0
0.11.0
0.10.0
0.9.0
0.8.1
0.8.0
0.7.0
0.6.0
0.5.0
0.4.0
0.3.1
0.3.0
0.2.1
0.2.0
0.1.0
Milestone
Clear milestone
No items
No milestone
Projects
Clear projects
No items
No project
Assignees
Clear assignees
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
zkat/miette#366
Reference in a new issue
zkat/miette
No description provided.
Delete branch "%!s()"

Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?