use std::ops::Deref;
use image::ImageFormat;
use mime::{self, Mime};
#[derive(Clone, Debug)]
#[must_use = "unused caption output which must be used"]
pub struct CaptionOutput {
format: ImageFormat,
bytes: Vec<u8>,
}
impl CaptionOutput {
#[inline]
pub(super) fn new(format: ImageFormat, bytes: Vec<u8>) -> Self {
CaptionOutput{format, bytes}
}
}
impl CaptionOutput {
#[inline]
pub fn format(&self) -> ImageFormat {
self.format
}
#[inline]
pub fn bytes(&self) -> &[u8] {
&self.bytes[..]
}
#[inline]
pub fn into_bytes(self) -> Vec<u8> {
self.bytes
}
#[inline]
pub fn into_boxed_bytes(self) -> Box<[u8]> {
self.bytes.into_boxed_slice()
}
pub fn mime_type(&self) -> Option<Mime> {
match self.format {
ImageFormat::GIF => Some(mime::IMAGE_GIF),
ImageFormat::JPEG => Some(mime::IMAGE_JPEG),
ImageFormat::PNG => Some(mime::IMAGE_PNG),
_ => None,
}
}
}
impl Deref for CaptionOutput {
type Target = [u8];
fn deref(&self) -> &Self::Target {
self.bytes()
}
}
impl Into<Vec<u8>> for CaptionOutput {
fn into(self) -> Vec<u8> {
self.into_bytes()
}
}
impl Into<Box<[u8]>> for CaptionOutput {
fn into(self) -> Box<[u8]> {
self.into_boxed_bytes()
}
}