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
use super::{BuildError, Protection, SvdError, ValidateLevel};
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct AddressBlock {
pub offset: u32,
pub size: u32,
pub usage: AddressBlockUsage,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub protection: Option<Protection>,
}
#[cfg_attr(
feature = "serde",
derive(serde::Deserialize, serde::Serialize),
serde(rename_all = "kebab-case")
)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum AddressBlockUsage {
Registers,
Buffer,
Reserved,
}
impl Default for AddressBlockUsage {
fn default() -> Self {
Self::Registers
}
}
impl AddressBlockUsage {
pub fn parse_str(s: &str) -> Option<Self> {
match s {
"registers" => Some(Self::Registers),
"buffer" => Some(Self::Buffer),
"reserved" => Some(Self::Reserved),
_ => None,
}
}
pub const fn as_str(self) -> &'static str {
match self {
Self::Registers => "registers",
Self::Buffer => "buffer",
Self::Reserved => "reserved",
}
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct AddressBlockBuilder {
offset: Option<u32>,
size: Option<u32>,
usage: Option<AddressBlockUsage>,
protection: Option<Protection>,
}
impl From<AddressBlock> for AddressBlockBuilder {
fn from(d: AddressBlock) -> Self {
Self {
offset: Some(d.offset),
size: Some(d.size),
usage: Some(d.usage),
protection: d.protection,
}
}
}
impl AddressBlockBuilder {
pub fn offset(mut self, value: u32) -> Self {
self.offset = Some(value);
self
}
pub fn size(mut self, value: u32) -> Self {
self.size = Some(value);
self
}
pub fn usage(mut self, value: AddressBlockUsage) -> Self {
self.usage = Some(value);
self
}
pub fn protection(mut self, value: Option<Protection>) -> Self {
self.protection = value;
self
}
pub fn build(self, lvl: ValidateLevel) -> Result<AddressBlock, SvdError> {
let mut de = AddressBlock {
offset: self
.offset
.ok_or_else(|| BuildError::Uninitialized("offset".to_string()))?,
size: self
.size
.ok_or_else(|| BuildError::Uninitialized("size".to_string()))?,
usage: self
.usage
.ok_or_else(|| BuildError::Uninitialized("usage".to_string()))?,
protection: self.protection,
};
if !lvl.is_disabled() {
de.validate(lvl)?;
}
Ok(de)
}
}
impl AddressBlock {
pub fn builder() -> AddressBlockBuilder {
AddressBlockBuilder::default()
}
pub fn modify_from(
&mut self,
builder: AddressBlockBuilder,
lvl: ValidateLevel,
) -> Result<(), SvdError> {
if let Some(offset) = builder.offset {
self.offset = offset;
}
if let Some(size) = builder.size {
self.size = size;
}
if let Some(usage) = builder.usage {
self.usage = usage;
}
if builder.protection.is_some() {
self.protection = builder.protection;
}
if !lvl.is_disabled() {
self.validate(lvl)
} else {
Ok(())
}
}
pub fn validate(&mut self, _lvl: ValidateLevel) -> Result<(), SvdError> {
Ok(())
}
}