apple_clis/open/
well_known.rs

1use crate::prelude::*;
2
3use super::OpenCLIInstance;
4
5#[derive(Clone, Copy, PartialEq, Eq, Debug)]
6#[cfg_attr(feature = "cli", derive(clap::ValueEnum))]
7pub enum WellKnown {
8	/// Opens "/Applications/Xcode.app/Contents/Developer/Applications/Simulator.app"
9	Simulator,
10	/// Opens "/Applications/Xcode.app"
11	Xcode,
12}
13
14impl TryFrom<&WellKnown> for &'static Utf8Path {
15	type Error = error::Error;
16
17	#[tracing::instrument(level = "trace", skip(value))]
18	/// Path may be invalid
19	fn try_from(value: &WellKnown) -> std::result::Result<Self, Self::Error> {
20		let path: &'static Utf8Path = match value {
21			WellKnown::Simulator => {
22				Utf8Path::new("/Applications/Xcode.app/Contents/Developer/Applications/Simulator.app")
23			}
24			WellKnown::Xcode => Utf8Path::new("/Applications/Xcode.app"),
25		};
26		match path.try_exists() {
27			Ok(true) => Ok(path),
28			Ok(false) => Err(Error::WellKnownPathNotFound {
29				hard_coded_path: path.to_owned(),
30				err: None,
31			}),
32			Err(err) => Err(Error::WellKnownPathNotFound {
33				hard_coded_path: path.to_owned(),
34				err: Some(err),
35			}),
36		}
37	}
38}
39
40impl WellKnown {
41	#[tracing::instrument(level = "trace", skip(self))]
42	/// Fails if path doesn't exist
43	pub fn get_path(&self) -> error::Result<&Utf8Path> {
44		self.try_into()
45	}
46}
47
48impl OpenCLIInstance {
49	#[instrument(skip_all, ret)]
50	pub fn open_well_known(&self, well_known: &WellKnown) -> Result<ExitStatus> {
51		let path = well_known.get_path()?;
52		Ok(self.bossy_command().with_arg(path).run_and_wait()?)
53	}
54}