apple_clis/shared/
types.rs

1pub use app_directory::AppDirectory;
2mod app_directory {
3	use std::str::FromStr;
4
5	use crate::prelude::*;
6
7	/// Represents a path that points to a .app directory
8	/// Exists for type-safety and to provide a consistent API
9	#[derive(Debug, Serialize, Clone)]
10	pub struct AppDirectory(Utf8PathBuf);
11
12	impl AsRef<Utf8Path> for AppDirectory {
13		fn as_ref(&self) -> &Utf8Path {
14			self.0.as_ref()
15		}
16	}
17
18	impl AsRef<std::path::Path> for AppDirectory {
19		fn as_ref(&self) -> &std::path::Path {
20			self.0.as_ref()
21		}
22	}
23
24	impl FromStr for AppDirectory {
25		type Err = Error;
26
27		fn from_str(s: &str) -> std::prelude::v1::Result<Self, Self::Err> {
28			Self::new(s)
29		}
30	}
31
32	impl<'de> serde::Deserialize<'de> for AppDirectory {
33		fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
34		where
35			D: serde::Deserializer<'de>,
36		{
37			let string = String::deserialize(deserializer)?;
38			AppDirectory::from_str(&string).map_err(serde::de::Error::custom)
39		}
40	}
41
42	impl AppDirectory {
43		/// # Safety
44		/// Assets that the path exists, and points to a .app bundle directory
45		unsafe fn new_unchecked(path: impl AsRef<Utf8Path>) -> Self {
46			Self(Utf8PathBuf::from(path.as_ref()))
47		}
48
49		pub fn new(path: impl AsRef<Utf8Path>) -> Result<Self> {
50			let path = path.as_ref();
51			match path.try_exists() {
52				Ok(true) => Ok(unsafe { Self::new_unchecked(path) }),
53				Ok(false) => Err(Error::PathDoesNotExist {
54					path: path.to_owned(),
55					err: None,
56				}),
57				Err(err) => Err(Error::PathDoesNotExist {
58					path: path.to_owned(),
59					err: Some(err),
60				}),
61			}
62		}
63
64		pub fn get(&self) -> &Utf8Path {
65			self.0.as_ref()
66		}
67	}
68}