Skip to main content

resopt/
capabilities.rs

1//! Runtime capability report shared by `doctor`, the web UI and the inventory.
2use serde::Serialize;
3
4#[derive(Debug, Clone, Serialize)]
5pub struct Capability {
6    pub id: &'static str,
7    pub available: bool,
8    /// Why it is unavailable, or what it is limited to, on this platform.
9    pub note: &'static str,
10}
11
12#[derive(Debug, Clone, Serialize)]
13pub struct Capabilities {
14    pub version: &'static str,
15    pub platform: &'static str,
16    pub features: Vec<Capability>,
17    pub tools: Vec<crate::tools::Tool>,
18}
19
20const IMAGEIO_NOTE: &str = "Requires Apple ImageIO; available on macOS only.";
21
22/// Formats with a decoder on this platform.
23pub(crate) fn decodable(format: &str) -> bool {
24    if crate::image_backend_available() {
25        matches!(
26            format,
27            "png" | "jpeg" | "heic" | "heif" | "webp" | "gif" | "tiff" | "bmp" | "avif"
28        )
29    } else {
30        matches!(format, "png" | "webp")
31    }
32}
33
34/// Whether any optimization backend exists for this resource on this platform.
35pub(crate) fn can_optimize(kind: &str, format: &str) -> bool {
36    match kind {
37        "image" => decodable(format),
38        "animation" => format == "svga",
39        _ => false,
40    }
41}
42
43pub fn capabilities() -> Capabilities {
44    let imageio = crate::image_backend_available();
45    let imageio_note = if imageio { "" } else { IMAGEIO_NOTE };
46    Capabilities {
47        version: env!("CARGO_PKG_VERSION"),
48        platform: std::env::consts::OS,
49        features: vec![
50            Capability {
51                id: "png_lossless",
52                available: true,
53                note: "",
54            },
55            Capability {
56                id: "webp",
57                available: true,
58                note: if imageio {
59                    ""
60                } else {
61                    "Inputs limited to PNG/WebP without embedded color profiles or EXIF orientation."
62                },
63            },
64            Capability {
65                id: "jpeg",
66                available: imageio,
67                note: imageio_note,
68            },
69            Capability {
70                id: "heic",
71                available: imageio,
72                note: imageio_note,
73            },
74            Capability {
75                id: "svga",
76                available: true,
77                note: "SVGA 2.x: lossless recompression of embedded PNG images.",
78            },
79            Capability {
80                id: "apply_restore",
81                available: true,
82                note: "",
83            },
84        ],
85        tools: crate::tools::detect(),
86    }
87}