use derive_more::{IsVariant, TryUnwrap, Unwrap};
#[cfg(any(feature = "std", feature = "alloc"))]
extern crate alloc;
#[cfg(any(feature = "std", feature = "alloc"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
#[derive(Debug, Clone)]
pub struct BitmapRegion<B> {
x: u32,
y: u32,
width: u32,
height: u32,
stride: u32,
data: B,
palette: B,
}
#[cfg(any(feature = "std", feature = "alloc"))]
impl<B> BitmapRegion<B> {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn new(
x: u32,
y: u32,
width: u32,
height: u32,
stride: u32,
data: B,
palette: B,
) -> Self {
Self {
x,
y,
width,
height,
stride,
data,
palette,
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn x(&self) -> u32 {
self.x
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn y(&self) -> u32 {
self.y
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn width(&self) -> u32 {
self.width
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn height(&self) -> u32 {
self.height
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn stride(&self) -> u32 {
self.stride
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn data(&self) -> &B {
&self.data
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn palette(&self) -> &B {
&self.palette
}
}
#[derive(Debug, Clone)]
pub struct Text<B> {
text: B,
language: Option<[u8; 3]>,
}
impl<B> Text<B> {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn new(text: B, language: Option<[u8; 3]>) -> Self {
Self { text, language }
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn text(&self) -> &B {
&self.text
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn language(&self) -> Option<[u8; 3]> {
self.language
}
}
#[cfg(any(feature = "std", feature = "alloc"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
#[derive(Debug, Clone)]
pub struct Bitmap<B> {
regions: alloc::vec::Vec<BitmapRegion<B>>,
}
#[cfg(any(feature = "std", feature = "alloc"))]
impl<B> Bitmap<B> {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn new(regions: alloc::vec::Vec<BitmapRegion<B>>) -> Self {
Self { regions }
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn regions(&self) -> &[BitmapRegion<B>] {
&self.regions
}
}
#[derive(Debug, Clone, IsVariant, Unwrap, TryUnwrap)]
#[unwrap(ref, ref_mut)]
#[try_unwrap(ref, ref_mut)]
pub enum SubtitlePayload<B> {
Text(Text<B>),
#[cfg(any(feature = "std", feature = "alloc"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
Bitmap(Bitmap<B>),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn text_payload_constructs() {
let p: SubtitlePayload<&[u8]> = SubtitlePayload::Text(Text::new(b"hello", Some(*b"eng")));
match p {
SubtitlePayload::Text(payload) => {
assert_eq!(payload.text(), b"hello");
assert_eq!(payload.language(), Some(*b"eng"));
}
#[cfg(any(feature = "std", feature = "alloc"))]
_ => panic!("unexpected variant"),
}
}
#[cfg(any(feature = "std", feature = "alloc"))]
#[test]
fn bitmap_region_construction() {
let data: &[u8] = &[0; 16];
let pal: &[u8] = &[0; 16];
let r = BitmapRegion::new(10, 20, 4, 4, 4, data, pal);
assert_eq!(r.x(), 10);
assert_eq!(r.width(), 4);
assert_eq!(*r.data(), data);
}
#[cfg(any(feature = "std", feature = "alloc"))]
#[test]
fn bitmap_payload_constructs() {
let data: &[u8] = &[0; 16];
let pal: &[u8] = &[0; 16];
let p: SubtitlePayload<&[u8]> =
SubtitlePayload::Bitmap(Bitmap::new(alloc::vec![BitmapRegion::new(
0, 0, 4, 4, 4, data, pal
)]));
if let SubtitlePayload::Bitmap(payload) = p {
assert_eq!(payload.regions().len(), 1);
} else {
panic!("unexpected variant");
}
}
}