|
1 |
| -use super::{DimElement, Name}; |
| 1 | +use super::{Description, DimElement, Name}; |
2 | 2 | use core::ops::{Deref, DerefMut};
|
3 | 3 |
|
4 | 4 | /// A single SVD instance or array of instances
|
@@ -50,11 +50,48 @@ where
|
50 | 50 | }
|
51 | 51 | }
|
52 | 52 |
|
| 53 | +impl<T> Description for MaybeArray<T> |
| 54 | +where |
| 55 | + T: Description, |
| 56 | +{ |
| 57 | + fn description(&self) -> Option<&str> { |
| 58 | + T::description(self) |
| 59 | + } |
| 60 | +} |
| 61 | + |
53 | 62 | /// Return list of names of instances in array
|
54 | 63 | pub fn names<'a, T: Name>(info: &'a T, dim: &'a DimElement) -> impl Iterator<Item = String> + 'a {
|
55 | 64 | let name = info.name();
|
56 |
| - dim.indexes() |
57 |
| - .map(move |i| name.replace("[%s]", &i).replace("%s", &i)) |
| 65 | + dim.indexes().map(move |i| { |
| 66 | + dim.dim_array_index |
| 67 | + .as_ref() |
| 68 | + .and_then(|dai| { |
| 69 | + dai.values |
| 70 | + .iter() |
| 71 | + .find(|e| e.value.map(|v| v.to_string().as_str() == i.deref()) == Some(true)) |
| 72 | + }) |
| 73 | + .map(|n| n.name.clone()) |
| 74 | + .unwrap_or_else(|| name.replace("[%s]", &i).replace("%s", &i)) |
| 75 | + }) |
| 76 | +} |
| 77 | + |
| 78 | +/// Return list of descriptions of instances in array |
| 79 | +pub fn descriptions<'a, T: Description>( |
| 80 | + info: &'a T, |
| 81 | + dim: &'a DimElement, |
| 82 | +) -> impl Iterator<Item = Option<String>> + 'a { |
| 83 | + let description = info.description(); |
| 84 | + dim.indexes().map(move |i| { |
| 85 | + dim.dim_array_index |
| 86 | + .as_ref() |
| 87 | + .and_then(|dai| { |
| 88 | + dai.values |
| 89 | + .iter() |
| 90 | + .find(|e| e.value.map(|v| v.to_string().as_str() == i.deref()) == Some(true)) |
| 91 | + }) |
| 92 | + .and_then(|n| n.description.clone()) |
| 93 | + .or_else(|| description.map(|d| d.replace("[%s]", &i).replace("%s", &i))) |
| 94 | + }) |
58 | 95 | }
|
59 | 96 |
|
60 | 97 | #[cfg(feature = "serde")]
|
|
0 commit comments