rtcm_rs/util/
mod.rs

1use tinyvec::ArrayVec;
2
3mod array_string;
4mod data_vec;
5mod grid16p;
6
7#[cfg(feature = "serde")]
8use crate::{Deserialize, Serialize, Visitor};
9#[allow(unused)]
10pub use array_string::ArrayString;
11use core::{fmt::Write, slice::Iter};
12pub use data_vec::DataVec;
13pub use grid16p::Grid16P;
14
15#[derive(Clone, PartialEq)]
16pub struct Df88591String<const N: usize>(ArrayVec<[u8; N]>);
17impl<const N: usize> Df88591String<N> {
18    pub fn new() -> Self {
19        Df88591String(ArrayVec::new())
20    }
21    pub fn chars(&self) -> Df88591StringChars<'_> {
22        Df88591StringChars {
23            iter: self.0.iter(),
24        }
25    }
26    pub fn iter(&self) -> Iter<'_, u8> {
27        self.0.iter()
28    }
29    pub fn len(&self) -> usize {
30        self.0.len()
31    }
32    pub fn push(&mut self, val: u8) {
33        self.0.push(if val == 0 { 0xa4 } else { val });
34    }
35    pub fn push_char(&mut self, ch: char) {
36        self.0.push(Df88591StringChars::<'_>::from_char(ch));
37    }
38    #[inline]
39    pub fn try_push(&mut self, ch: char) -> Result<(), ()> {
40        if self.0.len() + 1 > self.0.capacity() {
41            return Err(());
42        }
43        self.0.push(Df88591StringChars::<'_>::from_char(ch));
44        Ok(())
45    }
46}
47impl<const N: usize> From<&str> for Df88591String<N> {
48    fn from(value: &str) -> Self {
49        value.chars().collect()
50    }
51}
52impl<const N: usize> FromIterator<char> for Df88591String<N> {
53    fn from_iter<I: IntoIterator<Item = char>>(iter: I) -> Df88591String<N> {
54        let mut buf = Df88591String::new();
55        for c in iter.into_iter() {
56            if buf.try_push(c).is_err() {
57                break;
58            }
59        }
60        buf
61    }
62}
63impl<const N: usize> Default for Df88591String<N> {
64    fn default() -> Self {
65        Df88591String::new()
66    }
67}
68impl<const N: usize> core::fmt::Display for Df88591String<N> {
69    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
70        //use core::fmt::Write;
71        write!(f, "Df88591String<{}>(\"", N)?;
72        for c in self.chars() {
73            f.write_char(c)?;
74        }
75        f.write_str("\")")
76    }
77}
78impl<const N: usize> core::fmt::Debug for Df88591String<N> {
79    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
80        let val: ArrayVec<[char; N]> = self
81            .0
82            .iter()
83            .map(|v| Df88591StringChars::to_char(*v))
84            .collect();
85        f.debug_tuple("Df88591String").field(&val).finish()
86    }
87}
88
89pub struct Df88591StringChars<'a> {
90    iter: core::slice::Iter<'a, u8>,
91}
92
93impl Df88591StringChars<'_> {
94    fn to_char(code: u8) -> char {
95        if code == 0 {
96            //Character 0x00 not allowed for Rtcm
97            char::from_u32(0xa4).unwrap()
98        } else {
99            char::from_u32(code as u32).unwrap()
100        }
101    }
102    fn from_char(ch: char) -> u8 {
103        let code = ch as u32;
104        if code > 0 && code < 256 {
105            code as u8
106        } else {
107            0xa4
108        }
109    }
110}
111
112impl<'a> Iterator for Df88591StringChars<'a> {
113    type Item = char;
114
115    #[inline]
116    fn next(&mut self) -> Option<char> {
117        Some(Df88591StringChars::to_char(*self.iter.next()?))
118    }
119
120    #[inline]
121    fn count(self) -> usize {
122        self.iter.count()
123    }
124
125    #[inline]
126    fn size_hint(&self) -> (usize, Option<usize>) {
127        let len = self.iter.len();
128        (len, Some(len))
129    }
130}
131#[cfg(feature = "serde")]
132impl<const N: usize> Serialize for Df88591String<N> {
133    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
134    where
135        S: sd::Serializer,
136    {
137        let value: ArrayString<N> = self.chars().collect();
138
139        serializer.serialize_str(&value)
140    }
141}
142#[cfg(feature = "serde")]
143impl<'de, const N: usize> Deserialize<'de> for Df88591String<N> {
144    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
145    where
146        D: sd::Deserializer<'de>,
147    {
148        struct Str88591Visitor<const N: usize>;
149
150        impl<'de, const N: usize> Visitor<'de> for Str88591Visitor<N> {
151            type Value = Df88591String<N>;
152
153            fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
154                formatter.write_str("an ISO 8859-1 encoded string")
155            }
156
157            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> {
158                let mut value = Df88591String::<N>::new();
159                for ch in v.chars().take(N) {
160                    value.push_char(ch);
161                }
162                Ok(value)
163            }
164        }
165
166        deserializer.deserialize_str(Str88591Visitor::<N>)
167    }
168}
169
170#[cfg(feature = "test_gen")]
171use crate::source_repr::SourceRepr;
172
173#[cfg(feature = "test_gen")]
174impl<const N: usize> SourceRepr for Df88591String<N> {
175    fn to_source(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
176        let s = self.chars().collect::<ArrayString<N>>();
177        write!(f, "Df88591String::<{}>::from(\"{}\")", N, s.as_ref())
178    }
179}
180
181/*
182pub struct DfString<const N: usize>(ArrayVec<[u8; N]>);
183*/
184
185/*
186#[cfg(test)]
187mod util_tests {
188    use super::Df88591String;
189}
190 */