diff --git a/fea-rs/src/compile.rs b/fea-rs/src/compile.rs index 604ce3719..7a95fcad5 100644 --- a/fea-rs/src/compile.rs +++ b/fea-rs/src/compile.rs @@ -44,7 +44,7 @@ mod validate; mod variations; /// Run the validation pass, returning any diagnostics. -pub(crate) fn validate( +pub fn validate( node: &ParseTree, glyph_map: &GlyphMap, fvar: Option<&V>, @@ -54,6 +54,29 @@ pub(crate) fn validate( DiagnosticSet::new(ctx.errors, node, usize::MAX) } +/// Run the compilation pass. +/// +/// If successful, returns the [`Compilation`] result, and any warnings. +pub fn compile( + tree: &ParseTree, + glyph_map: &GlyphMap, + var_info: Option<&V>, + extra_features: Option<&T>, +) -> Result<(Compilation, DiagnosticSet), DiagnosticSet> { + let mut ctx = CompilationCtx::new(glyph_map, tree.source_map(), var_info, extra_features); + ctx.compile(&tree.typed_root()); + match ctx.build() { + Ok((compilation, warnings)) => { + let warnings = DiagnosticSet::new(warnings, tree, usize::MAX); + Ok((compilation, warnings)) + } + Err(errors) => { + let errors = DiagnosticSet::new(errors, tree, usize::MAX); + Err(errors) + } + } +} + /// A helper function for extracting the glyph order from a UFO /// /// If the public.glyphOrder key is missing, or the glyphOrder is malformed, diff --git a/fea-rs/src/compile/compile_ctx.rs b/fea-rs/src/compile/compile_ctx.rs index 08366f185..74b4eea2b 100644 --- a/fea-rs/src/compile/compile_ctx.rs +++ b/fea-rs/src/compile/compile_ctx.rs @@ -170,7 +170,7 @@ impl<'a, F: FeatureProvider, V: VariationInfo> CompilationCtx<'a, F, V> { self.features.sort_and_dedupe_lookups(); } - pub(crate) fn build(&mut self) -> Result> { + pub(crate) fn build(&mut self) -> Result<(Compilation, Vec), Vec> { if self.errors.iter().any(Diagnostic::is_error) { return Err(self.errors.clone()); } @@ -237,19 +237,21 @@ impl<'a, F: FeatureProvider, V: VariationInfo> CompilationCtx<'a, F, V> { } } - Ok(Compilation { - warnings: self.errors.clone(), - head: self.tables.head.as_ref().map(|raw| raw.build(None)), - hhea: self.tables.hhea.clone(), - vhea: self.tables.vhea.clone(), - os2: self.tables.os2.as_ref().map(|raw| raw.build()), - gdef, - base: self.tables.base.as_ref().map(|raw| raw.build()), - name: name_builder.build(), - stat, - gsub, - gpos, - }) + Ok(( + Compilation { + head: self.tables.head.as_ref().map(|raw| raw.build(None)), + hhea: self.tables.hhea.clone(), + vhea: self.tables.vhea.clone(), + os2: self.tables.os2.as_ref().map(|raw| raw.build()), + gdef, + base: self.tables.base.as_ref().map(|raw| raw.build()), + name: name_builder.build(), + stat, + gsub, + gpos, + }, + self.errors.clone(), + )) } fn run_feature_writer_if_present(&mut self) { diff --git a/fea-rs/src/compile/compiler.rs b/fea-rs/src/compile/compiler.rs index 8a9a751ef..bc6f5bbfc 100644 --- a/fea-rs/src/compile/compiler.rs +++ b/fea-rs/src/compile/compiler.rs @@ -160,7 +160,7 @@ impl<'a, F: FeatureProvider, V: VariationInfo> Compiler<'a, F, V> { let diagnostics = DiagnosticSet::new(messages, &tree, self.max_n_errors); print_warnings_return_errors(diagnostics, self.print_warnings, self.max_n_errors) .map_err(CompilerError::CompilationFail)?; - Ok(ctx.build().unwrap()) // we've taken the errors, so this can't fail + Ok(ctx.build().unwrap().0) // we've taken the errors, so this can't fail } /// Compile to a binary font. diff --git a/fea-rs/src/compile/output.rs b/fea-rs/src/compile/output.rs index 873d8e5cf..e77220293 100644 --- a/fea-rs/src/compile/output.rs +++ b/fea-rs/src/compile/output.rs @@ -7,7 +7,7 @@ use write_fonts::{ use super::Opts; -use crate::{Diagnostic, GlyphMap}; +use crate::GlyphMap; /// The tables generated by this compilation. /// @@ -20,8 +20,6 @@ use crate::{Diagnostic, GlyphMap}; /// /// [`to_binary`]: Compilation::to_binary pub struct Compilation { - /// Any warnings encountered during parsing or compilation - pub warnings: Vec, /// The `head` table, if one was generated pub head: Option, /// The `hhea` table, if one was generated diff --git a/fea-rs/src/diagnostic.rs b/fea-rs/src/diagnostic.rs index 26f9554e1..235f282b9 100644 --- a/fea-rs/src/diagnostic.rs +++ b/fea-rs/src/diagnostic.rs @@ -139,6 +139,11 @@ impl DiagnosticSet { self.max_to_print = max_to_print; } + /// Discard any warnings, keeping only errors + pub fn discard_warnings(&mut self) { + self.messages.retain(|x| x.is_error()); + } + /// Remove and return any warnings in this set. pub fn split_off_warnings(&mut self) -> Option { self.messages.sort_unstable_by_key(|d| d.level);