|
1 |
| -use core::ops::{Deref, DerefMut}; |
2 |
| -use std::borrow::Cow; |
3 |
| - |
4 |
| -use super::{ClusterInfo, DimElement}; |
| 1 | +use super::{array::SingleArray, ClusterInfo}; |
5 | 2 |
|
6 | 3 | /// Cluster describes a sequence of neighboring registers within a peripheral.
|
7 |
| -#[derive(Clone, Debug, PartialEq)] |
8 |
| -pub enum Cluster { |
9 |
| - /// A single cluster, without any dimension. |
10 |
| - Single(ClusterInfo), |
11 |
| - /// A cluster array |
12 |
| - Array(ClusterInfo, DimElement), |
13 |
| -} |
14 |
| - |
15 |
| -impl Deref for Cluster { |
16 |
| - type Target = ClusterInfo; |
17 |
| - |
18 |
| - fn deref(&self) -> &ClusterInfo { |
19 |
| - match self { |
20 |
| - Self::Single(info) => info, |
21 |
| - Self::Array(info, _) => info, |
22 |
| - } |
23 |
| - } |
24 |
| -} |
25 |
| - |
26 |
| -impl DerefMut for Cluster { |
27 |
| - fn deref_mut(&mut self) -> &mut ClusterInfo { |
28 |
| - match self { |
29 |
| - Self::Single(info) => info, |
30 |
| - Self::Array(info, _) => info, |
31 |
| - } |
32 |
| - } |
33 |
| -} |
34 |
| - |
35 |
| -impl Cluster { |
36 |
| - /// Return `true` if cluster instance is single |
37 |
| - pub const fn is_single(&self) -> bool { |
38 |
| - matches!(self, Self::Single(_)) |
39 |
| - } |
40 |
| - /// Return `true` if it is cluster array |
41 |
| - pub const fn is_array(&self) -> bool { |
42 |
| - matches!(self, Self::Array(_, _)) |
43 |
| - } |
44 |
| - /// Returns list of register or register array names |
45 |
| - pub fn names(&self) -> Vec<Cow<str>> { |
46 |
| - match self { |
47 |
| - Self::Single(info) => vec![info.name.as_str().into()], |
48 |
| - Self::Array(info, dim) => dim |
49 |
| - .indexes() |
50 |
| - .map(|i| info.name.replace("[%s]", &i).replace("%s", &i).into()) |
51 |
| - .collect(), |
52 |
| - } |
53 |
| - } |
54 |
| - /// Returns list of register or register array address_offsets |
55 |
| - pub fn address_offsets(&self) -> Vec<u32> { |
56 |
| - match self { |
57 |
| - Self::Single(info) => vec![info.address_offset], |
58 |
| - Self::Array(info, dim) => (0..dim.dim) |
59 |
| - .map(|n| info.address_offset + n * dim.dim_increment) |
60 |
| - .collect(), |
61 |
| - } |
62 |
| - } |
63 |
| -} |
64 |
| - |
65 |
| -#[cfg(feature = "serde")] |
66 |
| -mod ser_de { |
67 |
| - use super::*; |
68 |
| - use crate::{DeserArray, SerArray}; |
69 |
| - use serde::{Deserialize, Deserializer, Serialize, Serializer}; |
70 |
| - |
71 |
| - impl Serialize for Cluster { |
72 |
| - fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
73 |
| - where |
74 |
| - S: Serializer, |
75 |
| - { |
76 |
| - match self { |
77 |
| - Self::Single(info) => info.serialize(serializer), |
78 |
| - Self::Array(info, dim) => SerArray { dim, info }.serialize(serializer), |
79 |
| - } |
80 |
| - } |
81 |
| - } |
82 |
| - |
83 |
| - impl<'de> Deserialize<'de> for Cluster { |
84 |
| - fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
85 |
| - where |
86 |
| - D: Deserializer<'de>, |
87 |
| - { |
88 |
| - let DeserArray { dim, info } = DeserArray::<ClusterInfo>::deserialize(deserializer)?; |
89 |
| - if let Some(dim) = dim { |
90 |
| - Ok(info.array(dim)) |
91 |
| - } else { |
92 |
| - Ok(info.single()) |
93 |
| - } |
94 |
| - } |
95 |
| - } |
96 |
| -} |
| 4 | +pub type Cluster = SingleArray<ClusterInfo>; |
0 commit comments