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        "archive" => format == "zip",
40        _ => false,
41    }
42}
43
44pub fn capabilities() -> Capabilities {
45    let imageio = crate::image_backend_available();
46    let imageio_note = if imageio { "" } else { IMAGEIO_NOTE };
47    Capabilities {
48        version: env!("CARGO_PKG_VERSION"),
49        platform: std::env::consts::OS,
50        features: vec![
51            Capability {
52                id: "png_lossless",
53                available: true,
54                note: "",
55            },
56            Capability {
57                id: "webp",
58                available: true,
59                note: if imageio {
60                    ""
61                } else {
62                    "Inputs limited to PNG/WebP without embedded color profiles or EXIF orientation."
63                },
64            },
65            Capability {
66                id: "jpeg",
67                available: imageio,
68                note: imageio_note,
69            },
70            Capability {
71                id: "heic",
72                available: imageio,
73                note: imageio_note,
74            },
75            Capability {
76                id: "svga",
77                available: true,
78                note: "SVGA 2.x: lossless recompression of embedded PNG images.",
79            },
80            Capability {
81                id: "apply_restore",
82                available: true,
83                note: "",
84            },
85        ],
86        tools: crate::tools::detect(),
87    }
88}