use crate::mode::PreviewMode;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct PreviewOptions {
width: Option<u32>,
height: Option<u32>,
keep_aspect_ratio: Option<bool>,
mode: Option<PreviewMode>,
}
impl PreviewOptions {
pub const fn new() -> Self {
Self {
width: None,
height: None,
keep_aspect_ratio: None,
mode: None,
}
}
#[must_use]
pub const fn width(mut self, width: u32) -> Self {
self.width = Some(width);
self
}
#[must_use]
pub const fn height(mut self, height: u32) -> Self {
self.height = Some(height);
self
}
#[must_use]
pub const fn dimensions(mut self, width: u32, height: u32) -> Self {
self.width = Some(width);
self.height = Some(height);
self
}
#[must_use]
pub const fn keep_aspect_ratio(mut self, keep: bool) -> Self {
self.keep_aspect_ratio = Some(keep);
self
}
#[must_use]
pub const fn mode(mut self, mode: PreviewMode) -> Self {
self.mode = Some(mode);
self
}
pub const fn width_px(&self) -> Option<u32> {
self.width
}
pub const fn height_px(&self) -> Option<u32> {
self.height
}
pub const fn aspect_ratio(&self) -> Option<bool> {
self.keep_aspect_ratio
}
pub const fn preview_mode(&self) -> Option<PreviewMode> {
self.mode
}
}
#[cfg(test)]
mod tests {
use super::PreviewOptions;
use crate::mode::PreviewMode;
#[test]
fn default_options_are_empty() {
let options = PreviewOptions::new();
assert_eq!(options.width_px(), None);
assert_eq!(options.height_px(), None);
assert_eq!(options.aspect_ratio(), None);
assert_eq!(options.preview_mode(), None);
}
#[test]
fn builder_sets_all_options() {
let options = PreviewOptions::new()
.width(256)
.height(512)
.keep_aspect_ratio(true)
.mode(PreviewMode::Cover);
assert_eq!(options.width_px(), Some(256));
assert_eq!(options.height_px(), Some(512));
assert_eq!(options.aspect_ratio(), Some(true));
assert_eq!(options.preview_mode(), Some(PreviewMode::Cover));
}
#[test]
fn dimensions_helper_sets_both_axes() {
let options = PreviewOptions::new().dimensions(1024, 768);
assert_eq!(options.width_px(), Some(1024));
assert_eq!(options.height_px(), Some(768));
}
}