Skip to main content

mbook_converter/
options.rs

1//! Options and limits of a conversion.
2
3use crate::format::Format;
4use mbook::{Limits, WriteOptions};
5
6/// How a source is converted. Start from [`ConvertOptions::default`] and
7/// change the fields that matter.
8///
9/// # Examples
10///
11/// ```
12/// use mbook_converter::{ConvertOptions, Format};
13///
14/// let mut options = ConvertOptions::default();
15/// options.format = Some(Format::Fb2);
16/// options.pictures.quality = 60;
17/// ```
18#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
19#[non_exhaustive]
20pub struct ConvertOptions {
21    /// The format of the source. `None`, the default, detects it from the
22    /// content.
23    pub format: Option<Format>,
24    /// How pictures are encoded.
25    pub pictures: PictureOptions,
26    /// Bounds on what a source may ask for.
27    pub limits: ConvertLimits,
28    /// How the mbook file is written by [`Converter::convert`] and
29    /// [`Converter::convert_slice`].
30    ///
31    /// [`Converter::convert`]: crate::Converter::convert
32    /// [`Converter::convert_slice`]: crate::Converter::convert_slice
33    pub write: WriteOptions,
34}
35
36/// How pictures are encoded as AVIF. A value outside its range is brought
37/// into the range.
38#[derive(Clone, Copy, Debug, PartialEq, Eq)]
39#[non_exhaustive]
40pub struct PictureOptions {
41    /// Quality from 1 to 100. The default is 80.
42    pub quality: u8,
43    /// Speed from 1, the smallest file, to 10, the fastest. The default is 6.
44    pub speed: u8,
45}
46
47impl Default for PictureOptions {
48    fn default() -> Self {
49        Self {
50            quality: 80,
51            speed: 6,
52        }
53    }
54}
55
56/// Bounds applied before memory is reserved for something a source declares.
57#[derive(Clone, Copy, Debug, PartialEq, Eq)]
58#[non_exhaustive]
59pub struct ConvertLimits {
60    /// The largest source. The default is 2 GiB.
61    pub max_source_bytes: u64,
62    /// The largest picture, as width times height. The default is
63    /// 100,000,000 pixels.
64    pub max_picture_pixels: u64,
65    /// The limits of the converted book. `max_resource_bytes` also bounds
66    /// one unpacked entry of an archive, and `max_file_bytes` everything
67    /// unpacked together.
68    pub book: Limits,
69}
70
71impl Default for ConvertLimits {
72    fn default() -> Self {
73        Self {
74            max_source_bytes: 2 << 30,
75            max_picture_pixels: 100_000_000,
76            book: Limits::default(),
77        }
78    }
79}
80
81#[cfg(test)]
82mod tests {
83    use super::{ConvertLimits, ConvertOptions, PictureOptions};
84
85    #[test]
86    fn the_defaults_match_the_converter_rfc() {
87        let options = ConvertOptions::default();
88        assert_eq!(options.format, None);
89        assert_eq!((options.pictures.quality, options.pictures.speed), (80, 6));
90        assert_eq!(
91            (
92                options.limits.max_source_bytes,
93                options.limits.max_picture_pixels
94            ),
95            (2 << 30, 100_000_000)
96        );
97        assert_eq!(options.limits.book, mbook::Limits::default());
98    }
99
100    #[test]
101    fn options_are_changed_by_assigning_fields() {
102        let mut options = ConvertOptions::default();
103        options.pictures.quality = 60;
104        options.limits.max_source_bytes = 1;
105        assert_eq!(
106            (options.pictures, options.limits.max_source_bytes),
107            (
108                PictureOptions {
109                    quality: 60,
110                    speed: 6
111                },
112                1
113            )
114        );
115        assert_eq!(options.limits.book, ConvertLimits::default().book);
116    }
117}