Skip to main content

nextcloud_preview/
mode.rs

1//! Preview scaling modes for Nextcloud thumbnails.
2
3use std::fmt;
4
5/// Scaling mode for generating previews.
6///
7/// Controls how Nextcloud resizes or crops the image when generating a thumbnail
8/// with dimensions that do not match the original aspect ratio.
9///
10/// See the Nextcloud preview endpoint parameters for details.
11#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
12pub enum PreviewMode {
13    /// Scale image to fill dimensions and crop excess to fit the requested box.
14    Cover,
15    /// Scale image to fit within dimensions without cropping (preserving aspect ratio).
16    Fill,
17}
18
19impl PreviewMode {
20    /// Returns the wire parameter string for this preview mode.
21    pub const fn as_str(self) -> &'static str {
22        match self {
23            Self::Cover => "cover",
24            Self::Fill => "fill",
25        }
26    }
27}
28
29impl fmt::Display for PreviewMode {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        f.write_str(self.as_str())
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use super::PreviewMode;
38
39    #[test]
40    fn as_str_returns_expected_wire_values() {
41        assert_eq!(PreviewMode::Cover.as_str(), "cover");
42        assert_eq!(PreviewMode::Fill.as_str(), "fill");
43    }
44
45    #[test]
46    fn display_matches_as_str() {
47        assert_eq!(PreviewMode::Cover.to_string(), "cover");
48        assert_eq!(PreviewMode::Fill.to_string(), "fill");
49    }
50
51    #[test]
52    fn equality_and_copy() {
53        let mode = PreviewMode::Cover;
54        let copied = mode;
55        assert_eq!(mode, copied);
56        assert_ne!(mode, PreviewMode::Fill);
57    }
58}