From f1025071edfb787309a75d4bb68bec0c2bd906bb Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sat, 24 Jun 2023 15:05:59 +0200 Subject: Tidy up --- src/diag.rs | 69 +++++++++++++++++++++++++--------------------------- src/export/render.rs | 12 ++++++--- 2 files changed, 41 insertions(+), 40 deletions(-) (limited to 'src') diff --git a/src/diag.rs b/src/diag.rs index 0f669f64..9399058a 100644 --- a/src/diag.rs +++ b/src/diag.rs @@ -82,7 +82,8 @@ pub struct SourceError { pub message: EcoString, /// The trace of function calls leading to the error. pub trace: Vec>, - /// Additonal hints to the user, indicating how this error could be avoided or worked around. + /// Additonal hints to the user, indicating how this error could be avoided + /// or worked around. pub hints: Vec, } @@ -104,6 +105,12 @@ impl SourceError { self } + /// Adds user-facing hints to the error. + pub fn with_hints(mut self, hints: impl IntoIterator) -> Self { + self.hints.extend(hints); + self + } + /// The range in the source file identified by /// [`self.span.source()`](Span::source) where the error should be /// annotated. @@ -115,12 +122,6 @@ impl SourceError { ErrorPos::End => full.end..full.end, } } - - /// Adds a user-facing hint to the error. - pub fn with_hints(mut self, hints: &mut Vec) -> Self { - self.hints.append(hints); - self - } } /// A part of an error's [trace](SourceError::trace). @@ -203,48 +204,44 @@ where } } -pub type StrWithHintResult = Result; +/// A result type with a string error message and hints. +pub type HintedStrResult = Result; -pub struct StrWithHint { - message: EcoString, - hints: Vec, +/// A string message with hints. +#[derive(Debug, Clone, Eq, PartialEq, Hash)] +pub struct HintedString { + /// A diagnostic message describing the problem. + pub message: EcoString, + /// Additonal hints to the user, indicating how this error could be avoided + /// or worked around. + pub hints: Vec, } -impl At for Result { +impl At for Result { fn at(self, span: Span) -> SourceResult { - self.map_err(|mut diags| { - Box::new(vec![ - SourceError::new(span, diags.message).with_hints(&mut diags.hints) - ]) + self.map_err(|diags| { + Box::new(vec![SourceError::new(span, diags.message).with_hints(diags.hints)]) }) } } -/// Allows adding a user-facing hint in addition to the error. -pub trait Hint -where - S: Into, -{ - fn hint(self, hint: S) -> StrWithHintResult; +/// Enrich a [`StrResult`] or [`HintedStrResult`] with a hint. +pub trait Hint { + /// Add the hint. + fn hint(self, hint: impl Into) -> HintedStrResult; } -impl Hint for StrResult -where - S: Into, -{ - fn hint(self, hint: S) -> StrWithHintResult { - self.map_err(|message| StrWithHint { message, hints: vec![hint.into()] }) +impl Hint for StrResult { + fn hint(self, hint: impl Into) -> HintedStrResult { + self.map_err(|message| HintedString { message, hints: vec![hint.into()] }) } } -impl Hint for StrWithHintResult -where - S: Into, -{ - fn hint(self, hint: S) -> StrWithHintResult { - self.map_err(|mut diags| { - diags.hints.push(hint.into()); - diags +impl Hint for HintedStrResult { + fn hint(self, hint: impl Into) -> HintedStrResult { + self.map_err(|mut error| { + error.hints.push(hint.into()); + error }) } } diff --git a/src/export/render.rs b/src/export/render.rs index 02c10e61..ef0fc9f2 100644 --- a/src/export/render.rs +++ b/src/export/render.rs @@ -488,14 +488,18 @@ fn render_image( let view_width = size.x.to_f32(); let view_height = size.y.to_f32(); - let aspect = (image.width() as f32) / (image.height() as f32); - // For better-looking output, resize `image` to its final size before painting it to `canvas`. - // See https://github.com/typst/typst/issues/1404#issuecomment-1598374652 for the math. + // For better-looking output, resize `image` to its final size before + // painting it to `canvas`. For the math, see: + // https://github.com/typst/typst/issues/1404#issuecomment-1598374652 let theta = f32::atan2(-ts.kx, ts.sx); - // To avoid division by 0, choose the one of { sin, cos } that is further from 0. + + // To avoid division by 0, choose the one of { sin, cos } that is + // further from 0. let prefer_sin = theta.sin().abs() > std::f32::consts::FRAC_1_SQRT_2; let scale_x = f32::abs(if prefer_sin { ts.kx / theta.sin() } else { ts.sx / theta.cos() }); + + let aspect = (image.width() as f32) / (image.height() as f32); let w = (scale_x * view_width.max(aspect * view_height)).ceil() as u32; let h = ((w as f32) / aspect).ceil() as u32; -- cgit v1.2.3