dis-rs 0.13.0

An implementation of the Distributed Interactive Simulation protocol (IEEE-1278.1) in Rust. This main crate contains PDU implementations and facilities to read/write PDUs from Rust data structures to the wire format and vice versa. It supports versions 6 and 7 of the protocol.
Documentation
use crate::comment_r::builder::CommentRBuilder;
use crate::common::model::{
    length_padded_to_num, EntityId, PduBody, VariableDatum, BASE_VARIABLE_DATUM_LENGTH,
};
use crate::common::{BodyInfo, Interaction};
use crate::constants::EIGHT_OCTETS;
use crate::enumerations::PduType;
use crate::BodyRaw;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

const BASE_COMMENT_R_BODY_LENGTH: u16 = 20;

/// 5.12.4.13 Comment-R PDU
///
/// 7.11.13 Comment-R PDU
#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct CommentR {
    pub originating_id: EntityId,
    pub receiving_id: EntityId,
    pub variable_datum_records: Vec<VariableDatum>,
}

impl BodyRaw for CommentR {
    type Builder = CommentRBuilder;

    fn builder() -> CommentRBuilder {
        CommentRBuilder::new()
    }

    fn into_builder(self) -> CommentRBuilder {
        CommentRBuilder::new_from_body(self)
    }

    fn into_pdu_body(self) -> PduBody {
        PduBody::CommentR(self)
    }
}

impl BodyInfo for CommentR {
    fn body_length(&self) -> u16 {
        BASE_COMMENT_R_BODY_LENGTH
            + (self
                .variable_datum_records
                .iter()
                .map(|datum| {
                    let padded_record = length_padded_to_num(
                        BASE_VARIABLE_DATUM_LENGTH as usize + datum.datum_value.len(),
                        EIGHT_OCTETS,
                    );
                    padded_record.record_length as u16
                })
                .sum::<u16>())
    }

    fn body_type(&self) -> PduType {
        PduType::CommentR
    }
}

impl Interaction for CommentR {
    fn originator(&self) -> Option<&EntityId> {
        Some(&self.originating_id)
    }

    fn receiver(&self) -> Option<&EntityId> {
        Some(&self.receiving_id)
    }
}