From 1201237dd81111584bbb05437a7995c0a181a504 Mon Sep 17 00:00:00 2001 From: Jonathan Cornaz Date: Wed, 22 Jun 2022 21:27:59 +0000 Subject: [PATCH] feat(unstable-load-from-file): `yaml` and `ron` feature flags to avoid pulling unnecessary dependencies --- .github/workflows/build.yml | 3 ++- Cargo.toml | 6 +++--- README.md | 5 ++++- src/animation/load.rs | 30 +++++++++++++++++++++++++++--- src/animation/parse.rs | 8 +++++++- 5 files changed, 43 insertions(+), 9 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f26366d..de8908e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -58,7 +58,8 @@ jobs: override: true profile: minimal - uses: Swatinem/rust-cache@v1 - - run: cargo test --features unstable-load-from-file + - run: cargo test --features "unstable-load-from-file, yaml" + - run: cargo test --features "unstable-load-from-file, ron" code-style: runs-on: ubuntu-latest diff --git a/Cargo.toml b/Cargo.toml index d625baf..acd998f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ all-features = true [features] default = [] -unstable-load-from-file = ["serde", "serde_yaml", "ron", "anyhow", "bevy_utils"] +unstable-load-from-file = ["serde", "anyhow", "bevy_utils"] [dependencies] bevy_core = { version = "0.7.0", default-features = false } @@ -26,7 +26,7 @@ bevy_sprite = { version = "0.7.0", default-features = false } bevy_asset = { version = "0.7.0", default-features = false } bevy_utils = { version = "0.7.0", default-features = false, optional = true } serde = { version = "1.0", default-features = false, features = ["derive"], optional = true } -serde_yaml = { version = "0.8.24", default-features = false, optional = true } +yaml = { package = "serde_yaml", version = "0.8.24", default-features = false, optional = true } ron = { version = "0.7.1", default-features = false, optional = true } anyhow = { version = "1.0", default-features = false, optional = true } @@ -44,4 +44,4 @@ harness = false [[example]] name = "using_animation_file" -required-features = ["unstable-load-from-file"] +required-features = ["unstable-load-from-file", "yaml"] diff --git a/README.md b/README.md index 657c002..eeac337 100644 --- a/README.md +++ b/README.md @@ -80,11 +80,14 @@ benimator = "3" ## Cargo features +* `yaml` deserialization from yaml asset files (also requires `unstable-load-from-file`) +* `ron` deserialization from ron asset files (also requires `unstable-load-from-file`) + ### Unstable features **Any API behind one of theses feature flags is unstable, should not be considered complete nor part of the public API. Breaking changes to that API may happen in minor releases** -* `unstable-load-from-file` Load animation assets from yaml/ron files. +* `unstable-load-from-file` Load animation assets from yaml/ron files. It also requires either `ron` or `yaml` (or both) features. ## MSRV diff --git a/src/animation/load.rs b/src/animation/load.rs index c2cde75..54d0b81 100644 --- a/src/animation/load.rs +++ b/src/animation/load.rs @@ -2,8 +2,28 @@ use super::SpriteSheetAnimation; use bevy_asset::{AssetLoader, LoadContext, LoadedAsset}; use bevy_utils::BoxedFuture; -#[derive(Debug, Default)] -pub(crate) struct SpriteSheetAnimationLoader; +#[derive(Debug)] +pub(crate) struct SpriteSheetAnimationLoader { + extensions: Vec<&'static str>, +} + +impl Default for SpriteSheetAnimationLoader { + #[allow(clippy::vec_init_then_push)] + fn default() -> Self { + let mut extensions = Vec::with_capacity(3); + + #[cfg(feature = "yaml")] + extensions.push("animation.yml"); + + #[cfg(feature = "yaml")] + extensions.push("animation.yaml"); + + #[cfg(feature = "ron")] + extensions.push("animation.ron"); + + Self { extensions } + } +} impl AssetLoader for SpriteSheetAnimationLoader { fn load<'a>( @@ -13,8 +33,12 @@ impl AssetLoader for SpriteSheetAnimationLoader { ) -> BoxedFuture<'a, Result<(), anyhow::Error>> { Box::pin(async move { let custom_asset = match load_context.path().extension().unwrap().to_str().unwrap() { + #[cfg(feature = "yaml")] "yaml" | "yml" => SpriteSheetAnimation::from_yaml_bytes(bytes)?, + + #[cfg(feature = "ron")] "ron" => SpriteSheetAnimation::from_ron_bytes(bytes)?, + _ => unreachable!(), }; load_context.set_default_asset(LoadedAsset::new(custom_asset)); @@ -23,6 +47,6 @@ impl AssetLoader for SpriteSheetAnimationLoader { } fn extensions(&self) -> &[&str] { - &["animation.yml", "animation.yaml", "animation.ron"] + &self.extensions } } diff --git a/src/animation/parse.rs b/src/animation/parse.rs index b72e898..12a9cba 100644 --- a/src/animation/parse.rs +++ b/src/animation/parse.rs @@ -153,6 +153,7 @@ impl SpriteSheetAnimation { /// # Errors /// /// Returns an error if the content is not a valid yaml representation of an animation + #[cfg(feature = "yaml")] pub fn from_yaml_str(yaml: &str) -> Result { Self::from_yaml_bytes(yaml.as_bytes()) } @@ -184,8 +185,9 @@ impl SpriteSheetAnimation { /// # Errors /// /// Returns an error if the content is not a valid yaml representation of an animation + #[cfg(feature = "yaml")] pub fn from_yaml_bytes(yaml: &[u8]) -> Result { - serde_yaml::from_slice(yaml).map_err(AnimationParseError::new) + yaml::from_slice(yaml).map_err(AnimationParseError::new) } /// Parse content of a ron file represenging the animation @@ -212,6 +214,7 @@ impl SpriteSheetAnimation { /// # Errors /// /// Returns an error if the content is not a valid ron representation of an animation + #[cfg(feature = "ron")] pub fn from_ron_str(ron: &str) -> Result { Self::from_ron_bytes(ron.as_bytes()) } @@ -240,6 +243,7 @@ impl SpriteSheetAnimation { /// # Errors /// /// Returns an error if the content is not a valid ron representation of an animation + #[cfg(feature = "ron")] pub fn from_ron_bytes(ron: &[u8]) -> Result { ron::Options::default() .with_default_extension(ron::extensions::Extensions::IMPLICIT_SOME) @@ -270,6 +274,7 @@ impl Error for AnimationParseError {} mod tests { use super::*; + #[cfg(feature = "yaml")] mod yaml { use super::*; @@ -431,6 +436,7 @@ mod tests { } } + #[cfg(feature = "ron")] mod ron { use super::*;