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
use super::IconSize;
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::{
cmp::Ordering,
convert::{TryFrom, TryInto},
error::Error,
fmt::{self, Display},
ops::Deref,
};
use vec1::{vec1, Vec1};
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
pub struct IconSizes(Vec1<IconSize>);
impl Display for IconSizes {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&self.0.iter().join(" "))
}
}
impl IconSizes {
pub fn add_size(&mut self, size: IconSize) {
match self.0.binary_search(&size) {
Ok(_) => {}
Err(pos) => self.0.insert(pos, size),
}
}
pub fn largest(&self) -> &IconSize {
self.0.first()
}
}
impl TryFrom<&str> for IconSizes {
type Error = Box<dyn Error>;
fn try_from(sizes_str: &str) -> Result<Self, Self::Error> {
let size_strs = sizes_str.split(" ");
let mut sizes = Vec::new();
for size in size_strs {
if let Ok(size) = serde_json::from_value(Value::String(size.to_string())) {
sizes.push(size);
}
}
Ok(sizes.try_into()?)
}
}
impl TryFrom<&String> for IconSizes {
type Error = Box<dyn Error>;
fn try_from(sizes_str: &String) -> Result<Self, Self::Error> {
IconSizes::try_from(sizes_str.as_str())
}
}
impl TryFrom<String> for IconSizes {
type Error = Box<dyn Error>;
fn try_from(sizes_str: String) -> Result<Self, Self::Error> {
IconSizes::try_from(sizes_str.as_str())
}
}
impl Deref for IconSizes {
type Target = Vec1<IconSize>;
fn deref(&self) -> &Vec1<IconSize> {
&self.0
}
}
impl IntoIterator for IconSizes {
type Item = IconSize;
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl Ord for IconSizes {
fn cmp(&self, other: &Self) -> Ordering {
self.largest().cmp(&other.largest())
}
}
impl PartialOrd for IconSizes {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl TryFrom<Vec<IconSize>> for IconSizes {
type Error = String;
fn try_from(mut vec: Vec<IconSize>) -> Result<Self, Self::Error> {
vec.sort();
Ok(IconSizes(
vec.try_into().map_err(|_| "must contain a size")?,
))
}
}
impl From<IconSize> for IconSizes {
fn from(size: IconSize) -> Self {
IconSizes(vec1![size])
}
}