Skip to main content

agentmux_srv_core/backend/file_icons/
request.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! What the page may ask the icon route for. Every field is enumerated or
5//! validated here, so nothing the page sends reaches the operating system as
6//! anything but a file type and a size (spec §4.1, §6).
7
8/// The sizes an icon can be asked for, in pixels: CSS size times the display
9/// scale and the pane's zoom, snapped up to one of these by the page.
10pub const PX_SIZES: [u32; 10] = [16, 20, 24, 32, 40, 48, 64, 96, 128, 256];
11
12/// The longest extension accepted (without its dot).
13pub const MAX_EXT_LEN: usize = 24;
14
15/// What an icon is for.
16#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
17pub enum Kind {
18    /// A file of some type, by extension. An empty extension is a file without one.
19    File,
20    /// A folder (the generic one; a folder with its own icon is a later phase).
21    Dir,
22}
23
24impl Kind {
25    pub fn as_str(self) -> &'static str {
26        match self {
27            Kind::File => "file",
28            Kind::Dir => "dir",
29        }
30    }
31}
32
33/// A validated request for the icon of a file type.
34#[derive(Clone, Debug, PartialEq, Eq, Hash)]
35pub struct IconRequest {
36    pub kind: Kind,
37    /// Lower case, no dot, `[a-z0-9_+-]`; empty for a folder or a file with no extension.
38    pub ext: String,
39    pub px: u32,
40}
41
42impl IconRequest {
43    /// Parse the route's three query parameters; the error is the sentence the 400 carries.
44    pub fn parse(kind: Option<&str>, ext: Option<&str>, px: Option<&str>) -> Result<Self, &'static str> {
45        let kind = match kind {
46            Some("file") => Kind::File,
47            Some("dir") => Kind::Dir,
48            _ => return Err("kind must be file or dir"),
49        };
50        let ext = ext.unwrap_or("");
51        if ext.len() > MAX_EXT_LEN
52            || !ext.bytes().all(|b| b.is_ascii_alphanumeric() || matches!(b, b'_' | b'+' | b'-'))
53        {
54            return Err("ext must be up to 24 letters, digits, _, + or -");
55        }
56        if kind == Kind::Dir && !ext.is_empty() {
57            return Err("a dir has no ext");
58        }
59        let px = px.and_then(|p| p.parse::<u32>().ok()).filter(|p| PX_SIZES.contains(p)).ok_or("px is not an offered size")?;
60        Ok(IconRequest { kind, ext: ext.to_ascii_lowercase(), px })
61    }
62
63    /// The name the shell is asked about: it never exists, the shell only reads its type.
64    pub fn shell_name(&self) -> String {
65        match (self.kind, self.ext.as_str()) {
66            (Kind::Dir, _) => "folder".to_string(),
67            (Kind::File, "") => "file".to_string(),
68            (Kind::File, ext) => format!("file.{ext}"),
69        }
70    }
71}
72
73/// The native icon list that covers `px`, as (list number, its pixel size): the
74/// smallest one at least that big, so the picture is only ever scaled down.
75/// 0 is the shell's large list (32), 1 small (16), 2 extra large (48), 4 jumbo (256).
76pub fn native_list(px: u32) -> (i32, u32) {
77    match px {
78        0..=16 => (1, 16),
79        17..=32 => (0, 32),
80        33..=48 => (2, 48),
81        _ => (4, 256),
82    }
83}
84
85#[cfg(test)]
86mod tests {
87    use super::*;
88
89    fn parse(kind: &str, ext: &str, px: &str) -> Result<IconRequest, &'static str> {
90        IconRequest::parse(Some(kind), Some(ext), Some(px))
91    }
92
93    #[test]
94    fn a_good_request_is_lowercased_and_named_for_the_shell() {
95        let r = parse("file", "RS", "16").unwrap();
96        assert_eq!((r.kind, r.ext.as_str(), r.px), (Kind::File, "rs", 16));
97        assert_eq!(r.shell_name(), "file.rs");
98        assert_eq!(parse("file", "", "32").unwrap().shell_name(), "file");
99        assert_eq!(parse("dir", "", "48").unwrap().shell_name(), "folder");
100    }
101
102    #[test]
103    fn an_extension_cannot_carry_a_path_or_a_dot() {
104        for bad in ["..", "a.b", "a/b", "a\\b", "a b", "a:b", "é", "x\0", &"a".repeat(25)] {
105            assert!(parse("file", bad, "16").is_err(), "{bad:?} must be refused");
106        }
107        assert!(parse("file", "c++", "16").is_ok());
108        assert!(parse("file", "7z", "16").is_ok());
109        assert!(parse("file", "tar-gz_x", "16").is_ok());
110    }
111
112    #[test]
113    fn only_the_offered_sizes_and_kinds_are_accepted() {
114        for px in PX_SIZES {
115            assert!(parse("file", "rs", &px.to_string()).is_ok());
116        }
117        for bad in ["0", "15", "17", "257", "-16", "16.0", "", "1000000"] {
118            assert!(parse("file", "rs", bad).is_err(), "px {bad:?}");
119        }
120        assert!(IconRequest::parse(Some("file"), Some("rs"), None).is_err());
121        assert!(IconRequest::parse(None, Some("rs"), Some("16")).is_err());
122        assert!(parse("drive", "", "16").is_err());
123        assert!(parse("dir", "rs", "16").is_err(), "a folder has no extension");
124    }
125
126    #[test]
127    fn a_picture_is_taken_from_the_smallest_list_that_is_big_enough() {
128        let got: Vec<(u32, u32)> = PX_SIZES.iter().map(|&p| (p, native_list(p).1)).collect();
129        assert_eq!(
130            got,
131            [(16, 16), (20, 32), (24, 32), (32, 32), (40, 48), (48, 48), (64, 256), (96, 256), (128, 256), (256, 256)]
132        );
133        assert!(PX_SIZES.iter().all(|&p| native_list(p).1 >= p), "never scaled up");
134    }
135}