Skip to content

Commit 8194739

Browse files
authored
Merge pull request #261 from rust-embedded/datatype
add Datatype
2 parents 992f0bb + f17d21b commit 8194739

File tree

11 files changed

+150
-5
lines changed

11 files changed

+150
-5
lines changed

svd-encoder/src/datatype.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use super::{Config, Element, Encode, EncodeError, XMLNode};
2+
3+
impl Encode for crate::svd::DataType {
4+
type Error = EncodeError;
5+
6+
fn encode_with_config(&self, _config: &Config) -> Result<Element, EncodeError> {
7+
let mut elem = Element::new("dataType");
8+
elem.children.push(XMLNode::Text(self.as_str().to_string()));
9+
Ok(elem)
10+
}
11+
}

svd-encoder/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ mod bitrange;
8888
mod cluster;
8989
mod config;
9090
mod cpu;
91+
mod datatype;
9192
mod device;
9293
mod dimelement;
9394
mod endian;

svd-encoder/src/register.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ impl Encode for RegisterInfo {
6363
elem.children
6464
.extend(self.properties.encode_with_config(config)?);
6565

66+
if let Some(v) = &self.datatype {
67+
elem.children.push(v.encode_node_with_config(config)?);
68+
}
69+
6670
if let Some(v) = &self.modified_write_values {
6771
elem.children.push(v.encode_node_with_config(config)?);
6872
}

svd-parser/src/datatype.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use super::*;
2+
use crate::svd::DataType;
3+
4+
impl Parse for DataType {
5+
type Object = Self;
6+
type Error = SVDErrorAt;
7+
type Config = Config;
8+
9+
fn parse(tree: &Node, _config: &Self::Config) -> Result<Self, Self::Error> {
10+
let text = tree.get_text()?;
11+
12+
Self::parse_str(text).ok_or_else(|| SVDError::InvalidDatatype(text.into()).at(tree.id()))
13+
}
14+
}

svd-parser/src/device.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use super::*;
2-
use crate::svd::{
3-
cpu::Cpu, peripheral::Peripheral, registerproperties::RegisterProperties, Device,
4-
};
2+
use crate::svd::{cpu::Cpu, peripheral::Peripheral, registerproperties::RegisterProperties};
53

64
/// Parses a SVD file
75
impl Parse for Device {

svd-parser/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ mod addressblock;
196196
mod bitrange;
197197
mod cluster;
198198
mod cpu;
199+
mod datatype;
199200
mod device;
200201
mod dimelement;
201202
mod endian;
@@ -247,6 +248,8 @@ pub enum SVDError {
247248
NotExpectedTag(String),
248249
#[error("Invalid RegisterCluster (expected register or cluster), found {0}")]
249250
InvalidRegisterCluster(String),
251+
#[error("Invalid datatype variant, found {0}")]
252+
InvalidDatatype(String),
250253
#[error("Invalid modifiedWriteValues variant, found {0}")]
251254
InvalidModifiedWriteValues(String),
252255
#[error("Invalid readAction variant, found {0}")]

svd-parser/src/register.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::*;
22
use crate::svd::{
3-
Field, ModifiedWriteValues, ReadAction, Register, RegisterInfo, RegisterProperties,
3+
DataType, Field, ModifiedWriteValues, ReadAction, Register, RegisterInfo, RegisterProperties,
44
WriteConstraint,
55
};
66

@@ -28,6 +28,7 @@ impl Parse for RegisterInfo {
2828
.alternate_register(tree.get_child_text_opt("alternateRegister")?)
2929
.address_offset(tree.get_child_u32("addressOffset")?)
3030
.properties(RegisterProperties::parse(tree, config)?)
31+
.datatype(optional::<DataType>("dataType", tree, config)?)
3132
.modified_write_values(optional::<ModifiedWriteValues>(
3233
"modifiedWriteValues",
3334
tree,

svd-rs/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## Unreleased
99

10+
- Add `DataType`
11+
1012
## [v0.14.8] - 2024-02-13
1113

1214
- add `maybe_array` constructors

svd-rs/src/datatype.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/// Register data type
2+
#[cfg_attr(
3+
feature = "serde",
4+
derive(serde::Deserialize, serde::Serialize),
5+
serde(rename_all = "snake_case")
6+
)]
7+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
8+
pub enum DataType {
9+
/// unsigned byte
10+
U8,
11+
/// unsigned half word
12+
U16,
13+
/// unsigned word
14+
U32,
15+
/// unsigned double word
16+
U64,
17+
/// signed byte
18+
I8,
19+
/// signed half word
20+
I16,
21+
/// signed world
22+
I32,
23+
/// signed double word
24+
I64,
25+
/// pointer to unsigned byte
26+
U8Ptr,
27+
/// pointer to unsigned half word
28+
U16Ptr,
29+
/// pointer to unsigned word
30+
U32Ptr,
31+
/// pointer to unsigned double word
32+
U64Ptr,
33+
/// pointer to signed byte
34+
I8Ptr,
35+
/// pointer to signed half word
36+
I16Ptr,
37+
/// pointer to signed world
38+
I32Ptr,
39+
/// pointer to signed double word
40+
I64Ptr,
41+
}
42+
43+
impl DataType {
44+
/// Parse a string into an [`DataType`] value, returning [`Option::None`] if the string is not valid.
45+
pub fn parse_str(s: &str) -> Option<Self> {
46+
match s {
47+
"uint8_t" => Some(Self::U8),
48+
"uint16_t" => Some(Self::U16),
49+
"uint32_t" => Some(Self::U32),
50+
"uint64_t" => Some(Self::U64),
51+
"int8_t" => Some(Self::I8),
52+
"int16_t" => Some(Self::I16),
53+
"int32_t" => Some(Self::I32),
54+
"int64_t" => Some(Self::I64),
55+
"uint8_t *" => Some(Self::U8Ptr),
56+
"uint16_t *" => Some(Self::U16Ptr),
57+
"uint32_t *" => Some(Self::U32Ptr),
58+
"uint64_t *" => Some(Self::U64Ptr),
59+
"int8_t *" => Some(Self::I8Ptr),
60+
"int16_t *" => Some(Self::I16Ptr),
61+
"int32_t *" => Some(Self::I32Ptr),
62+
"int64_t *" => Some(Self::I64Ptr),
63+
_ => None,
64+
}
65+
}
66+
67+
/// Convert this [`DataType`] into a static string.
68+
pub const fn as_str(self) -> &'static str {
69+
match self {
70+
Self::U8 => "uint8_t",
71+
Self::U16 => "uint16_t",
72+
Self::U32 => "uint32_t",
73+
Self::U64 => "uint64_t",
74+
Self::I8 => "int8_t",
75+
Self::I16 => "int16_t",
76+
Self::I32 => "int32_t",
77+
Self::I64 => "int64_t",
78+
Self::U8Ptr => "uint8_t *",
79+
Self::U16Ptr => "uint16_t *",
80+
Self::U32Ptr => "uint32_t *",
81+
Self::U64Ptr => "uint64_t *",
82+
Self::I8Ptr => "int8_t *",
83+
Self::I16Ptr => "int16_t *",
84+
Self::I32Ptr => "int32_t *",
85+
Self::I64Ptr => "int64_t *",
86+
}
87+
}
88+
}

svd-rs/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ pub use self::readaction::ReadAction;
9090
pub mod protection;
9191
pub use self::protection::Protection;
9292

93+
/// DataType objects
94+
pub mod datatype;
95+
pub use self::datatype::DataType;
96+
9397
/// Level of validation
9498
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
9599
pub enum ValidateLevel {

0 commit comments

Comments
 (0)