rust to add uuid support
diff --git a/lib/rs/src/protocol/binary.rs b/lib/rs/src/protocol/binary.rs
index 9f8af43..5da8018 100644
--- a/lib/rs/src/protocol/binary.rs
+++ b/lib/rs/src/protocol/binary.rs
@@ -190,6 +190,14 @@
self.transport.read_f64::<BigEndian>().map_err(From::from)
}
+ fn read_uuid(&mut self) -> crate::Result<uuid::Uuid> {
+ let mut buf = [0u8; 16];
+ self.transport
+ .read_exact(&mut buf)
+ .map(|_| uuid::Uuid::from_bytes(buf))
+ .map_err(From::from)
+ }
+
fn read_string(&mut self) -> crate::Result<String> {
let bytes = self.read_bytes()?;
String::from_utf8(bytes).map_err(From::from)
@@ -389,6 +397,12 @@
self.write_bytes(s.as_bytes())
}
+ fn write_uuid(&mut self, uuid: &uuid::Uuid) -> crate::Result<()> {
+ self.transport
+ .write_all(uuid.as_bytes())
+ .map_err(From::from)
+ }
+
fn write_list_begin(&mut self, identifier: &TListIdentifier) -> crate::Result<()> {
self.write_byte(field_type_to_u8(identifier.element_type))?;
self.write_i32(identifier.size)
@@ -470,8 +484,7 @@
TType::Map => 0x0D,
TType::Set => 0x0E,
TType::List => 0x0F,
- TType::Utf8 => 0x10,
- TType::Utf16 => 0x11,
+ TType::Uuid => 0x10,
}
}
@@ -490,8 +503,7 @@
0x0D => Ok(TType::Map),
0x0E => Ok(TType::Set),
0x0F => Ok(TType::List),
- 0x10 => Ok(TType::Utf8),
- 0x11 => Ok(TType::Utf16),
+ 0x10 => Ok(TType::Uuid),
unkn => Err(crate::Error::Protocol(ProtocolError {
kind: ProtocolErrorKind::InvalidData,
message: format!("cannot convert {} to TType", unkn),
@@ -886,6 +898,25 @@
}
#[test]
+ fn must_write_uuid() {
+ let (_, mut o_prot) = test_objects(true);
+ let uuid = uuid::Uuid::new_v4();
+ assert!(o_prot.write_uuid(&uuid).is_ok());
+ let buf = o_prot.transport.write_bytes();
+ assert_eq!(&buf, uuid.as_bytes());
+ }
+
+ #[test]
+ fn must_round_trip_uuid() {
+ let (mut i_prot, mut o_prot) = test_objects(true);
+ let uuid = uuid::uuid!("F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4");
+ assert!(o_prot.write_uuid(&uuid).is_ok());
+ copy_write_buffer_to_read_buffer!(o_prot);
+ let received_uuid = assert_success!(i_prot.read_uuid());
+ assert_eq!(&received_uuid, &uuid);
+ }
+
+ #[test]
fn must_round_trip_bytes() {
let (mut i_prot, mut o_prot) = test_objects(true);
diff --git a/lib/rs/src/protocol/compact.rs b/lib/rs/src/protocol/compact.rs
index 87cfbfc..a1aa253 100644
--- a/lib/rs/src/protocol/compact.rs
+++ b/lib/rs/src/protocol/compact.rs
@@ -252,6 +252,10 @@
.map_err(From::from)
}
+ fn read_uuid(&mut self) -> crate::Result<uuid::Uuid> {
+ uuid::Uuid::from_slice(&self.read_bytes()?).map_err(From::from)
+ }
+
fn read_string(&mut self) -> crate::Result<String> {
let bytes = self.read_bytes()?;
String::from_utf8(bytes).map_err(From::from)
@@ -538,6 +542,10 @@
.map_err(From::from)
}
+ fn write_uuid(&mut self, uuid: &uuid::Uuid) -> crate::Result<()> {
+ self.write_bytes(uuid.as_bytes())
+ }
+
fn write_string(&mut self, s: &str) -> crate::Result<()> {
self.write_bytes(s.as_bytes())
}
@@ -637,6 +645,7 @@
TType::Set => 0x0A,
TType::Map => 0x0B,
TType::Struct => 0x0C,
+ TType::Uuid => 0x0D,
_ => panic!("should not have attempted to convert {} to u8", field_type),
}
}
@@ -661,6 +670,7 @@
0x0A => Ok(TType::Set),
0x0B => Ok(TType::Map),
0x0C => Ok(TType::Struct),
+ 0x0D => Ok(TType::Uuid),
unkn => Err(crate::Error::Protocol(crate::ProtocolError {
kind: crate::ProtocolErrorKind::InvalidData,
message: format!("cannot convert {} into TType", unkn),
diff --git a/lib/rs/src/protocol/mod.rs b/lib/rs/src/protocol/mod.rs
index 019f717..0e47795 100644
--- a/lib/rs/src/protocol/mod.rs
+++ b/lib/rs/src/protocol/mod.rs
@@ -171,6 +171,8 @@
fn read_i64(&mut self) -> crate::Result<i64>;
/// Read a 64-bit float.
fn read_double(&mut self) -> crate::Result<f64>;
+ /// Read a UUID.
+ fn read_uuid(&mut self) -> crate::Result<uuid::Uuid>;
/// Read a fixed-length string (not null terminated).
fn read_string(&mut self) -> crate::Result<String>;
/// Read the beginning of a list.
@@ -323,6 +325,8 @@
fn write_i64(&mut self, i: i64) -> crate::Result<()>;
/// Write a 64-bit float.
fn write_double(&mut self, d: f64) -> crate::Result<()>;
+ /// Write a UUID
+ fn write_uuid(&mut self, uuid: &uuid::Uuid) -> crate::Result<()>;
/// Write a fixed-length string.
fn write_string(&mut self, s: &str) -> crate::Result<()>;
/// Write the beginning of a list.
@@ -405,6 +409,10 @@
(**self).read_double()
}
+ fn read_uuid(&mut self) -> crate::Result<uuid::Uuid> {
+ (**self).read_uuid()
+ }
+
fn read_string(&mut self) -> crate::Result<String> {
(**self).read_string()
}
@@ -498,6 +506,10 @@
(**self).write_double(d)
}
+ fn write_uuid(&mut self, uuid: &uuid::Uuid) -> crate::Result<()> {
+ (**self).write_uuid(uuid)
+ }
+
fn write_string(&mut self, s: &str) -> crate::Result<()> {
(**self).write_string(s)
}
@@ -823,8 +835,8 @@
List,
/// UTF-8 string.
Utf8,
- /// UTF-16 string. *Unsupported*.
- Utf16,
+ /// Uuid.
+ Uuid,
}
impl Display for TType {
@@ -845,7 +857,7 @@
TType::Set => write!(f, "set"),
TType::List => write!(f, "list"),
TType::Utf8 => write!(f, "UTF8"),
- TType::Utf16 => write!(f, "UTF16"),
+ TType::Uuid => write!(f, "UUID"),
}
}
}
diff --git a/lib/rs/src/protocol/multiplexed.rs b/lib/rs/src/protocol/multiplexed.rs
index 697b7e6..48d4989 100644
--- a/lib/rs/src/protocol/multiplexed.rs
+++ b/lib/rs/src/protocol/multiplexed.rs
@@ -152,6 +152,10 @@
self.inner.write_string(s)
}
+ fn write_uuid(&mut self, uuid: &uuid::Uuid) -> crate::Result<()> {
+ self.inner.write_uuid(uuid)
+ }
+
fn write_list_begin(&mut self, identifier: &TListIdentifier) -> crate::Result<()> {
self.inner.write_list_begin(identifier)
}
diff --git a/lib/rs/src/protocol/stored.rs b/lib/rs/src/protocol/stored.rs
index 179ae07..f4bdfb1 100644
--- a/lib/rs/src/protocol/stored.rs
+++ b/lib/rs/src/protocol/stored.rs
@@ -158,6 +158,10 @@
self.inner.read_double()
}
+ fn read_uuid(&mut self) -> crate::Result<uuid::Uuid> {
+ self.inner.read_uuid()
+ }
+
fn read_string(&mut self) -> crate::Result<String> {
self.inner.read_string()
}