Files
asciinema/src/encoder/mod.rs

39 lines
916 B
Rust
Raw Normal View History

2024-01-25 16:01:14 +01:00
mod asciicast;
mod raw;
mod txt;
2025-03-25 23:02:35 +01:00
use std::fs::File;
use std::io::Write;
use anyhow::Result;
2024-01-25 16:01:14 +01:00
use crate::asciicast::Event;
2024-11-12 13:46:38 +01:00
use crate::asciicast::Header;
pub use asciicast::{AsciicastV2Encoder, AsciicastV3Encoder};
2024-11-12 13:46:38 +01:00
pub use raw::RawEncoder;
pub use txt::TextEncoder;
2024-01-25 16:01:14 +01:00
pub trait Encoder {
2024-11-12 13:46:38 +01:00
fn header(&mut self, header: &Header) -> Vec<u8>;
fn event(&mut self, event: Event) -> Vec<u8>;
2024-11-12 13:46:38 +01:00
fn flush(&mut self) -> Vec<u8>;
2024-01-25 16:01:14 +01:00
}
2024-01-25 16:41:27 +01:00
pub trait EncoderExt {
fn encode_to_file(&mut self, cast: crate::asciicast::Asciicast, file: &mut File) -> Result<()>;
2024-01-25 16:41:27 +01:00
}
impl<E: Encoder + ?Sized> EncoderExt for E {
fn encode_to_file(&mut self, cast: crate::asciicast::Asciicast, file: &mut File) -> Result<()> {
2024-11-12 13:46:38 +01:00
file.write_all(&self.header(&cast.header))?;
2024-01-25 16:41:27 +01:00
for event in cast.events {
file.write_all(&self.event(event?))?;
2024-01-25 16:41:27 +01:00
}
2024-11-12 13:46:38 +01:00
file.write_all(&self.flush())?;
2024-01-25 16:41:27 +01:00
Ok(())
}
}