Improve handling of numbers in IntoDiagnosticArg

This commit is contained in:
Urgau 2024-01-26 20:32:55 +01:00
parent 69db514ed9
commit 304361a10c

View File

@ -58,16 +58,29 @@ fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
} }
} }
macro_rules! into_diagnostic_arg_for_number {
($( $ty:ty ),+ $(,)?) => {
$(
impl IntoDiagnosticArg for $ty {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
// HACK: `FluentNumber` the underline backing struct represent
// numbers using a f64 which can represent all the i128 numbers
// So in order to be able to use fluent selectors and still
// have all the numbers representable we only convert numbers
// below a certain threshold.
if let Ok(n) = TryInto::<i128>::try_into(self) && n >= -100 && n <= 100 {
DiagnosticArgValue::Number(n)
} else {
self.to_string().into_diagnostic_arg()
}
}
}
)+
}
}
into_diagnostic_arg_using_display!( into_diagnostic_arg_using_display!(
ast::ParamKindOrd, ast::ParamKindOrd,
i8,
u8,
i16,
u16,
u32,
i64,
i128,
u128,
std::io::Error, std::io::Error,
Box<dyn std::error::Error>, Box<dyn std::error::Error>,
std::num::NonZeroU32, std::num::NonZeroU32,
@ -82,17 +95,7 @@ fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
ExitStatus, ExitStatus,
); );
impl IntoDiagnosticArg for i32 { into_diagnostic_arg_for_number!(i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, isize, usize);
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Number(self.into())
}
}
impl IntoDiagnosticArg for u64 {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Number(self.into())
}
}
impl IntoDiagnosticArg for bool { impl IntoDiagnosticArg for bool {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> { fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
@ -154,12 +157,6 @@ fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
} }
} }
impl IntoDiagnosticArg for usize {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Number(self as i128)
}
}
impl IntoDiagnosticArg for PanicStrategy { impl IntoDiagnosticArg for PanicStrategy {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> { fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Str(Cow::Owned(self.desc().to_string())) DiagnosticArgValue::Str(Cow::Owned(self.desc().to_string()))