1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
mod unsubscribe;

use crate::protocol::packet::{write_mqtt_bytes, write_mqtt_string};
use crate::protocol::packet::{RetainForwardRule, Subscribe};
use crate::protocol::PropertyType;
use crate::{Protocol, ProtocolV5, QoS, TraceSubscribe};
use bytes::{BufMut, Bytes, BytesMut};
use std::marker::PhantomData;
pub use unsubscribe::*;

pub struct SubscribeBuilder<T: Protocol> {
    pub trace_id: u32,
    pub id: Option<SubscribeId>,
    pub user_properties: Vec<(String, String)>,
    pub filters: Vec<FilterBuilder<T>>,
}

impl<T: Protocol> SubscribeBuilder<T> {
    pub fn add_filter(&mut self, path: String, qos: QoS) -> &mut FilterBuilder<T> {
        self.filters.push(FilterBuilder::new(path, qos));
        let index = self.filters.len() - 1;
        unsafe { self.filters.get_unchecked_mut(index) }
    }
}
impl SubscribeBuilder<ProtocolV5> {
    pub fn add_user_properties(&mut self, key: String, val: String) -> &mut Self {
        self.user_properties.push((key, val));
        self
    }
    // pub fn set_id(&mut self, id: SubscribeId) -> &mut Self {
    //     self.id = Some(id);
    //     self
    // }
}

pub struct SubscribeId {
    datas: Bytes,
}

// impl SubscribeId {
//     pub fn new(id: u32) -> anyhow::Result<Self> {
//         if id > 268_435_455u32 && id == 0 {
//             bail!("should not be reached")
//         }
//         let mut buffer = BytesMut::new();
//         write_remaining_length(&mut buffer, id as usize);
//         Ok(Self {
//             datas: buffer.freeze(),
//         })
//     }
// }

pub struct FilterBuilder<T: Protocol> {
    path: String,
    qos: QoS,
    no_local: bool,
    preserve_retain: bool,
    retain_forward_rule: RetainForwardRule,
    protocol: PhantomData<T>,
}

impl<T: Protocol> FilterBuilder<T> {
    pub fn new(path: String, qos: QoS) -> Self {
        Self {
            path,
            qos,
            no_local: false,
            preserve_retain: false,
            retain_forward_rule: Default::default(),
            protocol: Default::default(),
        }
    }

    pub fn build(self, trace_id: u32) -> SubscribeBuilder<T> {
        SubscribeBuilder {
            trace_id,
            id: None,
            user_properties: vec![],
            filters: vec![self],
        }
    }
}
impl FilterBuilder<ProtocolV5> {
    pub fn set_nolocal(&mut self, no_local: bool) -> &mut Self {
        self.no_local = no_local;
        self
    }
    pub fn set_preserve_retain(&mut self, preserve_retain: bool) -> &mut Self {
        self.preserve_retain = preserve_retain;
        self
    }
    pub fn set_retain_forward_rule(&mut self, retain_forward_rule: RetainForwardRule) -> &mut Self {
        self.retain_forward_rule = retain_forward_rule;
        self
    }
}

impl<T: Protocol> From<SubscribeBuilder<T>> for TraceSubscribe {
    fn from(value: SubscribeBuilder<T>) -> Self {
        let SubscribeBuilder {
            trace_id,
            id,
            user_properties,
            filters,
        } = value;

        let subscribe = if T::is_v4() {
            let mut buffer = BytesMut::new();
            for filter in filters {
                write_filter(filter, &mut buffer)
            }

            Subscribe::V4 {
                packet_id: 0,
                payload: buffer.freeze(),
            }
        } else {
            let mut buffer = BytesMut::new();
            for filter in filters {
                write_filter(filter, &mut buffer)
            }

            let properties_datas = write_properties(id, user_properties);
            // let mut buffer_properties = BytesMut::with_capacity(properties_datas.len() + 2);
            // buffer_properties.put_u16(properties_datas.len() as u16);
            // write_mqtt_bytes(&mut buffer_properties, properties_datas.as_ref());
            Subscribe::V5 {
                packet_id: 0,
                properties: properties_datas,
                filters: buffer.freeze(),
            }
        };
        TraceSubscribe {
            id: trace_id,
            subscribe,
        }
    }
}
fn write_properties(id: Option<SubscribeId>, user_properties: Vec<(String, String)>) -> Bytes {
    let mut buffer = BytesMut::new();
    if let Some(id) = id {
        buffer.put_u8(PropertyType::SubscriptionIdentifier as u8);
        write_mqtt_bytes(&mut buffer, id.datas.as_ref());
    }
    for (key, value) in user_properties.iter() {
        buffer.put_u8(PropertyType::UserProperty as u8);
        write_mqtt_string(&mut buffer, key);
        write_mqtt_string(&mut buffer, value);
    }
    buffer.freeze()
}
fn write_filter<T: Protocol>(value: FilterBuilder<T>, buffer: &mut BytesMut) {
    if T::is_v4() {
        let FilterBuilder { path, qos, .. } = value;
        let options = qos as u8;

        write_mqtt_string(buffer, path.as_str());
        buffer.put_u8(options);
    } else {
        let FilterBuilder {
            path,
            qos,
            no_local,
            preserve_retain,
            retain_forward_rule,
            protocol: _,
        } = value;
        let mut options = qos as u8;
        if no_local {
            options |= 1 << 2;
        }
        if preserve_retain {
            options |= 1 << 3;
        }
        retain_forward_rule.merge_to_u8(&mut options);

        write_mqtt_string(buffer, path.as_str());
        buffer.put_u8(options);
    }
}