dis_rs/common/comment/
model.rs

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