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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
use crate::errors::*; use crate::types::*; use crate::version::Version; use bytes::*; use std::cell::RefCell; use std::convert::From; use std::fmt::Display; use std::mem; use std::rc::Rc; pub const TINY: u8 = 0x80; pub const SMALL: u8 = 0xD0; pub const MEDIUM: u8 = 0xD1; pub const LARGE: u8 = 0xD2; #[derive(Debug, PartialEq, Eq, Hash, Clone)] pub struct BoltString { pub value: String, } impl BoltString { pub fn new(value: &str) -> Self { BoltString { value: value.to_string(), } } pub fn can_parse(_: Version, input: Rc<RefCell<Bytes>>) -> bool { let marker = input.borrow()[0]; (TINY..=(TINY | 0x0F)).contains(&marker) || marker == SMALL || marker == MEDIUM || marker == LARGE } } impl Display for BoltString { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.value) } } impl From<&str> for BoltString { fn from(v: &str) -> Self { BoltString::new(v) } } impl From<String> for BoltString { fn from(v: String) -> Self { BoltString::new(&v) } } impl BoltString { pub fn to_bytes(self, _: Version) -> Result<Bytes> { let mut bytes = BytesMut::with_capacity( mem::size_of::<u8>() + mem::size_of::<u32>() + self.value.len(), ); match self.value.len() { 0..=15 => bytes.put_u8(TINY | self.value.len() as u8), 16..=255 => { bytes.put_u8(SMALL); bytes.put_u8(self.value.len() as u8); } 256..=65_535 => { bytes.put_u8(MEDIUM); bytes.put_u16(self.value.len() as u16); } 65_536..=4_294_967_295 => { bytes.put_u8(LARGE); bytes.put_u32(self.value.len() as u32); } _ => return Err(Error::StringTooLong), }; bytes.put_slice(self.value.as_bytes()); Ok(bytes.freeze()) } pub fn parse(_: Version, input: Rc<RefCell<Bytes>>) -> Result<BoltString> { let mut input = input.borrow_mut(); let marker = input.get_u8(); let length = match marker { 0x80..=0x8F => 0x0F & marker as usize, SMALL => input.get_u8() as usize, MEDIUM => input.get_u16() as usize, LARGE => input.get_u32() as usize, _ => { return Err(Error::InvalidTypeMarker(format!( "invalid string marker {}", marker ))) } }; let byte_array = input.split_to(length).to_vec(); let string_value = std::string::String::from_utf8(byte_array) .map_err(|e| Error::DeserializationError(e.to_string()))?; Ok(string_value.into()) } } #[cfg(test)] mod tests { use super::*; #[test] fn should_serialize_empty_string() { let s = BoltString::new(""); let b: Bytes = s.to_bytes(Version::V4_1).unwrap(); assert_eq!(b.bytes(), Bytes::from_static(&[TINY])); } #[test] fn should_deserialize_empty_string() { let input = Rc::new(RefCell::new(Bytes::from_static(&[TINY]))); let s: BoltString = BoltString::parse(Version::V4_1, input).unwrap(); assert_eq!(s, "".into()); } #[test] fn should_serialize_tiny_string() { let s = BoltString::new("a"); let b: Bytes = s.to_bytes(Version::V4_1).unwrap(); assert_eq!(b.bytes(), Bytes::from_static(&[0x81, 0x61])); } #[test] fn should_deserialize_tiny_string() { let serialized_bytes = Rc::new(RefCell::new(Bytes::from_static(&[0x81, 0x61]))); let result: BoltString = BoltString::parse(Version::V4_1, serialized_bytes).unwrap(); assert_eq!(result, "a".into()); } #[test] fn should_serialize_small_string() { let s = BoltString::new(&"a".repeat(16)); let mut b: Bytes = s.to_bytes(Version::V4_1).unwrap(); assert_eq!(b.get_u8(), SMALL); assert_eq!(b.get_u8(), 0x10); assert_eq!(b.len(), 0x10); for value in b { assert_eq!(value, 0x61); } } #[test] fn should_deserialize_small_string() { let serialized_bytes = Rc::new(RefCell::new(Bytes::from_static(&[SMALL, 0x01, 0x61]))); let result: BoltString = BoltString::parse(Version::V4_1, serialized_bytes).unwrap(); assert_eq!(result, "a".into()); } #[test] fn should_serialize_medium_string() { let s = BoltString::new(&"a".repeat(256)); let mut b: Bytes = s.to_bytes(Version::V4_1).unwrap(); assert_eq!(b.get_u8(), MEDIUM); assert_eq!(b.get_u16(), 0x100); assert_eq!(b.len(), 0x100); for value in b { assert_eq!(value, 0x61); } } #[test] fn should_deserialize_medium_string() { let serialized_bytes = Rc::new(RefCell::new(Bytes::from_static(&[ MEDIUM, 0x00, 0x01, 0x61, ]))); let result: BoltString = BoltString::parse(Version::V4_1, serialized_bytes).unwrap(); assert_eq!(result, "a".into()); } #[test] fn should_serialize_large_string() { let s = BoltString::new(&"a".repeat(65_536)); let mut b: Bytes = s.to_bytes(Version::V4_1).unwrap(); assert_eq!(b.get_u8(), LARGE); assert_eq!(b.get_u32(), 0x10000); assert_eq!(b.len(), 0x10000); for value in b { assert_eq!(value, 0x61); } } #[test] fn should_deserialize_large_string() { let serialized_bytes = Rc::new(RefCell::new(Bytes::from_static(&[ LARGE, 0x00, 0x00, 0x00, 0x01, 0x61, ]))); let result: BoltString = BoltString::parse(Version::V4_1, serialized_bytes).unwrap(); assert_eq!(result, "a".into()); } }