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
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: 2023 Denis Drakhnia <numas13@gmail.com>

//! Wrappers for byte slices with pretty-printers.

use std::fmt;
use std::ops::Deref;

use crate::cursor::{CursorMut, PutKeyValue};
use crate::CursorError;

/// Wrapper for slice of bytes with printing the bytes as a string.
///
/// # Examples
///
/// ```rust
/// # use xash3d_protocol::wrappers::Str;
/// let s = format!("{}", Str(b"\xff\talex\n"));
/// assert_eq!(s, "\\xff\\talex\\n");
/// ```
#[derive(Copy, Clone, PartialEq, Eq, Default)]
pub struct Str<T>(pub T);

impl<T> From<T> for Str<T> {
    fn from(value: T) -> Self {
        Self(value)
    }
}

impl PutKeyValue for Str<&[u8]> {
    fn put_key_value<'a, 'b>(
        &self,
        cur: &'b mut CursorMut<'a>,
    ) -> Result<&'b mut CursorMut<'a>, CursorError> {
        cur.put_bytes(self.0)
    }
}

impl<T> fmt::Debug for Str<T>
where
    T: AsRef<[u8]>,
{
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        write!(fmt, "\"{}\"", self)
    }
}

impl<T> fmt::Display for Str<T>
where
    T: AsRef<[u8]>,
{
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        for &c in self.0.as_ref() {
            match c {
                b'\n' => write!(fmt, "\\n")?,
                b'\t' => write!(fmt, "\\t")?,
                b'\\' => write!(fmt, "\\\\")?,
                _ if c.is_ascii_graphic() || c == b' ' => {
                    write!(fmt, "{}", c as char)?;
                }
                _ => write!(fmt, "\\x{:02x}", c)?,
            }
        }
        Ok(())
    }
}

impl<T> Deref for Str<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

/// Wrapper for slice of bytes without printing.
///
/// # Examples
///
/// ```rust
/// # use xash3d_protocol::wrappers::Hide;
/// let s = format!("{}", Hide([1, 2, 3, 4]));
/// assert_eq!(s, "<hidden>");
/// ```
#[derive(Copy, Clone, PartialEq, Eq, Default)]
pub struct Hide<T>(pub T);

impl<T> fmt::Debug for Hide<T> {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        write!(fmt, "<hidden>")
    }
}

impl<T> fmt::Display for Hide<T> {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        write!(fmt, "<hidden>")
    }
}

impl<T> Deref for Hide<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}