Skip to main content

openlogi_core/
brand.rs

1//! Brand constants shared across the workspace: the project's public URLs and
2//! the `openlogi://` deep-link command vocabulary.
3//!
4//! Both live here, in the platform-free core crate, so the agent (which *emits*
5//! tray deep links and renders help links) and the GUI (which *parses* the deep
6//! links and renders the same help links) share a single source of truth — the
7//! command names can't drift across the process boundary, and a repo move
8//! touches one file instead of three.
9
10/// The OpenLogi GitHub repository.
11pub const REPO_URL: &str = "https://github.com/AprilNEA/OpenLogi";
12/// The README, used as the in-app "Help" link.
13pub const HELP_URL: &str = "https://github.com/AprilNEA/OpenLogi#readme";
14/// The "latest release" page.
15pub const RELEASES_URL: &str = "https://github.com/AprilNEA/OpenLogi/releases/latest";
16
17/// The release page for a specific version tag (e.g. the running build).
18#[must_use]
19pub fn release_tag_url(version: &str) -> String {
20    format!("{REPO_URL}/releases/tag/v{version}")
21}
22
23/// A GUI action the agent's tray (or any external caller) requests by opening
24/// an `openlogi://<name>` URL. macOS delivers it to the running GUI via an
25/// Apple Event; the GUI parses it back into this enum and dispatches.
26///
27/// The agent builds URLs with [`DeeplinkCommand::to_url`]; the GUI reads them
28/// with [`DeeplinkCommand::parse_url`]. The command names are defined once, in
29/// [`DeeplinkCommand::as_name`], so the two sides cannot disagree.
30#[derive(Clone, Copy, Debug, Eq, PartialEq)]
31pub enum DeeplinkCommand {
32    /// Show / foreground the main window.
33    Show,
34    /// Open the Settings window.
35    OpenSettings,
36    /// Open the About window.
37    OpenAbout,
38    /// Run a manual update check (and show where its status is rendered).
39    CheckForUpdates,
40    /// Quit the GUI.
41    Quit,
42}
43
44impl DeeplinkCommand {
45    /// The URL scheme OpenLogi registers with LaunchServices.
46    pub const SCHEME: &str = "openlogi";
47
48    /// The wire name for this command — the host component of its URL.
49    #[must_use]
50    pub const fn as_name(self) -> &'static str {
51        match self {
52            Self::Show => "show",
53            Self::OpenSettings => "open-settings",
54            Self::OpenAbout => "open-about",
55            Self::CheckForUpdates => "check-for-updates",
56            Self::Quit => "quit",
57        }
58    }
59
60    /// Build the `openlogi://<name>` URL for this command.
61    #[must_use]
62    pub fn to_url(self) -> String {
63        format!("{}://{}", Self::SCHEME, self.as_name())
64    }
65
66    /// Parse a command from its wire name (the part after `openlogi://`).
67    #[must_use]
68    pub fn from_name(name: &str) -> Option<Self> {
69        match name {
70            "show" => Some(Self::Show),
71            "open-settings" => Some(Self::OpenSettings),
72            "open-about" => Some(Self::OpenAbout),
73            "check-for-updates" => Some(Self::CheckForUpdates),
74            "quit" => Some(Self::Quit),
75            _ => None,
76        }
77    }
78
79    /// Parse a full `openlogi://…` URL. The command lives in the URL's host
80    /// component, so any trailing path or query (`openlogi://show/`,
81    /// `openlogi://show?x=1`) is ignored. Returns `None` for a foreign scheme
82    /// or an unknown command.
83    #[must_use]
84    pub fn parse_url(url: &str) -> Option<Self> {
85        let rest = url.strip_prefix(Self::SCHEME)?.strip_prefix("://")?;
86        let name = rest.split(['/', '?']).next().unwrap_or(rest);
87        Self::from_name(name)
88    }
89}
90
91#[cfg(test)]
92mod tests {
93    use super::DeeplinkCommand;
94
95    const ALL: [DeeplinkCommand; 5] = [
96        DeeplinkCommand::Show,
97        DeeplinkCommand::OpenSettings,
98        DeeplinkCommand::OpenAbout,
99        DeeplinkCommand::CheckForUpdates,
100        DeeplinkCommand::Quit,
101    ];
102
103    #[test]
104    fn url_round_trips() {
105        for cmd in ALL {
106            assert_eq!(DeeplinkCommand::parse_url(&cmd.to_url()), Some(cmd));
107        }
108    }
109
110    #[test]
111    fn parse_url_ignores_trailing_path_and_query() {
112        assert_eq!(
113            DeeplinkCommand::parse_url("openlogi://show/"),
114            Some(DeeplinkCommand::Show)
115        );
116        assert_eq!(
117            DeeplinkCommand::parse_url("openlogi://open-settings?from=tray"),
118            Some(DeeplinkCommand::OpenSettings)
119        );
120    }
121
122    #[test]
123    fn parse_url_rejects_foreign_scheme_and_unknown_command() {
124        assert_eq!(DeeplinkCommand::parse_url("https://example.com/show"), None);
125        assert_eq!(DeeplinkCommand::parse_url("openlogi://bogus"), None);
126        assert_eq!(DeeplinkCommand::parse_url("openlogi://"), None);
127    }
128}