xash3d_protocol/
wrappers.rs1use std::fmt;
7use std::ops::Deref;
8
9use crate::cursor::{CursorMut, PutKeyValue};
10use crate::CursorError;
11
12#[derive(Copy, Clone, PartialEq, Eq, Default)]
22pub struct Str<T>(pub T);
23
24impl<T> From<T> for Str<T> {
25 fn from(value: T) -> Self {
26 Self(value)
27 }
28}
29
30impl PutKeyValue for Str<&[u8]> {
31 fn put_key_value<'a, 'b>(
32 &self,
33 cur: &'b mut CursorMut<'a>,
34 ) -> Result<&'b mut CursorMut<'a>, CursorError> {
35 cur.put_bytes(self.0)
36 }
37}
38
39impl<T> fmt::Debug for Str<T>
40where
41 T: AsRef<[u8]>,
42{
43 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
44 write!(fmt, "\"{}\"", self)
45 }
46}
47
48impl<T> fmt::Display for Str<T>
49where
50 T: AsRef<[u8]>,
51{
52 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
53 for &c in self.0.as_ref() {
54 match c {
55 b'\n' => write!(fmt, "\\n")?,
56 b'\t' => write!(fmt, "\\t")?,
57 b'\\' => write!(fmt, "\\\\")?,
58 _ if c.is_ascii_graphic() || c == b' ' => {
59 write!(fmt, "{}", c as char)?;
60 }
61 _ => write!(fmt, "\\x{:02x}", c)?,
62 }
63 }
64 Ok(())
65 }
66}
67
68impl<T> Deref for Str<T> {
69 type Target = T;
70
71 fn deref(&self) -> &Self::Target {
72 &self.0
73 }
74}
75
76#[derive(Copy, Clone, PartialEq, Eq, Default)]
86pub struct Hide<T>(pub T);
87
88impl<T> fmt::Debug for Hide<T> {
89 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
90 write!(fmt, "<hidden>")
91 }
92}
93
94impl<T> fmt::Display for Hide<T> {
95 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
96 write!(fmt, "<hidden>")
97 }
98}
99
100impl<T> Deref for Hide<T> {
101 type Target = T;
102
103 fn deref(&self) -> &Self::Target {
104 &self.0
105 }
106}