dbus_server_address_parser/decode/
autolaunch.rs1#[cfg(target_family = "windows")]
2use super::unescape::{unescape, UnescapeError};
3use super::{guid::decode_guid, GuidError};
4use crate::{Autolaunch, Guid};
5use std::convert::TryFrom;
6use thiserror::Error;
7
8#[derive(Debug, Clone, Error)]
9pub enum AutolaunchError {
10 #[cfg(target_family = "windows")]
11 #[error("Could not unescape: {0}")]
12 UnescapeError(#[from] UnescapeError),
13 #[error("GUID error: {0}")]
14 GuidError(#[from] GuidError),
15 #[error("Unknown key")]
16 UnknownKey,
17 #[cfg(target_family = "windows")]
18 #[error("Env is duplicate")]
19 ScopeDuplicate,
20}
21
22impl Autolaunch {
23 #[cfg(target_family = "windows")]
24 fn decode_scope(scope_str: &str, scope: &mut Option<String>) -> Result<(), AutolaunchError> {
25 if scope.is_none() {
26 let scope_str = unescape(scope_str)?;
27 *scope = Some(scope_str);
28 Ok(())
29 } else {
30 Err(AutolaunchError::ScopeDuplicate)
31 }
32 }
33
34 #[cfg(target_family = "windows")]
35 fn decode_key_value(
36 key_value: &str,
37 scope: &mut Option<String>,
38 guid: &mut Option<Guid>,
39 ) -> Result<(), AutolaunchError> {
40 if let Some(scope_str) = key_value.strip_prefix("scope=") {
41 Autolaunch::decode_scope(scope_str, scope)
42 } else if let Some(guid_str) = key_value.strip_prefix("guid=") {
43 decode_guid(guid_str, guid)?;
44 Ok(())
45 } else {
46 Err(AutolaunchError::UnknownKey)
47 }
48 }
49
50 #[cfg(target_family = "unix")]
51 fn decode_key_value(key_value: &str, guid: &mut Option<Guid>) -> Result<(), AutolaunchError> {
52 if let Some(guid_str) = key_value.strip_prefix("guid=") {
53 decode_guid(guid_str, guid)?;
54 Ok(())
55 } else {
56 Err(AutolaunchError::UnknownKey)
57 }
58 }
59}
60
61impl TryFrom<&str> for Autolaunch {
62 type Error = AutolaunchError;
63
64 #[cfg(target_family = "windows")]
65 fn try_from(server_address: &str) -> Result<Self, Self::Error> {
66 let mut scope = None;
67 let mut guid = None;
68
69 if !server_address.is_empty() {
70 for key_value in server_address.split(',') {
71 Autolaunch::decode_key_value(key_value, &mut scope, &mut guid)?;
72 }
73 }
74
75 Ok(Autolaunch { scope, guid })
76 }
77
78 #[cfg(target_family = "unix")]
79 fn try_from(server_address: &str) -> Result<Self, Self::Error> {
80 let mut guid = None;
81
82 if !server_address.is_empty() {
83 for key_value in server_address.split(',') {
84 Autolaunch::decode_key_value(key_value, &mut guid)?;
85 }
86 }
87
88 Ok(Autolaunch { guid })
89 }
90}