Skip to content

Commit

Permalink
[fea-rs] Add CompilerError::display_verbose
Browse files Browse the repository at this point in the history
fea-rs collects extensive diagnostic information during parsing and
compilation, and is able to report errors associated to specific
locations in the input FEA. This wasn't very fine-grained, though, and
was causing problems with crater (because sometimes the stderr text for
a given error would be extremely long, and include a bunch of ANSI
escape codes) and so I turned off this finer-grained reporting.

It would be nice to have it available as an option, though, especially
now that I'm trying to add support for new syntax, and would like to see
what the actual errors are.

This adds a new method to the CompilerError that will print the error in
the old verbose style, and makes it so that when you call the fea-rs
binary directly (which is really not useful for debugging) it uses this
method when printing the returned error.
  • Loading branch information
cmyr committed Jul 30, 2024
1 parent 85795bf commit 13269ce
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 13269ce

Please sign in to comment.