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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use crate::prelude::*;

/// [Deserialize]s from a [String] representation.
#[derive(
	Debug, Clone, PartialEq, derive_more::Display, derive_more::From, Serialize, Deserialize,
)]
#[serde(try_from = "&str")]
#[serde(into = "String")]
pub enum DeviceName {
	IPhone(IPhoneVariant),

	IPad(IPadVariant),

	#[from(ignore)]
	#[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/docs/inline/TODO.md"))]
	UnImplemented(String),
}

impl From<DeviceName> for String {
	#[tracing::instrument(level = "trace", skip(variant))]
	fn from(variant: DeviceName) -> Self {
		variant.to_string()
	}
}

impl TryFrom<&str> for DeviceName {
	type Error = <Self as FromStr>::Err;

	fn try_from(value: &str) -> std::result::Result<Self, Self::Error> {
		value.parse()
	}
}

impl_from_str_nom!(DeviceName);

impl From<&IPhoneVariant> for DeviceName {
	#[tracing::instrument(level = "trace", skip(variant))]
	fn from(variant: &IPhoneVariant) -> Self {
		Self::IPhone(*variant)
	}
}

impl From<&IPadVariant> for DeviceName {
	#[tracing::instrument(level = "trace", skip(variant))]
	fn from(variant: &IPadVariant) -> Self {
		Self::IPad(*variant)
	}
}

impl From<&DeviceName> for DeviceName {
	#[tracing::instrument(level = "trace", skip(variant))]
	fn from(variant: &DeviceName) -> Self {
		variant.clone()
	}
}

pub use iphone::*;
mod iphone;

pub use ipad::*;
mod ipad;

impl DeviceName {
	pub fn parsed_successfully(&self) -> bool {
		!matches!(self, DeviceName::UnImplemented(_))
	}

	pub fn is_iphone(&self) -> bool {
		matches!(self, DeviceName::IPhone(_))
	}

	pub fn is_ipad(&self) -> bool {
		matches!(self, DeviceName::IPad(_))
	}

	pub fn get_ipad(&self) -> Option<&IPadVariant> {
		match self {
			DeviceName::IPad(ipad) => Some(ipad),
			_ => None,
		}
	}

	pub fn get_iphone(&self) -> Option<&IPhoneVariant> {
		match self {
			DeviceName::IPhone(iphone) => Some(iphone),
			_ => None,
		}
	}
}

impl NomFromStr for DeviceName {
	fn nom_from_str(input: &str) -> IResult<&str, Self> {
		alt((
			map(ws(IPadVariant::nom_from_str), DeviceName::IPad),
			map(ws(IPhoneVariant::nom_from_str), DeviceName::IPhone),
			map(rest, |s: &str| DeviceName::UnImplemented(s.to_owned())),
		))(input)
	}
}

#[cfg(test)]
mod tests {
	use crate::shared::assert_nom_parses;

	use super::*;

	#[test]
	fn hardcoded_parse_device_names() {
		let examples = [
			"iPad Air 11-inch (M2)",
			"iPad Pro (11-inch) (4th generation)",
		];
		assert_nom_parses::<DeviceName>(examples, |d| d.parsed_successfully());
	}

	#[test]
	fn generated_parse_device_name() {
		let examples = include!("../../../tests/device-names.json");
		assert_nom_parses::<DeviceName>(examples, |d| d.parsed_successfully())
	}
}