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
use super::{BuildError, EmptyToNone, Name, SvdError, ValidateLevel};
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct EnumeratedValue {
pub name: String,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub description: Option<String>,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub value: Option<u64>,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub is_default: Option<bool>,
}
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
pub enum Error {
#[error("EnumeratedValue must contain one of `value` (passed {0:?}) or `is_default` (passed {1:?}) tags")]
AbsentValue(Option<u64>, Option<bool>),
#[error("Value {0} out of range {1:?}")]
OutOfRange(u64, core::ops::Range<u64>),
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct EnumeratedValueBuilder {
name: Option<String>,
description: Option<String>,
value: Option<u64>,
is_default: Option<bool>,
}
impl From<EnumeratedValue> for EnumeratedValueBuilder {
fn from(e: EnumeratedValue) -> Self {
Self {
name: Some(e.name),
description: e.description,
value: e.value,
is_default: e.is_default,
}
}
}
impl EnumeratedValueBuilder {
pub fn name(mut self, value: String) -> Self {
self.name = Some(value);
self
}
pub fn description(mut self, value: Option<String>) -> Self {
self.description = value;
self
}
pub fn value(mut self, value: Option<u64>) -> Self {
self.value = value;
self
}
#[allow(clippy::wrong_self_convention)]
pub fn is_default(mut self, value: Option<bool>) -> Self {
self.is_default = value;
self
}
pub fn build(self, lvl: ValidateLevel) -> Result<EnumeratedValue, SvdError> {
let mut ev = EnumeratedValue {
name: self
.name
.ok_or_else(|| BuildError::Uninitialized("name".to_string()))?,
description: self.description.empty_to_none(),
value: self.value,
is_default: self.is_default,
};
if !lvl.is_disabled() {
ev.validate(lvl)?;
}
Ok(ev)
}
}
impl EnumeratedValue {
pub fn builder() -> EnumeratedValueBuilder {
EnumeratedValueBuilder::default()
}
pub fn modify_from(
&mut self,
builder: EnumeratedValueBuilder,
lvl: ValidateLevel,
) -> Result<(), SvdError> {
if let Some(name) = builder.name {
self.name = name;
}
if builder.description.is_some() {
self.description = builder.description.empty_to_none();
}
if builder.value.is_some() {
self.value = builder.value;
}
if builder.is_default.is_some() {
self.is_default = builder.is_default;
}
if !lvl.is_disabled() {
self.validate(lvl)
} else {
Ok(())
}
}
pub fn validate(&mut self, lvl: ValidateLevel) -> Result<(), SvdError> {
if lvl.is_strict() {
super::check_name(&self.name, "name")?;
}
match (&self.value, &self.is_default) {
(Some(_), None) | (None, Some(_)) => Ok(()),
_ => Err(Error::AbsentValue(self.value, self.is_default).into()),
}
}
pub(crate) fn check_range(&self, range: &core::ops::Range<u64>) -> Result<(), SvdError> {
match &self.value {
Some(x) if !range.contains(x) => Err(Error::OutOfRange(*x, range.clone()).into()),
_ => Ok(()),
}
}
}
impl Name for EnumeratedValue {
fn name(&self) -> &str {
&self.name
}
}