|
| 1 | +// Copyright 2013-2014 The Rust Project Developers. |
| 2 | +// Copyright 2018 The Uuid Project Developers. |
| 3 | +// |
| 4 | +// See the COPYRIGHT file at the top-level directory of this distribution. |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 8 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 9 | +// option. This file may not be copied, modified, or distributed |
| 10 | +// except according to those terms. |
| 11 | + |
| 12 | +use crate::{ |
| 13 | + convert::TryFrom, |
| 14 | + fmt::{Braced, Hyphenated, Simple, Urn}, |
| 15 | + non_nil::NonNilUuid, |
| 16 | + Uuid, |
| 17 | +}; |
| 18 | +use bincode::{ |
| 19 | + de::{BorrowDecoder, Decoder}, |
| 20 | + enc::Encoder, |
| 21 | + error::{DecodeError, EncodeError}, |
| 22 | + Decode, Encode, |
| 23 | +}; |
| 24 | +use std::string::ToString; |
| 25 | + |
| 26 | +impl Encode for Uuid { |
| 27 | + fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> { |
| 28 | + Encode::encode(self.as_bytes(), encoder) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +impl Encode for NonNilUuid { |
| 33 | + fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> { |
| 34 | + let uuid = Uuid::from(*self); |
| 35 | + |
| 36 | + Encode::encode(uuid.as_bytes(), encoder) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +impl Encode for Hyphenated { |
| 41 | + fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> { |
| 42 | + Encode::encode(self.as_uuid().as_bytes(), encoder) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl Encode for Simple { |
| 47 | + fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> { |
| 48 | + Encode::encode(self.as_uuid().as_bytes(), encoder) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl Encode for Urn { |
| 53 | + fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> { |
| 54 | + Encode::encode(self.as_uuid().as_bytes(), encoder) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +impl Encode for Braced { |
| 59 | + fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> { |
| 60 | + Encode::encode(self.as_uuid().as_bytes(), encoder) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +impl<Context> Decode<Context> for Uuid { |
| 65 | + fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 66 | + Ok(Uuid::from_bytes(Decode::decode(decoder)?)) |
| 67 | + } |
| 68 | +} |
| 69 | +impl<'de, Context> bincode::BorrowDecode<'de, Context> for Uuid { |
| 70 | + fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>( |
| 71 | + decoder: &mut D, |
| 72 | + ) -> core::result::Result<Self, bincode::error::DecodeError> { |
| 73 | + Ok(Uuid::from_bytes(Decode::decode(decoder)?)) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +impl<Context> Decode<Context> for NonNilUuid { |
| 78 | + fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 79 | + let uuid = Uuid::decode(decoder)?; |
| 80 | + |
| 81 | + NonNilUuid::try_from(uuid).map_err(|e| DecodeError::OtherString(e.to_string())) |
| 82 | + } |
| 83 | +} |
| 84 | +impl<'de, Context> bincode::BorrowDecode<'de, Context> for NonNilUuid { |
| 85 | + fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>( |
| 86 | + decoder: &mut D, |
| 87 | + ) -> core::result::Result<Self, bincode::error::DecodeError> { |
| 88 | + let uuid = Uuid::decode(decoder)?; |
| 89 | + |
| 90 | + NonNilUuid::try_from(uuid).map_err(|e| DecodeError::OtherString(e.to_string())) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +#[cfg(test)] |
| 95 | +mod bincode_tests { |
| 96 | + use super::*; |
| 97 | + use bincode::config; |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn test_encode_readable_string() { |
| 101 | + let uuid_str = "f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4"; |
| 102 | + let u = Uuid::parse_str(uuid_str).unwrap(); |
| 103 | + |
| 104 | + bincode::encode_to_vec(&u, config::standard()) |
| 105 | + .expect(&format!("Failed to encode {uuid_str}.")); |
| 106 | + } |
| 107 | + |
| 108 | + #[test] |
| 109 | + fn test_encode_hyphenated() { |
| 110 | + let uuid_str = "f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4"; |
| 111 | + let u = Uuid::parse_str(uuid_str).unwrap(); |
| 112 | + |
| 113 | + bincode::encode_to_vec(&u, config::standard()) |
| 114 | + .expect(&format!("Failed to encode {uuid_str}.")); |
| 115 | + } |
| 116 | + |
| 117 | + #[test] |
| 118 | + fn test_encode_simple() { |
| 119 | + let uuid_str = "f9168c5eceb24faab6bf329bf39fa1e4"; |
| 120 | + let u = Uuid::parse_str(uuid_str).unwrap(); |
| 121 | + |
| 122 | + bincode::encode_to_vec(&u, config::standard()) |
| 123 | + .expect(&format!("Failed to encode {uuid_str}.")); |
| 124 | + } |
| 125 | + |
| 126 | + #[test] |
| 127 | + fn test_encode_urn() { |
| 128 | + let uuid_str = "urn:uuid:f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4"; |
| 129 | + let u = Uuid::parse_str(uuid_str).unwrap(); |
| 130 | + |
| 131 | + bincode::encode_to_vec(&u, config::standard()) |
| 132 | + .expect(&format!("Failed to encode {uuid_str}.")); |
| 133 | + } |
| 134 | + |
| 135 | + #[test] |
| 136 | + fn test_encode_braced() { |
| 137 | + let uuid_str = "{f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4}"; |
| 138 | + let u = Uuid::parse_str(uuid_str).unwrap(); |
| 139 | + |
| 140 | + bincode::encode_to_vec(&u, config::standard()) |
| 141 | + .expect(&format!("Failed to encode {uuid_str}.")); |
| 142 | + } |
| 143 | + |
| 144 | + #[test] |
| 145 | + fn test_encode_non_human_readable() { |
| 146 | + let uuid_bytes = b"F9168C5E-CEB2-4F"; |
| 147 | + let u = Uuid::from_slice(uuid_bytes).unwrap(); |
| 148 | + |
| 149 | + bincode::encode_to_vec(&u, config::standard()) |
| 150 | + .expect(&format!("{:?} failed to encode.", uuid_bytes)); |
| 151 | + } |
| 152 | + |
| 153 | + #[test] |
| 154 | + fn test_decode() { |
| 155 | + let uuid_str = "f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4"; |
| 156 | + let u = Uuid::parse_str(uuid_str).unwrap(); |
| 157 | + |
| 158 | + let bytes = bincode::encode_to_vec(&u, config::standard()) |
| 159 | + .expect(&format!("Failed to encode {uuid_str}.")); |
| 160 | + |
| 161 | + let (decoded_uuid, decoded_size) = |
| 162 | + bincode::decode_from_slice::<Uuid, _>(&bytes, config::standard()) |
| 163 | + .expect(&format!("Failed to decode {bytes:?}.")); |
| 164 | + |
| 165 | + assert_eq!(u, decoded_uuid); |
| 166 | + assert_eq!(16, decoded_size); |
| 167 | + } |
| 168 | + |
| 169 | + #[test] |
| 170 | + fn test_decode_failure() { |
| 171 | + let bytes = "hello_world".as_bytes(); |
| 172 | + let error = bincode::decode_from_slice::<Uuid, _>(bytes, config::standard()) |
| 173 | + .expect_err(&format!("Should not have been able to decode {bytes:?}.")); |
| 174 | + |
| 175 | + match error { |
| 176 | + DecodeError::UnexpectedEnd { additional: 5 } => {} |
| 177 | + _ => panic!("Unexpected error"), |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + #[test] |
| 182 | + fn test_decode_non_nil_uuid() { |
| 183 | + let uuid_str = "f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4"; |
| 184 | + let uuid = Uuid::parse_str(uuid_str).unwrap(); |
| 185 | + |
| 186 | + let bytes = bincode::encode_to_vec(&uuid, config::standard()) |
| 187 | + .expect(&format!("Failed to encode {uuid_str}.")); |
| 188 | + |
| 189 | + let (decoded_uuid, decoded_size) = |
| 190 | + bincode::decode_from_slice::<NonNilUuid, _>(&bytes, config::standard()).expect( |
| 191 | + &format!("Should have been able to decode {bytes:?} to NonNilUuid."), |
| 192 | + ); |
| 193 | + |
| 194 | + assert_eq!(uuid, decoded_uuid); |
| 195 | + assert_eq!(16, decoded_size); |
| 196 | + } |
| 197 | + |
| 198 | + #[test] |
| 199 | + fn test_decode_nil_uuid() { |
| 200 | + let uuid = Uuid::nil(); |
| 201 | + |
| 202 | + let bytes = bincode::encode_to_vec(&uuid, config::standard()) |
| 203 | + .expect(&format!("Failed to encode {}.", uuid.to_string())); |
| 204 | + |
| 205 | + let error = bincode::decode_from_slice::<NonNilUuid, _>(&bytes, config::standard()) |
| 206 | + .expect_err(&format!( |
| 207 | + "Should not have been able to decode {bytes:?} to NonNilUuid." |
| 208 | + )); |
| 209 | + |
| 210 | + match error { |
| 211 | + DecodeError::OtherString(s) if s == "the UUID is nil" => {} |
| 212 | + _ => panic!("Unexpected error"), |
| 213 | + } |
| 214 | + } |
| 215 | +} |
0 commit comments