-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add serde support #102
Add serde support #102
Changes from 10 commits
da45515
abde839
089ace4
2ff00d3
9828f42
62eb5c1
3ba5480
6a76707
254dee2
e308156
0f63995
7ce11dd
29dacae
411fc52
e5f9bc9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,16 +26,17 @@ all-features = true | |
rustdoc-args = ["--cfg", "docsrs"] | ||
|
||
[dependencies] | ||
arrayvec = { version = "0.7.2", default-features = false } | ||
arrayvec = { version = "0.7.2", default-features = false} | ||
chrono = { version = "0.4.19", default-features = false } | ||
heapless = "0.7.15" | ||
heapless = { version ="0.7.15"} | ||
nom = { version = "7.1.1", default-features = false } | ||
|
||
# we include num-traits only when `std` is not enabled | ||
# because of `fract()` and `trunc()` methods | ||
num-traits = { version = "0.2", default-features = false, features = ["libm"]} | ||
|
||
cfg-if = "1" | ||
serde = { version = "1.0.163", default-features = false, optional = true } | ||
serde_with = { version = "3.0.0", default-features = false, optional = true } | ||
|
||
[dev-dependencies] | ||
approx = "0.5.1" | ||
|
@@ -47,6 +48,14 @@ criterion = "0.4" | |
[features] | ||
default = ["std", "all-sentences"] | ||
std = ["nom/std", "chrono/std", "arrayvec/std"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you mind adding |
||
serde = [ | ||
"serde/derive", | ||
"serde_with/macros", | ||
"serde_with/chrono_0_4", | ||
"heapless/serde", | ||
"chrono/serde", | ||
"arrayvec/serde" | ||
] | ||
|
||
|
||
all-sentences = ["GNSS", "waypoint", "maritime", "water", "vendor-specific", "other"] | ||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -11,6 +11,9 @@ use crate::{ | |||||||
Error, ParseResult, | ||||||||
}; | ||||||||
|
||||||||
#[cfg(feature = "serde")] | ||||||||
use serde::{de::Visitor, ser::SerializeSeq, Deserialize, Serialize}; | ||||||||
|
||||||||
/// NMEA parser | ||||||||
/// | ||||||||
/// This struct parses NMEA sentences, including checksum checks and sentence | ||||||||
|
@@ -31,6 +34,7 @@ use crate::{ | |||||||
/// # } | ||||||||
|
||||||||
/// ``` | ||||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | ||||||||
#[derive(Debug, Clone, Default)] | ||||||||
pub struct Nmea { | ||||||||
pub fix_time: Option<NaiveTime>, | ||||||||
|
@@ -434,6 +438,7 @@ impl fmt::Display for Nmea { | |||||||
} | ||||||||
} | ||||||||
|
||||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | ||||||||
#[derive(Debug, Clone, Default)] | ||||||||
struct SatsPack { | ||||||||
/// max number of visible GNSS satellites per hemisphere, assuming global coverage | ||||||||
|
@@ -442,10 +447,58 @@ struct SatsPack { | |||||||
/// BeiDou: 12 + 3 IGSO + 3 GEO | ||||||||
/// Galileo: 12 | ||||||||
/// => 58 total Satellites => max 15 rows of data | ||||||||
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_deque"))] | ||||||||
#[cfg_attr(feature = "serde", serde(deserialize_with = "deserialize_deque"))] | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
mod serde_deque {
fn serialize(...) { ... }
fn deserialize(...) { ... }
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for reviewing this PR @wiktorwieclaw . Your comment has been updated in the PR. We can fit it into 1 like but I have approved the PR as is. |
||||||||
data: Deque<Vec<Option<Satellite>, 4>, 15>, | ||||||||
max_len: usize, | ||||||||
} | ||||||||
|
||||||||
#[cfg(feature = "serde")] | ||||||||
fn serialize_deque<S>(v: &Deque<Vec<Option<Satellite>, 4>, 15>, s: S) -> Result<S::Ok, S::Error> | ||||||||
where | ||||||||
S: serde::Serializer, | ||||||||
{ | ||||||||
let mut seq = s.serialize_seq(Some(15))?; | ||||||||
for e in v.iter() { | ||||||||
seq.serialize_element(e)?; | ||||||||
} | ||||||||
seq.end() | ||||||||
} | ||||||||
|
||||||||
#[cfg(feature = "serde")] | ||||||||
struct DequeVisitor; | ||||||||
|
||||||||
#[cfg(feature = "serde")] | ||||||||
impl<'de> Visitor<'de> for DequeVisitor { | ||||||||
type Value = Deque<Vec<Option<Satellite>, 4>, 15>; | ||||||||
|
||||||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | ||||||||
formatter.write_str("deque") | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should be a bit more specific here |
||||||||
} | ||||||||
|
||||||||
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> | ||||||||
where | ||||||||
A: serde::de::SeqAccess<'de>, | ||||||||
{ | ||||||||
let mut deq: Deque<Vec<Option<Satellite>, 4>, 15> = Deque::new(); | ||||||||
|
||||||||
while let Some(v) = seq.next_element()? { | ||||||||
deq.push_back(v).expect("Cannot deserialize"); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||
} | ||||||||
|
||||||||
Ok(deq) | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
#[cfg(feature = "serde")] | ||||||||
fn deserialize_deque<'de, D>(d: D) -> Result<Deque<Vec<Option<Satellite>, 4>, 15>, D::Error> | ||||||||
where | ||||||||
D: serde::Deserializer<'de>, | ||||||||
{ | ||||||||
d.deserialize_seq(DequeVisitor) | ||||||||
} | ||||||||
|
||||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | ||||||||
#[derive(Clone, PartialEq)] | ||||||||
/// Satellite information | ||||||||
pub struct Satellite { | ||||||||
|
@@ -708,6 +761,7 @@ define_sentence_type_enum! { | |||||||
/// ### Vendor extensions | ||||||||
/// | ||||||||
/// - [`SentenceType::RMZ`] | ||||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | ||||||||
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)] | ||||||||
#[repr(u32)] | ||||||||
#[allow(rustdoc::bare_urls)] | ||||||||
|
@@ -1191,6 +1245,7 @@ define_sentence_type_enum! { | |||||||
} | ||||||||
} | ||||||||
|
||||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | ||||||||
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)] | ||||||||
pub struct SentenceMask { | ||||||||
mask: u128, | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please make sure to fix the formatting