tuwunel_service/sending/
dest.rs1use std::fmt::Debug;
2
3use ruma::{OwnedServerName, OwnedUserId, ServerName, UserId};
4use tuwunel_core::implement;
5use tuwunel_matrix::pdu::RawPduId;
6
7#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
8pub enum Destination {
9 Appservice(String),
10 Push(OwnedUserId, String), Federation(OwnedServerName),
12}
13
14#[derive(Hash)]
18pub(super) enum DestinationRef<'a> {
19 Appservice(&'a str),
21
22 Push(&'a UserId, &'a str),
24
25 Federation(&'a ServerName),
27}
28
29#[implement(Destination)]
30#[inline]
31pub(super) fn borrowed(&self) -> DestinationRef<'_> {
32 match self {
33 | Self::Appservice(id) => DestinationRef::Appservice(id),
34 | Self::Push(user, key) => DestinationRef::Push(user, key),
35 | Self::Federation(server) => DestinationRef::Federation(server),
36 }
37}
38
39#[implement(Destination)]
40#[inline]
41#[must_use]
42pub(super) fn event_key(&self, pdu_id: &RawPduId) -> Vec<u8> {
43 self.suffixed_key(pdu_id.as_ref())
44}
45
46#[implement(Destination)]
47#[inline]
48#[must_use]
49pub(super) fn count_key(&self, count: u64) -> Vec<u8> { self.suffixed_key(&count.to_be_bytes()) }
50
51#[implement(Destination)]
52#[must_use]
53fn suffixed_key(&self, suffix: &[u8]) -> Vec<u8> {
54 let mut key = self.get_prefix_with_capacity(suffix.len());
55
56 key.extend_from_slice(suffix);
57 key
58}
59
60#[implement(Destination)]
61#[inline]
62#[must_use]
63pub(super) fn get_prefix(&self) -> Vec<u8> { self.get_prefix_with_capacity(0) }
64
65#[implement(Destination)]
66#[must_use]
67pub(super) fn get_prefix_with_capacity(&self, additional: usize) -> Vec<u8> {
68 match self {
69 | Self::Federation(server) => {
70 let len = server
71 .as_bytes()
72 .len()
73 .saturating_add(1)
74 .saturating_add(additional);
75
76 let mut p = Vec::with_capacity(len);
77 p.extend_from_slice(server.as_bytes());
78 p.push(0xFF);
79 p
80 },
81 | Self::Appservice(server) => {
82 let sigil = b"+";
83 let len = sigil
84 .len()
85 .saturating_add(server.len())
86 .saturating_add(1)
87 .saturating_add(additional);
88
89 let mut p = Vec::with_capacity(len);
90 p.extend_from_slice(sigil);
91 p.extend_from_slice(server.as_bytes());
92 p.push(0xFF);
93 p
94 },
95 | Self::Push(user, pushkey) => {
96 let sigil = b"$";
97 let len = sigil
98 .len()
99 .saturating_add(user.as_bytes().len())
100 .saturating_add(1)
101 .saturating_add(pushkey.len())
102 .saturating_add(1)
103 .saturating_add(additional);
104
105 let mut p = Vec::with_capacity(len);
106 p.extend_from_slice(sigil);
107 p.extend_from_slice(user.as_bytes());
108 p.push(0xFF);
109 p.extend_from_slice(pushkey.as_bytes());
110 p.push(0xFF);
111 p
112 },
113 }
114}