|
| 1 | +use super::{DimElement, Name}; |
| 2 | +use core::ops::{Deref, DerefMut}; |
| 3 | + |
| 4 | +/// A single SVD instance or array of instances |
| 5 | +#[derive(Clone, Debug, PartialEq)] |
| 6 | +pub enum SingleArray<T> { |
| 7 | + /// A single instance |
| 8 | + Single(T), |
| 9 | + /// An array of instances |
| 10 | + Array(T, DimElement), |
| 11 | +} |
| 12 | + |
| 13 | +impl<T> Deref for SingleArray<T> { |
| 14 | + type Target = T; |
| 15 | + |
| 16 | + fn deref(&self) -> &T { |
| 17 | + match self { |
| 18 | + Self::Single(info) => info, |
| 19 | + Self::Array(info, _) => info, |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +impl<T> DerefMut for SingleArray<T> { |
| 25 | + fn deref_mut(&mut self) -> &mut T { |
| 26 | + match self { |
| 27 | + Self::Single(info) => info, |
| 28 | + Self::Array(info, _) => info, |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +impl<T> SingleArray<T> { |
| 34 | + /// Return `true` if instance is single |
| 35 | + pub const fn is_single(&self) -> bool { |
| 36 | + matches!(self, Self::Single(_)) |
| 37 | + } |
| 38 | + /// Return `true` if it is an array |
| 39 | + pub const fn is_array(&self) -> bool { |
| 40 | + matches!(self, Self::Array(_, _)) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl<T> Name for SingleArray<T> |
| 45 | +where |
| 46 | + T: Name, |
| 47 | +{ |
| 48 | + fn name(&self) -> &str { |
| 49 | + T::name(self) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +#[cfg(feature = "serde")] |
| 54 | +mod ser_de { |
| 55 | + use super::*; |
| 56 | + use serde::{Deserialize, Deserializer, Serialize, Serializer}; |
| 57 | + |
| 58 | + #[derive(serde::Serialize)] |
| 59 | + struct SerArray<'a, T> { |
| 60 | + #[serde(flatten)] |
| 61 | + dim: &'a DimElement, |
| 62 | + #[serde(flatten)] |
| 63 | + info: &'a T, |
| 64 | + } |
| 65 | + |
| 66 | + #[derive(serde::Deserialize)] |
| 67 | + struct DeserArray<T> { |
| 68 | + #[serde(flatten, default)] |
| 69 | + dim: Option<DimElement>, |
| 70 | + #[serde(flatten)] |
| 71 | + info: T, |
| 72 | + } |
| 73 | + |
| 74 | + impl<T> Serialize for SingleArray<T> |
| 75 | + where |
| 76 | + T: Serialize, |
| 77 | + { |
| 78 | + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 79 | + where |
| 80 | + S: Serializer, |
| 81 | + { |
| 82 | + match self { |
| 83 | + Self::Single(info) => info.serialize(serializer), |
| 84 | + Self::Array(info, dim) => SerArray::<T> { dim, info }.serialize(serializer), |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + impl<'de, T> Deserialize<'de> for SingleArray<T> |
| 90 | + where |
| 91 | + T: Deserialize<'de>, |
| 92 | + { |
| 93 | + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 94 | + where |
| 95 | + D: Deserializer<'de>, |
| 96 | + { |
| 97 | + let DeserArray { dim, info } = DeserArray::<T>::deserialize(deserializer)?; |
| 98 | + if let Some(dim) = dim { |
| 99 | + Ok(Self::Array(info, dim)) |
| 100 | + } else { |
| 101 | + Ok(Self::Single(info)) |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments