#[cfg(any(feature = "std", feature = "alloc"))]
extern crate alloc;
use core::fmt::Debug;
#[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
}
}
pub enum SubtitlePayload<B> {
Text {
text: B,
language: Option<[u8; 3]>,
},
#[cfg(any(feature = "std", feature = "alloc"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
Bitmap {
regions: alloc::vec::Vec<BitmapRegion<B>>,
},
}
impl<B: Debug> Debug for SubtitlePayload<B> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Text { text, language } => f
.debug_struct("SubtitlePayload::Text")
.field("text", text)
.field("language", language)
.finish(),
#[cfg(any(feature = "std", feature = "alloc"))]
Self::Bitmap { regions } => f
.debug_struct("SubtitlePayload::Bitmap")
.field("regions", ®ions.len())
.finish(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn text_payload_constructs() {
let p: SubtitlePayload<&[u8]> = SubtitlePayload::Text {
text: b"hello",
language: Some(*b"eng"),
};
match p {
SubtitlePayload::Text { text, language } => {
assert_eq!(text, b"hello");
assert_eq!(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 {
regions: alloc::vec![BitmapRegion::new(0, 0, 4, 4, 4, data, pal)],
};
if let SubtitlePayload::Bitmap { regions } = p {
assert_eq!(regions.len(), 1);
} else {
panic!("unexpected variant");
}
}
}