apt_release_file/
image_size.rs1use std::str::FromStr;
2
3#[derive(Debug, Clone, Hash, PartialEq)]
5pub struct ImageSize {
6 pub pixels: u16,
7 pub hidpi: u16,
8}
9
10impl FromStr for ImageSize {
11 type Err = &'static str;
12
13 fn from_str(input: &str) -> Result<Self, Self::Err> {
14 input
15 .find('x')
16 .ok_or("not a recognizable image string")
17 .and_then(|pos| {
18 let x = &input[..pos];
19 let y = &input[pos + 1..];
20 match y.find('@') {
21 Some(pos) => {
22 let z = &y[pos + 1..];
23 let y = &y[..pos];
24
25 match (x.parse::<u16>(), y.parse::<u16>(), z.parse::<u16>()) {
26 (Ok(pixels), Ok(y), Ok(hidpi)) => if pixels == y {
27 Ok(ImageSize { pixels, hidpi })
28 } else {
29 Err("width and height do not match")
30 },
31 _ => Err("width, height, and/or scale did not parse as integers"),
32 }
33 }
34 None => match (x.parse::<u16>(), y.parse::<u16>()) {
35 (Ok(pixels), Ok(y)) => if pixels == y {
36 Ok(ImageSize { pixels, hidpi: 0 })
37 } else {
38 Err("width and height do not match")
39 },
40 _ => Err("width and/or height failed to parse as integers"),
41 },
42 }
43 })
44 }
45}