1use std::fmt;
2use std::str::FromStr;
3
4use bytes::Bytes;
5use jiff::Timestamp;
6use mime::Mime;
7
8use crate::error::{Error, Result};
9use crate::model::DataBoxId;
10
11#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
19pub struct MessageId(String);
20
21impl MessageId {
22 pub fn new(value: impl Into<String>) -> Result<Self> {
24 let value = value.into();
25 if value.is_empty() || value.chars().count() > 20 {
26 return Err(Error::invalid_input(
27 "message_id",
28 "message ID must contain between 1 and 20 characters",
29 ));
30 }
31 Ok(Self(value))
32 }
33
34 pub fn as_str(&self) -> &str {
36 &self.0
37 }
38}
39
40impl AsRef<str> for MessageId {
41 fn as_ref(&self) -> &str {
42 self.as_str()
43 }
44}
45
46impl fmt::Display for MessageId {
47 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
48 formatter.write_str(self.as_str())
49 }
50}
51
52impl fmt::Debug for MessageId {
53 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
54 fmt::Display::fmt(self, formatter)
55 }
56}
57
58impl FromStr for MessageId {
59 type Err = Error;
60
61 fn from_str(value: &str) -> Result<Self> {
62 Self::new(value)
63 }
64}
65
66impl TryFrom<&str> for MessageId {
67 type Error = Error;
68
69 fn try_from(value: &str) -> Result<Self> {
70 Self::new(value)
71 }
72}
73
74impl TryFrom<String> for MessageId {
75 type Error = Error;
76
77 fn try_from(value: String) -> Result<Self> {
78 Self::new(value)
79 }
80}
81
82impl From<MessageId> for String {
83 fn from(id: MessageId) -> Self {
84 id.0
85 }
86}
87
88#[cfg(feature = "serde")]
89impl serde::Serialize for MessageId {
90 fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
91 where
92 S: serde::Serializer,
93 {
94 serializer.serialize_str(self.as_str())
95 }
96}
97
98#[cfg(feature = "serde")]
99impl<'de> serde::Deserialize<'de> for MessageId {
100 fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
101 where
102 D: serde::Deserializer<'de>,
103 {
104 let value = <String as serde::Deserialize>::deserialize(deserializer)?;
105 Self::new(value).map_err(serde::de::Error::custom)
106 }
107}
108
109#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
114#[non_exhaustive]
115pub enum MessageMode {
116 Standard,
118 Large,
120}
121
122#[derive(Clone, Debug, Eq, Hash, PartialEq)]
124#[non_exhaustive]
125pub enum AttachmentType {
126 Main,
128 Enclosure,
130 Signature,
132 Meta,
134 Other(String),
136}
137
138impl AttachmentType {
139 pub fn as_str(&self) -> &str {
141 match self {
142 Self::Main => "main",
143 Self::Enclosure => "enclosure",
144 Self::Signature => "signature",
145 Self::Meta => "meta",
146 Self::Other(value) => value,
147 }
148 }
149
150 pub(crate) fn from_raw(value: &str) -> Self {
151 match value {
152 "main" => Self::Main,
153 "enclosure" => Self::Enclosure,
154 "signature" => Self::Signature,
155 "meta" => Self::Meta,
156 value => Self::Other(value.to_owned()),
157 }
158 }
159}
160
161#[derive(Clone)]
166pub struct Attachment {
167 description: String,
168 mime_type: Mime,
169 content: Bytes,
170 xml_element: bool,
171 meta_type: AttachmentType,
172 guid: Option<String>,
173 up_file_guid: Option<String>,
174 format: Option<String>,
175}
176
177impl Attachment {
178 pub fn new(description: impl Into<String>, mime_type: Mime, content: impl Into<Bytes>) -> Self {
180 Self {
181 description: description.into(),
182 mime_type,
183 content: content.into(),
184 xml_element: false,
185 meta_type: AttachmentType::Enclosure,
186 guid: None,
187 up_file_guid: None,
188 format: None,
189 }
190 }
191
192 pub fn xml(description: impl Into<String>, mime_type: Mime, xml: impl Into<String>) -> Self {
197 let mut attachment = Self::new(description, mime_type, Bytes::from(xml.into()));
198 attachment.xml_element = true;
199 attachment
200 }
201
202 pub fn main(
204 description: impl Into<String>,
205 mime_type: Mime,
206 content: impl Into<Bytes>,
207 ) -> Self {
208 Self::new(description, mime_type, content).with_meta_type(AttachmentType::Main)
209 }
210
211 pub fn with_meta_type(mut self, meta_type: AttachmentType) -> Self {
213 self.meta_type = meta_type;
214 self
215 }
216
217 pub fn with_guid(mut self, guid: impl Into<String>) -> Self {
219 self.guid = Some(guid.into());
220 self
221 }
222
223 pub fn with_up_file_guid(mut self, guid: impl Into<String>) -> Self {
225 self.up_file_guid = Some(guid.into());
226 self
227 }
228
229 pub fn with_format(mut self, format: impl Into<String>) -> Self {
233 self.format = Some(format.into());
234 self
235 }
236
237 pub fn description(&self) -> &str {
239 &self.description
240 }
241
242 pub fn mime_type(&self) -> &Mime {
244 &self.mime_type
245 }
246
247 pub fn content(&self) -> &Bytes {
249 &self.content
250 }
251
252 pub fn xml_content(&self) -> Option<&str> {
254 if self.xml_element {
255 std::str::from_utf8(&self.content).ok()
256 } else {
257 None
258 }
259 }
260
261 pub fn meta_type(&self) -> &AttachmentType {
263 &self.meta_type
264 }
265
266 pub fn guid(&self) -> Option<&str> {
268 self.guid.as_deref()
269 }
270
271 pub fn up_file_guid(&self) -> Option<&str> {
273 self.up_file_guid.as_deref()
274 }
275
276 pub fn format(&self) -> Option<&str> {
278 self.format.as_deref()
279 }
280}
281
282impl fmt::Debug for Attachment {
283 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
284 formatter
285 .debug_struct("Attachment")
286 .field("description", &self.description)
287 .field("mime_type", &self.mime_type)
288 .field("content_length", &self.content.len())
289 .field("xml_element", &self.xml_element)
290 .field("meta_type", &self.meta_type)
291 .field("guid", &self.guid)
292 .field("up_file_guid", &self.up_file_guid)
293 .field("format", &self.format)
294 .finish()
295 }
296}
297
298#[derive(Clone, Debug, Default, Eq, PartialEq)]
300pub struct MessageMetadata {
301 pub sender_org_unit: Option<String>,
303 pub sender_org_unit_number: Option<i64>,
305 pub recipient_org_unit: Option<String>,
307 pub recipient_org_unit_number: Option<i64>,
309 pub to_hands: Option<String>,
311 pub recipient_reference_number: Option<String>,
313 pub sender_reference_number: Option<String>,
315 pub recipient_identification: Option<String>,
317 pub sender_identification: Option<String>,
319 pub legal_title_law: Option<i64>,
321 pub legal_title_year: Option<i64>,
323 pub legal_title_section: Option<String>,
325 pub legal_title_paragraph: Option<String>,
327 pub legal_title_point: Option<String>,
329 pub personal_delivery: Option<bool>,
331 pub allow_substitute_delivery: Option<bool>,
333}
334
335#[derive(Clone, Copy, Debug, Eq, PartialEq)]
337pub struct PublishOwnId {
338 pub enabled: bool,
340 pub identification_level: Option<i64>,
342}
343
344#[derive(Clone, Debug)]
346pub struct OutgoingMessage {
347 pub recipient: DataBoxId,
349 pub annotation: String,
351 pub metadata: MessageMetadata,
353 pub public_authority_mode: Option<bool>,
355 pub publish_own_id: Option<PublishOwnId>,
357 pub message_type: Option<String>,
359 pub attachments: Vec<Attachment>,
361}
362
363impl OutgoingMessage {
364 pub fn new(recipient: DataBoxId, annotation: impl Into<String>) -> Self {
366 Self {
367 recipient,
368 annotation: annotation.into(),
369 metadata: MessageMetadata::default(),
370 public_authority_mode: None,
371 publish_own_id: None,
372 message_type: None,
373 attachments: Vec::new(),
374 }
375 }
376
377 pub fn with_attachment(mut self, attachment: Attachment) -> Self {
379 self.attachments.push(attachment);
380 self
381 }
382
383 pub fn add_attachment(&mut self, attachment: Attachment) {
385 self.attachments.push(attachment);
386 }
387}
388
389#[derive(Clone, Debug, Eq, PartialEq)]
393#[non_exhaustive]
394pub struct SentMessage {
395 pub id: MessageId,
397}
398
399#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
401#[non_exhaustive]
402pub enum MessageState {
403 Submitted,
405 SubmissionTimestamped,
407 AntivirusRejected,
409 PlacedInRecipientBox,
411 DeliveredByFiction,
413 DeliveredByLogin,
415 Read,
417 Undeliverable,
419 Erased,
421 InDataVault,
423 Unknown(i32),
425}
426
427impl MessageState {
428 pub fn code(self) -> i32 {
430 match self {
431 Self::Submitted => 1,
432 Self::SubmissionTimestamped => 2,
433 Self::AntivirusRejected => 3,
434 Self::PlacedInRecipientBox => 4,
435 Self::DeliveredByFiction => 5,
436 Self::DeliveredByLogin => 6,
437 Self::Read => 7,
438 Self::Undeliverable => 8,
439 Self::Erased => 9,
440 Self::InDataVault => 10,
441 Self::Unknown(code) => code,
442 }
443 }
444
445 pub(crate) fn from_code(code: i32) -> Self {
446 match code {
447 1 => Self::Submitted,
448 2 => Self::SubmissionTimestamped,
449 3 => Self::AntivirusRejected,
450 4 => Self::PlacedInRecipientBox,
451 5 => Self::DeliveredByFiction,
452 6 => Self::DeliveredByLogin,
453 7 => Self::Read,
454 8 => Self::Undeliverable,
455 9 => Self::Erased,
456 10 => Self::InDataVault,
457 code => Self::Unknown(code),
458 }
459 }
460}
461
462#[derive(Clone, Debug, Eq, PartialEq)]
466pub struct MessageListQuery {
467 pub from: Option<Timestamp>,
469 pub to: Option<Timestamp>,
471 pub organization_unit_number: Option<i64>,
473 pub states: Option<Vec<MessageState>>,
475 pub offset: u64,
477 pub limit: u64,
479}
480
481impl Default for MessageListQuery {
482 fn default() -> Self {
483 Self {
484 from: None,
485 to: None,
486 organization_unit_number: None,
487 states: None,
488 offset: 1,
489 limit: 1000,
490 }
491 }
492}
493
494#[derive(Clone, Debug, Eq, PartialEq)]
496#[non_exhaustive]
497pub struct MessageEnvelope {
498 pub id: MessageId,
500 pub sender_id: Option<DataBoxId>,
502 pub sender: Option<String>,
504 pub sender_address: Option<String>,
506 pub sender_type: i32,
508 pub recipient: Option<String>,
510 pub recipient_address: Option<String>,
512 pub ambiguous_recipient: Option<bool>,
514 pub recipient_id: Option<DataBoxId>,
516 pub annotation: Option<String>,
518 pub metadata: MessageMetadata,
520}
521
522#[derive(Clone, Debug, Eq, PartialEq)]
524#[non_exhaustive]
525pub struct MessageRecord {
526 pub ordinal: u32,
528 pub envelope: MessageEnvelope,
530 pub state: MessageState,
532 pub attachment_size: Option<i64>,
534 pub delivery_time: Option<Timestamp>,
536 pub acceptance_time: Option<Timestamp>,
538 pub message_type: Option<String>,
540 pub mode: Option<MessageMode>,
542 pub special_message_flag: Option<i64>,
544}
545
546#[derive(Clone, Debug, Eq, PartialEq)]
548#[non_exhaustive]
549pub struct MessagePage {
550 pub records: Vec<MessageRecord>,
552 pub offset: u64,
554 pub limit: u64,
556}
557
558#[derive(Clone, Eq, PartialEq)]
560#[non_exhaustive]
561pub struct MessageHash {
562 pub bytes: Bytes,
564 pub algorithm: Option<String>,
566}
567
568impl fmt::Debug for MessageHash {
569 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
570 formatter
571 .debug_struct("MessageHash")
572 .field("length", &self.bytes.len())
573 .field("algorithm", &self.algorithm)
574 .finish()
575 }
576}
577
578#[derive(Clone, Debug)]
580#[non_exhaustive]
581pub struct Message {
582 pub envelope: MessageEnvelope,
584 pub attachments: Vec<Attachment>,
586 pub hash: MessageHash,
588 pub qualified_timestamp: Option<Bytes>,
590 pub delivery_time: Option<Timestamp>,
592 pub acceptance_time: Option<Timestamp>,
594 pub state: MessageState,
596 pub attachment_size: Option<i64>,
598 pub message_type: Option<String>,
600 pub special_message_flag: Option<i64>,
602 pub mode: Option<MessageMode>,
604 pub attachment_count: Option<u32>,
606}
607
608#[derive(Clone, Debug)]
612#[non_exhaustive]
613pub struct MessageEnvelopeDetails {
614 pub envelope: MessageEnvelope,
616 pub hash: MessageHash,
618 pub qualified_timestamp: Bytes,
620 pub delivery_time: Option<Timestamp>,
622 pub acceptance_time: Option<Timestamp>,
624 pub state: MessageState,
626 pub attachment_size: Option<i64>,
628 pub message_type: Option<String>,
630 pub mode: Option<MessageMode>,
632 pub attachment_count: Option<u32>,
634 pub special_message_flag: Option<i64>,
636}
637
638#[derive(Clone, Debug, Eq, PartialEq)]
640#[non_exhaustive]
641pub struct DeliveryEvent {
642 pub time: Option<Timestamp>,
644 pub description: Option<String>,
646}
647
648#[derive(Clone, Debug)]
650#[non_exhaustive]
651pub struct DeliveryInfo {
652 pub envelope: MessageEnvelope,
654 pub hash: MessageHash,
656 pub qualified_timestamp: Bytes,
658 pub delivery_time: Option<Timestamp>,
660 pub acceptance_time: Option<Timestamp>,
662 pub state: MessageState,
664 pub events: Vec<DeliveryEvent>,
666}