1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//! Defines the output of a captioning operation.

use std::ops::Deref;

use image::ImageFormat;
use mime::{self, Mime};


/// Output of the captioning process.
#[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 {
    /// Image format of the output.
    #[inline]
    pub fn format(&self) -> ImageFormat {
        self.format
    }

    /// Raw bytes of the output.
    ///
    /// See `CaptionOutput::format` for how to interpret it.
    #[inline]
    pub fn bytes(&self) -> &[u8] {
        &self.bytes[..]
    }

    /// Convert the output into a vector of bytes.
    #[inline]
    pub fn into_bytes(self) -> Vec<u8> {
        self.bytes
    }

    /// Convert the output into boxed slice of bytes.
    #[inline]
    pub fn into_boxed_bytes(self) -> Box<[u8]> {
        self.bytes.into_boxed_slice()
    }

    /// The MIME type that matches output's format.
    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()
    }
}