| 1 | use std::borrow::{Borrow, Cow};
|
| 2 | use std::fmt::{self, Debug, Formatter};
|
| 3 | use std::ops::Deref;
|
| 4 |
|
| 5 | #[cfg (feature = "serialize" )]
|
| 6 | use serde::de::{Deserialize, Deserializer, Error, Visitor};
|
| 7 | #[cfg (feature = "serialize" )]
|
| 8 | use serde::ser::{Serialize, Serializer};
|
| 9 |
|
| 10 | #[allow (clippy::ptr_arg)]
|
| 11 | pub 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 |
|
| 25 | pub 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
|
| 47 | pub enum CowRef<'i, 's, B>
|
| 48 | where
|
| 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 | }
|
| 58 | impl<'i, 's, B> Deref for CowRef<'i, 's, B>
|
| 59 | where
|
| 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: &'i B) => borrowed,
|
| 68 | Self::Slice(borrowed: &'s B) => borrowed,
|
| 69 | Self::Owned(ref owned: &impl Borrow) => owned.borrow(),
|
| 70 | }
|
| 71 | }
|
| 72 | }
|
| 73 |
|
| 74 | impl<'i, 's, B> Debug for CowRef<'i, 's, B>
|
| 75 | where
|
| 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: &'i B) => Debug::fmt(self:borrowed, f),
|
| 82 | Self::Slice(borrowed: &'s B) => Debug::fmt(self:borrowed, f),
|
| 83 | Self::Owned(ref owned: &impl Debug) => 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 | #[derive (PartialEq, Eq)]
|
| 97 | pub struct ByteBuf(pub Vec<u8>);
|
| 98 |
|
| 99 | impl Debug for ByteBuf {
|
| 100 | fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
| 101 | write_byte_string(f, &self.0)
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | #[cfg (feature = "serialize" )]
|
| 106 | impl<'de> Deserialize<'de> for ByteBuf {
|
| 107 | fn deserialize<D>(d: D) -> Result<Self, D::Error>
|
| 108 | where
|
| 109 | D: Deserializer<'de>,
|
| 110 | {
|
| 111 | struct ValueVisitor;
|
| 112 |
|
| 113 | impl<'de> Visitor<'de> for ValueVisitor {
|
| 114 | type Value = ByteBuf;
|
| 115 |
|
| 116 | fn expecting(&self, f: &mut Formatter) -> fmt::Result {
|
| 117 | f.write_str("byte data" )
|
| 118 | }
|
| 119 |
|
| 120 | fn visit_bytes<E: Error>(self, v: &[u8]) -> Result<Self::Value, E> {
|
| 121 | Ok(ByteBuf(v.to_vec()))
|
| 122 | }
|
| 123 |
|
| 124 | fn visit_byte_buf<E: Error>(self, v: Vec<u8>) -> Result<Self::Value, E> {
|
| 125 | Ok(ByteBuf(v))
|
| 126 | }
|
| 127 | }
|
| 128 |
|
| 129 | d.deserialize_byte_buf(ValueVisitor)
|
| 130 | }
|
| 131 | }
|
| 132 |
|
| 133 | #[cfg (feature = "serialize" )]
|
| 134 | impl Serialize for ByteBuf {
|
| 135 | fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
| 136 | where
|
| 137 | S: Serializer,
|
| 138 | {
|
| 139 | serializer.serialize_bytes(&self.0)
|
| 140 | }
|
| 141 | }
|
| 142 |
|
| 143 | ////////////////////////////////////////////////////////////////////////////////////////////////////
|
| 144 |
|
| 145 | /// Wrapper around `&[u8]` that has a human-readable debug representation:
|
| 146 | /// printable ASCII symbols output as is, all other output in HEX notation.
|
| 147 | ///
|
| 148 | /// Also, when `serialize` feature is on, this type deserialized using
|
| 149 | /// [`deserialize_bytes`](serde::Deserializer::deserialize_bytes) instead
|
| 150 | /// of vector's generic [`deserialize_seq`](serde::Deserializer::deserialize_seq)
|
| 151 | #[derive (PartialEq, Eq)]
|
| 152 | pub struct Bytes<'de>(pub &'de [u8]);
|
| 153 |
|
| 154 | impl<'de> Debug for Bytes<'de> {
|
| 155 | fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
| 156 | write_byte_string(f, self.0)
|
| 157 | }
|
| 158 | }
|
| 159 |
|
| 160 | #[cfg (feature = "serialize" )]
|
| 161 | impl<'de> Deserialize<'de> for Bytes<'de> {
|
| 162 | fn deserialize<D>(d: D) -> Result<Self, D::Error>
|
| 163 | where
|
| 164 | D: Deserializer<'de>,
|
| 165 | {
|
| 166 | struct ValueVisitor;
|
| 167 |
|
| 168 | impl<'de> Visitor<'de> for ValueVisitor {
|
| 169 | type Value = Bytes<'de>;
|
| 170 |
|
| 171 | fn expecting(&self, f: &mut Formatter) -> fmt::Result {
|
| 172 | f.write_str(data:"borrowed bytes" )
|
| 173 | }
|
| 174 |
|
| 175 | fn visit_borrowed_bytes<E: Error>(self, v: &'de [u8]) -> Result<Self::Value, E> {
|
| 176 | Ok(Bytes(v))
|
| 177 | }
|
| 178 | }
|
| 179 |
|
| 180 | d.deserialize_bytes(visitor:ValueVisitor)
|
| 181 | }
|
| 182 | }
|
| 183 |
|
| 184 | #[cfg (feature = "serialize" )]
|
| 185 | impl<'de> Serialize for Bytes<'de> {
|
| 186 | fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
| 187 | where
|
| 188 | S: Serializer,
|
| 189 | {
|
| 190 | serializer.serialize_bytes(self.0)
|
| 191 | }
|
| 192 | }
|
| 193 |
|
| 194 | ////////////////////////////////////////////////////////////////////////////////////////////////////
|
| 195 |
|
| 196 | #[cfg (test)]
|
| 197 | mod tests {
|
| 198 | use super::*;
|
| 199 | use pretty_assertions::assert_eq;
|
| 200 |
|
| 201 | #[test ]
|
| 202 | fn write_byte_string0() {
|
| 203 | let bytes = ByteBuf(vec![10, 32, 32, 32, 32, 32, 32, 32, 32]);
|
| 204 | assert_eq!(format!("{:?}" , bytes), " \"0xA \"" );
|
| 205 | }
|
| 206 |
|
| 207 | #[test ]
|
| 208 | fn write_byte_string1() {
|
| 209 | let bytes = ByteBuf(vec![
|
| 210 | 104, 116, 116, 112, 58, 47, 47, 119, 119, 119, 46, 119, 51, 46, 111, 114, 103, 47, 50,
|
| 211 | 48, 48, 50, 47, 48, 55, 47, 111, 119, 108, 35,
|
| 212 | ]);
|
| 213 | assert_eq!(
|
| 214 | format!("{:?}" , bytes),
|
| 215 | r##""http://www.w3.org/2002/07/owl#""##
|
| 216 | );
|
| 217 | }
|
| 218 |
|
| 219 | #[test ]
|
| 220 | fn write_byte_string3() {
|
| 221 | let bytes = ByteBuf(vec![
|
| 222 | 67, 108, 97, 115, 115, 32, 73, 82, 73, 61, 34, 35, 66, 34,
|
| 223 | ]);
|
| 224 | assert_eq!(format!("{:?}" , bytes), r##""Class IRI=\"#B\"""## );
|
| 225 | }
|
| 226 | }
|
| 227 | |