weechat-relay-rs 0.3.0

A library for interfacing with the WeeChat Relay protocol
Documentation
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
pub use crate::basic_types::{Compression, PasswordHashAlgo, Pointer};
use std::collections::HashMap;

/// A message received from WeeChat.
#[derive(Debug, PartialEq, Eq)]
pub struct Message {
    pub id: Identifier,
    pub objects: Vec<Object>,
}

impl Message {
    /// Constructor for a new `Message`. You are unlikely to need to call this directly, typically a
    /// message is created via parser.
    pub fn new(id: Identifier, objects: Vec<Object>) -> Self {
        Self { id, objects }
    }
}

/// Any type that can be an object in a message.
pub trait MessageType {
    fn to_object(self) -> Object;
    fn to_object_ref(&self) -> ObjectRef;
    fn to_warray(vec: Vec<Self>) -> WArray
    where
        Self: Sized;
}

/// A WeeChat
/// [Char](https://weechat.org/files/doc/devel/weechat_relay_protocol.en.html#object_char). Note
/// that WeeChat Char types are signed.
pub type WChar = i8;
/// A WeeChat
/// [Integer](https://weechat.org/files/doc/devel/weechat_relay_protocol.en.html#object_integer).
pub type WInteger = i32;
/// A WeeChat [Long
/// integer](https://weechat.org/files/doc/devel/weechat_relay_protocol.en.html#object_long_integer).
pub type WLongInteger = i64;
/// A WeeChat
/// [String](https://weechat.org/files/doc/devel/weechat_relay_protocol.en.html#object_string).
///
/// This type would be identical to the [`WBuffer`] type, except that it is intended to be a
/// human-readable string.  It is not just a Rust String because there is no way, at the protocol
/// level, to know what the encoding of the string is (it is the job of the application to know
/// this). The `None` variant represents a `NULL` string.
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct WString {
    bytes: Option<Vec<u8>>,
}

impl WString {
    pub fn new(bytes: Option<Vec<u8>>) -> Self {
        Self { bytes }
    }

    pub fn from_ref(bytes: &[u8]) -> Self {
        Self::new(Some(bytes.to_vec()))
    }

    /// Get the bytes representing this string.
    pub fn bytes(&self) -> &Option<Vec<u8>> {
        &self.bytes
    }
}

/// A WeeChat
/// [Buffer](https://weechat.org/files/doc/devel/weechat_relay_protocol.en.html#object_buffer). The
/// `None` variant represents a `NULL` buffer.
pub type WBuffer = Option<Vec<u8>>;
/// A WeeChat [Time](https://weechat.org/files/doc/devel/weechat_relay_protocol.en.html#object_time)
/// (i.e., number of seconds).
pub type WTime = u64;

/** A WeeChat
[Hashtable](https://weechat.org/files/doc/devel/weechat_relay_protocol.en.html#object_hashtable).

Why is this not just a [`HashMap`]?

While we do provide a [function to create a true `HashMap`](to_hashmap) from this object, we do not
use one by default because the spec is under-specified as to how valid such a transformation
is. Namely, the spec does not detail whether it is valid for a hashtable to contain multiple
instances of the same key, or whether ordering is/can be significant. While the answer is almost
certainly "keys cannot be duplicated and ordering is not significant", as is the case in Rust's
`HashMap` (so you probably almost always want to convert this immediately), it's possible that
custom extensions could violate these assumptions without violating the spec, so we opt to defer to
the safer interpretation.
*/
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct WHashtable {
    pub keys: WArray,
    pub vals: WArray,
}

/// A generic WeeChat
/// [Hdata](https://weechat.org/files/doc/devel/weechat_relay_protocol.en.html#object_hdata).
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct GenericHdata {
    pub hpath: WString, // FIXME: this should prolly be some kind of "path" type
    pub ppaths: Vec<Vec<Pointer>>,
    // in order to preserve type information better, we represent sets "column-wise"
    // (i.e., the n'th HdataValues represents the n'th values of each set),
    // as opposed to the "row-wise" order they are sent in (set1, set2, etc.)
    pub set_values: Vec<HdataValues>,
}

impl GenericHdata {
    /// Returns a Vec of the object sets in the Hdata.
    ///
    /// This renders some of the type consistency inaccessible to the compiler, so only use this in
    /// more general cases, where you're not actually doing anything with the particular Hdata.
    pub fn sets(&self) -> Vec<HashMap<&Vec<u8>, ObjectRef>> {
        let mut ret = vec![HashMap::new(); self.set_values[0].values.len()];
        for hdata_values in self.set_values.iter() {
            let values = hdata_values.values.to_ref_vec();
            for (i, v) in values.into_iter().enumerate() {
                ret[i].insert(&hdata_values.key, v);
            }
        }
        ret
    }
}

#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct HdataValues {
    pub key: Vec<u8>,
    pub values: WArray,
}

/// A WeeChat
/// [Info](https://weechat.org/files/doc/devel/weechat_relay_protocol.en.html#object_info).
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct WInfo {
    pub name: WString,
    pub value: WString,
}

/// A WeeChat
/// [Infolist](https://weechat.org/files/doc/devel/weechat_relay_protocol.en.html#object_infolist).
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct WInfolist {
    pub name: WString,
    pub items: Vec<InfolistItem>,
}

/// An
/// [Infolist](https://weechat.org/files/doc/devel/weechat_relay_protocol.en.html#object_infolist)
/// item---an element of an Infolist.
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct InfolistItem {
    pub variables: Vec<InfolistVariable>,
}

/// An
/// [Infolist](https://weechat.org/files/doc/devel/weechat_relay_protocol.en.html#object_infolist)
/// variable---an element of an Infolist item.
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct InfolistVariable {
    pub name: WString,
    pub value: Object,
}

impl MessageType for WChar {
    fn to_object(self) -> Object {
        Object::Chr(self)
    }
    fn to_object_ref(&self) -> ObjectRef {
        ObjectRef::Chr(self)
    }
    fn to_warray(vec: Vec<Self>) -> WArray {
        WArray::Chr(vec)
    }
}
impl MessageType for WInteger {
    fn to_object(self) -> Object {
        Object::Int(self)
    }
    fn to_object_ref(&self) -> ObjectRef {
        ObjectRef::Int(self)
    }
    fn to_warray(vec: Vec<Self>) -> WArray {
        WArray::Int(vec)
    }
}
impl MessageType for WLongInteger {
    fn to_object(self) -> Object {
        Object::Lon(self)
    }
    fn to_object_ref(&self) -> ObjectRef {
        ObjectRef::Lon(self)
    }
    fn to_warray(vec: Vec<Self>) -> WArray {
        WArray::Lon(vec)
    }
}
impl MessageType for WString {
    fn to_object(self) -> Object {
        Object::Str(self)
    }
    fn to_object_ref(&self) -> ObjectRef {
        ObjectRef::Str(self)
    }
    fn to_warray(vec: Vec<Self>) -> WArray {
        WArray::Str(vec)
    }
}
impl MessageType for WBuffer {
    fn to_object(self) -> Object {
        Object::Buf(self)
    }
    fn to_object_ref(&self) -> ObjectRef {
        ObjectRef::Buf(self)
    }
    fn to_warray(vec: Vec<Self>) -> WArray {
        WArray::Buf(vec)
    }
}
impl MessageType for Pointer {
    fn to_object(self) -> Object {
        Object::Ptr(self)
    }
    fn to_object_ref(&self) -> ObjectRef {
        ObjectRef::Ptr(self)
    }
    fn to_warray(vec: Vec<Self>) -> WArray {
        WArray::Ptr(vec)
    }
}
impl MessageType for WTime {
    fn to_object(self) -> Object {
        Object::Tim(self)
    }
    fn to_object_ref(&self) -> ObjectRef {
        ObjectRef::Tim(self)
    }
    fn to_warray(vec: Vec<Self>) -> WArray {
        WArray::Tim(vec)
    }
}
impl MessageType for WHashtable {
    fn to_object(self) -> Object {
        Object::Htb(self)
    }
    fn to_object_ref(&self) -> ObjectRef {
        ObjectRef::Htb(self)
    }
    fn to_warray(vec: Vec<Self>) -> WArray {
        WArray::Htb(vec)
    }
}
impl MessageType for GenericHdata {
    fn to_object(self) -> Object {
        Object::Hda(self)
    }
    fn to_object_ref(&self) -> ObjectRef {
        ObjectRef::Hda(self)
    }
    fn to_warray(vec: Vec<Self>) -> WArray {
        WArray::Hda(vec)
    }
}
impl MessageType for WInfo {
    fn to_object(self) -> Object {
        Object::Inf(self)
    }
    fn to_object_ref(&self) -> ObjectRef {
        ObjectRef::Inf(self)
    }
    fn to_warray(vec: Vec<Self>) -> WArray {
        WArray::Inf(vec)
    }
}
impl MessageType for WInfolist {
    fn to_object(self) -> Object {
        Object::Inl(self)
    }
    fn to_object_ref(&self) -> ObjectRef {
        ObjectRef::Inl(self)
    }
    fn to_warray(vec: Vec<Self>) -> WArray {
        WArray::Inl(vec)
    }
}
impl MessageType for WArray {
    fn to_object(self) -> Object {
        Object::Arr(self)
    }
    fn to_object_ref(&self) -> ObjectRef {
        ObjectRef::Arr(self)
    }
    fn to_warray(vec: Vec<Self>) -> WArray {
        WArray::Arr(vec)
    }
}

/// A WeeChat
/// [identifier](https://weechat.org/files/doc/devel/weechat_relay_protocol.en.html#message_identifier).
#[derive(Debug, PartialEq, Eq)]
pub enum Identifier {
    Client(Vec<u8>),
    Event(Event),
}

/// A WeeChat
/// [event](https://weechat.org/files/doc/devel/weechat_relay_protocol.en.html#message_identifier)---i.e.,
/// a reserved identifier.
#[derive(Debug, PartialEq, Eq)]
pub enum Event {
    BufferOpened,
    BufferTypeChanged,
    BufferMoved,
    BufferMerged,
    BufferUnmerged,
    BufferHidden,
    BufferUnhidden,
    BufferRenamed,
    BufferTitleChanged,
    BufferLocalvarAdded,
    BufferLocalvarChanged,
    BufferLocalvarRemoved,
    BufferClosing,
    BufferCleared,
    BufferLineAdded,
    Nicklist,
    NicklistDiff,
    Pong,
    Upgrade,
    UpgradeEnded,
}

/// The possible object types (but not the objects themselves).
#[derive(Clone, Copy)]
pub enum ObjectType {
    Chr,
    Int,
    Lon,
    Str,
    Buf,
    Ptr,
    Tim,
    Htb,
    Hda,
    Inf,
    Inl,
    Arr,
}

impl ObjectType {
    pub fn new_warray(&self, capacity: usize) -> WArray {
        match self {
            Self::Chr => WArray::Chr(Vec::with_capacity(capacity)),
            Self::Int => WArray::Int(Vec::with_capacity(capacity)),
            Self::Lon => WArray::Lon(Vec::with_capacity(capacity)),
            Self::Str => WArray::Str(Vec::with_capacity(capacity)),
            Self::Buf => WArray::Buf(Vec::with_capacity(capacity)),
            Self::Ptr => WArray::Ptr(Vec::with_capacity(capacity)),
            Self::Tim => WArray::Tim(Vec::with_capacity(capacity)),
            Self::Htb => WArray::Htb(Vec::with_capacity(capacity)),
            Self::Hda => WArray::Hda(Vec::with_capacity(capacity)),
            Self::Inf => WArray::Inf(Vec::with_capacity(capacity)),
            Self::Inl => WArray::Inl(Vec::with_capacity(capacity)),
            Self::Arr => WArray::Arr(Vec::with_capacity(capacity)),
        }
    }
}

/// One of the valid WeeChat
/// [Objects](https://weechat.org/files/doc/devel/weechat_relay_protocol.en.html#objects).
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum Object {
    Chr(WChar),
    Int(WInteger),
    Lon(WLongInteger),
    Str(WString),
    Buf(WBuffer),
    Ptr(Pointer),
    Tim(WTime),
    Htb(WHashtable),
    Hda(GenericHdata),
    Inf(WInfo),
    Inl(WInfolist),
    Arr(WArray),
    // ^ WArrays look a lot like this enum, but are Vecs over the possible types,
    // to better preserve the type information than a Vec<Object> would be able
}

impl Object {
    fn vec_to_object_vec<T: MessageType>(v: Vec<T>) -> Vec<Self> {
        v.into_iter().map(|o: T| o.to_object()).collect()
    }

    pub fn object_ref(&self) -> ObjectRef {
        match self {
            Self::Chr(v) => ObjectRef::Chr(v),
            Self::Int(v) => ObjectRef::Int(v),
            Self::Lon(v) => ObjectRef::Lon(v),
            Self::Str(v) => ObjectRef::Str(v),
            Self::Buf(v) => ObjectRef::Buf(v),
            Self::Ptr(v) => ObjectRef::Ptr(v),
            Self::Tim(v) => ObjectRef::Tim(v),
            Self::Htb(v) => ObjectRef::Htb(v),
            Self::Hda(v) => ObjectRef::Hda(v),
            Self::Inf(v) => ObjectRef::Inf(v),
            Self::Inl(v) => ObjectRef::Inl(v),
            Self::Arr(v) => ObjectRef::Arr(v),
        }
    }
}

#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum ObjectRef<'a> {
    Chr(&'a WChar),
    Int(&'a WInteger),
    Lon(&'a WLongInteger),
    Str(&'a WString),
    Buf(&'a WBuffer),
    Ptr(&'a Pointer),
    Tim(&'a WTime),
    Htb(&'a WHashtable),
    Hda(&'a GenericHdata),
    Inf(&'a WInfo),
    Inl(&'a WInfolist),
    Arr(&'a WArray),
}

impl<'a> ObjectRef<'a> {
    fn vec_to_object_ref_vec<T: MessageType + 'a>(v: &'a [T]) -> Vec<Self> {
        v.iter().map(|o: &T| o.to_object_ref()).collect()
    }
}

/// A WeeChat
/// [Array](https://weechat.org/files/doc/devel/weechat_relay_protocol.en.html#object_array)---a vec
/// of a single WeeChat type.
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum WArray {
    Chr(Vec<WChar>),
    Int(Vec<WInteger>),
    Lon(Vec<WLongInteger>),
    Str(Vec<WString>),
    Buf(Vec<WBuffer>),
    Ptr(Vec<Pointer>),
    Tim(Vec<WTime>),
    Htb(Vec<WHashtable>),
    Hda(Vec<GenericHdata>),
    Inf(Vec<WInfo>),
    Inl(Vec<WInfolist>),
    Arr(Vec<WArray>),
}

/// Apply an expression to any variant of a [`WArray`].
///
/// Each variant of a `WArray` holds a different type, so there is no way to generically apply an
/// identical expression to all variants.  However, sometimes we don't care about the type, and just
/// want to apply an expression with the same syntax and analogous semantics regardless (e.g., we
/// want to know the length).  This macro does just that.  This obviously means the expression must
/// resolve to valid code when called on any of the particular types.  The expression is applied as
/// a function call.
///
/// E.g., `len` is defined as
/// `
/// pub fn len(&self) -> usize {
///     apply_to_warray!(self, Vec::len)
/// }
/// `
#[macro_export]
macro_rules! apply_to_warray {
    ( $warray:expr, $function:expr ) => {
        match $warray {
            WArray::Chr(v) => $function(v),
            WArray::Int(v) => $function(v),
            WArray::Lon(v) => $function(v),
            WArray::Str(v) => $function(v),
            WArray::Buf(v) => $function(v),
            WArray::Ptr(v) => $function(v),
            WArray::Tim(v) => $function(v),
            WArray::Htb(v) => $function(v),
            WArray::Hda(v) => $function(v),
            WArray::Inf(v) => $function(v),
            WArray::Inl(v) => $function(v),
            WArray::Arr(v) => $function(v),
        }
    };
}

/// Apply an expression to any variant of a [`WArray`], with auxiliary data.
///
/// This is the same as [`apply_to_warray`], but with the second argument given as the second
/// argument to the function call.
#[macro_export]
macro_rules! apply_to_warray_with {
    ( $warray:expr, $data:expr, $function:expr ) => {
        match $warray {
            WArray::Chr(v) => $function(v, $data),
            WArray::Int(v) => $function(v, $data),
            WArray::Lon(v) => $function(v, $data),
            WArray::Str(v) => $function(v, $data),
            WArray::Buf(v) => $function(v, $data),
            WArray::Ptr(v) => $function(v, $data),
            WArray::Tim(v) => $function(v, $data),
            WArray::Htb(v) => $function(v, $data),
            WArray::Hda(v) => $function(v, $data),
            WArray::Inf(v) => $function(v, $data),
            WArray::Inl(v) => $function(v, $data),
            WArray::Arr(v) => $function(v, $data),
        }
    };
}

impl WArray {
    pub fn len(&self) -> usize {
        apply_to_warray!(self, Vec::len)
    }

    pub fn is_empty(&self) -> bool {
        apply_to_warray!(self, Vec::is_empty)
    }

    pub fn to_vec(self) -> Vec<Object> {
        apply_to_warray!(self, Object::vec_to_object_vec)
    }

    pub fn to_ref_vec(&self) -> Vec<ObjectRef> {
        apply_to_warray!(self, ObjectRef::vec_to_object_ref_vec)
    }
}

/// For converting a [`WHashtable`] into a [`HashMap`]
pub fn to_hashmap<K, V>(keys: Vec<K>, vals: Vec<V>) -> HashMap<K, V>
where
    K: Eq + std::hash::Hash,
{
    keys.into_iter().zip(vals).collect::<HashMap<K, V>>()
}