Skip to content
This repository was archived by the owner on Feb 16, 2025. It is now read-only.

Commit

Permalink
feat(unstable-load-from-file): use a more yaml-y syntax for mode 'rep…
Browse files Browse the repository at this point in the history
…eat-from'
  • Loading branch information
jcornaz committed Jun 6, 2022
1 parent 6462f42 commit 4db7d9c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 48 deletions.
54 changes: 7 additions & 47 deletions src/animation/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
time::Duration,
};

use serde::{de, Deserialize, Deserializer};
use serde::Deserialize;

use super::{Frame, Mode, SpriteSheetAnimation};

Expand All @@ -15,6 +15,8 @@ pub(super) struct AnimationDto {
frames: Vec<FrameDto>,
}

#[derive(Deserialize)]
#[serde(rename_all = "kebab-case")]
enum ModeDto {
Repeat,
RepeatFrom(usize),
Expand Down Expand Up @@ -82,7 +84,7 @@ impl SpriteSheetAnimation {
///
/// ```yaml
/// # The mode can be one of: 'once', 'repeat', 'ping-pong'
/// # or 'repeatFrom(n)' (where 'n' is the frame-index to repeat from)
/// # or 'repeat-from: n' (where 'n' is the frame-index to repeat from)
/// # The default is 'repeat'
/// mode: ping-pong
/// frames:
Expand All @@ -108,7 +110,7 @@ impl SpriteSheetAnimation {
///
/// ```yaml
/// # The mode can be one of: 'once', 'repeat', 'ping-pong'
/// # or 'repeatFrom(n)' (where 'n' is the frame-index to repeat from)
/// # or 'repeat-from: n' (where 'n' is the frame-index to repeat from)
/// # The default is 'repeat'
/// mode: ping-pong
/// frames:
Expand Down Expand Up @@ -141,49 +143,6 @@ impl Display for AnimationParseError {

impl Error for AnimationParseError {}

impl<'de> Deserialize<'de> for ModeDto {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_str(ModeVisitor)
}
}

struct ModeVisitor;

impl<'de> de::Visitor<'de> for ModeVisitor {
type Value = ModeDto;

fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
match s {
"ping-pong" => Ok(ModeDto::PingPong),
"repeat" => Ok(ModeDto::Repeat),
"once" => Ok(ModeDto::Once),
_ => {
match s
.strip_prefix("repeat-from(")
.and_then(|s| s.strip_suffix(')'))
.and_then(|s| s.parse::<usize>().ok())
{
Some(index) => Ok(ModeDto::RepeatFrom(index)),
None => Err(de::Error::invalid_value(de::Unexpected::Str(s), &self)),
}
}
}
}

fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
write!(
formatter,
"one of: 'repeat', 'once', 'ping-pong', 'repeat-from(n)'"
)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -267,7 +226,8 @@ mod tests {
fn parse_yaml_repeat_from() {
// given
let content = "
mode: repeat-from(1)
mode:
repeat-from: 1
frames:
- index: 0
duration: 100
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
//! First, create an asset file with the extension `.animation.yml`:
//! ```yaml
//! # The mode can be one of: 'once', 'repeat', 'ping-pong'
//! # or 'repeatFrom(n)' (where 'n' is the frame-index to repeat from)
//! # or 'repeat-from: n' (where 'n' is the frame-index to repeat from)
//! # The default is 'repeat'
//! mode: ping-pong
//! frames:
Expand Down

0 comments on commit 4db7d9c

Please sign in to comment.