pub const REPO_URL: &str = "https://github.com/AprilNEA/OpenLogi";
pub const HELP_URL: &str = "https://github.com/AprilNEA/OpenLogi#readme";
pub const RELEASES_URL: &str = "https://github.com/AprilNEA/OpenLogi/releases/latest";
#[must_use]
pub fn release_tag_url(version: &str) -> String {
format!("{REPO_URL}/releases/tag/v{version}")
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DeeplinkCommand {
Show,
OpenSettings,
OpenAbout,
CheckForUpdates,
Quit,
}
impl DeeplinkCommand {
pub const SCHEME: &str = "openlogi";
#[must_use]
pub const fn as_name(self) -> &'static str {
match self {
Self::Show => "show",
Self::OpenSettings => "open-settings",
Self::OpenAbout => "open-about",
Self::CheckForUpdates => "check-for-updates",
Self::Quit => "quit",
}
}
#[must_use]
pub fn to_url(self) -> String {
format!("{}://{}", Self::SCHEME, self.as_name())
}
#[must_use]
pub fn from_name(name: &str) -> Option<Self> {
match name {
"show" => Some(Self::Show),
"open-settings" => Some(Self::OpenSettings),
"open-about" => Some(Self::OpenAbout),
"check-for-updates" => Some(Self::CheckForUpdates),
"quit" => Some(Self::Quit),
_ => None,
}
}
#[must_use]
pub fn parse_url(url: &str) -> Option<Self> {
let rest = url.strip_prefix(Self::SCHEME)?.strip_prefix("://")?;
let name = rest.split(['/', '?']).next().unwrap_or(rest);
Self::from_name(name)
}
}
#[cfg(test)]
mod tests {
use super::DeeplinkCommand;
const ALL: [DeeplinkCommand; 5] = [
DeeplinkCommand::Show,
DeeplinkCommand::OpenSettings,
DeeplinkCommand::OpenAbout,
DeeplinkCommand::CheckForUpdates,
DeeplinkCommand::Quit,
];
#[test]
fn url_round_trips() {
for cmd in ALL {
assert_eq!(DeeplinkCommand::parse_url(&cmd.to_url()), Some(cmd));
}
}
#[test]
fn parse_url_ignores_trailing_path_and_query() {
assert_eq!(
DeeplinkCommand::parse_url("openlogi://show/"),
Some(DeeplinkCommand::Show)
);
assert_eq!(
DeeplinkCommand::parse_url("openlogi://open-settings?from=tray"),
Some(DeeplinkCommand::OpenSettings)
);
}
#[test]
fn parse_url_rejects_foreign_scheme_and_unknown_command() {
assert_eq!(DeeplinkCommand::parse_url("https://example.com/show"), None);
assert_eq!(DeeplinkCommand::parse_url("openlogi://bogus"), None);
assert_eq!(DeeplinkCommand::parse_url("openlogi://"), None);
}
}