make serde dependency optional
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good

This commit is contained in:
Robin Müller 2022-12-04 12:17:36 +01:00
parent 97c70bf03b
commit 938c4ba770
No known key found for this signature in database
GPG Key ID: 71B58F8A3CDFA9AC
6 changed files with 47 additions and 14 deletions

View File

@ -18,6 +18,7 @@ delegate = "0.8"
[dependencies.serde]
version = "1.0"
optional = true
default-features = false
features = ["derive"]
@ -32,8 +33,11 @@ default-features = false
[dev-dependencies.postcard]
version = "1.0"
[dev-dependencies.serde_json]
version = "1.0"
[features]
default = ["std"]
default = ["std", "serde"]
std = ["chrono/std", "chrono/clock", "alloc"]
alloc = ["postcard/alloc"]

View File

@ -4,6 +4,7 @@ use crate::{ByteConversionError, CcsdsPacket, SizeMissmatch};
use core::fmt::Debug;
use core::mem::size_of;
use crc::{Crc, CRC_16_IBM_3740};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
pub type CrcType = u16;
@ -13,7 +14,8 @@ pub const CRC_CCITT_FALSE: Crc<u16> = Crc::<u16>::new(&CRC_16_IBM_3740);
pub const CCSDS_HEADER_LEN: usize = size_of::<crate::zc::SpHeader>();
/// All PUS versions. Only PUS C is supported by this library.
#[derive(PartialEq, Eq, Copy, Clone, Serialize, Deserialize, Debug)]
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum PusVersion {
EsaPus = 0,
PusA = 1,

View File

@ -51,6 +51,7 @@ extern crate std;
use crate::ecss::CCSDS_HEADER_LEN;
use delegate::delegate;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
pub mod ecss;
@ -62,12 +63,14 @@ pub const MAX_APID: u16 = 2u16.pow(11) - 1;
pub const MAX_SEQ_COUNT: u16 = 2u16.pow(14) - 1;
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct SizeMissmatch {
pub found: usize,
pub expected: usize,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum ByteConversionError {
/// The passed slice is too small. Returns the passed slice length and expected minimum size
ToSliceTooSmall(SizeMissmatch),
@ -78,7 +81,8 @@ pub enum ByteConversionError {
ZeroCopyFromError,
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Copy, Clone)]
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum PacketType {
Tm = 0,
Tc = 1,
@ -100,7 +104,8 @@ pub fn packet_type_in_raw_packet_id(packet_id: u16) -> PacketType {
PacketType::try_from((packet_id >> 12) as u8 & 0b1).unwrap()
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Copy, Clone)]
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum SequenceFlags {
ContinuationSegment = 0b00,
FirstSegment = 0b01,
@ -124,7 +129,8 @@ impl TryFrom<u8> for SequenceFlags {
}
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Copy, Clone)]
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct PacketId {
pub ptype: PacketType,
pub sec_header_flag: bool,
@ -171,7 +177,8 @@ impl From<u16> for PacketId {
}
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Copy, Clone)]
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct PacketSequenceCtrl {
pub seq_flags: SequenceFlags,
seq_count: u16,
@ -318,7 +325,8 @@ pub trait CcsdsPrimaryHeader {
/// 13 bits of the first two bytes of the raw header
/// * `psc` - Packet Sequence Control, occupies the third and fourth byte of the raw header
/// * `data_len` - Data length field occupies the fifth and the sixth byte of the raw header
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Eq, Copy, Clone)]
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct SpHeader {
pub version: u8,
pub packet_id: PacketId,
@ -550,6 +558,7 @@ mod tests {
packet_type_in_raw_packet_id, zc, CcsdsPacket, PacketId, PacketSequenceCtrl, PacketType,
SequenceFlags,
};
#[cfg(feature = "alloc")]
use alloc::vec;
#[cfg(not(feature = "std"))]
use num::pow;
@ -558,6 +567,7 @@ mod tests {
use postcard::from_bytes;
#[cfg(feature = "alloc")]
use postcard::to_allocvec;
use std::println;
#[test]
fn test_seq_flag_helpers() {
@ -640,7 +650,7 @@ mod tests {
}
#[test]
#[cfg(feature = "std")]
#[cfg(all(feature = "std", feature = "serde"))]
fn test_serde_sph() {
let sp_header = SpHeader::tc(0x42, 12, 0).expect("Error creating SP header");
assert_eq!(sp_header.ccsds_version(), 0b000);
@ -763,4 +773,13 @@ mod tests {
assert_eq!(sp_header.ptype(), PacketType::Tc);
assert_eq!(sp_header.data_len(), 0);
}
#[test]
#[cfg(feature = "serde")]
fn test_serde_serialization() {
let sp_header = SpHeader::tc(0x42, 12, 0).expect("Error creating SP header");
let as_str =
serde_json::to_string(&sp_header).expect("Converting SP header to JSON string failed");
println!("{:?}", as_str);
}
}

View File

@ -41,6 +41,7 @@ use crate::{
};
use core::mem::size_of;
use delegate::delegate;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use zerocopy::AsBytes;
@ -136,7 +137,8 @@ pub mod zc {
}
}
#[derive(PartialEq, Eq, Copy, Clone, Serialize, Deserialize, Debug)]
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct PusTcSecondaryHeader {
pub service: u8,
pub subservice: u8,
@ -211,14 +213,15 @@ impl PusTcSecondaryHeader {
/// [postcard](https://docs.rs/postcard/latest/postcard/).
///
/// There is no spare bytes support yet.
#[derive(PartialEq, Eq, Copy, Clone, Serialize, Deserialize, Debug)]
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct PusTc<'slice> {
sp_header: SpHeader,
pub sec_header: PusTcSecondaryHeader,
/// If this is set to false, a manual call to [PusTc::calc_own_crc16] or
/// [PusTc::update_packet_fields] is necessary for the serialized or cached CRC16 to be valid.
pub calc_crc_on_serialization: bool,
#[serde(skip)]
#[cfg_attr(feature = "serde", serde(skip))]
raw_data: Option<&'slice [u8]>,
app_data: Option<&'slice [u8]>,
crc16: Option<u16>,
@ -480,6 +483,7 @@ mod tests {
use crate::tc::{PusTc, PusTcSecondaryHeader, PusTcSecondaryHeaderT};
use crate::{ByteConversionError, SpHeader};
use crate::{CcsdsPacket, SequenceFlags};
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
fn base_ping_tc_full_ctor() -> PusTc<'static> {

View File

@ -272,6 +272,7 @@ mod tests {
use super::*;
use crate::time::TimestampError::{InvalidTimeCode, OtherPacketError};
use crate::ByteConversionError::{FromSliceTooSmall, ToSliceTooSmall};
#[cfg(feature = "alloc")]
use alloc::format;
use chrono::{Datelike, Timelike};

View File

@ -9,6 +9,7 @@ use crate::{
CCSDS_HEADER_LEN,
};
use core::mem::size_of;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use zerocopy::AsBytes;
@ -104,7 +105,8 @@ pub mod zc {
}
}
#[derive(PartialEq, Eq, Serialize, Deserialize, Copy, Clone, Debug)]
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct PusTmSecondaryHeader<'slice> {
pus_version: PusVersion,
pub sc_time_ref_status: u8,
@ -198,14 +200,15 @@ impl<'slice> TryFrom<zc::PusTmSecHeader<'slice>> for PusTmSecondaryHeader<'slice
/// [postcard](https://docs.rs/postcard/latest/postcard/).
///
/// There is no spare bytes support yet.
#[derive(PartialEq, Eq, Serialize, Deserialize, Debug, Copy, Clone)]
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct PusTm<'slice> {
pub sp_header: SpHeader,
pub sec_header: PusTmSecondaryHeader<'slice>,
/// If this is set to false, a manual call to [PusTm::calc_own_crc16] or
/// [PusTm::update_packet_fields] is necessary for the serialized or cached CRC16 to be valid.
pub calc_crc_on_serialization: bool,
#[serde(skip)]
#[cfg_attr(feature = "serde", serde(skip))]
raw_data: Option<&'slice [u8]>,
source_data: Option<&'slice [u8]>,
crc16: Option<u16>,