Skip to content

Commit 4d0c2dd

Browse files
committed
parse array
1 parent aa65d88 commit 4d0c2dd

File tree

6 files changed

+41
-95
lines changed

6 files changed

+41
-95
lines changed

svd-parser/src/array.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use super::*;
2+
use svd_rs::{DimElement, Name, SingleArray};
3+
4+
pub fn parse_array<T>(tag: &str, tree: &Node, config: &Config) -> Result<SingleArray<T>, SVDErrorAt>
5+
where
6+
T: Parse<Object = T, Error = SVDErrorAt, Config = Config> + Name,
7+
{
8+
if !tree.has_tag_name(tag) {
9+
return Err(SVDError::NotExpectedTag(tag.into()).at(tree.id()));
10+
}
11+
12+
let info = T::parse(tree, config)?;
13+
14+
if tree.get_child("dimIncrement").is_some() {
15+
let array_info = DimElement::parse(tree, config)?;
16+
check_has_placeholder(info.name(), tag).map_err(|e| e.at(tree.id()))?;
17+
if let Some(indexes) = &array_info.dim_index {
18+
if array_info.dim as usize != indexes.len() {
19+
return Err(SVDError::IncorrectDimIndexesCount(
20+
array_info.dim as usize,
21+
indexes.len(),
22+
)
23+
.at(tree.id()));
24+
}
25+
}
26+
Ok(SingleArray::Array(info, array_info))
27+
} else {
28+
Ok(SingleArray::Single(info))
29+
}
30+
}

svd-parser/src/cluster.rs

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,12 @@
11
use super::*;
2-
use crate::svd::{Cluster, ClusterInfo, DimElement};
2+
use crate::svd::Cluster;
33

44
impl Parse for Cluster {
55
type Object = Self;
66
type Error = SVDErrorAt;
77
type Config = Config;
88

99
fn parse(tree: &Node, config: &Self::Config) -> Result<Self, Self::Error> {
10-
if !tree.has_tag_name("cluster") {
11-
return Err(SVDError::NotExpectedTag("cluster".to_string()).at(tree.id()));
12-
}
13-
14-
let info = ClusterInfo::parse(tree, config)?;
15-
16-
if tree.get_child("dimIncrement").is_some() {
17-
let array_info = DimElement::parse(tree, config)?;
18-
check_has_placeholder(&info.name, "cluster").map_err(|e| e.at(tree.id()))?;
19-
if let Some(indexes) = &array_info.dim_index {
20-
if array_info.dim as usize != indexes.len() {
21-
return Err(SVDError::IncorrectDimIndexesCount(
22-
array_info.dim as usize,
23-
indexes.len(),
24-
)
25-
.at(tree.id()));
26-
}
27-
}
28-
Ok(info.array(array_info))
29-
} else {
30-
Ok(info.single())
31-
}
10+
parse_array("cluster", tree, config)
3211
}
3312
}

svd-parser/src/field.rs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,12 @@
11
use super::*;
2-
use crate::svd::{DimElement, Field, FieldInfo};
3-
use crate::{elementext::ElementExt, Config};
2+
use crate::svd::Field;
43

54
impl Parse for Field {
65
type Object = Self;
76
type Error = SVDErrorAt;
87
type Config = Config;
98

109
fn parse(tree: &Node, config: &Self::Config) -> Result<Self, Self::Error> {
11-
if !tree.has_tag_name("field") {
12-
return Err(SVDError::NotExpectedTag("field".to_string()).at(tree.id()));
13-
}
14-
15-
let info = FieldInfo::parse(tree, config)?;
16-
17-
if tree.get_child("dimIncrement").is_some() {
18-
let array_info = DimElement::parse(tree, config)?;
19-
check_has_placeholder(&info.name, "field").map_err(|e| e.at(tree.id()))?;
20-
if let Some(indexes) = &array_info.dim_index {
21-
if array_info.dim as usize != indexes.len() {
22-
return Err(SVDError::IncorrectDimIndexesCount(
23-
array_info.dim as usize,
24-
indexes.len(),
25-
)
26-
.at(tree.id()));
27-
}
28-
}
29-
Ok(info.array(array_info))
30-
} else {
31-
Ok(info.single())
32-
}
10+
parse_array("field", tree, config)
3311
}
3412
}

svd-parser/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ fn trim_utf8_bom(s: &str) -> &str {
146146
}
147147
}
148148

149+
mod array;
150+
use array::parse_array;
151+
149152
mod access;
150153
mod addressblock;
151154
mod bitrange;

svd-parser/src/peripheral.rs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,12 @@
11
use super::*;
2-
use crate::elementext::ElementExt;
3-
use crate::svd::{DimElement, Peripheral, PeripheralInfo};
2+
use crate::svd::Peripheral;
43

54
impl Parse for Peripheral {
65
type Object = Self;
76
type Error = SVDErrorAt;
87
type Config = Config;
98

109
fn parse(tree: &Node, config: &Self::Config) -> Result<Self, Self::Error> {
11-
if !tree.has_tag_name("peripheral") {
12-
return Err(SVDError::NotExpectedTag("peripheral".to_string()).at(tree.id()));
13-
}
14-
15-
let info = PeripheralInfo::parse(tree, config)?;
16-
17-
if tree.get_child("dimIncrement").is_some() {
18-
let array_info = DimElement::parse(tree, config)?;
19-
check_has_placeholder(&info.name, "peripheral").map_err(|e| e.at(tree.id()))?;
20-
if let Some(indexes) = &array_info.dim_index {
21-
if array_info.dim as usize != indexes.len() {
22-
return Err(SVDError::IncorrectDimIndexesCount(
23-
array_info.dim as usize,
24-
indexes.len(),
25-
)
26-
.at(tree.id()));
27-
}
28-
}
29-
Ok(info.array(array_info))
30-
} else {
31-
Ok(info.single())
32-
}
10+
parse_array("peripheral", tree, config)
3311
}
3412
}

svd-parser/src/register.rs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,12 @@
11
use super::*;
2-
use crate::elementext::ElementExt;
3-
use crate::svd::{DimElement, Register, RegisterInfo};
2+
use crate::svd::Register;
43

54
impl Parse for Register {
65
type Object = Self;
76
type Error = SVDErrorAt;
87
type Config = Config;
98

109
fn parse(tree: &Node, config: &Self::Config) -> Result<Self, Self::Error> {
11-
if !tree.has_tag_name("register") {
12-
return Err(SVDError::NotExpectedTag("register".to_string()).at(tree.id()));
13-
}
14-
15-
let info = RegisterInfo::parse(tree, config)?;
16-
17-
if tree.get_child("dimIncrement").is_some() {
18-
let array_info = DimElement::parse(tree, config)?;
19-
check_has_placeholder(&info.name, "register").map_err(|e| e.at(tree.id()))?;
20-
if let Some(indexes) = &array_info.dim_index {
21-
if array_info.dim as usize != indexes.len() {
22-
return Err(SVDError::IncorrectDimIndexesCount(
23-
array_info.dim as usize,
24-
indexes.len(),
25-
)
26-
.at(tree.id()));
27-
}
28-
}
29-
Ok(info.array(array_info))
30-
} else {
31-
Ok(info.single())
32-
}
10+
parse_array("register", tree, config)
3311
}
3412
}

0 commit comments

Comments
 (0)