Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom missing mandatory UFO path error types & missing mandatory UFO path checks #146

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ pub enum Error {
MissingDefaultLayer,
MissingLayer(String),
DuplicateLayer(String),
MissingLayerContents,
InvalidColor(InvalidColorString),
DuplicateGlyph {
layer: String,
Expand All @@ -44,6 +43,8 @@ pub enum Error {
ExpectedPositiveValue,
#[cfg(feature = "kurbo")]
ConvertContour(ErrorKind),
MissingFile(String),
MissingUFODir(String),
chrissimpkins marked this conversation as resolved.
Show resolved Hide resolved
}

/// An error representing a failure to validate UFO groups.
Expand Down Expand Up @@ -160,9 +161,6 @@ impl std::fmt::Display for Error {
Error::MissingDefaultLayer => write!(f, "Missing default ('glyphs') layer."),
Error::DuplicateLayer(name) => write!(f, "Layer name '{}' already exists.", name),
Error::MissingLayer(name) => write!(f, "Layer name '{}' does not exist.", name),
Error::MissingLayerContents => {
write!(f, "Missing required 'layercontents.plist' file.")
}
Error::DuplicateGlyph { layer, glyph } => {
write!(f, "Glyph named '{}' already exists in layer '{}'", glyph, layer)
}
Expand Down Expand Up @@ -194,6 +192,12 @@ impl std::fmt::Display for Error {
Error::ExpectedPositiveValue => {
write!(f, "PositiveIntegerOrFloat expects a positive value.")
}
Error::MissingFile(path) => {
write!(f, "missing required {} file", path)
}
Error::MissingUFODir(path) => {
write!(f, "{} directory was not found", path)
}
#[cfg(feature = "kurbo")]
Error::ConvertContour(cause) => write!(f, "Failed to convert contour: '{}'", cause),
}
Expand Down
17 changes: 16 additions & 1 deletion src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ impl Font {
}

fn load_impl(path: &Path, request: DataRequest) -> Result<Font, Error> {
if !path.exists() {
return Err(Error::MissingUFODir(path.display().to_string()));
}

let meta_path = path.join(METAINFO_FILE);
let mut meta: MetaInfo = plist::from_file(meta_path)?;

Expand Down Expand Up @@ -393,7 +397,7 @@ fn load_layers(
) -> Result<LayerSet, Error> {
let layercontents_path = ufo_path.join(LAYER_CONTENTS_FILE);
if meta.format_version == FormatVersion::V3 && !layercontents_path.exists() {
return Err(Error::MissingLayerContents);
return Err(Error::MissingFile(layercontents_path.display().to_string()));
}
LayerSet::load(ufo_path, glyph_names)
}
Expand Down Expand Up @@ -438,6 +442,17 @@ mod tests {
assert_eq!(font_obj.features.unwrap(), "# this is the feature from lightWide\n");
}

#[test]
fn loading_invalid_ufo_dir_path() {
let path = "totally/bogus/filepath/font.ufo";
let font_load_res = Font::load(path);
match font_load_res {
Ok(_) => panic!("unxpected Ok result"),
Err(Error::MissingUFODir(_)) => (), // expected value
_ => panic!("incorrect error type returned"),
}
}

#[test]
fn data_request() {
let path = "testdata/mutatorSans/MutatorSansLightWide.ufo";
Expand Down