Skip to content

Commit

Permalink
feat(unstable-load-from-file): yaml and ron feature flags to avoi…
Browse files Browse the repository at this point in the history
…d pulling unnecessary dependencies
  • Loading branch information
jcornaz committed Jun 22, 2022
1 parent 30941ff commit 1201237
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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 }

Expand All @@ -44,4 +44,4 @@ harness = false

[[example]]
name = "using_animation_file"
required-features = ["unstable-load-from-file"]
required-features = ["unstable-load-from-file", "yaml"]
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
30 changes: 27 additions & 3 deletions src/animation/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>(
Expand All @@ -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));
Expand All @@ -23,6 +47,6 @@ impl AssetLoader for SpriteSheetAnimationLoader {
}

fn extensions(&self) -> &[&str] {
&["animation.yml", "animation.yaml", "animation.ron"]
&self.extensions
}
}
8 changes: 7 additions & 1 deletion src/animation/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, AnimationParseError> {
Self::from_yaml_bytes(yaml.as_bytes())
}
Expand Down Expand Up @@ -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<Self, AnimationParseError> {
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
Expand All @@ -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, AnimationParseError> {
Self::from_ron_bytes(ron.as_bytes())
}
Expand Down Expand Up @@ -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<Self, AnimationParseError> {
ron::Options::default()
.with_default_extension(ron::extensions::Extensions::IMPLICIT_SOME)
Expand Down Expand Up @@ -270,6 +274,7 @@ impl Error for AnimationParseError {}
mod tests {
use super::*;

#[cfg(feature = "yaml")]
mod yaml {
use super::*;

Expand Down Expand Up @@ -431,6 +436,7 @@ mod tests {
}
}

#[cfg(feature = "ron")]
mod ron {
use super::*;

Expand Down

0 comments on commit 1201237

Please sign in to comment.