1use std::borrow::{Borrow, Cow};
2use std::fmt::{self, Debug, Formatter};
3use std::ops::Deref;
4
5#[cfg(feature = "serialize")]
6use serde::de::{Deserialize, Deserializer, Error, Visitor};
7#[cfg(feature = "serialize")]
8use serde::ser::{Serialize, Serializer};
9
10#[allow(clippy::ptr_arg)]
11pub fn write_cow_string(f: &mut Formatter, cow_string: &Cow<[u8]>) -> fmt::Result {
12 match cow_string {
13 Cow::Owned(s: &Vec) => {
14 write!(f, "Owned(")?;
15 write_byte_string(f, byte_string:s)?;
16 }
17 Cow::Borrowed(s: &&[u8]) => {
18 write!(f, "Borrowed(")?;
19 write_byte_string(f, byte_string:s)?;
20 }
21 }
22 write!(f, ")")
23}
24
25pub fn write_byte_string(f: &mut Formatter, byte_string: &[u8]) -> fmt::Result {
26 write!(f, "\"")?;
27 for b: &u8 in byte_string {
28 match *b {
29 32..=33 | 35..=126 => write!(f, "{}", *b as char)?,
30 34 => write!(f, "\\\"")?,
31 _ => write!(f, "{:#02X}", b)?,
32 }
33 }
34 write!(f, "\"")?;
35 Ok(())
36}
37
38////////////////////////////////////////////////////////////////////////////////////////////////////
39
40/// A version of [`Cow`] that can borrow from two different buffers, one of them
41/// is a deserializer input.
42///
43/// # Lifetimes
44///
45/// - `'i`: lifetime of the data that deserializer borrow from the parsed input
46/// - `'s`: lifetime of the data that owned by a deserializer
47pub enum CowRef<'i, 's, B>
48where
49 B: ToOwned + ?Sized,
50{
51 /// An input borrowed from the parsed data
52 Input(&'i B),
53 /// An input borrowed from the buffer owned by another deserializer
54 Slice(&'s B),
55 /// An input taken from an external deserializer, owned by that deserializer
56 Owned(<B as ToOwned>::Owned),
57}
58impl<'i, 's, B> Deref for CowRef<'i, 's, B>
59where
60 B: ToOwned + ?Sized,
61 B::Owned: Borrow<B>,
62{
63 type Target = B;
64
65 fn deref(&self) -> &B {
66 match *self {
67 Self::Input(borrowed: &B) => borrowed,
68 Self::Slice(borrowed: &B) => borrowed,
69 Self::Owned(ref owned: &::Owned) => owned.borrow(),
70 }
71 }
72}
73
74impl<'i, 's, B> Debug for CowRef<'i, 's, B>
75where
76 B: ToOwned + ?Sized + Debug,
77 B::Owned: Debug,
78{
79 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
80 match *self {
81 Self::Input(borrowed: &B) => Debug::fmt(self:borrowed, f),
82 Self::Slice(borrowed: &B) => Debug::fmt(self:borrowed, f),
83 Self::Owned(ref owned: &::Owned) => Debug::fmt(self:owned, f),
84 }
85 }
86}
87
88////////////////////////////////////////////////////////////////////////////////////////////////////
89
90/// Wrapper around `Vec<u8>` that has a human-readable debug representation:
91/// printable ASCII symbols output as is, all other output in HEX notation.
92///
93/// Also, when [`serialize`] feature is on, this type deserialized using
94/// [`deserialize_byte_buf`](serde::Deserializer::deserialize_byte_buf) instead
95/// of vector's generic [`deserialize_seq`](serde::Deserializer::deserialize_seq)
96///
97/// [`serialize`]: ../index.html#serialize
98#[derive(PartialEq, Eq)]
99pub struct ByteBuf(pub Vec<u8>);
100
101impl Debug for ByteBuf {
102 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
103 write_byte_string(f, &self.0)
104 }
105}
106
107#[cfg(feature = "serialize")]
108impl<'de> Deserialize<'de> for ByteBuf {
109 fn deserialize<D>(d: D) -> Result<Self, D::Error>
110 where
111 D: Deserializer<'de>,
112 {
113 struct ValueVisitor;
114
115 impl<'de> Visitor<'de> for ValueVisitor {
116 type Value = ByteBuf;
117
118 fn expecting(&self, f: &mut Formatter) -> fmt::Result {
119 f.write_str("byte data")
120 }
121
122 fn visit_bytes<E: Error>(self, v: &[u8]) -> Result<Self::Value, E> {
123 Ok(ByteBuf(v.to_vec()))
124 }
125
126 fn visit_byte_buf<E: Error>(self, v: Vec<u8>) -> Result<Self::Value, E> {
127 Ok(ByteBuf(v))
128 }
129 }
130
131 d.deserialize_byte_buf(ValueVisitor)
132 }
133}
134
135#[cfg(feature = "serialize")]
136impl Serialize for ByteBuf {
137 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
138 where
139 S: Serializer,
140 {
141 serializer.serialize_bytes(&self.0)
142 }
143}
144
145////////////////////////////////////////////////////////////////////////////////////////////////////
146
147/// Wrapper around `&[u8]` that has a human-readable debug representation:
148/// printable ASCII symbols output as is, all other output in HEX notation.
149///
150/// Also, when [`serialize`] feature is on, this type deserialized using
151/// [`deserialize_bytes`](serde::Deserializer::deserialize_bytes) instead
152/// of vector's generic [`deserialize_seq`](serde::Deserializer::deserialize_seq)
153///
154/// [`serialize`]: ../index.html#serialize
155#[derive(PartialEq, Eq)]
156pub struct Bytes<'de>(pub &'de [u8]);
157
158impl<'de> Debug for Bytes<'de> {
159 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
160 write_byte_string(f, self.0)
161 }
162}
163
164#[cfg(feature = "serialize")]
165impl<'de> Deserialize<'de> for Bytes<'de> {
166 fn deserialize<D>(d: D) -> Result<Self, D::Error>
167 where
168 D: Deserializer<'de>,
169 {
170 struct ValueVisitor;
171
172 impl<'de> Visitor<'de> for ValueVisitor {
173 type Value = Bytes<'de>;
174
175 fn expecting(&self, f: &mut Formatter) -> fmt::Result {
176 f.write_str("borrowed bytes")
177 }
178
179 fn visit_borrowed_bytes<E: Error>(self, v: &'de [u8]) -> Result<Self::Value, E> {
180 Ok(Bytes(v))
181 }
182 }
183
184 d.deserialize_bytes(ValueVisitor)
185 }
186}
187
188#[cfg(feature = "serialize")]
189impl<'de> Serialize for Bytes<'de> {
190 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
191 where
192 S: Serializer,
193 {
194 serializer.serialize_bytes(self.0)
195 }
196}
197
198////////////////////////////////////////////////////////////////////////////////////////////////////
199
200#[cfg(test)]
201mod tests {
202 use super::*;
203 use pretty_assertions::assert_eq;
204
205 #[test]
206 fn write_byte_string0() {
207 let bytes = ByteBuf(vec![10, 32, 32, 32, 32, 32, 32, 32, 32]);
208 assert_eq!(format!("{:?}", bytes), "\"0xA \"");
209 }
210
211 #[test]
212 fn write_byte_string1() {
213 let bytes = ByteBuf(vec![
214 104, 116, 116, 112, 58, 47, 47, 119, 119, 119, 46, 119, 51, 46, 111, 114, 103, 47, 50,
215 48, 48, 50, 47, 48, 55, 47, 111, 119, 108, 35,
216 ]);
217 assert_eq!(
218 format!("{:?}", bytes),
219 r##""http://www.w3.org/2002/07/owl#""##
220 );
221 }
222
223 #[test]
224 fn write_byte_string3() {
225 let bytes = ByteBuf(vec![
226 67, 108, 97, 115, 115, 32, 73, 82, 73, 61, 34, 35, 66, 34,
227 ]);
228 assert_eq!(format!("{:?}", bytes), r##""Class IRI=\"#B\"""##);
229 }
230}
231