Skip to content

Commit

Permalink
Merge pull request #887 from googlefonts/fea-rs-verbose-error
Browse files Browse the repository at this point in the history
[fea-rs] Add CompilerError::display_verbose
  • Loading branch information
cmyr authored Aug 2, 2024
2 parents 4f4d165 + 13269ce commit 4cdee64
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
6 changes: 3 additions & 3 deletions fea-rs/src/bin/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use clap::Parser;
use fea_rs::{
compile::{
self,
error::{FontGlyphOrderError, GlyphOrderError, UfoGlyphOrderError},
error::{CompilerError, FontGlyphOrderError, GlyphOrderError, UfoGlyphOrderError},
Compiler, MockVariationInfo, NopFeatureProvider, Opts,
},
GlyphMap,
Expand Down Expand Up @@ -80,8 +80,8 @@ enum Error {
MissingGlyphOrder,
#[error("Error parsing axis info: L{line}, '{message}'")]
BadAxisInfo { line: usize, message: String },
#[error("{0}")]
CompileFail(#[from] compile::error::CompilerError),
#[error("{}", .0.display_verbose())]
CompileFail(#[from] CompilerError),
}

/// Compile FEA files
Expand Down
22 changes: 22 additions & 0 deletions fea-rs/src/compile/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Error types related to compilation

use std::fmt::Display;

use write_fonts::{read::ReadError, BuilderError};

use crate::{parse::SourceLoadError, DiagnosticSet};
Expand Down Expand Up @@ -58,6 +60,26 @@ pub enum CompilerError {
WriteFail(#[from] BuilderError),
}

impl CompilerError {
/// Return a `Display` type that reports the location and nature of syntax errors
pub fn display_verbose(&self) -> impl Display + '_ {
struct Verbose<'a>(&'a CompilerError);
impl std::fmt::Display for Verbose<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.0)?;
let diagnostic = match self.0 {
CompilerError::ParseFail(x)
| CompilerError::ValidationFail(x)
| CompilerError::CompilationFail(x) => x,
_ => return Ok(()),
};
write!(f, "\n{}", diagnostic.display())
}
}
Verbose(self)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 4cdee64

Please sign in to comment.