Skip to content

Commit

Permalink
Draft: use thiserror
Browse files Browse the repository at this point in the history
  • Loading branch information
madig committed Dec 16, 2021
1 parent 7c86886 commit 47f663c
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 340 deletions.
405 changes: 104 additions & 301 deletions src/error.rs

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ impl Font {
return Err(Error::MissingFile(meta_path.display().to_string()));
}
let mut meta: MetaInfo = plist::from_file(&meta_path)
.map_err(|error| Error::PlistLoad { path: meta_path, error })?;
.map_err(|source| Error::PlistLoad { path: meta_path, source })?;

let lib_path = path.join(LIB_FILE);
let mut lib =
Expand Down Expand Up @@ -418,9 +418,9 @@ impl Font {

if path.exists() {
fs::remove_dir_all(path)
.map_err(|inner| Error::UfoWrite { path: path.into(), inner })?;
.map_err(|source| Error::UfoWrite { path: path.into(), source })?;
}
fs::create_dir(path).map_err(|inner| Error::UfoWrite { path: path.into(), inner })?;
fs::create_dir(path).map_err(|source| Error::UfoWrite { path: path.into(), source })?;

// we want to always set ourselves as the creator when serializing,
// but we also don't have mutable access to self.
Expand Down Expand Up @@ -468,10 +468,10 @@ impl Font {
let feature_file_path = path.join(FEATURES_FILE);
if self.features.as_bytes().contains(&b'\r') {
fs::write(&feature_file_path, self.features.replace("\r\n", "\n"))
.map_err(|inner| Error::UfoWrite { path: feature_file_path, inner })?;
.map_err(|source| Error::UfoWrite { path: feature_file_path, source })?;
} else {
fs::write(&feature_file_path, &self.features)
.map_err(|inner| Error::UfoWrite { path: feature_file_path, inner })?;
.map_err(|source| Error::UfoWrite { path: feature_file_path, source })?;
}
}

Expand All @@ -491,11 +491,11 @@ impl Font {
Ok(data) => {
let destination = data_dir.join(data_path);
let destination_parent = destination.parent().unwrap();
fs::create_dir_all(&destination_parent).map_err(|inner| {
Error::UfoWrite { path: destination_parent.into(), inner }
fs::create_dir_all(&destination_parent).map_err(|source| {
Error::UfoWrite { path: destination_parent.into(), source }
})?;
fs::write(&destination, &*data)
.map_err(|inner| Error::UfoWrite { path: destination, inner })?;
.map_err(|source| Error::UfoWrite { path: destination, source })?;
}
Err(e) => return Err(Error::InvalidStoreEntry(data_path.clone(), e)),
}
Expand All @@ -505,13 +505,13 @@ impl Font {
if !self.images.is_empty() {
let images_dir = path.join(IMAGES_DIR);
fs::create_dir(&images_dir) // Only a flat directory.
.map_err(|inner| Error::UfoWrite { path: images_dir.to_owned(), inner })?;
.map_err(|source| Error::UfoWrite { path: images_dir.to_owned(), source })?;
for (image_path, contents) in self.images.iter() {
match contents {
Ok(data) => {
let destination = images_dir.join(image_path);
fs::write(&destination, &*data)
.map_err(|inner| Error::UfoWrite { path: destination, inner })?;
.map_err(|source| Error::UfoWrite { path: destination, source })?;
}
Err(e) => return Err(Error::InvalidStoreEntry(image_path.clone(), e)),
}
Expand Down Expand Up @@ -581,7 +581,7 @@ impl Font {

fn load_lib(lib_path: &Path) -> Result<plist::Dictionary, Error> {
plist::Value::from_file(lib_path)
.map_err(|error| Error::PlistLoad { path: lib_path.to_owned(), error })?
.map_err(|source| Error::PlistLoad { path: lib_path.to_owned(), source })?
.into_dictionary()
.ok_or_else(|| Error::ExpectedPlistDictionary(lib_path.to_string_lossy().into_owned()))
}
Expand All @@ -597,20 +597,20 @@ fn load_fontinfo(

fn load_groups(groups_path: &Path) -> Result<Groups, Error> {
let groups: Groups = plist::from_file(groups_path)
.map_err(|error| Error::PlistLoad { path: groups_path.to_owned(), error })?;
.map_err(|source| Error::PlistLoad { path: groups_path.to_owned(), source })?;
validate_groups(&groups).map_err(Error::InvalidGroups)?;
Ok(groups)
}

fn load_kerning(kerning_path: &Path) -> Result<Kerning, Error> {
let kerning: Kerning = plist::from_file(kerning_path)
.map_err(|error| Error::PlistLoad { path: kerning_path.to_owned(), error })?;
.map_err(|source| Error::PlistLoad { path: kerning_path.to_owned(), source })?;
Ok(kerning)
}

fn load_features(features_path: &Path) -> Result<String, Error> {
let features = fs::read_to_string(features_path)
.map_err(|inner| Error::UfoLoad { path: features_path.into(), inner })?;
.map_err(|source| Error::UfoLoad { path: features_path.into(), source })?;
Ok(features)
}

Expand Down
6 changes: 3 additions & 3 deletions src/fontinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,14 +464,14 @@ impl FontInfo {
match format_version {
FormatVersion::V3 => {
let mut fontinfo: FontInfo = plist::from_file(path)
.map_err(|error| Error::PlistLoad { path: path.to_owned(), error })?;
.map_err(|source| Error::PlistLoad { path: path.to_owned(), source })?;
fontinfo.validate()?;
fontinfo.load_object_libs(lib)?;
Ok(fontinfo)
}
FormatVersion::V2 => {
let fontinfo_v2: FontInfoV2 = plist::from_file(path)
.map_err(|error| Error::PlistLoad { path: path.to_owned(), error })?;
.map_err(|source| Error::PlistLoad { path: path.to_owned(), source })?;
let fontinfo = FontInfo {
ascender: fontinfo_v2.ascender,
cap_height: fontinfo_v2.capHeight,
Expand Down Expand Up @@ -626,7 +626,7 @@ impl FontInfo {
}
FormatVersion::V1 => {
let fontinfo_v1: FontInfoV1 = plist::from_file(path)
.map_err(|error| Error::PlistLoad { path: path.to_owned(), error })?;
.map_err(|source| Error::PlistLoad { path: path.to_owned(), source })?;
let fontinfo = FontInfo {
ascender: fontinfo_v1.ascender,
cap_height: fontinfo_v1.capHeight,
Expand Down
2 changes: 1 addition & 1 deletion src/glyph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl Glyph {

let data = self.encode_xml_with_options(opts)?;
std::fs::write(path, &data)
.map_err(|inner| Error::UfoWrite { path: path.into(), inner })?;
.map_err(|source| Error::UfoWrite { path: path.into(), source })?;
Ok(())
}

Expand Down
7 changes: 1 addition & 6 deletions src/glyph/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::names::NameList;

use quick_xml::{
events::{BytesStart, Event},
Error as XmlError, Reader,
Reader,
};

#[cfg(test)]
Expand Down Expand Up @@ -522,8 +522,3 @@ impl From<ErrorKind> for GlifLoadError {
GlifLoadError::Parse(src)
}
}
impl From<XmlError> for GlifLoadError {
fn from(src: XmlError) -> GlifLoadError {
GlifLoadError::Xml(src)
}
}
2 changes: 1 addition & 1 deletion src/glyph/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Glyph {
/// [ufonormalizer]: https://github.com/unified-font-object/ufoNormalizer/
pub fn encode_xml_with_options(&self, opts: &WriteOptions) -> Result<Vec<u8>, GlifWriteError> {
self.encode_xml_impl(opts)
.map_err(|inner| GlifWriteError { name: self.name.clone(), inner })
.map_err(|source| GlifWriteError { name: self.name.clone(), source })
}

fn encode_xml_impl(&self, options: &WriteOptions) -> Result<Vec<u8>, WriteError> {
Expand Down
10 changes: 5 additions & 5 deletions src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl LayerSet {
let layer_contents_path = base_dir.join(LAYER_CONTENTS_FILE);
let to_load: Vec<(LayerName, PathBuf)> = if layer_contents_path.exists() {
plist::from_file(&layer_contents_path)
.map_err(|error| Error::PlistLoad { path: layer_contents_path, error })?
.map_err(|source| Error::PlistLoad { path: layer_contents_path, source })?
} else {
vec![(Arc::from(DEFAULT_LAYER_NAME), PathBuf::from(DEFAULT_GLYPHS_DIRNAME))]
};
Expand Down Expand Up @@ -248,7 +248,7 @@ impl Layer {
// these keys are never used; a future optimization would be to skip the
// names and deserialize to a vec; that would not be a one-liner, though.
let contents: BTreeMap<GlyphName, PathBuf> = plist::from_file(&contents_path)
.map_err(|error| Error::PlistLoad { path: contents_path, error })?;
.map_err(|source| Error::PlistLoad { path: contents_path, source })?;

#[cfg(feature = "rayon")]
let iter = contents.par_iter();
Expand All @@ -265,7 +265,7 @@ impl Layer {
glyph.name = name.clone();
(name, Arc::new(glyph))
})
.map_err(|e| Error::GlifLoad { path: glyph_path, inner: e })
.map_err(|source| Error::GlifLoad { path: glyph_path, source })
})
.collect::<Result<_, _>>()?;

Expand All @@ -286,7 +286,7 @@ impl Layer {
// Ser/de must be done manually...
fn layerinfo_from_file(path: &Path) -> Result<(Option<Color>, Plist), Error> {
let mut info_content = plist::Value::from_file(path)
.map_err(|error| Error::PlistLoad { path: path.to_owned(), error })?
.map_err(|source| Error::PlistLoad { path: path.to_owned(), source })?
.into_dictionary()
.ok_or_else(|| Error::ExpectedPlistDictionary(path.to_string_lossy().into_owned()))?;

Expand Down Expand Up @@ -346,7 +346,7 @@ impl Layer {
///
/// The path should not exist.
pub fn save_with_options(&self, path: &Path, opts: &WriteOptions) -> Result<(), Error> {
fs::create_dir(&path).map_err(|inner| Error::UfoWrite { path: path.into(), inner })?;
fs::create_dir(&path).map_err(|source| Error::UfoWrite { path: path.into(), source })?;
crate::write::write_xml_to_file(&path.join(CONTENTS_FILE), &self.contents, opts)?;

self.layerinfo_to_file_if_needed(path, opts)?;
Expand Down
2 changes: 1 addition & 1 deletion src/upconversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ pub(crate) fn upconvert_ufov1_robofab_data(

// Read lib.plist again because it is easier than pulling out the data manually.
let lib_data: LibData = plist::from_file(lib_path)
.map_err(|error| Error::PlistLoad { path: lib_path.to_owned(), error })?;
.map_err(|source| Error::PlistLoad { path: lib_path.to_owned(), source })?;

// Convert features.
let mut features = String::new();
Expand Down
16 changes: 8 additions & 8 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ pub fn write_plist_value_to_file(
options: &WriteOptions,
) -> Result<(), Error> {
let mut file =
File::create(path).map_err(|inner| Error::UfoWrite { path: path.into(), inner })?;
File::create(path).map_err(|source| Error::UfoWrite { path: path.into(), source })?;
let writer = BufWriter::new(&mut file);
value
.to_writer_xml_with_options(writer, options.xml_options())
.map_err(|error| Error::PlistWrite { path: path.to_owned(), error })?;
.map_err(|source| Error::PlistWrite { path: path.to_owned(), source })?;
write_quote_style(&file, options)
.map_err(|inner| Error::UfoWrite { path: path.into(), inner })?;
file.sync_all().map_err(|inner| Error::UfoWrite { path: path.into(), inner })?;
.map_err(|source| Error::UfoWrite { path: path.into(), source })?;
file.sync_all().map_err(|source| Error::UfoWrite { path: path.into(), source })?;
Ok(())
}

Expand All @@ -134,13 +134,13 @@ pub fn write_xml_to_file(
options: &WriteOptions,
) -> Result<(), Error> {
let mut file =
File::create(path).map_err(|inner| Error::UfoWrite { path: path.into(), inner })?;
File::create(path).map_err(|source| Error::UfoWrite { path: path.into(), source })?;
let buf_writer = BufWriter::new(&mut file);
plist::to_writer_xml_with_options(buf_writer, value, options.xml_options())
.map_err(|error| Error::PlistWrite { path: path.to_owned(), error })?;
.map_err(|source| Error::PlistWrite { path: path.to_owned(), source })?;
write_quote_style(&file, options)
.map_err(|inner| Error::UfoWrite { path: path.into(), inner })?;
file.sync_all().map_err(|inner| Error::UfoWrite { path: path.into(), inner })?;
.map_err(|source| Error::UfoWrite { path: path.into(), source })?;
file.sync_all().map_err(|source| Error::UfoWrite { path: path.into(), source })?;
Ok(())
}

Expand Down

0 comments on commit 47f663c

Please sign in to comment.