Skip to main content

subspace_client_github/model/
helpers.rs

1use serde::Serialize;
2
3use super::ItemKind;
4
5/// One gh-dash-style section: a saved GitHub search.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
7pub struct SectionDef {
8    /// Stable, URL-safe.
9    pub id: &'static str,
10    pub kind: ItemKind,
11    pub title: &'static str,
12    /// GitHub search query; `@me` resolves to the account.
13    pub query: &'static str,
14    /// The section searches only the repositories it is given, and is empty
15    /// without any: it has no account qualifier of its own.
16    pub repos_only: bool,
17}
18
19const fn section(
20    id: &'static str,
21    kind: ItemKind,
22    title: &'static str,
23    query: &'static str,
24) -> SectionDef {
25    SectionDef {
26        id,
27        kind,
28        title,
29        query,
30        repos_only: false,
31    }
32}
33
34const fn repos_section(
35    id: &'static str,
36    kind: ItemKind,
37    title: &'static str,
38    query: &'static str,
39) -> SectionDef {
40    SectionDef {
41        repos_only: true,
42        ..section(id, kind, title, query)
43    }
44}
45
46/// The gh-dash-style sections (GH-03).
47pub const SECTIONS: &[SectionDef] = &[
48    section(
49        "mine",
50        ItemKind::Pr,
51        "Mine",
52        "is:pr is:open author:@me archived:false",
53    ),
54    section(
55        "review",
56        ItemKind::Pr,
57        "Review requested",
58        "is:pr is:open review-requested:@me archived:false",
59    ),
60    section(
61        "involved",
62        ItemKind::Pr,
63        "Involved",
64        "is:pr is:open involves:@me -author:@me archived:false",
65    ),
66    repos_section(
67        "open",
68        ItemKind::Pr,
69        "All open",
70        "is:pr is:open archived:false",
71    ),
72    section(
73        "assigned",
74        ItemKind::Issue,
75        "Assigned",
76        "is:issue is:open assignee:@me archived:false",
77    ),
78    section(
79        "created",
80        ItemKind::Issue,
81        "Created",
82        "is:issue is:open author:@me archived:false",
83    ),
84    section(
85        "mentioned",
86        ItemKind::Issue,
87        "Mentioned",
88        "is:issue is:open mentions:@me archived:false",
89    ),
90];
91
92/// `GET /api/gh/image`: an image's bytes, up to 10 MB.
93#[derive(Debug, Clone, PartialEq, Eq)]
94pub struct Image {
95    pub content_type: &'static str,
96    pub bytes: Vec<u8>,
97}
98
99/// The image `Content-Type` for a repository path, by its extension.
100pub fn image_type(path: &str) -> Option<&'static str> {
101    let (_, extension) = path.rsplit_once('.')?;
102    if extension.is_empty() || extension.contains('/') {
103        return None;
104    }
105    Some(match extension.to_ascii_lowercase().as_str() {
106        "png" => "image/png",
107        "jpg" | "jpeg" => "image/jpeg",
108        "gif" => "image/gif",
109        "webp" => "image/webp",
110        "avif" => "image/avif",
111        "bmp" => "image/bmp",
112        "ico" => "image/x-icon",
113        "svg" => "image/svg+xml",
114        _ => return None,
115    })
116}
117
118/// EMU logins end in the enterprise shortcode.
119pub fn short_login(login: &str) -> &str {
120    match login.rsplit_once('_') {
121        Some((head, code))
122            if !code.is_empty() && code.chars().all(|c| c.is_ascii_alphanumeric()) =>
123        {
124            head
125        }
126        _ => login,
127    }
128}
129
130/// The name, else the short login.
131pub fn display_name(user: &super::GhUser) -> &str {
132    user.name
133        .as_deref()
134        .map(str::trim)
135        .filter(|name| !name.is_empty())
136        .unwrap_or_else(|| short_login(&user.login))
137}
138
139#[cfg(test)]
140mod tests {
141    use super::{ItemKind, SECTIONS, repos_section, section};
142
143    #[test]
144    fn only_all_open_is_limited_to_repositories() {
145        let open = repos_section("open", ItemKind::Pr, "All open", "is:pr is:open");
146        assert!(open.repos_only);
147        assert!(!section("mine", ItemKind::Pr, "Mine", "is:pr").repos_only);
148        let scoped: Vec<&str> = SECTIONS
149            .iter()
150            .filter(|section| section.repos_only)
151            .map(|section| section.id)
152            .collect();
153        assert_eq!(scoped, ["open"]);
154    }
155}