Skip to main content

dis_rs/common/comment/
model.rs

1use crate::BodyRaw;
2use crate::common::comment::builder::CommentBuilder;
3use crate::common::model::{
4    BASE_VARIABLE_DATUM_LENGTH, EntityId, PduBody, VariableDatum, length_padded_to_num,
5};
6use crate::common::{BodyInfo, Interaction};
7use crate::constants::EIGHT_OCTETS;
8use crate::enumerations::PduType;
9use alloc::vec::Vec;
10#[cfg(feature = "serde")]
11use serde::{Deserialize, Serialize};
12
13const BASE_COMMENT_BODY_LENGTH: u16 = 20;
14
15/// 5.6.5.13 Comment PDU
16///
17/// 7.5.13 Comment PDU
18#[derive(Clone, Debug, Default, PartialEq)]
19#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
20pub struct Comment {
21    pub originating_id: EntityId,
22    pub receiving_id: EntityId,
23    pub variable_datum_records: Vec<VariableDatum>,
24}
25
26impl BodyRaw for Comment {
27    type Builder = CommentBuilder;
28
29    fn builder() -> CommentBuilder {
30        CommentBuilder::new()
31    }
32
33    fn into_builder(self) -> CommentBuilder {
34        CommentBuilder::new_from_body(self)
35    }
36
37    fn into_pdu_body(self) -> PduBody {
38        PduBody::Comment(self)
39    }
40}
41
42impl BodyInfo for Comment {
43    fn body_length(&self) -> u16 {
44        BASE_COMMENT_BODY_LENGTH
45            + (self
46                .variable_datum_records
47                .iter()
48                .map(|datum| {
49                    let padded_record = length_padded_to_num(
50                        BASE_VARIABLE_DATUM_LENGTH as usize + datum.datum_value.len(),
51                        EIGHT_OCTETS,
52                    );
53                    padded_record.record_length as u16
54                })
55                .sum::<u16>())
56    }
57
58    fn body_type(&self) -> PduType {
59        PduType::Comment
60    }
61}
62
63impl Interaction for Comment {
64    fn originator(&self) -> Option<&EntityId> {
65        Some(&self.originating_id)
66    }
67
68    fn receiver(&self) -> Option<&EntityId> {
69        Some(&self.receiving_id)
70    }
71}