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

Upgarde to v0.10 spreet crate, cleanup SpreetResult #984

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
267 changes: 145 additions & 122 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ serde_json = "1"
serde_with = "3"
serde_yaml = "0.9"
size_format = "1.0.2"
spreet = { version = "0.9", default-features = false }
spreet = { version = "0.10", default-features = false }
sqlite-hashes = { version = "0.5", default-features = false, features = ["md5", "window", "hex"] }
sqlx = { version = "0.7", features = ["sqlite", "runtime-tokio"] }
subst = { version = "0.3", features = ["yaml"] }
Expand Down
7 changes: 5 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,16 @@ test-int: clean-test install-sqlx
fi

# Run integration tests and save its output as the new expected output
bless: restart clean-test bless-insta-martin bless-insta-mbtiles
bless: restart clean-test bless-tests bless-insta-martin bless-insta-mbtiles
rm -rf tests/temp
cargo test -p martin --features bless-tests
tests/test.sh
rm -rf tests/expected
mv tests/output tests/expected

# Run test with bless-tests feature
bless-tests:
cargo test -p martin --features bless-tests

# Run integration tests and save its output as the new expected output
bless-insta-mbtiles *ARGS: (cargo-install "cargo-insta")
#rm -rf mbtiles/tests/snapshots
Expand Down
82 changes: 46 additions & 36 deletions martin/src/sprites/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ use std::path::PathBuf;
use futures::future::try_join_all;
use log::{info, warn};
use serde::{Deserialize, Serialize};
use spreet::fs::get_svg_input_paths;
use spreet::resvg::usvg::{Error as ResvgError, Options, Tree, TreeParsing};
use spreet::sprite::{sprite_name, Sprite, Spritesheet, SpritesheetBuilder};
use spreet::{
get_svg_input_paths, sprite_name, SpreetError, Sprite, Spritesheet, SpritesheetBuilder,
};
use tokio::io::AsyncReadExt;

use self::SpriteError::{SpriteInstError, SpriteParsingError, SpriteProcessingError};
use crate::file_config::{FileConfigEnum, FileResult};

pub type SpriteResult<T> = Result<T, SpriteError>;

#[derive(thiserror::Error, Debug)]
pub enum SpriteError {
#[error("Sprite {0} not found")]
Expand All @@ -34,13 +38,16 @@ pub enum SpriteError {
UnableToReadSprite(PathBuf),

#[error("{0} in file {}", .1.display())]
SpriteProcessingError(spreet::error::Error, PathBuf),
SpriteProcessingError(SpreetError, PathBuf),

#[error("{0} in file {}", .1.display())]
SpriteParsingError(ResvgError, PathBuf),

#[error("Unable to generate spritesheet")]
UnableToGenerateSpritesheet,

#[error("Unable to create a sprite from file {}", .0.display())]
SpriteInstError(PathBuf),
}

#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
Expand Down Expand Up @@ -87,20 +94,23 @@ impl SpriteSources {
Ok(results)
}

pub fn get_catalog(&self) -> FileResult<SpriteCatalog> {
pub fn get_catalog(&self) -> SpriteResult<SpriteCatalog> {
// TODO: all sprite generation should be pre-cached
Ok(self
.0
.iter()
.map(|(id, source)| {
let mut images = get_svg_input_paths(&source.path, true)
.into_iter()
.map(|svg_path| sprite_name(svg_path, &source.path))
.collect::<Vec<_>>();
images.sort();
(id.clone(), CatalogSpriteEntry { images })
})
.collect())
let mut entries = SpriteCatalog::new();
for (id, source) in &self.0 {
let paths = get_svg_input_paths(&source.path, true)
.map_err(|e| SpriteProcessingError(e, source.path.clone()))?;
let mut images = Vec::with_capacity(paths.len());
for path in paths {
images.push(
sprite_name(&path, &source.path)
.map_err(|e| SpriteProcessingError(e, source.path.clone()))?,
);
}
images.sort();
entries.insert(id.clone(), CatalogSpriteEntry { images });
}
Ok(entries)
}

fn add_source(&mut self, id: String, path: PathBuf) {
Expand All @@ -123,7 +133,7 @@ impl SpriteSources {

/// Given a list of IDs in a format "id1,id2,id3", return a spritesheet with them all.
/// `ids` may optionally end with "@2x" to request a high-DPI spritesheet.
pub async fn get_sprites(&self, ids: &str) -> Result<Spritesheet, SpriteError> {
pub async fn get_sprites(&self, ids: &str) -> SpriteResult<Spritesheet> {
let (ids, dpi) = if let Some(ids) = ids.strip_suffix("@2x") {
(ids, 2)
} else {
Expand All @@ -137,7 +147,7 @@ impl SpriteSources {
.get(id)
.ok_or_else(|| SpriteError::SpriteNotFound(id.to_string()))
})
.collect::<Result<Vec<_>, SpriteError>>()?;
.collect::<SpriteResult<Vec<_>>>()?;

get_spritesheet(sprite_ids.into_iter(), dpi).await
}
Expand All @@ -152,7 +162,7 @@ async fn parse_sprite(
name: String,
path: PathBuf,
pixel_ratio: u8,
) -> Result<(String, Sprite), SpriteError> {
) -> SpriteResult<(String, Sprite)> {
let on_err = |e| SpriteError::IoError(e, path.clone());

let mut file = tokio::fs::File::open(&path).await.map_err(on_err)?;
Expand All @@ -161,31 +171,31 @@ async fn parse_sprite(
file.read_to_end(&mut buffer).await.map_err(on_err)?;

let tree = Tree::from_data(&buffer, &Options::default())
.map_err(|e| SpriteError::SpriteParsingError(e, path.clone()))?;
.map_err(|e| SpriteParsingError(e, path.clone()))?;

Ok((name, Sprite { tree, pixel_ratio }))
let sprite = Sprite::new(tree, pixel_ratio).ok_or_else(|| SpriteInstError(path.clone()))?;

Ok((name, sprite))
}

pub async fn get_spritesheet(
sources: impl Iterator<Item = &SpriteSource>,
pixel_ratio: u8,
) -> Result<Spritesheet, SpriteError> {
) -> SpriteResult<Spritesheet> {
// Asynchronously load all SVG files from the given sources
let sprites = try_join_all(sources.flat_map(|source| {
get_svg_input_paths(&source.path, true)
.into_iter()
.map(|svg_path| {
let name = sprite_name(&svg_path, &source.path);
parse_sprite(name, svg_path, pixel_ratio)
})
.collect::<Vec<_>>()
}))
.await?;

let mut futures = Vec::new();
for source in sources {
let paths = get_svg_input_paths(&source.path, true)
.map_err(|e| SpriteProcessingError(e, source.path.clone()))?;
for path in paths {
let name = sprite_name(&path, &source.path)
.map_err(|e| SpriteProcessingError(e, source.path.clone()))?;
futures.push(parse_sprite(name, path, pixel_ratio));
}
}
let sprites = try_join_all(futures).await?;
let mut builder = SpritesheetBuilder::new();
builder
.sprites(sprites.into_iter().collect())
.pixel_ratio(pixel_ratio);
builder.sprites(sprites.into_iter().collect());

// TODO: decide if this is needed and/or configurable
// builder.make_unique();
Expand Down
Binary file modified tests/expected/configured/spr_cmp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/expected/configured/spr_cmp.png.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
tests/output/configured/spr_cmp.png: PNG image data, 50 x 31, 8-bit colormap, non-interlaced
tests/output/configured/spr_cmp.png: PNG image data, 50 x 31, 8-bit/color RGBA, non-interlaced
Binary file modified tests/expected/configured/spr_cmp_2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/expected/configured/spr_mysrc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/expected/configured/spr_mysrc.png.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
tests/output/configured/spr_mysrc.png: PNG image data, 15 x 15, 8-bit colormap, non-interlaced
tests/output/configured/spr_mysrc.png: PNG image data, 15 x 15, 8-bit gray+alpha, non-interlaced
Binary file modified tests/expected/configured/spr_mysrc_2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/expected/configured/spr_mysrc_2x.png.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
tests/output/configured/spr_mysrc_2x.png: PNG image data, 30 x 30, 8-bit colormap, non-interlaced
tests/output/configured/spr_mysrc_2x.png: PNG image data, 30 x 30, 8-bit gray+alpha, non-interlaced
Binary file modified tests/expected/configured/spr_src1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/expected/configured/spr_src1.png.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
tests/output/configured/spr_src1.png: PNG image data, 36 x 31, 8-bit colormap, non-interlaced
tests/output/configured/spr_src1.png: PNG image data, 36 x 31, 8-bit/color RGBA, non-interlaced
Binary file modified tests/expected/configured/spr_src1_2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/fixtures/sprites/expected/all_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/fixtures/sprites/expected/all_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/fixtures/sprites/expected/src1_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/fixtures/sprites/expected/src1_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/fixtures/sprites/expected/src2_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/fixtures/sprites/expected/src2_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading