Skip to content

Commit

Permalink
rust to add uuid support
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiayu Liu authored and Jens-G committed Oct 12, 2023
1 parent 653d184 commit b6b6dc7
Show file tree
Hide file tree
Showing 14 changed files with 697 additions and 744 deletions.
1,334 changes: 600 additions & 734 deletions compiler/cpp/src/thrift/generate/t_rs_generator.cc

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions lib/rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ keywords = ["thrift"]
[dependencies]
byteorder = "1.3"
integer-encoding = "3.0.3"
uuid = "1"
log = {version = "0.4", optional = true}
ordered-float = "3.0"
threadpool = {version = "1.7", optional = true}

[features]
default = ["server"]
server = ["threadpool", "log"]

[dev-dependencies]
uuid = { version = "*", features = ["v4"] }
9 changes: 9 additions & 0 deletions lib/rs/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,15 @@ impl From<io::Error> for Error {
}
}

impl From<uuid::Error> for Error {
fn from(err: uuid::Error) -> Self {
Error::Protocol(ProtocolError {
kind: ProtocolErrorKind::InvalidData,
message: err.to_string(), // FIXME: use fmt::Error's debug string
})
}
}

impl From<string::FromUtf8Error> for Error {
fn from(err: string::FromUtf8Error) -> Self {
Error::Protocol(ProtocolError {
Expand Down
39 changes: 35 additions & 4 deletions lib/rs/src/protocol/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ where
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)
Expand Down Expand Up @@ -389,6 +397,12 @@ where
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)
Expand Down Expand Up @@ -470,8 +484,7 @@ fn field_type_to_u8(field_type: TType) -> u8 {
TType::Map => 0x0D,
TType::Set => 0x0E,
TType::List => 0x0F,
TType::Utf8 => 0x10,
TType::Utf16 => 0x11,
TType::Uuid => 0x10,
}
}

Expand All @@ -490,8 +503,7 @@ fn field_type_from_u8(b: u8) -> crate::Result<TType> {
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),
Expand Down Expand Up @@ -885,6 +897,25 @@ mod tests {
assert_eq!(&buf[4..], bytes); // actual bytes
}

#[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);
Expand Down
10 changes: 10 additions & 0 deletions lib/rs/src/protocol/compact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ where
.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)
Expand Down Expand Up @@ -538,6 +542,10 @@ where
.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())
}
Expand Down Expand Up @@ -637,6 +645,7 @@ fn type_to_u8(field_type: TType) -> u8 {
TType::Set => 0x0A,
TType::Map => 0x0B,
TType::Struct => 0x0C,
TType::Uuid => 0x0D,
_ => panic!("should not have attempted to convert {} to u8", field_type),
}
}
Expand All @@ -661,6 +670,7 @@ fn u8_to_type(b: u8) -> crate::Result<TType> {
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),
Expand Down
18 changes: 15 additions & 3 deletions lib/rs/src/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ pub trait TInputProtocol {
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.
Expand Down Expand Up @@ -323,6 +325,8 @@ pub trait TOutputProtocol {
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.
Expand Down Expand Up @@ -405,6 +409,10 @@ where
(**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()
}
Expand Down Expand Up @@ -498,6 +506,10 @@ where
(**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)
}
Expand Down Expand Up @@ -823,8 +835,8 @@ pub enum TType {
List,
/// UTF-8 string.
Utf8,
/// UTF-16 string. *Unsupported*.
Utf16,
/// Uuid.
Uuid,
}

impl Display for TType {
Expand All @@ -845,7 +857,7 @@ impl Display for TType {
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"),
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions lib/rs/src/protocol/multiplexed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ where
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)
}
Expand Down
4 changes: 4 additions & 0 deletions lib/rs/src/protocol/stored.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ impl<'a> TInputProtocol for TStoredInputProtocol<'a> {
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()
}
Expand Down
1 change: 1 addition & 0 deletions lib/rs/test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ publish = false
clap = "~2.33"
bitflags = "=1.2"
log = "0.4"
uuid = "1"

[dependencies.thrift]
path = "../"
Expand Down
1 change: 1 addition & 0 deletions lib/rs/test/thrifts/Base_One.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ struct Recipe {
1: string recipeName
2: string cuisine
3: i8 page
4: uuid recipeId
}

union CookingTools {
Expand Down
2 changes: 1 addition & 1 deletion test/rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ clap = "~2.33"
bitflags = "=1.2"
env_logger = "0.8"
log = "0.4"
uuid = "1"

[dependencies.thrift]
path = "../../lib/rs"

4 changes: 2 additions & 2 deletions test/rs/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
# under the License.
#

stubs: ../v0.16/ThriftTest.thrift
$(THRIFT) -I ./thrifts -out src --gen rs ../v0.16/ThriftTest.thrift
stubs: ../ThriftTest.thrift
$(THRIFT) -I ./thrifts -out src --gen rs ../ThriftTest.thrift

precross: stubs
$(CARGO) build
Expand Down
6 changes: 6 additions & 0 deletions test/rs/src/bin/test_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ fn make_thrift_calls(
"thing".to_owned(),
)?;

info!("testUuid");
verify_expected_result(
thrift_test_client.test_uuid(uuid::uuid!("00010203-0405-0607-0809-0a0b0c0d0e0f")),
uuid::uuid!("00010203-0405-0607-0809-0a0b0c0d0e0f"),
)?;

info!("testBool");
verify_expected_result(thrift_test_client.test_bool(true), true)?;

Expand Down
5 changes: 5 additions & 0 deletions test/rs/src/bin/test_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ impl ThriftTestSyncHandler for ThriftTestSyncHandlerImpl {
Ok(thing)
}

fn handle_test_uuid(&self, thing: uuid::Uuid) -> thrift::Result<uuid::Uuid> {
info!("testUUID({})", &thing);
Ok(thing)
}

fn handle_test_bool(&self, thing: bool) -> thrift::Result<bool> {
info!("testBool({})", thing);
Ok(thing)
Expand Down

0 comments on commit b6b6dc7

Please sign in to comment.