From 0b9e43334551861dbdd75732785d0cb5f3431568 Mon Sep 17 00:00:00 2001 From: Nikolaus Waxweiler Date: Mon, 11 Mar 2024 12:15:24 +0000 Subject: [PATCH 1/3] Always retain the first (default) layer --- src/layer.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/layer.rs b/src/layer.rs index a63d54ff..82ea87e5 100644 --- a/src/layer.rs +++ b/src/layer.rs @@ -237,7 +237,15 @@ impl LayerContents { where F: FnMut(&Layer) -> bool, { - self.layers.retain(|layer| layer.name == DEFAULT_LAYER_NAME || predicate(layer)) + // Always keep the default layer, which is always the first. + let mut is_first = true; + self.layers.retain(|layer| { + if is_first { + is_first = false; + return true; + } + predicate(layer) + }); } /// Removes any layers that contain no glyphs @@ -904,12 +912,13 @@ mod tests { ], ..Default::default() }; + layers.rename_layer("public.default", "foreground", false).unwrap(); layers.retain(|l| l.name().starts_with("fizz")); assert_eq!(layers.len(), 3, "wrong number of layers deleted"); let names = layers.iter().map(|l| l.name().as_str()).collect::>(); - assert_eq!(names.as_slice(), &[DEFAULT_LAYER_NAME, "fizz", "fizzbuzz"]); + assert_eq!(names.as_slice(), &["foreground", "fizz", "fizzbuzz"]); } #[test] From 38cd34974b3ca4d8419294a644d0e9d89ed9dfee Mon Sep 17 00:00:00 2001 From: Nikolaus Waxweiler Date: Mon, 11 Mar 2024 14:08:04 +0000 Subject: [PATCH 2/3] Test for default layer path --- src/layer.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/layer.rs b/src/layer.rs index 82ea87e5..2d31a289 100644 --- a/src/layer.rs +++ b/src/layer.rs @@ -237,11 +237,8 @@ impl LayerContents { where F: FnMut(&Layer) -> bool, { - // Always keep the default layer, which is always the first. - let mut is_first = true; self.layers.retain(|layer| { - if is_first { - is_first = false; + if layer.is_default() { return true; } predicate(layer) @@ -460,6 +457,14 @@ impl Layer { self.glyphs.is_empty() } + /// Returns `true` if this layer is the default layer. + /// + /// The default layer can have any name, but always uses the "glyphs" + /// directory. + pub fn is_default(&self) -> bool { + self.path == Path::new(DEFAULT_GLYPHS_DIRNAME) + } + /// Returns the name of the layer. /// /// This can only be mutated through the [`LayerContents`]. From e5495490561313aeb2ebd41dbfb171932f17d0ff Mon Sep 17 00:00:00 2001 From: Nikolaus Waxweiler Date: Mon, 11 Mar 2024 14:35:17 +0000 Subject: [PATCH 3/3] Shorten closure --- src/layer.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/layer.rs b/src/layer.rs index 2d31a289..ea9a3464 100644 --- a/src/layer.rs +++ b/src/layer.rs @@ -237,12 +237,7 @@ impl LayerContents { where F: FnMut(&Layer) -> bool, { - self.layers.retain(|layer| { - if layer.is_default() { - return true; - } - predicate(layer) - }); + self.layers.retain(|layer| layer.is_default() || predicate(layer)); } /// Removes any layers that contain no glyphs