use core::fmt;
use crate::data_type::CssString;
use crate::to_css::{write_number, ToCss};
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum AnimationDirection {
Normal,
Reverse,
Alternate,
AlternateReverse,
}
impl ToCss for AnimationDirection {
fn to_css(&self, dest: &mut dyn fmt::Write) -> fmt::Result {
dest.write_str(match self {
AnimationDirection::Normal => "normal",
AnimationDirection::Reverse => "reverse",
AnimationDirection::Alternate => "alternate",
AnimationDirection::AlternateReverse => "alternate-reverse",
})
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum AnimationFillMode {
None,
Forwards,
Backwards,
Both,
}
impl ToCss for AnimationFillMode {
fn to_css(&self, dest: &mut dyn fmt::Write) -> fmt::Result {
dest.write_str(match self {
AnimationFillMode::None => "none",
AnimationFillMode::Forwards => "forwards",
AnimationFillMode::Backwards => "backwards",
AnimationFillMode::Both => "both",
})
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum AnimationIterationCount {
Infinite,
Count(f32),
}
impl ToCss for AnimationIterationCount {
fn to_css(&self, dest: &mut dyn fmt::Write) -> fmt::Result {
match self {
AnimationIterationCount::Infinite => dest.write_str("infinite"),
AnimationIterationCount::Count(n) => write_number(dest, *n),
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum AnimationPlayState {
Running,
Paused,
}
impl ToCss for AnimationPlayState {
fn to_css(&self, dest: &mut dyn fmt::Write) -> fmt::Result {
dest.write_str(match self {
AnimationPlayState::Running => "running",
AnimationPlayState::Paused => "paused",
})
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum TransitionPropertyKind {
All,
None,
Name(CssString),
}
impl TransitionPropertyKind {
pub fn name(s: impl Into<String>) -> Self {
Self::Name(CssString::new(s))
}
}
impl ToCss for TransitionPropertyKind {
fn to_css(&self, dest: &mut dyn fmt::Write) -> fmt::Result {
match self {
TransitionPropertyKind::All => dest.write_str("all"),
TransitionPropertyKind::None => dest.write_str("none"),
TransitionPropertyKind::Name(n) => dest.write_str(n.as_str()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn animation_direction_all() {
let cases = [
(AnimationDirection::Normal, "normal"),
(AnimationDirection::Reverse, "reverse"),
(AnimationDirection::Alternate, "alternate"),
(AnimationDirection::AlternateReverse, "alternate-reverse"),
];
for (k, expected) in cases {
assert_eq!(k.to_css_string(), expected);
}
}
#[test]
fn animation_fill_mode_all() {
let cases = [
(AnimationFillMode::None, "none"),
(AnimationFillMode::Forwards, "forwards"),
(AnimationFillMode::Backwards, "backwards"),
(AnimationFillMode::Both, "both"),
];
for (k, expected) in cases {
assert_eq!(k.to_css_string(), expected);
}
}
#[test]
fn iteration_count_keyword() {
assert_eq!(
AnimationIterationCount::Infinite.to_css_string(),
"infinite"
);
}
#[test]
fn iteration_count_number() {
assert_eq!(AnimationIterationCount::Count(2.0).to_css_string(), "2");
assert_eq!(AnimationIterationCount::Count(0.5).to_css_string(), "0.5");
}
#[test]
fn play_state_all() {
assert_eq!(AnimationPlayState::Running.to_css_string(), "running");
assert_eq!(AnimationPlayState::Paused.to_css_string(), "paused");
}
#[test]
fn transition_property_all() {
assert_eq!(TransitionPropertyKind::All.to_css_string(), "all");
assert_eq!(TransitionPropertyKind::None.to_css_string(), "none");
assert_eq!(
TransitionPropertyKind::name("opacity").to_css_string(),
"opacity"
);
}
}