Open mina86 opened 1 month ago
clap's derive builder provides a pretty flexible pattern for this approach:
#[derive(Parser, Debug, Clone)]
#[command(name = "pretty-print-file")]
pub struct MyPrinter {
#[arg(short, long, value_parser = |p: &str| PrettyFile::from_path(p))]
pub file_to_print: Option<PrettyFile>,
}
In that same way, a closure could be passed in so that the message (or possibly Self
) can be changed:
#[derive(derive_more::TryInto)]
#[try_into(error = |msg| MyError(format!("try_into: {msg}")))]
pub enum Blah {
Foo(third_party::Foo),
Bar(third_party::Bar),
Baz(third_party::Baz),
}
I think this is very reasonable. I think the best solution would contain the following three things:
From
implementation for TryIntoError
.TryIntoError
.output_type
and variant_names
public members of TryIntoError
. I think we should probably make variant_names
an &'static []
in that case in that case. PRs for this feature are welcome (to be clear, any of the three features I described above features could be contributed separately in a PR).
I’m working with a third-party crate which defines a
ClientError
type and aFrom<&'static str>
trait for that type. Further, the crate has a generic method with the following two bounds:T: TryInto<Foo>
andT::Error: Into<ClientError>
. Lastly, in my code I have an enum whose one of the variant carriesFoo
value and usesderive_more::TryInto
to define the requiredTryInto<Foo>
implementation.With derive_more 0.99 this worked fine. derive_more created the implementation with
&'static str
error which satisfiesInto<ClientError>
bound.Sadly, with derive_more 1.0 the conversion error is now
TryIntoError
which cannot be converted intoClientError
and because both of those types are third-party I cannot add the implementation.I wonder if it would be possible to add a customisable error. For example I could then do:
and then define all necessary conversions on
MyError
. Even more flexible if I could define error type and constructor separately.This actually is something I’ve suggested a while back, see https://github.com/JelteF/derive_more/issues/315, and back then I concluded that TryIntoError would work for my case. Alas, turns out not quite.
PS. variant_names and output_type be pub?