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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
use std::hash::Hash;

use crate::prelude::*;

pub use maybe::MaybeScreenSize;

mod maybe {
	use crate::prelude::*;

	/// Wrapper around Option<[ScreenSize]> for easy of [Display] impl.
	/// The display impl *includes a prefix space*.
	#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
	pub struct MaybeScreenSize(Option<ScreenSize>);

	impl MaybeScreenSize {
		pub fn as_ref(&self) -> Option<&ScreenSize> {
			self.0.as_ref()
		}

		pub fn get(&self) -> Option<&ScreenSize> {
			self.as_ref()
		}
	}

	impl Display for MaybeScreenSize {
		fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
			if let Some(screen_size) = self.0.as_ref() {
				write!(f, " {}", screen_size)
			} else {
				Ok(())
			}
		}
	}

	impl NomFromStr for MaybeScreenSize {
		fn nom_from_str(input: &str) -> IResult<&str, Self> {
			map(opt(ScreenSize::nom_from_str), MaybeScreenSize)(input)
		}
	}

	#[cfg(test)]
	mod test {
	}
}

#[derive(Debug, Clone, Copy, Eq, PartialOrd, Ord)]
pub struct ScreenSize {
	/// divide by 10 to get number of actual inches
	ten_inches: u16,
	/// whether to show \" or -inch
	/// used to differentiate "11\"" and "11-inch"
	short: bool,
	/// whether to show brackets
	/// used to differentiate "(11-inch)" and "11-inch"
	brackets: bool,
}

impl PartialEq for ScreenSize {
	fn eq(&self, other: &Self) -> bool {
		self.ten_inches == other.ten_inches
	}
}

impl Hash for ScreenSize {
	fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
		self.ten_inches.hash(state);
	}
}

impl ScreenSize {
	/// Long format and brackets
	/// ```rust
	/// use apple_clis::prelude::*;
	///
	/// #[cfg(feature = "unstable-construction")]
	/// {
	/// 	let example = "(11-inch)";
	/// 	let screen_size = ScreenSize::long_brackets(11.0);
	/// 	assert_eq!(format!("{}", screen_size), example);
	/// }
	/// ```
	#[stability::unstable(feature = "construction")]
	pub fn long_brackets(inches: f32) -> Self {
		let inches = inches * 10.0;

		Self {
			ten_inches: inches as u16,
			short: false,
			brackets: true,
		}
	}

	/// Long format and no brackets
	/// ```rust
	/// use apple_clis::prelude::*;
	///
	/// #[cfg(feature = "unstable-construction")]
	/// {
	/// 	let example = "11-inch";
	/// 	let screen_size = ScreenSize::long_bracketless(11.0);
	/// 	assert_eq!(format!("{}", screen_size), example);
	/// }
	/// ```
	#[stability::unstable(feature = "construction")]
	pub fn long_bracketless(inches: f32) -> Self {
		let inches = inches * 10.0;

		Self {
			ten_inches: inches as u16,
			short: false,
			brackets: false,
		}
	}

	/// Short format and brackets
	/// ```rust
	/// use apple_clis::prelude::*;
	///
	/// #[cfg(feature = "unstable-construction")]
	/// {
	/// 	let example = "(13\")";
	/// 	let screen_size = ScreenSize::short_brackets(13.0);
	/// 	assert_eq!(format!("{}", screen_size), example);
	/// }
	/// ```
	#[stability::unstable(feature = "construction")]
	pub fn short_brackets(inches: f32) -> Self {
		let inches = inches * 10.0;

		Self {
			ten_inches: inches as u16,
			short: true,
			brackets: true,
		}
	}

	/// Short format and no brackets
	/// ```rust
	/// use apple_clis::prelude::*;
	///
	/// #[cfg(feature = "unstable-construction")]
	/// {
	/// 	let example = "13\"";
	/// 	let screen_size = ScreenSize::short_bracketless(13.0);
	/// 	assert_eq!(format!("{}", screen_size), example);
	/// }
	/// ```
	#[stability::unstable(feature = "construction")]
	pub fn short_bracketless(inches: f32) -> Self {
		let inches = inches * 10.0;

		Self {
			ten_inches: inches as u16,
			short: true,
			brackets: false,
		}
	}

	pub fn inches(&self) -> f32 {
		self.ten_inches as f32 / 10.0
	}
}

fn brackets_long(input: &str) -> IResult<&str, ScreenSize> {
	delimited(
		ws(tag("(")),
		map(float, ScreenSize::long_brackets),
		ws(tag("-inch)")),
	)(input)
}

fn bracketless_long(input: &str) -> IResult<&str, ScreenSize> {
	terminated(map(float, ScreenSize::long_bracketless), ws(tag("-inch")))(input)
}

fn brackets_short(input: &str) -> IResult<&str, ScreenSize> {
	delimited(
		ws(tag("(")),
		map(float, ScreenSize::short_brackets),
		ws(tag("\")")),
	)(input)
}

fn bracketless_short(input: &str) -> IResult<&str, ScreenSize> {
	terminated(map(float, ScreenSize::short_bracketless), ws(tag("\"")))(input)
}

impl NomFromStr for ScreenSize {
	#[tracing::instrument(level = "trace", skip(input))]
	fn nom_from_str(input: &str) -> IResult<&str, Self> {
		alt((
			brackets_long,
			brackets_short,
			bracketless_long,
			bracketless_short,
		))(input)
	}
}

impl Display for ScreenSize {
	#[tracing::instrument(level = "trace", skip(self, f))]
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match (self.brackets, self.short) {
			(true, false) => write!(f, "({}-inch)", self.inches()),
			(true, true) => write!(f, "({}\")", self.inches()),
			(false, true) => write!(f, "{}\"", self.inches()),
			(false, false) => write!(f, "{}-inch", self.inches()),
		}
	}
}

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

	use super::*;

	#[test]
	fn screen_size_hardcoded() {
		let examples = ["(5.5-inch)", "(11-inch)", "(6.1\")", "13-inch", "11-inch"];
		assert_nom_parses::<ScreenSize>(examples, |_| true)
	}

	#[test]
	fn screen_size_ordering() {
		let small = ScreenSize::long_brackets(5.0);
		let large = ScreenSize::short_brackets(6.0);
		assert!(large > small);

		let small = ScreenSize::long_brackets(5.0);
		let large = ScreenSize::short_brackets(6.0);
		assert!(large > small);
	}
}