maple_render_core/anim.rs
1//! Common trait for animation encoders (GIF / WebP / Video).
2//!
3//! Each encoder consumes a [`Renders`] stream of fully-rasterized frames and
4//! produces its own binary format. The trait lets the CLI dispatch on an
5//! `OutputFormat` enum without duplicating orchestration logic.
6
7use std::path::Path;
8
9use crate::error::Result;
10
11/// The binary container format to emit for an animation.
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13#[repr(u8)]
14pub enum OutputFormat {
15 Gif,
16 Webp,
17 Video,
18}
19
20/// Something that can consume rendered frames and emit an encoded animation.
21pub trait AnimEncoder {
22 /// Encode all frames into the target format, returning the bytes.
23 fn encode(&mut self) -> Result<Vec<u8>>;
24
25 /// Encode all frames and write them to `path`.
26 fn save<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
27 let data = self.encode()?;
28 let mut file = std::fs::File::create(path.as_ref())?;
29 std::io::Write::write_all(&mut file, &data)?;
30 Ok(())
31 }
32}