use crate::{Error, Result};
use std::collections::HashMap;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Autolaunch {
pub(super) scope: Option<AutolaunchScope>,
}
impl std::fmt::Display for Autolaunch {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "autolaunch:")?;
if let Some(scope) = &self.scope {
write!(f, "scope={scope}")?;
}
Ok(())
}
}
impl Default for Autolaunch {
fn default() -> Self {
Self::new()
}
}
impl Autolaunch {
pub fn new() -> Self {
Self { scope: None }
}
pub fn set_scope(mut self, scope: Option<AutolaunchScope>) -> Self {
self.scope = scope;
self
}
pub fn scope(&self) -> Option<&AutolaunchScope> {
self.scope.as_ref()
}
pub(super) fn from_options(opts: HashMap<&str, &str>) -> Result<Self> {
opts.get("scope")
.map(|scope| -> Result<_> {
let decoded = super::decode_percents(scope)?;
match decoded.as_slice() {
b"install-path" => Ok(AutolaunchScope::InstallPath),
b"user" => Ok(AutolaunchScope::User),
_ => String::from_utf8(decoded)
.map(AutolaunchScope::Other)
.map_err(|_| {
Error::Address("autolaunch scope is not valid UTF-8".to_owned())
}),
}
})
.transpose()
.map(|scope| Self { scope })
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum AutolaunchScope {
InstallPath,
User,
Other(String),
}
impl std::fmt::Display for AutolaunchScope {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::InstallPath => write!(f, "*install-path"),
Self::User => write!(f, "*user"),
Self::Other(o) => write!(f, "{o}"),
}
}
}