Skip to content

Commit

Permalink
Implement a more complete file name generation
Browse files Browse the repository at this point in the history
  • Loading branch information
madig committed Jan 21, 2022
1 parent 42011e8 commit 05531dd
Show file tree
Hide file tree
Showing 2 changed files with 274 additions and 68 deletions.
39 changes: 21 additions & 18 deletions src/layer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::BTreeMap;
use std::collections::{BTreeMap, HashSet};
use std::fs;
use std::path::{Path, PathBuf};

Expand Down Expand Up @@ -107,7 +107,11 @@ impl LayerSet {
if let Some(index) = self.layers.iter().position(|l| l.name == name) {
self.layers.get_mut(index).unwrap()
} else {
let layer = Layer::new(name, None).unwrap();
let name = Name::new_raw(name);
let existing_paths: HashSet<String> =
self.layers.iter().map(|l| l.path.to_string_lossy().to_lowercase()).collect();
let path = crate::util::default_file_name_for_layer_name(&name, &existing_paths);
let layer = Layer::new(name, path);
self.layers.push(layer);
self.layers.last_mut().unwrap()
}
Expand Down Expand Up @@ -140,7 +144,11 @@ impl LayerSet {
if self.layers.iter().any(|l| l.name == name) {
Err(NamingError::Duplicate(name.to_string()))
} else {
let layer = Layer::new(name, None)?;
let name = Name::new(name).map_err(|_| NamingError::Invalid(name.into()))?;
let existing_paths: HashSet<String> =
self.layers.iter().map(|l| l.path.to_string_lossy().to_lowercase()).collect();
let path = crate::util::default_file_name_for_layer_name(&name, &existing_paths);
let layer = Layer::new(name, path);
self.layers.push(layer);
Ok(self.layers.last_mut().unwrap())
}
Expand Down Expand Up @@ -224,24 +232,17 @@ pub struct Layer {
impl Layer {
/// Returns a new [`Layer`] with the provided `name` and `path`.
///
/// The `path` argument, if provided, will be the directory within the UFO
/// that the layer is saved. If it is not provided, it will be derived from
/// the layer name.
pub(crate) fn new(name: &str, path: Option<PathBuf>) -> Result<Self, NamingError> {
let path = match path {
Some(path) => path,
None if &*name == DEFAULT_LAYER_NAME => DEFAULT_GLYPHS_DIRNAME.into(),
_ => crate::util::default_file_name_for_layer_name(name).into(),
};
let name = Name::new(name).map_err(|_| NamingError::Invalid(name.into()))?;
Ok(Layer {
/// The `path` argument will be the directory within the UFO that the layer
/// is saved.
pub(crate) fn new(name: Name, path: PathBuf) -> Self {
Layer {
glyphs: BTreeMap::new(),
name,
path,
contents: BTreeMap::new(),
color: None,
lib: Default::default(),
})
}
}

/// Returns a new [`Layer`] that is loaded from `path` with the provided `name`.
Expand Down Expand Up @@ -458,8 +459,10 @@ impl Layer {
) {
let glyph = glyph.into();
if !self.contents.contains_key(&glyph.name) {
let path = crate::util::default_file_name_for_glyph_name(&glyph.name);
self.contents.insert(glyph.name.clone(), path.into());
let existing_paths: HashSet<String> =
self.contents.values().map(|p| p.to_string_lossy().to_lowercase()).collect();
let path = crate::util::default_file_name_for_glyph_name(&glyph.name, &existing_paths);
self.contents.insert(glyph.name.clone(), path);
}
self.glyphs.insert(glyph.name.clone(), glyph);
}
Expand Down Expand Up @@ -558,7 +561,7 @@ impl Layer {

impl Default for Layer {
fn default() -> Self {
Layer::new(DEFAULT_LAYER_NAME, None).unwrap()
Layer::new(Name::new_raw(DEFAULT_LAYER_NAME), DEFAULT_GLYPHS_DIRNAME.into())
}
}

Expand Down
Loading

0 comments on commit 05531dd

Please sign in to comment.