1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use crate::prelude::*;

pub use self::signed_keys::SignedKeys;
mod signed_keys;

#[derive(Debug, Serialize)]
#[non_exhaustive]
#[must_use = include_doc!(must_use_cmd_output)]
pub enum DisplayOutput {
	/// Considered an error case
	NotSignedAtAll {
		path: Utf8PathBuf,
	},

	/// Successfully extracted key-value pairs from codesign -d
	SignedKeys(signed_keys::SignedKeys),

	#[doc = include_doc!(cmd_success)]
	SuccessUnimplemented {
		stdout: String,
	},

	#[doc = include_doc!(cmd_error)]
	ErrorUnImplemented {
		stderr: String,
	},
}

impl CommandNomParsable for DisplayOutput {
	fn success_unimplemented(str: String) -> Self {
		Self::SuccessUnimplemented { stdout: str }
	}

	fn error_unimplemented(str: String) -> Self {
		Self::ErrorUnImplemented { stderr: str }
	}

	fn success_nom_from_str(input: &str) -> IResult<&str, Self> {
		alt((
			parse_key_value,
			map_res(rest, |s| {
				SignedKeys::from_raw(s).map(DisplayOutput::SignedKeys)
			}),
		))(input)
	}
}

impl PublicCommandOutput for DisplayOutput {
	type PrimarySuccess = signed_keys::SignedKeys;

	fn success(&self) -> Result<&Self::PrimarySuccess> {
		match self {
			DisplayOutput::SignedKeys(keys) => Ok(keys),
			_ => Err(Error::output_errored(self)),
		}
	}
}

fn parse_key_value(input: &str) -> IResult<&str, DisplayOutput> {
	let (remaining, path) = map(
		terminated(take_while(|c| c != ':'), tag(": ")),
		Utf8Path::new,
	)(input)?;
	map(ws(tag("code object is not signed at all")), move |_| {
		debug!("Parsed NotSignedAtAll");
		DisplayOutput::NotSignedAtAll {
			path: path.to_owned(),
		}
	})(remaining)
}