#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum Device {
#[default]
X4,
X3,
}
impl Device {
pub fn max_size(self) -> (u32, u32) {
match self {
Device::X4 => (440, 733),
Device::X3 => (484, 726),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Rotation {
Clockwise,
Counterclockwise,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Options {
max_width: u32,
max_height: u32,
jpeg_quality: u8,
sharpen: Option<f32>,
rotate: Option<Rotation>,
}
impl Options {
pub fn builder() -> OptionsBuilder {
OptionsBuilder::default()
}
pub fn max_width(&self) -> u32 {
self.max_width
}
pub fn max_height(&self) -> u32 {
self.max_height
}
pub fn jpeg_quality(&self) -> u8 {
self.jpeg_quality
}
pub fn sharpen(&self) -> Option<f32> {
self.sharpen
}
pub fn rotate(&self) -> Option<Rotation> {
self.rotate
}
}
impl Default for Options {
fn default() -> Self {
let (max_width, max_height) = Device::default().max_size();
Self {
max_width,
max_height,
jpeg_quality: 92,
sharpen: Some(0.8),
rotate: None,
}
}
}
#[derive(Debug, Clone, Default, PartialEq)]
#[must_use = "a builder does nothing until `build` is called"]
pub struct OptionsBuilder {
opts: Options,
}
impl OptionsBuilder {
pub fn max_size(mut self, width: u32, height: u32) -> Self {
self.opts.max_width = width;
self.opts.max_height = height;
self
}
pub fn device(self, device: Device) -> Self {
let (width, height) = device.max_size();
self.max_size(width, height)
}
pub fn jpeg_quality(mut self, quality: u8) -> Self {
self.opts.jpeg_quality = quality;
self
}
pub fn sharpen(mut self, sigma: f32) -> Self {
self.opts.sharpen = Some(sigma);
self
}
pub fn no_sharpen(mut self) -> Self {
self.opts.sharpen = None;
self
}
pub fn rotate(mut self, rotation: Rotation) -> Self {
self.opts.rotate = Some(rotation);
self
}
pub fn no_rotate(mut self) -> Self {
self.opts.rotate = None;
self
}
pub fn build(self) -> Result<Options, InvalidOptions> {
let o = self.opts;
if o.max_width == 0 || o.max_height == 0 {
return Err(InvalidOptions::MaxSize {
width: o.max_width,
height: o.max_height,
});
}
if !(1..=100).contains(&o.jpeg_quality) {
return Err(InvalidOptions::JpegQuality(o.jpeg_quality));
}
if let Some(sigma) = o.sharpen
&& !(sigma.is_finite() && sigma > 0.0)
{
return Err(InvalidOptions::Sharpen(sigma));
}
Ok(o)
}
}
#[derive(Debug, Clone, PartialEq, thiserror::Error)]
#[non_exhaustive]
pub enum InvalidOptions {
#[error("maximum image size must be at least 1x1, got {width}x{height}")]
MaxSize {
width: u32,
height: u32,
},
#[error("JPEG quality must be between 1 and 100, got {0}")]
JpegQuality(u8),
#[error("sharpening sigma must be finite and greater than 0, got {0}")]
Sharpen(f32),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn builder_starts_from_the_defaults() {
assert_eq!(Options::builder().build(), Ok(Options::default()));
}
#[test]
fn setters_change_only_their_setting() {
let opts = Options::builder()
.max_size(600, 800)
.jpeg_quality(85)
.no_sharpen()
.build()
.unwrap();
assert_eq!((opts.max_width(), opts.max_height()), (600, 800));
assert_eq!(opts.jpeg_quality(), 85);
assert_eq!(opts.sharpen(), None);
let sharp = Options::builder().sharpen(1.5).build().unwrap();
assert_eq!(sharp.sharpen(), Some(1.5));
assert_eq!(sharp.max_width(), Options::default().max_width());
}
#[test]
fn device_sets_the_image_box() {
let x3 = Options::builder().device(Device::X3).build().unwrap();
assert_eq!((x3.max_width(), x3.max_height()), (484, 726));
let x4 = Options::builder().device(Device::X4).build().unwrap();
assert_eq!(x4, Options::default());
let custom = Options::builder()
.device(Device::X3)
.max_size(600, 800)
.build()
.unwrap();
assert_eq!((custom.max_width(), custom.max_height()), (600, 800));
}
#[test]
fn x3_box_keeps_the_x4_margins() {
let (w, h) = Device::X3.max_size();
assert_eq!(w, (528.0_f64 * 440.0 / 480.0).round() as u32);
assert_eq!(h, (792.0_f64 * 733.0 / 800.0).round() as u32);
}
#[test]
fn rotation_is_off_by_default() {
assert_eq!(Options::default().rotate(), None);
let cw = Options::builder()
.rotate(Rotation::Clockwise)
.build()
.unwrap();
assert_eq!(cw.rotate(), Some(Rotation::Clockwise));
let ccw = Options::builder()
.rotate(Rotation::Counterclockwise)
.build()
.unwrap();
assert_eq!(ccw.rotate(), Some(Rotation::Counterclockwise));
let off = Options::builder().no_rotate().build().unwrap();
assert_eq!(off.rotate(), None);
}
#[test]
fn build_rejects_out_of_range_settings() {
let b = Options::builder;
assert_eq!(
b().max_size(0, 800).build(),
Err(InvalidOptions::MaxSize {
width: 0,
height: 800
})
);
assert!(matches!(
b().max_size(600, 0).build(),
Err(InvalidOptions::MaxSize { .. })
));
assert_eq!(
b().jpeg_quality(0).build(),
Err(InvalidOptions::JpegQuality(0))
);
assert_eq!(
b().jpeg_quality(101).build(),
Err(InvalidOptions::JpegQuality(101))
);
for sigma in [0.0, -1.0, f32::INFINITY, f32::NAN] {
assert!(
matches!(b().sharpen(sigma).build(), Err(InvalidOptions::Sharpen(_))),
"sigma {sigma}"
);
}
}
#[test]
fn errors_name_the_value() {
let err = Options::builder().jpeg_quality(150).build().unwrap_err();
assert_eq!(
err.to_string(),
"JPEG quality must be between 1 and 100, got 150"
);
}
}