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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// name_value.rs
//
// This file is part of the Eclipse Paho MQTT Rust Client library.
//
// A name/value is a helper to bridge between a collection of
// string pairs in Rust to an array of NUL terminated char string pointer
// pairs that the C library expects.
//
// It is useful when a C API takes a `const char *arg[]` parameter.
//

/*******************************************************************************
 * Copyright (c) 2020-2022 Frank Pagliughi <fpagliughi@mindspring.com>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompany this distribution.
 *
 * The Eclipse Public License is available at
 *    http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 *   http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *    Frank Pagliughi - initial implementation and documentation
 *******************************************************************************/

use std::{collections::hash_map::HashMap, ffi::CString, pin::Pin};

/// The name/value pointer pair from the C library.
type CNameValue = ffi::MQTTAsync_nameValue;

/// A collection of C-compatible (NUL-terminated) string pairs that is
/// useful with C APIs that require an array of string pair pointers,
/// normally specified as `const* MQTTAsync_nameValue`
#[derive(Debug)]
pub struct NameValueCollection {
    /// A vector of c-compatible pointers into the data collection.
    /// Note that this will have an addition entry at the end containing
    /// NULL name/value pointers to indicate the end-of-collection to
    /// the C library.
    c_coll: Vec<CNameValue>,
    /// The pinned data cache
    data: Pin<Box<NameValueData>>,
}

/// The cache of Rust name/value string pairs
#[derive(Debug, Default, Clone)]
struct NameValueData {
    /// A collection of C-compatible (NUL-terminated) string pairs.
    coll: Vec<(CString, CString)>,
}

impl NameValueCollection {
    /// Creates a NameValueCollection from a vector of string pair references.
    ///
    /// # Arguments
    ///
    /// `coll` A collection of string pair references.
    ///
    pub fn new<N, V>(coll: &[(N, V)]) -> Self
    where
        N: AsRef<str>,
        V: AsRef<str>,
    {
        let data = NameValueData {
            coll: Self::to_cstring_pair(coll),
        };
        Self::from_data(data)
    }

    // Convert a collection of string references to a vector of CString pairs.
    fn to_cstring_pair<N, V>(coll: &[(N, V)]) -> Vec<(CString, CString)>
    where
        N: AsRef<str>,
        V: AsRef<str>,
    {
        coll.iter()
            .map(|(n, v)| {
                (
                    CString::new(n.as_ref()).unwrap(),
                    CString::new(v.as_ref()).unwrap(),
                )
            })
            .collect()
    }

    // Convert a collection of CString's to a vector of pairs of C
    // char pointers.
    //
    // This also appends a pair of (NULL,NULL) pointers at the end of the
    // collection which is used to indicate the end of the collection to
    // the C library.
    //
    // Note that the pointers are invalidated if the original vector or
    // any of the strings in it change.
    fn to_c_vec(sv: &[(CString, CString)]) -> Vec<CNameValue> {
        let mut coll: Vec<CNameValue> = sv
            .iter()
            .map(|csp| CNameValue::new(csp.0.as_ptr(), csp.1.as_ptr()))
            .collect();
        coll.push(CNameValue::default());
        coll
    }

    // Updates the cached vector to correspond to the string.
    fn from_data(data: NameValueData) -> Self {
        let data = Box::pin(data);
        let c_coll = Self::to_c_vec(&data.coll);
        Self { c_coll, data }
    }

    /// Returns true if the collection contains elements.
    pub fn is_empty(&self) -> bool {
        self.data.coll.is_empty()
    }

    /// Gets the number of strings in the collection.
    pub fn len(&self) -> usize {
        self.data.coll.len()
    }

    /// Gets the collection as a pointer to const C string pair pointers.
    ///
    /// This returns a pointer that can be sent to a C API that takes a
    /// pointer to an array of name/value char pointer pairs, like
    /// `const MQTTAsync_nameValue*`
    ///
    /// This function is inherently unsafe. The pointer it returns is only
    /// valid while the collection remains unmodified. In general, it
    /// should be requested when needed and not stored for future use.
    pub fn as_c_arr_ptr(&self) -> *const CNameValue {
        self.c_coll.as_ptr()
    }
}

impl Default for NameValueCollection {
    fn default() -> Self {
        Self::from_data(NameValueData::default())
    }
}

impl Clone for NameValueCollection {
    fn clone(&self) -> Self {
        Self::from_data((*self.data).clone())
    }
}

impl From<HashMap<&str, &str>> for NameValueCollection {
    fn from(hmap: HashMap<&str, &str>) -> Self {
        let v: Vec<(CString, CString)> = hmap
            .into_iter()
            .map(|(n, v)| (CString::new(n).unwrap(), CString::new(v).unwrap()))
            .collect();
        Self::from_data(NameValueData { coll: v })
    }
}

/////////////////////////////////////////////////////////////////////////////
//                              Unit Tests
/////////////////////////////////////////////////////////////////////////////

#[cfg(test)]
mod tests {
    use super::*;

    macro_rules! vec_of_string_pairs {
        ($($x:expr),*) => (vec![$(($x.0.to_string(),$x.1.to_string())),*]);
    }

    #[test]
    fn test_default() {
        let sc = NameValueCollection::default();
        assert_eq!(0, sc.len());
    }

    #[test]
    fn test_new() {
        let v = [("name0", "val0"), ("name1", "val1"), ("name2", "val2")];
        let n = v.len();

        let sc = NameValueCollection::new(&v);

        assert_eq!(n, sc.len());
        assert_eq!(n + 1, sc.c_coll.len());
        assert_eq!(n, sc.data.coll.len());

        assert_eq!(v[0].0.as_bytes(), sc.data.coll[0].0.as_bytes());
        assert_eq!(v[0].1.as_bytes(), sc.data.coll[0].1.as_bytes());

        assert_eq!(v[1].0.as_bytes(), sc.data.coll[1].0.as_bytes());
        assert_eq!(v[1].1.as_bytes(), sc.data.coll[1].1.as_bytes());

        assert_eq!(v[2].0.as_bytes(), sc.data.coll[2].0.as_bytes());
        assert_eq!(v[2].1.as_bytes(), sc.data.coll[2].1.as_bytes());

        assert_eq!(sc.data.coll[0].0.as_ptr(), sc.c_coll[0].name);
        assert_eq!(sc.data.coll[0].1.as_ptr(), sc.c_coll[0].value);

        assert_eq!(sc.data.coll[1].0.as_ptr(), sc.c_coll[1].name);
        assert_eq!(sc.data.coll[1].1.as_ptr(), sc.c_coll[1].value);

        assert_eq!(sc.data.coll[2].0.as_ptr(), sc.c_coll[2].name);
        assert_eq!(sc.data.coll[2].1.as_ptr(), sc.c_coll[2].value);
    }

    #[test]
    fn test_new_from_vec_strings() {
        let v = vec_of_string_pairs![("name0", "val0"), ("name1", "val1"), ("name2", "val2")];
        let n = v.len();

        let sc = NameValueCollection::new(&v);

        assert_eq!(n, sc.len());
        assert_eq!(n + 1, sc.c_coll.len());
        assert_eq!(n, sc.data.coll.len());

        assert_eq!(v[0].0.as_bytes(), sc.data.coll[0].0.as_bytes());
        assert_eq!(v[0].1.as_bytes(), sc.data.coll[0].1.as_bytes());

        assert_eq!(v[1].0.as_bytes(), sc.data.coll[1].0.as_bytes());
        assert_eq!(v[1].1.as_bytes(), sc.data.coll[1].1.as_bytes());

        assert_eq!(v[2].0.as_bytes(), sc.data.coll[2].0.as_bytes());
        assert_eq!(v[2].1.as_bytes(), sc.data.coll[2].1.as_bytes());

        assert_eq!(sc.data.coll[0].0.as_ptr(), sc.c_coll[0].name);
        assert_eq!(sc.data.coll[0].1.as_ptr(), sc.c_coll[0].value);

        assert_eq!(sc.data.coll[1].0.as_ptr(), sc.c_coll[1].name);
        assert_eq!(sc.data.coll[1].1.as_ptr(), sc.c_coll[1].value);

        assert_eq!(sc.data.coll[2].0.as_ptr(), sc.c_coll[2].name);
        assert_eq!(sc.data.coll[2].1.as_ptr(), sc.c_coll[2].value);
    }

    #[test]
    fn test_from_hashmap_str() {
        let mut hmap = HashMap::new();
        hmap.insert("name0", "val0");
        hmap.insert("name1", "val1");
        hmap.insert("name2", "val2");

        let n = hmap.len();

        let sc: NameValueCollection = hmap.into();

        assert_eq!(n, sc.len());
        assert_eq!(n + 1, sc.c_coll.len());
        assert_eq!(n, sc.data.coll.len());

        // TODO: Check the entries, remembering they may be in any order.

        /*
        assert_eq!(v[0].0.as_bytes(), sc.data.coll[0].0.as_bytes());
        assert_eq!(v[0].1.as_bytes(), sc.data.coll[0].1.as_bytes());

        assert_eq!(v[1].0.as_bytes(), sc.data.coll[1].0.as_bytes());
        assert_eq!(v[1].1.as_bytes(), sc.data.coll[1].1.as_bytes());

        assert_eq!(v[2].0.as_bytes(), sc.data.coll[2].0.as_bytes());
        assert_eq!(v[2].1.as_bytes(), sc.data.coll[2].1.as_bytes());

        assert_eq!(sc.data.coll[0].0.as_ptr(), sc.c_coll[0].name);
        assert_eq!(sc.data.coll[0].1.as_ptr(), sc.c_coll[0].value);

        assert_eq!(sc.data.coll[1].0.as_ptr(), sc.c_coll[1].name);
        assert_eq!(sc.data.coll[1].1.as_ptr(), sc.c_coll[1].value);

        assert_eq!(sc.data.coll[2].0.as_ptr(), sc.c_coll[2].name);
        assert_eq!(sc.data.coll[2].1.as_ptr(), sc.c_coll[2].value);
        */
    }

    #[test]
    fn test_assign() {
        let v = [("name0", "val0"), ("name1", "val1"), ("name2", "val2")];
        let n = v.len();

        let org_sc = NameValueCollection::new(&v);

        let sc = org_sc;

        assert_eq!(n, sc.len());
        assert_eq!(n + 1, sc.c_coll.len());
        assert_eq!(n, sc.data.coll.len());

        assert_eq!(v[0].0.as_bytes(), sc.data.coll[0].0.as_bytes());
        assert_eq!(v[0].1.as_bytes(), sc.data.coll[0].1.as_bytes());

        assert_eq!(v[1].0.as_bytes(), sc.data.coll[1].0.as_bytes());
        assert_eq!(v[1].1.as_bytes(), sc.data.coll[1].1.as_bytes());

        assert_eq!(v[2].0.as_bytes(), sc.data.coll[2].0.as_bytes());
        assert_eq!(v[2].1.as_bytes(), sc.data.coll[2].1.as_bytes());

        assert_eq!(sc.data.coll[0].0.as_ptr(), sc.c_coll[0].name);
        assert_eq!(sc.data.coll[0].1.as_ptr(), sc.c_coll[0].value);

        assert_eq!(sc.data.coll[1].0.as_ptr(), sc.c_coll[1].name);
        assert_eq!(sc.data.coll[1].1.as_ptr(), sc.c_coll[1].value);

        assert_eq!(sc.data.coll[2].0.as_ptr(), sc.c_coll[2].name);
        assert_eq!(sc.data.coll[2].1.as_ptr(), sc.c_coll[2].value);
    }

    #[test]
    fn test_clone() {
        let v = [("name0", "val0"), ("name1", "val1"), ("name2", "val2")];
        let n = v.len();

        let sc = {
            let org_sc = NameValueCollection::new(&v);
            org_sc.clone()
        };

        assert_eq!(n, sc.len());
        assert_eq!(n + 1, sc.c_coll.len());
        assert_eq!(n, sc.data.coll.len());

        assert_eq!(v[0].0.as_bytes(), sc.data.coll[0].0.as_bytes());
        assert_eq!(v[0].1.as_bytes(), sc.data.coll[0].1.as_bytes());

        assert_eq!(v[1].0.as_bytes(), sc.data.coll[1].0.as_bytes());
        assert_eq!(v[1].1.as_bytes(), sc.data.coll[1].1.as_bytes());

        assert_eq!(v[2].0.as_bytes(), sc.data.coll[2].0.as_bytes());
        assert_eq!(v[2].1.as_bytes(), sc.data.coll[2].1.as_bytes());

        assert_eq!(sc.data.coll[0].0.as_ptr(), sc.c_coll[0].name);
        assert_eq!(sc.data.coll[0].1.as_ptr(), sc.c_coll[0].value);

        assert_eq!(sc.data.coll[1].0.as_ptr(), sc.c_coll[1].name);
        assert_eq!(sc.data.coll[1].1.as_ptr(), sc.c_coll[1].value);

        assert_eq!(sc.data.coll[2].0.as_ptr(), sc.c_coll[2].name);
        assert_eq!(sc.data.coll[2].1.as_ptr(), sc.c_coll[2].value);
    }
}