Skip to content

Commit

Permalink
Merge pull request #856 from googlefonts/error-fixups
Browse files Browse the repository at this point in the history
[fontir] Rename SourceError::parse -> custom
  • Loading branch information
rsheeter authored Jun 26, 2024
2 parents f904f33 + 9f64e54 commit 37637fc
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 19 deletions.
9 changes: 5 additions & 4 deletions fontir/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub enum BadSourceKind {
ExpectedParent,
Io(io::Error),
/// Payload is a message to print; this error can originate from various parsers
ParseFail(String),
Custom(String),
}

/// An error that occurs when trying to access a file during change tracking
Expand Down Expand Up @@ -235,8 +235,9 @@ impl BadSource {
}
}

pub fn parse(path: impl Into<PathBuf>, msg: impl Display) -> Self {
Self::new(path, BadSourceKind::ParseFail(msg.to_string()))
/// A catch-all constructor for additional kinds of errors, such as various parsing failures
pub fn custom(path: impl Into<PathBuf>, msg: impl Display) -> Self {
Self::new(path, BadSourceKind::Custom(msg.to_string()))
}
}

Expand All @@ -263,7 +264,7 @@ impl std::fmt::Display for BadSourceKind {
BadSourceKind::UnrecognizedExtension => f.write_str("unknown file extension"),
BadSourceKind::ExpectedParent => f.write_str("missing parent directory"),
BadSourceKind::Io(e) => e.fmt(f),
BadSourceKind::ParseFail(e) => f.write_str(e),
BadSourceKind::Custom(e) => f.write_str(e),
}
}
}
2 changes: 1 addition & 1 deletion fontra2fontir/src/fontra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ where
for<'a> T: Deserialize<'a>,
{
let raw = fs::read_to_string(p).map_err(|e| BadSource::new(p, e))?;
serde_json::from_str(&raw).map_err(|e| BadSource::parse(p, e))
serde_json::from_str(&raw).map_err(|e| BadSource::custom(p, e))
}

/// serde type used to load font-data.json
Expand Down
14 changes: 6 additions & 8 deletions fontra2fontir/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,9 @@ impl FontraIrSource {
line.map_err(|e| BadSource::new(&self.glyphinfo_file, BadSourceKind::Io(e)))?;
let parts: Vec<_> = line.split(';').collect();
if parts.len() != 2 {
return Err(BadSource::new(
return Err(BadSource::custom(
&self.glyphinfo_file,
BadSourceKind::ParseFail(format!(
"Expected two parts in line {i} separated by ;"
)),
format!("Expected two parts in line {i} separated by ;"),
));
}
let glyph_name = GlyphName::new(parts[0].trim());
Expand All @@ -91,13 +89,13 @@ impl FontraIrSource {
return None;
}
let Some(codepoint) = codepoint.strip_prefix("U+") else {
return Some(Err(BadSource::parse(
return Some(Err(BadSource::custom(
&self.glyphinfo_file,
format!("Unintelligible codepoint {codepoint:?} at line {i}"),
)));
};
Some(u32::from_str_radix(codepoint, 16).map_err(|e| {
BadSource::parse(
BadSource::custom(
&self.glyphinfo_file,
format!("Unintelligible codepoint {codepoint:?} at line {i}: {e}"),
)
Expand All @@ -113,7 +111,7 @@ impl FontraIrSource {
.insert(glyph_name.clone(), (glyph_file, codepoints))
.is_some()
{
return Err(BadSource::parse(
return Err(BadSource::custom(
&self.glyphinfo_file,
format!("Multiple definitions of '{glyph_name}'"),
));
Expand All @@ -129,7 +127,7 @@ impl FontraIrSource {
let mut tracker = StateSet::new();
tracker.track_file(glyph_file)?;
if glyph_state.insert(glyph_name.clone(), tracker).is_some() {
Err(BadSource::parse(
Err(BadSource::custom(
&self.glyphinfo_file,
format!("Multiple definitions of '{glyph_name}'"),
))?;
Expand Down
2 changes: 1 addition & 1 deletion glyphs2fontir/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl Source for GlyphsIrSource {
fn inputs(&mut self) -> Result<Input, Error> {
// We have to read the glyphs file then shred it to figure out if anything changed
let font_info = FontInfo::try_from(Font::load(&self.glyphs_file).map_err(|e| {
BadSource::parse(
BadSource::custom(
&self.glyphs_file,
format!("Unable to read glyphs file: {e}"),
)
Expand Down
10 changes: 5 additions & 5 deletions ufo2fontir/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ fn glif_files(
if !glyph_list_file.is_file() {
return Err(BadSource::new(glyph_list_file, BadSourceKind::ExpectedFile).into());
}
let result: BTreeMap<String, PathBuf> = plist::from_file(&glyph_list_file)
.map_err(|e| BadSource::new(&glyph_list_file, BadSourceKind::ParseFail(e.to_string())))?;
let result: BTreeMap<String, PathBuf> =
plist::from_file(&glyph_list_file).map_err(|e| BadSource::custom(&glyph_list_file, e))?;

if result.is_empty() {
warn!("{:?} is empty", glyph_list_file);
Expand All @@ -114,8 +114,8 @@ fn layer_contents(ufo_dir: &Path) -> Result<HashMap<GlyphName, PathBuf>, BadSour
if !file.is_file() {
return Ok(HashMap::new());
}
let contents: Vec<(GlyphName, PathBuf)> = plist::from_file(&file)
.map_err(|e| BadSource::new(file, BadSourceKind::ParseFail(e.to_string())))?;
let contents: Vec<(GlyphName, PathBuf)> =
plist::from_file(&file).map_err(|e| BadSource::custom(file, e))?;
Ok(contents.into_iter().collect())
}

Expand Down Expand Up @@ -164,7 +164,7 @@ impl DesignSpaceIrSource {
Some("designspace") => {
DesignSpaceDocument::load(&designspace_or_ufo).map_err(|e| match e {
norad::error::DesignSpaceLoadError::Io(e) => BadSourceKind::Io(e),
other => BadSourceKind::ParseFail(other.to_string()),
other => BadSourceKind::Custom(other.to_string()),
})?
}
Some("ufo") => {
Expand Down

0 comments on commit 37637fc

Please sign in to comment.