vinezombie 0.3.1

A modular IRCv3 framework
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
use super::{Args, ClientMsg, ServerMsg, Source, Tags};
use crate::error::{InvalidString, ParseError};
use crate::string::{Line, Splitter, Word};
use std::io::Write;

macro_rules! read_msg {
    (
        $limit:path, $buf:ident, $read:ident: $read_type:ident, $read_expr:expr, $parse_expr:expr
    ) => {{
        use std::io::{Error, ErrorKind};
        let mut $read = $read_type::take($read, 1);
        loop {
            let buflen = $buf.len();
            if buflen < $limit {
                let read_count = $limit - buflen;
                $read.set_limit(read_count as u64);
                if $read_expr? == 0 {
                    return Err(Error::from(ErrorKind::UnexpectedEof));
                }
            }
            read_buf!(|$buf| $parse_expr)
        }
    }};
}

macro_rules! read_buf {
    (|$buf:ident| $parse_expr:expr) => {{
        use crate::error::InvalidString;
        let mut found_newline = false;
        while let Some(c) = $buf.last() {
            match c {
                b'\n' => {
                    found_newline = true;
                    $buf.truncate($buf.len() - 1);
                }
                b'\r' if found_newline => {
                    $buf.truncate($buf.len() - 1);
                }
                b'\r' | b'\0' => {
                    return Err(ParseError::InvalidLine(InvalidString::Byte(*c)).into())
                }
                _ if found_newline => {
                    return match $parse_expr {
                        Ok(msg) => Ok(msg),
                        Err(e) => Err(e.into()),
                    }
                }
                // We stumbled into a non-newline character at the end of a read that
                // was supposed to read up until the newline or the max msg len.
                _ => return Err(ParseError::TooLong.into()),
            }
        }
    }};
}

#[inline(always)]
pub(crate) fn parse<'a, S: 'a, K: 'a>(
    msg: Line<'a>,
    parse_source: impl Fn(Word<'a>) -> Result<S, ParseError>,
    parse_kind: impl FnOnce(Word<'a>) -> Result<K, ParseError>,
) -> Result<(Tags<'a>, Option<S>, K, Args<'a>), ParseError> {
    let mut tags = Tags::new();
    let mut source = None;
    let mut expect_tags = true;
    let mut expect_source = true;
    let mut msg = Splitter::new(msg);
    let kind = loop {
        msg.consume_whitespace();
        let word: Word = msg.string_or_default(false);
        if word.is_empty() {
            return Err(ParseError::InvalidKind(InvalidString::Empty));
        }
        match word.first() {
            Some(b'@') if expect_tags => {
                let mut word = Splitter::new(word);
                expect_tags = false;
                word.next_byte();
                tags = Tags::parse(word.rest_or_default::<Word>());
            }
            Some(b':') if expect_source => {
                let mut word = Splitter::new(word);
                expect_tags = false;
                expect_source = false;
                word.next_byte();
                // Maybe not quiet failure here?
                // Non-parsed sources can sometimes still be useful.
                source = Some(parse_source(word.rest_or_default())?);
            }
            Some(_) => break word,
            None => return Err(ParseError::InvalidKind(InvalidString::Empty)),
        }
    };
    let kind = parse_kind(kind)?;
    let args = Args::parse(msg.rest_or_default::<Line>());
    Ok((tags, source, kind, args))
}

#[inline(always)]
pub(crate) fn bytes_left(kind: &[u8], source: Option<&Source>, args: &Args) -> isize {
    let mut size = kind.len() + 2; // Newline.
    if let Some(src) = source {
        size += 2 + src.len();
    }
    if !args.is_empty() {
        size += args.len_bytes() + 1;
    }
    let size: isize = size.try_into().unwrap_or(isize::MAX);
    512 - size
}

#[inline(always)]
fn write_to(
    tags: &Tags,
    source: Option<&Source>,
    kind: &[u8],
    args: &Args,
    write: &mut (impl std::io::Write + ?Sized),
) -> std::io::Result<()> {
    if !tags.is_empty() {
        tags.write_to(write)?;
        write.write_all(b" ")?;
    }
    if let Some(source) = source {
        write.write_all(b":")?;
        source.write_to(write)?;
        write.write_all(b" ")?;
    }
    write.write_all(kind)?;
    let (words, last) = args.split_last();
    for word in words {
        write.write_all(b" ")?;
        write.write_all(word)?;
    }
    if let Some(last) = last {
        if args.is_last_long() {
            write.write_all(b" :")?;
        } else {
            write.write_all(b" ")?;
        }
        write.write_all(last)?;
    }
    Ok(())
}

/// Stateless encoder/decoder for raw IRC messages on a client.
///
/// Encodes [`ClientMsg`]s and decodes [`ServerMsg`]s.
///
/// If the `tokio-codec` feature is enabled, this type implements
/// [`Decoder`][tokio_util::codec::Decoder] and [`Encoder`][tokio_util::codec::Encoder].
#[derive(Clone, Copy, Debug, Default)]
pub struct ClientCodec;

/// Stateless encoder/decoder for raw IRC messages on a server.
///
/// Encodes [`ServerMsg`]s and decodes [`ClientMsg`]s.
///
/// If the `tokio-codec` feature is enabled, this type implements
/// [`Decoder`][tokio_util::codec::Decoder] and [`Encoder`][tokio_util::codec::Encoder].
#[derive(Clone, Copy, Debug, Default)]
pub struct ServerCodec;

impl ClientCodec {
    /// Reads an owning server message from `read`.
    /// This function may block.
    ///
    /// `buf` must either be empty or contain a partial message from
    /// a previous call to this function that errored due to
    /// non-blocking I/O or unexpected EOF.
    /// Other errors may leave `buf` in an invalid state for future calls.
    pub fn read_owning_from<'a>(
        read: &mut (impl std::io::BufRead + ?Sized),
        buf: &mut Vec<u8>,
    ) -> std::io::Result<ServerMsg<'a>> {
        use std::io::{BufRead, Read};
        read_msg!(
            ServerMsg::MAX_LEN,
            buf,
            read: Read,
            read.read_until(b'\n', buf),
            ServerMsg::parse(std::mem::take(buf))
        )
    }
    /// Asynchronously reads an owning server message from `read`.
    ///
    /// `buf` must either be empty or contain a partial message from
    /// a previous call to this function that errored due to
    /// non-blocking I/O or unexpected EOF.
    /// Other errors may leave `buf` in an invalid state for future calls.
    #[cfg(feature = "tokio")]
    pub async fn read_owning_from_tokio<'a>(
        read: &mut (impl tokio::io::AsyncBufReadExt + ?Sized + Unpin),
        buf: &mut Vec<u8>,
    ) -> std::io::Result<ServerMsg<'a>> {
        use tokio::io::{AsyncBufReadExt, AsyncReadExt};
        read_msg!(
            ServerMsg::MAX_LEN,
            buf,
            read: AsyncReadExt,
            read.read_until(b'\n', buf).await,
            ServerMsg::parse(std::mem::take(buf))
        )
    }
    /// Reads a server message from `read`.
    /// This function may block.
    ///
    /// Consider using [`ServerMsg::read_owning_from`] instead
    /// unless minimizing memory allocations is very important.
    ///
    /// `buf` must either be empty or contain a partial message from
    /// a previous call to this function that errored due to
    /// non-blocking I/O or unexpected EOF.
    /// Other errors may leave `buf` in an invalid state for future calls.
    pub fn read_borrowing_from<'a>(
        read: &mut (impl std::io::BufRead + ?Sized),
        buf: &'a mut Vec<u8>,
    ) -> std::io::Result<ServerMsg<'a>> {
        use std::io::{BufRead, Read};
        read_msg!(
            ServerMsg::MAX_LEN,
            buf,
            read: Read,
            read.read_until(b'\n', buf),
            ServerMsg::parse(buf.as_slice())
        )
    }
    /// Asynchronously reads a server message from `read`.
    ///
    /// Consider using [`ServerMsg::read_owning_from_tokio`] instead
    /// unless minimizing memory allocations is very important.
    ///
    /// `buf` must either be empty or contain a partial message from
    /// a previous call to this function that errored due to
    /// non-blocking I/O or unexpected EOF.
    /// Other errors may leave `buf` in an invalid state for future calls.
    #[cfg(feature = "tokio")]
    pub async fn read_borrowing_from_tokio<'a>(
        read: &mut (impl tokio::io::AsyncBufReadExt + ?Sized + Unpin),
        buf: &'a mut Vec<u8>,
    ) -> std::io::Result<ServerMsg<'a>> {
        use tokio::io::{AsyncBufReadExt, AsyncReadExt};
        read_msg!(
            ServerMsg::MAX_LEN,
            buf,
            read: AsyncReadExt,
            read.read_until(b'\n', buf).await,
            ServerMsg::parse(buf.as_slice())
        )
    }
    /// Writes a client message to the provided [`Write`] WITHOUT a trailing CRLF.
    ///
    /// This function makes many small writes. Buffering is strongly recommended.
    pub fn write_to(msg: &ClientMsg<'_>, write: &mut (impl Write + ?Sized)) -> std::io::Result<()> {
        write_to(&msg.tags, None, &msg.cmd, &msg.args, write)
    }
    /// Writes a client message to `write` WITH a trailing CRLF,
    /// using the provided buffer to minimize the necessary number of writes to `write`.
    ///
    /// The buffer will be cleared after successfully sending this message.
    /// If the buffer is non-empty, message data will be appended to the buffer's contents.
    pub fn send_to(
        msg: &ClientMsg<'_>,
        write: &mut (impl Write + ?Sized),
        buf: &mut Vec<u8>,
    ) -> std::io::Result<()> {
        Self::write_to(msg, buf)?;
        buf.extend_from_slice(b"\r\n");
        write.write_all(buf)?;
        buf.clear();
        Ok(())
    }
    /// Asynchronously writes a client message to `write` WITH a trailing CRLF,
    /// using the provided buffer to minimize the necessary number of writes to `write`.
    ///
    /// The buffer will be cleared after successfully sending this message.
    /// If the buffer is non-empty, message data will be appended to the buffer's contents.
    #[cfg(feature = "tokio")]
    pub async fn send_to_tokio(
        msg: &ClientMsg<'_>,
        write: &mut (impl tokio::io::AsyncWriteExt + ?Sized + Unpin),
        buf: &mut Vec<u8>,
    ) -> std::io::Result<()> {
        Self::write_to(msg, buf)?;
        buf.extend_from_slice(b"\r\n");
        write.write_all(buf).await?;
        buf.clear();
        Ok(())
    }
}

impl ServerCodec {
    /// Reads an owning client message from `read`.
    /// This function may block.
    ///
    /// `buf` must either be empty or contain a partial message from
    /// a previous call to this function that errored due to
    /// non-blocking I/O or unexpected EOF.
    /// Other errors may leave `buf` in an invalid state for future calls.
    pub fn read_owning_from<'a>(
        read: &mut (impl std::io::BufRead + ?Sized),
        buf: &mut Vec<u8>,
    ) -> std::io::Result<ClientMsg<'a>> {
        use std::io::{BufRead, Read};
        read_msg!(
            ClientMsg::MAX_LEN,
            buf,
            read: Read,
            read.read_until(b'\n', buf),
            ClientMsg::parse(std::mem::take(buf))
        )
    }
    /// Asynchronously reads an owning client message from `read`.
    ///
    /// `buf` must either be empty or contain a partial message from
    /// a previous call to this function that errored due to
    /// non-blocking I/O or unexpected EOF.
    /// Other errors may leave `buf` in an invalid state for future calls.
    #[cfg(feature = "tokio")]
    pub async fn read_owning_from_tokio<'a>(
        read: &mut (impl tokio::io::AsyncBufReadExt + ?Sized + Unpin),
        buf: &mut Vec<u8>,
    ) -> std::io::Result<ClientMsg<'a>> {
        use tokio::io::{AsyncBufReadExt, AsyncReadExt};
        read_msg!(
            ClientMsg::MAX_LEN,
            buf,
            read: AsyncReadExt,
            read.read_until(b'\n', buf).await,
            ClientMsg::parse(std::mem::take(buf))
        )
    }
    /// Reads a client message from `read`.
    /// This function may block.
    ///
    /// Consider using [`ClientMsg::read_owning_from`] instead
    /// unless minimizing memory allocations is very important.
    ///
    /// `buf` must either be empty or contain a partial message from
    /// a previous call to this function that errored due to
    /// non-blocking I/O or unexpected EOF.
    /// Other errors may leave `buf` in an invalid state for future calls.
    pub fn read_borrowing_from<'a>(
        read: &mut (impl std::io::BufRead + ?Sized),
        buf: &'a mut Vec<u8>,
    ) -> std::io::Result<ClientMsg<'a>> {
        use std::io::{BufRead, Read};
        read_msg!(
            ClientMsg::MAX_LEN,
            buf,
            read: Read,
            read.read_until(b'\n', buf),
            ClientMsg::parse(buf.as_slice())
        )
    }
    /// Asynchronously reads a client message from `read`.
    ///
    /// Consider using [`ClientMsg::read_owning_from_tokio`] instead
    /// unless minimizing memory allocations is very important.
    ///
    /// `buf` must either be empty or contain a partial message from
    /// a previous call to this function that errored due to
    /// non-blocking I/O or unexpected EOF.
    /// Other errors may leave `buf` in an invalid state for future calls.
    #[cfg(feature = "tokio")]
    pub async fn read_borrowing_from_tokio<'a>(
        read: &mut (impl tokio::io::AsyncBufReadExt + ?Sized + Unpin),
        buf: &'a mut Vec<u8>,
    ) -> std::io::Result<ClientMsg<'a>> {
        use tokio::io::{AsyncBufReadExt, AsyncReadExt};
        read_msg!(
            ClientMsg::MAX_LEN,
            buf,
            read: AsyncReadExt,
            read.read_until(b'\n', buf).await,
            ClientMsg::parse(buf.as_slice())
        )
    }
    /// Writes a server message to the provided [`Write`] WITHOUT a trailing CRLF.
    ///
    /// This function makes many small writes. Buffering is strongly recommended.
    pub fn write_to(msg: &ServerMsg<'_>, write: &mut (impl Write + ?Sized)) -> std::io::Result<()> {
        write_to(&msg.tags, msg.source.as_deref(), &msg.kind.as_arg(), &msg.args, write)
    }
    /// Writes a server message to `write` WITH a trailing CRLF,
    /// using the provided buffer to minimize the necessary number of writes to `write`.
    ///
    /// The buffer will be cleared after successfully sending this message.
    /// If the buffer is non-empty, message data will be appended to the buffer's contents.
    pub fn send_to(
        msg: &ServerMsg<'_>,
        write: &mut (impl Write + ?Sized),
        buf: &mut Vec<u8>,
    ) -> std::io::Result<()> {
        Self::write_to(msg, buf)?;
        buf.extend_from_slice(b"\r\n");
        write.write_all(buf)?;
        buf.clear();
        Ok(())
    }
    /// Asynchronously writes a server message to `write` WITH a trailing CRLF,
    /// using the provided buffer to minimize the necessary number of writes to `write`.
    ///
    /// The buffer will be cleared after successfully sending this message.
    /// If the buffer is non-empty, message data will be appended to the buffer's contents.
    #[cfg(feature = "tokio")]
    pub async fn send_to_tokio(
        msg: &ServerMsg<'_>,
        write: &mut (impl tokio::io::AsyncWriteExt + ?Sized + Unpin),
        buf: &mut Vec<u8>,
    ) -> std::io::Result<()> {
        Self::write_to(msg, buf)?;
        buf.extend_from_slice(b"\r\n");
        write.write_all(buf).await?;
        buf.clear();
        Ok(())
    }
}

#[cfg(feature = "tokio-codec")]
pub(super) mod tokio_codec {
    use super::{ClientCodec, ServerCodec};
    use crate::{
        ircmsg::{ClientMsg, ServerMsg},
        string::Line,
    };
    use std::num::NonZeroUsize;
    use tokio_util::{
        bytes::{Buf, BufMut, BytesMut},
        codec::{Decoder, Encoder},
    };

    impl Encoder<ClientMsg<'_>> for ClientCodec {
        type Error = std::io::Error;

        fn encode(&mut self, item: ClientMsg<'_>, dst: &mut BytesMut) -> Result<(), Self::Error> {
            Self::write_to(&item, &mut dst.writer())?;
            dst.extend_from_slice(b"\r\n");
            Ok(())
        }
    }
    impl Encoder<ServerMsg<'_>> for ServerCodec {
        type Error = std::io::Error;

        fn encode(&mut self, item: ServerMsg<'_>, dst: &mut BytesMut) -> Result<(), Self::Error> {
            Self::write_to(&item, &mut dst.writer())?;
            dst.extend_from_slice(b"\r\n");
            Ok(())
        }
    }

    pub fn scroll_buf(buf: &mut BytesMut, limit: usize) -> Option<NonZeroUsize> {
        let mut leading_ws = true;
        let mut trimmed_ws = 0usize;
        let mut end_idx = 0usize;
        for byte in buf.iter() {
            if leading_ws {
                if byte.is_ascii_whitespace() {
                    trimmed_ws += 1;
                    continue;
                }
                leading_ws = false;
            }
            end_idx += 1;
            if end_idx >= limit || *byte == b'\n' || *byte == b'\0' {
                buf.advance(trimmed_ws);
                return NonZeroUsize::new(end_idx);
            }
        }
        buf.advance(trimmed_ws);
        None
    }

    impl Decoder for ClientCodec {
        type Item = ServerMsg<'static>;
        type Error = std::io::Error;

        fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
            let Some(split_at) = scroll_buf(src, ServerMsg::MAX_LEN) else {
                src.reserve(ServerMsg::MAX_LEN.saturating_sub(src.len()));
                return Ok(None);
            };
            let line_raw = src.split_to(split_at.get());
            let line = Line::from_bytes(line_raw.as_ref())?;
            Ok(Some(ServerMsg::parse(line.owning())?))
        }
    }
    impl Decoder for ServerCodec {
        type Item = ClientMsg<'static>;
        type Error = std::io::Error;

        fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
            let Some(split_at) = scroll_buf(src, ClientMsg::MAX_LEN) else {
                src.reserve(ClientMsg::MAX_LEN.saturating_sub(src.len()));
                return Ok(None);
            };
            let line_raw = src.split_to(split_at.get());
            let line = Line::from_bytes(line_raw.as_ref())?;
            Ok(Some(ClientMsg::parse(line.owning())?))
        }
    }
}