Skip to content

Commit

Permalink
fixed serialization and deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
salsabiljb committed Nov 11, 2024
1 parent d60c16c commit 84c1d75
Show file tree
Hide file tree
Showing 28 changed files with 46 additions and 44 deletions.
2 changes: 0 additions & 2 deletions src/bin/aisparser.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use ais::lib;
use ais::sentence::{AisFragments, AisParser, AisSentence};
use lib::std::io::{self, BufRead};
use serde_json;


fn parse_nmea_line_to_json(parser: &mut AisParser, line: &[u8]) -> Result<(), ais::errors::Error> {
let sentence = parser.parse(line, true)?;
Expand Down
18 changes: 9 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! This library parses NMEA AIS (Automatic Identification System) sentences and provides
//! structured representations of the data, allowing further processing or analysis.
//!
//!
//! # Features
//! - Parses AIS NMEA sentences into structured types.
//! - Supports JSON serialization and deserialization for `AisSentence` objects.
Expand All @@ -14,7 +14,7 @@
//!
//! let line = b"!AIVDM,1,1,,B,E>kb9O9aS@7PUh10dh19@;0Tah2cWrfP:l?M`00003vP100,0*01";
//! let mut parser = AisParser::new();
//!
//!
//! if let AisFragments::Complete(sentence) = parser.parse(line, true).unwrap() {
//! assert_eq!(sentence.num_fragments, 1);
//! assert_eq!(sentence.channel, Some('B'));
Expand Down Expand Up @@ -85,8 +85,8 @@ pub mod sentence;
pub use errors::Result;
pub use sentence::{AisFragments, AisParser};

use serde_json::Error as SerdeError;
use sentence::AisSentence;
use serde_json::Error as SerdeError;

/// Serializes an `AisSentence` to JSON
pub fn serialize_to_json(sentence: &AisSentence) -> std::result::Result<String, SerdeError> {

Check failure

Code scanning / clippy

failed to resolve: use of undeclared crate or module std Error

failed to resolve: use of undeclared crate or module std

Check failure

Code scanning / clippy

cannot find type String in this scope Error

cannot find type String in this scope
Expand All @@ -112,7 +112,7 @@ mod test_helpers {
#[cfg(test)]
mod tests {
use super::*;
use crate::sentence::{AisReportType, TalkerId, AisSentence};
use crate::sentence::{AisReportType, AisSentence, TalkerId};

const TEST_MESSAGES: [&[u8]; 8] = [
b"!AIVDM,1,1,,B,E>kb9O9aS@7PUh10dh19@;0Tah2cWrfP:l?M`00003vP100,0*01",
Expand Down Expand Up @@ -144,8 +144,8 @@ mod tests {
println!("Serialized JSON: {}", json);

// Deserialize back from JSON
let deserialized_sentence = deserialize_from_json(&json)
.expect("Failed to deserialize from JSON");
let deserialized_sentence =
deserialize_from_json(&json).expect("Failed to deserialize from JSON");

assert_eq!(sentence, deserialized_sentence);
}
Expand All @@ -162,7 +162,7 @@ mod tests {
fragment_number: 1,
message_id: Some(123),
channel: Some('A'),
data: vec![69, 62, 107, 98, 57, 79], // sample data; replace with real data if needed
data: vec![69, 62, 107, 98, 57, 79], // sample data; replace with real data if needed
fill_bit_count: 0,
message_type: 1,
};
Expand All @@ -171,8 +171,8 @@ mod tests {
let json_data = serialize_to_json(&original_sentence).expect("Serialization failed");

// Deserialize back to an AisSentence
let deserialized_sentence: AisSentence = deserialize_from_json(&json_data)
.expect("Deserialization failed");
let deserialized_sentence: AisSentence =
deserialize_from_json(&json_data).expect("Deserialization failed");

// Check if the deserialized struct matches the original
assert_eq!(original_sentence, deserialized_sentence);
Expand Down
2 changes: 1 addition & 1 deletion src/messages/addressed_safety_related.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::errors::Result;
use nom::bits::{bits, complete::take as take_bits};
use nom::combinator::map;
use nom::IResult;
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Serialize, Deserialize, Eq)]
pub struct AddressedSafetyRelatedMessage {
Expand Down
2 changes: 1 addition & 1 deletion src/messages/aid_to_navigation_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::errors::Result;
use nom::bits::{bits, complete::take as take_bits};
use nom::combinator::map;
use nom::IResult;
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum NavaidType {
Expand Down
2 changes: 1 addition & 1 deletion src/messages/assignment_mode_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::AisMessageType;
use crate::errors::Result;
use nom::bits::{bits, complete::take as take_bits};
use nom::IResult;
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct AssignmentModeCommand {
Expand Down
2 changes: 1 addition & 1 deletion src/messages/base_station_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::errors::Result;
use nom::bits::{bits, complete::take as take_bits};
use nom::combinator::map;
use nom::IResult;
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct BaseStationReport {
Expand Down
8 changes: 4 additions & 4 deletions src/messages/binary_acknowledge.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//! Binary Acknowledge (type 7)
#[cfg(all(not(feature = "std"), not(feature = "alloc")))]
use super::nom_noalloc::many_m_n;
#[cfg(all(not(feature = "std"), not(feature = "alloc")))]
use super::nom_noalloc::many_m_n;
use super::AisMessageType;
use crate::errors::Result;
use crate::lib;
use nom::bits::{bits, complete::take as take_bits};
#[cfg(any(feature = "std", feature = "alloc"))]
#[cfg(any(feature = "std", feature = "alloc"))]
use nom::multi::many_m_n;
use nom::IResult;
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Acknowledgement {
Expand Down
2 changes: 1 addition & 1 deletion src/messages/binary_addressed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::lib;
use nom::bits::{bits, complete::take as take_bits};
use nom::combinator::map;
use nom::IResult;
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

#[cfg(all(not(feature = "std"), not(feature = "alloc")))]
const MAX_DATA_SIZE_BYTES: usize = 119;
Expand Down
2 changes: 1 addition & 1 deletion src/messages/binary_broadcast_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::errors::Result;
use crate::lib;
use nom::bits::{bits, complete::take as take_bits};
use nom::IResult;
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

#[cfg(all(not(feature = "std"), not(feature = "alloc")))]
const MAX_DATA_SIZE_BYTES: usize = 119;
Expand Down
2 changes: 1 addition & 1 deletion src/messages/data_link_management_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use nom::bits::{bits, complete::take as take_bits};
#[cfg(any(feature = "std", feature = "alloc"))]
use nom::multi::many_m_n;
use nom::IResult;
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct SlotReservation {
Expand Down
2 changes: 1 addition & 1 deletion src/messages/dgnss_broadcast_binary_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::lib;
use nom::bits::{bits, complete::take as take_bits};
use nom::combinator::map;
use nom::IResult;
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

#[cfg(all(not(feature = "std"), not(feature = "alloc")))]
const MAX_DATA_SIZE_BYTES: usize = 119;
Expand Down
2 changes: 1 addition & 1 deletion src/messages/extended_class_b_position_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::messages::types::ShipType;
use nom::bits::{bits, complete::take as take_bits};
use nom::combinator::map;
use nom::IResult;
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Serialize, Deserialize)]

Check failure

Code scanning / clippy

the trait bound heapless::String<20>: messages::_::_serde::Serialize is not satisfied Error

the trait bound heapless::String<20>: messages::_::_serde::Serialize is not satisfied

Check failure

Code scanning / clippy

the trait bound heapless::String<20>: messages::_::_serde::Deserialize<'_> is not satisfied Error

the trait bound heapless::String<20>: messages::_::_serde::Deserialize<'_> is not satisfied
pub struct ExtendedClassBPositionReport {
Expand Down
2 changes: 1 addition & 1 deletion src/messages/interrogation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::errors::Result;
use crate::lib;
use nom::bits::{bits, complete::take as take_bits};
use nom::IResult;
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Message {
Expand Down
2 changes: 1 addition & 1 deletion src/messages/long_range_ais_broadcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::errors::Result;
use nom::bits::{bits, complete::take as take_bits};
use nom::combinator::map;
use nom::IResult;
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct LongRangeAisBroadcastMessage {
Expand Down
3 changes: 1 addition & 2 deletions src/messages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use crate::lib;
use crate::sentence::AisRawData;
use serde::{Deserialize, Serialize};
#[cfg(feature = "std")]

pub mod addressed_safety_related;
pub mod aid_to_navigation_report;
pub mod assignment_mode_command;
pub mod base_station_report;
pub mod binary_acknowledge;
pub mod binary_addressed;
pub mod binary_broadcast_message;
pub mod data_link_management_message;
Expand All @@ -31,7 +31,6 @@ pub mod static_data_report;
pub mod types;
pub mod utc_date_inquiry;
pub mod utc_date_response;
pub mod binary_acknowledge;

pub use parsers::message_type;

Expand Down
2 changes: 1 addition & 1 deletion src/messages/navigation.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

pub fn parse_speed_over_ground(data: u16) -> Option<f32> {
match data {
Expand Down
2 changes: 1 addition & 1 deletion src/messages/position_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::errors::Result;
use nom::bits::{bits, complete::take as take_bits};
use nom::combinator::map;
use nom::IResult;
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct PositionReport {
Expand Down
2 changes: 1 addition & 1 deletion src/messages/radio_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use nom::bits::complete::take as take_bits;
use nom::combinator::map;
use nom::error::ErrorKind;
use nom::IResult;
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum RadioStatus {
Expand Down
2 changes: 1 addition & 1 deletion src/messages/safety_related_acknowledgment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use nom::bits::{bits, complete::take as take_bits};
#[cfg(any(feature = "std", feature = "alloc"))]
use nom::multi::many_m_n;
use nom::IResult;
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Acknowledgement {
Expand Down
2 changes: 1 addition & 1 deletion src/messages/safety_related_broadcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::AisMessageType;
use crate::errors::Result;
use nom::bits::{bits, complete::take as take_bits};
use nom::IResult;
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Serialize, Deserialize, Eq)]

Check failure

Code scanning / clippy

the trait bound heapless::String<20>: messages::_::_serde::Serialize is not satisfied Error

the trait bound heapless::String<20>: messages::_::_serde::Serialize is not satisfied

Check failure

Code scanning / clippy

the trait bound heapless::String<20>: messages::_::_serde::Deserialize<'_> is not satisfied Error

the trait bound heapless::String<20>: messages::_::_serde::Deserialize<'_> is not satisfied
pub struct SafetyRelatedBroadcastMessage {
Expand Down
2 changes: 1 addition & 1 deletion src/messages/standard_aircraft_position_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::errors::Result;
use nom::bits::{bits, complete::take as take_bits};
use nom::combinator::map;
use nom::IResult;
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct SARPositionReport {
Expand Down
2 changes: 1 addition & 1 deletion src/messages/standard_class_b_position_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::errors::Result;
use nom::bits::{bits, complete::take as take_bits};
use nom::combinator::map;
use nom::IResult;
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct StandardClassBPositionReport {
Expand Down
4 changes: 2 additions & 2 deletions src/messages/static_and_voyage_related_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use crate::lib;
use nom::bits::{bits, complete::take as take_bits};
use nom::combinator::map;
use nom::IResult;
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq,Serialize, Deserialize)]
#[derive(Debug, PartialEq, Serialize, Deserialize)]

Check failure

Code scanning / clippy

the trait bound heapless::String<20>: messages::_::_serde::Serialize is not satisfied Error

the trait bound heapless::String<20>: messages::_::_serde::Serialize is not satisfied

Check failure

Code scanning / clippy

the trait bound heapless::String<20>: messages::_::_serde::Deserialize<'_> is not satisfied Error

the trait bound heapless::String<20>: messages::_::_serde::Deserialize<'_> is not satisfied
pub struct StaticAndVoyageRelatedData {
pub message_type: u8,
pub repeat_indicator: u8,
Expand Down
2 changes: 1 addition & 1 deletion src/messages/static_data_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::lib;
use nom::bits::{bits, complete::take as take_bits};
use nom::combinator::map;
use nom::IResult;
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct StaticDataReport {
Expand Down
2 changes: 1 addition & 1 deletion src/messages/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Common data types
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

/// Electronic Position Fixing Device type. This is the
/// type of device used for determining the object's
Expand Down
2 changes: 1 addition & 1 deletion src/messages/utc_date_inquiry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::AisMessageType;
use crate::errors::Result;
use nom::bits::{bits, complete::take as take_bits};
use nom::IResult;
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct UtcDateInquiry {
Expand Down
2 changes: 1 addition & 1 deletion src/messages/utc_date_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::errors::Result;
use nom::bits::{bits, complete::take as take_bits};
use nom::combinator::map;
use nom::IResult;
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct UtcDateResponse {
Expand Down
11 changes: 8 additions & 3 deletions src/sentence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ use nom::combinator::{map, map_res, opt, peek, verify};
use nom::number::complete::hex_u32;
use nom::sequence::{delimited, terminated};
use nom::IResult;
use serde::{Serialize, Deserialize};

use serde::{Deserialize, Serialize};

pub const MAX_SENTENCE_SIZE_BYTES: usize = 384;

Expand Down Expand Up @@ -110,7 +109,7 @@ impl From<AisFragments> for Result<AisSentence> {
}
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
#[derive(Serialize, Deserialize, PartialEq, Debug)]

Check failure

Code scanning / clippy

the trait bound heapless::Vec<u8, 384>: messages::_::_serde::Serialize is not satisfied Error

the trait bound heapless::Vec<u8, 384>: messages::_::_serde::Serialize is not satisfied

Check failure

Code scanning / clippy

the trait bound heapless::Vec<u8, 384>: messages::_::_serde::Deserialize<'_> is not satisfied Error

the trait bound heapless::Vec<u8, 384>: messages::_::_serde::Deserialize<'_> is not satisfied
pub struct AisParser {
message_id: Option<u8>,
fragment_number: u8,
Expand Down Expand Up @@ -191,6 +190,12 @@ impl AisParser {
}
}

impl Default for AisParser {
fn default() -> Self {
Self::new()
}
}

#[derive(Serialize, Deserialize, PartialEq, Debug)]

Check failure

Code scanning / clippy

the trait bound heapless::Vec<u8, 384>: messages::_::_serde::Serialize is not satisfied Error

the trait bound heapless::Vec<u8, 384>: messages::_::_serde::Serialize is not satisfied

Check failure

Code scanning / clippy

the trait bound heapless::Vec<u8, 384>: messages::_::_serde::Deserialize<'_> is not satisfied Error

the trait bound heapless::Vec<u8, 384>: messages::_::_serde::Deserialize<'_> is not satisfied
/// Represents an NMEA sentence parsed as AIS
pub struct AisSentence {
Expand Down

0 comments on commit 84c1d75

Please sign in to comment.