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
use crate::errors::*; use bytes::*; use std::cell::RefCell; use std::convert::{TryFrom, TryInto}; use std::mem; use std::rc::Rc; pub const INT_8: u8 = 0xC8; pub const INT_16: u8 = 0xC9; pub const INT_32: u8 = 0xCA; pub const INT_64: u8 = 0xCB; #[derive(Debug, PartialEq, Eq, Hash, Clone)] pub struct BoltInteger { pub value: i64, } impl BoltInteger { pub fn new(value: i64) -> BoltInteger { BoltInteger { value } } pub fn can_parse(input: Rc<RefCell<Bytes>>) -> bool { let marker = input.borrow()[0]; (-16..=127).contains(&(marker as i8)) || marker == INT_8 || marker == INT_16 || marker == INT_32 || marker == INT_64 } } impl TryFrom<Rc<RefCell<Bytes>>> for BoltInteger { type Error = Error; fn try_from(input: Rc<RefCell<Bytes>>) -> Result<BoltInteger> { let mut input = input.borrow_mut(); let value: i64 = match input.get_u8() { marker if (-16..=127).contains(&(marker as i8)) => marker as i64, INT_8 => input.get_i8() as i64, INT_16 => input.get_i16() as i64, INT_32 => input.get_i32() as i64, INT_64 => input.get_i64() as i64, _ => { return Err(Error::InvalidTypeMarker { detail: "invalid integer marker".into(), }) } }; Ok(BoltInteger::new(value)) } } impl TryInto<Bytes> for BoltInteger { type Error = Error; fn try_into(self) -> Result<Bytes> { let mut bytes = BytesMut::with_capacity(mem::size_of::<u8>() + mem::size_of::<i64>()); match self.value { -16..=127 => bytes.put_u8(self.value as u8), -128..=-17 => { bytes.put_u8(INT_8); bytes.put_i8(self.value as i8); } 128..=32_767 | -32_768..=-129 => { bytes.put_u8(INT_16); bytes.put_i16(self.value as i16); } 32_768..=2_147_483_647 | -2_147_483_648..=-32_769 => { bytes.put_u8(INT_32); bytes.put_i32(self.value as i32); } 2_147_483_648..=9_223_372_036_854_775_807 | -9_223_372_036_854_775_808..=-2_147_483_649 => { bytes.put_u8(INT_64); bytes.put_i64(self.value as i64); } } Ok(bytes.freeze()) } } #[cfg(test)] mod tests { use super::*; #[test] fn should_serialize_integer() { let bolt_int = BoltInteger::new(42); let b: Bytes = bolt_int.try_into().unwrap(); assert_eq!(b.bytes(), &[0x2A]); let bolt_int = BoltInteger::new(-127); let b: Bytes = bolt_int.try_into().unwrap(); assert_eq!(b.bytes(), &[INT_8, 0x81]); let bolt_int = BoltInteger::new(129); let b: Bytes = bolt_int.try_into().unwrap(); assert_eq!(b.bytes(), &[INT_16, 0x00, 0x81]); let bolt_int = BoltInteger::new(32_768); let b: Bytes = bolt_int.try_into().unwrap(); assert_eq!(b.bytes(), &[INT_32, 0x00, 0x00, 0x80, 0x00]); let bolt_int = BoltInteger::new(2_147_483_648); let b: Bytes = bolt_int.try_into().unwrap(); assert_eq!( b.bytes(), &[INT_64, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00] ); } #[test] fn should_deserialize_integer() { let b = Rc::new(RefCell::new(Bytes::from_static(&[0x2A]))); let bolt_int: BoltInteger = b.try_into().unwrap(); assert_eq!(bolt_int.value, 42); let b = Rc::new(RefCell::new(Bytes::from_static(&[INT_8, 0x81]))); let bolt_int: BoltInteger = b.try_into().unwrap(); assert_eq!(bolt_int.value, -127); let b = Rc::new(RefCell::new(Bytes::from_static(&[INT_16, 0x00, 0x81]))); let bolt_int: BoltInteger = b.try_into().unwrap(); assert_eq!(bolt_int.value, 129); let b = Rc::new(RefCell::new(Bytes::from_static(&[ INT_32, 0x00, 0x00, 0x80, 0x00, ]))); let bolt_int: BoltInteger = b.try_into().unwrap(); assert_eq!(bolt_int.value, 32_768); let b = Rc::new(RefCell::new(Bytes::from_static(&[ INT_64, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, ]))); let bolt_int: BoltInteger = b.try_into().unwrap(); assert_eq!(bolt_int.value, 2_147_483_648); } }