miette icon indicating copy to clipboard operation
miette copied to clipboard

adding gettext support

Open saihaze opened this issue 2 years ago • 5 comments

Only string literals are accepted, which is not friendly to gettext.

saihaze avatar Jul 28 '22 14:07 saihaze

I'd love to hear more about where this can be improved. I thought most strings could be dynamic by using interpolation, but maybe there's patterns I'm missing? I'm happy to discuss designs and review PRs for this but I don't think I'd do a good job of it on my own.

zkat avatar Jul 28 '22 16:07 zkat

For instance, in this example, the label message cannot be dynamic.

#[derive(Error, Debug, Diagnostic)]
#[error("oops!")]
#[diagnostic(
    code(oops::my::bad),
    url(docsrs),
    help("try doing it better next time?")
)]
struct MyBad {
    // The Source that we're gonna be printing snippets out of.
    // This can be a String if you don't have or care about file names.
    #[source_code]
    src: NamedSource,
    // Snippets and highlights can be included in the diagnostic!
    #[label("This bit here")]
    bad_bit: SourceSpan,
}

When I change it, I get an error complaining that it can only be a string literal.

saihaze avatar Jul 28 '22 23:07 saihaze

You should manually implement Diagnostic in this case I guess?

abiriadev avatar Oct 30 '23 03:10 abiriadev

It would be great if the macro could automatically support LabeledSpan, as demonstrated below:

#[derive(Error, Debug, Diagnostic)]
#[error("oops!")]
#[diagnostic(
    code(oops::my::bad),
    url(docsrs),
    help("try doing it better next time?")
)]
struct MyBad {
    // The Source that we're gonna be printing snippets out of.
    // This can be a String if you don't have or care about file names.
    #[source_code]
    src: NamedSource,
    // Snippets and highlights can be included in the diagnostic!
    #[label]
    bad_bit: LabeledSpan, // <- dynamically generated label content
}

would generate something like

impl Diagnostic for RuntimeError {
	// ...
	
	fn labels(
		&self,
	) -> Option<Box<dyn Iterator<Item = miette::LabeledSpan> + '_>> {
		// ...
		Some(Box::new([self.bad_bit].into_iter())),
		// ...
	}
	
	// ...
}

This seems feasible. May I take it up?

abiriadev avatar Oct 30 '23 04:10 abiriadev

@abiriadev go for it!

zkat avatar Oct 30 '23 17:10 zkat