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
use std::fmt;
use std::str::FromStr;
use crate::x509::ValidationError;
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub enum ResourcesChoice<T> {
Inherit,
Blocks(T),
}
impl<T> ResourcesChoice<T> {
pub fn is_inherited(&self) -> bool {
match self {
ResourcesChoice::Inherit => true,
_ => false
}
}
pub fn as_blocks(&self) -> Option<&T> {
match self {
ResourcesChoice::Inherit => None,
ResourcesChoice::Blocks(ref some) => Some(some)
}
}
pub fn as_blocks_mut(&mut self) -> Option<&mut T> {
match self {
ResourcesChoice::Inherit => None,
ResourcesChoice::Blocks(ref mut some) => Some(some)
}
}
pub fn to_blocks(&self) -> Result<T, ValidationError>
where T: Clone {
match self {
ResourcesChoice::Inherit => Err(ValidationError),
ResourcesChoice::Blocks(ref some) => Ok(some.clone()),
}
}
pub fn map_blocks<U, F>(self, f: F) -> ResourcesChoice<U>
where F: FnOnce(T) -> U {
match self {
ResourcesChoice::Inherit => ResourcesChoice::Inherit,
ResourcesChoice::Blocks(t) => ResourcesChoice::Blocks(f(t))
}
}
}
impl<T: fmt::Display> fmt::Display for ResourcesChoice<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
ResourcesChoice::Inherit => write!(f, "inherit"),
ResourcesChoice::Blocks(ref inner) => inner.fmt(f)
}
}
}
impl<T: FromStr> FromStr for ResourcesChoice<T> {
type Err = FromStrErr;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s == "inherit" {
Ok(ResourcesChoice::Inherit)
} else {
let blocks = T::from_str(s).map_err(|_| FromStrErr::BlocksFromStr)?;
Ok(ResourcesChoice::Blocks(blocks))
}
}
}
#[derive(Clone, Debug, Display, Eq, PartialEq)]
pub enum FromStrErr {
#[display(fmt="Cannot parse blocks")]
BlocksFromStr,
}