From 685f52206246d9fc31ac55f97e2f7b742f153d2e Mon Sep 17 00:00:00 2001 From: Tom Kaitchuck Date: Thu, 19 Dec 2019 17:10:19 -0800 Subject: [PATCH 01/30] Move some files around --- Cargo.toml | 2 +- src/controller_client/client.rs | 59 ++++++++ src/controller_client/client/main.rs | 31 +++++ src/controller_client/client/test.rs | 46 ++++++ src/controllerclient.rs | 131 ------------------ src/lib.rs | 11 +- src/{ => wire_protocol}/commands.rs | 0 src/{ => wire_protocol}/connection_factory.rs | 0 .../wire_commands.rs} | 2 +- 9 files changed, 146 insertions(+), 136 deletions(-) create mode 100644 src/controller_client/client.rs create mode 100644 src/controller_client/client/main.rs create mode 100644 src/controller_client/client/test.rs delete mode 100644 src/controllerclient.rs rename src/{ => wire_protocol}/commands.rs (100%) rename src/{ => wire_protocol}/connection_factory.rs (100%) rename src/{wirecommands.rs => wire_protocol/wire_commands.rs} (99%) diff --git a/Cargo.toml b/Cargo.toml index a830da794..4caa012af 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,5 +38,5 @@ tonic-build = "0.1.0-alpha.5" #Simplify running the client. This should be removed. [[bin]] name = "controller-client" -path = "src/controllerclient.rs" +path = "src/controller_client/client/main.rs" diff --git a/src/controller_client/client.rs b/src/controller_client/client.rs new file mode 100644 index 000000000..79a24aaaf --- /dev/null +++ b/src/controller_client/client.rs @@ -0,0 +1,59 @@ +/** + * Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + */ +mod main; +#[allow(non_camel_case_types)] +mod controller { + tonic::include_proto!("io.pravega.controller.stream.api.grpc.v1"); + // this is the rs file name generated after compiling the proto file, located inside the target folder. +} + +#[cfg(test)] +mod test; + + +use controller::{ + client::ControllerServiceClient, scaling_policy::ScalingPolicyType, CreateScopeStatus, + CreateStreamStatus, ScalingPolicy, ScopeInfo, StreamConfig, StreamInfo, +}; +use tonic::transport::channel::Channel; + +/// create_connection with the given controller uri. +pub async fn create_connection(uri: &'static str) -> ControllerServiceClient { + // Placeholder to add authentication headers. + let connection: ControllerServiceClient = + ControllerServiceClient::connect(uri.to_string()) + .await + .expect("Failed to create a channel"); + connection +} + +/// Async function to create scope +pub async fn create_scope( + request: ScopeInfo, + ch: &mut ControllerServiceClient, +) -> CreateScopeStatus { + let op_status: tonic::Response = ch + .create_scope(tonic::Request::new(request)) + .await + .expect("Failed to create Scope"); + op_status.into_inner() // return the scope status +} + +pub async fn create_stream( + request: StreamConfig, + ch: &mut ControllerServiceClient, +) -> CreateStreamStatus { + let op_status: tonic::Response = ch + .create_stream(tonic::Request::new(request)) + .await + .expect("Failed to create Stream"); + op_status.into_inner() // return create Stream status +} + diff --git a/src/controller_client/client/main.rs b/src/controller_client/client/main.rs new file mode 100644 index 000000000..ca92f6816 --- /dev/null +++ b/src/controller_client/client/main.rs @@ -0,0 +1,31 @@ + +use super::*; + +#[tokio::main] +async fn main() -> Result<(), Box> { + // start Pravega standalone before invoking this function. + let mut client = create_connection("http://[::1]:9090").await; + let request = ScopeInfo { + scope: "testScope123".into(), + }; + let response: CreateScopeStatus = create_scope(request, &mut client).await; + println!("Response for create_scope is {:?}", response); + + let request2 = StreamConfig { + stream_info: Some(StreamInfo { + scope: "testScope123".into(), + stream: "testStream".into(), + }), + scaling_policy: Some(ScalingPolicy { + scale_type: ScalingPolicyType::FixedNumSegments as i32, + target_rate: 0, + scale_factor: 0, + min_num_segments: 1, + }), + retention_policy: None, + }; + let response2: CreateStreamStatus = create_stream(request2, &mut client).await; + println!("Response 2 for create_stream is {:?}", response2); + + Ok(()) +} \ No newline at end of file diff --git a/src/controller_client/client/test.rs b/src/controller_client/client/test.rs new file mode 100644 index 000000000..1b5b8a854 --- /dev/null +++ b/src/controller_client/client/test.rs @@ -0,0 +1,46 @@ + +// Note this useful idiom: importing names from outer (for mod tests) scope. +use super::*; +use tokio::runtime::Runtime; + +#[test] +#[should_panic] // since the controller is not running. +fn test_create_scope_error() { + let rt = Runtime::new().unwrap(); + + let client_future = create_connection("http://[::1]:9090"); + let mut client = rt.block_on(client_future); + + let request = ScopeInfo { + scope: "testScope124".into(), + }; + let fut = create_scope(request, &mut client); + + rt.block_on(fut); +} + +#[test] +#[should_panic] // since the controller is not running. +fn test_create_stream_error() { + let rt = Runtime::new().unwrap(); + + let client_future = create_connection("http://[::1]:9090"); + let mut client = rt.block_on(client_future); + + let request = StreamConfig { + stream_info: Some(StreamInfo { + scope: "testScope123".into(), + stream: "testStream".into(), + }), + scaling_policy: Some(ScalingPolicy { + scale_type: ScalingPolicyType::FixedNumSegments as i32, + target_rate: 0, + scale_factor: 0, + min_num_segments: 1, + }), + retention_policy: None, + }; + let fut = create_stream(request, &mut client); + + rt.block_on(fut); +} diff --git a/src/controllerclient.rs b/src/controllerclient.rs deleted file mode 100644 index b05ed5591..000000000 --- a/src/controllerclient.rs +++ /dev/null @@ -1,131 +0,0 @@ -/** - * Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - */ -pub mod controller { - #![allow(non_camel_case_types)] - tonic::include_proto!("io.pravega.controller.stream.api.grpc.v1"); - // this is the rs file name generated after compiling the proto file, located inside the target folder. -} - -use controller::{ - client::ControllerServiceClient, scaling_policy::ScalingPolicyType, CreateScopeStatus, - CreateStreamStatus, ScalingPolicy, ScopeInfo, StreamConfig, StreamInfo, -}; -use tonic::transport::channel::Channel; - -/// create_connection with the given controller uri. -async fn create_connection(uri: &'static str) -> ControllerServiceClient { - // Placeholder to add authentication headers. - let connection: ControllerServiceClient = - ControllerServiceClient::connect(uri.to_string()) - .await - .expect("Failed to create a channel"); - connection -} - -/// Async function to create scope -async fn create_scope( - request: ScopeInfo, - ch: &mut ControllerServiceClient, -) -> CreateScopeStatus { - let op_status: tonic::Response = ch - .create_scope(tonic::Request::new(request)) - .await - .expect("Failed to create Scope"); - op_status.into_inner() // return the scope status -} - -async fn create_stream( - request: StreamConfig, - ch: &mut ControllerServiceClient, -) -> CreateStreamStatus { - let op_status: tonic::Response = ch - .create_stream(tonic::Request::new(request)) - .await - .expect("Failed to create Stream"); - op_status.into_inner() // return create Stream status -} - -#[tokio::main] -async fn main() -> Result<(), Box> { - // start Pravega standalone before invoking this function. - let mut client = create_connection("http://[::1]:9090").await; - let request = ScopeInfo { - scope: "testScope123".into(), - }; - let response: CreateScopeStatus = create_scope(request, &mut client).await; - println!("Response for create_scope is {:?}", response); - - let request2 = StreamConfig { - stream_info: Some(StreamInfo { - scope: "testScope123".into(), - stream: "testStream".into(), - }), - scaling_policy: Some(ScalingPolicy { - scale_type: ScalingPolicyType::FixedNumSegments as i32, - target_rate: 0, - scale_factor: 0, - min_num_segments: 1, - }), - retention_policy: None, - }; - let response2: CreateStreamStatus = create_stream(request2, &mut client).await; - println!("Response 2 for create_stream is {:?}", response2); - - Ok(()) -} - -#[cfg(test)] -mod tests { - // Note this useful idiom: importing names from outer (for mod tests) scope. - use super::*; - use tokio::runtime::Runtime; - - #[test] - #[should_panic] // since the controller is not running. - fn test_create_scope_error() { - let rt = Runtime::new().unwrap(); - - let client_future = create_connection("http://[::1]:9090"); - let mut client = rt.block_on(client_future); - - let request = ScopeInfo { - scope: "testScope124".into(), - }; - let fut = create_scope(request, &mut client); - - rt.block_on(fut); - } - - #[test] - #[should_panic] // since the controller is not running. - fn test_create_stream_error() { - let rt = Runtime::new().unwrap(); - - let client_future = create_connection("http://[::1]:9090"); - let mut client = rt.block_on(client_future); - - let request = StreamConfig { - stream_info: Some(StreamInfo { - scope: "testScope123".into(), - stream: "testStream".into(), - }), - scaling_policy: Some(ScalingPolicy { - scale_type: ScalingPolicyType::FixedNumSegments as i32, - target_rate: 0, - scale_factor: 0, - min_num_segments: 1, - }), - retention_policy: None, - }; - let fut = create_stream(request, &mut client); - - rt.block_on(fut); - } -} diff --git a/src/lib.rs b/src/lib.rs index 53fb7256f..f503db12b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,6 +10,11 @@ #[macro_use] extern crate lazy_static; -pub mod commands; -pub mod connection_factory; -pub mod wirecommands; +mod wire_protocol { + mod commands; + mod wire_commands; + mod connection_factory; +} +mod controller_client { + mod client; +} \ No newline at end of file diff --git a/src/commands.rs b/src/wire_protocol/commands.rs similarity index 100% rename from src/commands.rs rename to src/wire_protocol/commands.rs diff --git a/src/connection_factory.rs b/src/wire_protocol/connection_factory.rs similarity index 100% rename from src/connection_factory.rs rename to src/wire_protocol/connection_factory.rs diff --git a/src/wirecommands.rs b/src/wire_protocol/wire_commands.rs similarity index 99% rename from src/wirecommands.rs rename to src/wire_protocol/wire_commands.rs index b49cf4ba9..45ad980b2 100644 --- a/src/wirecommands.rs +++ b/src/wire_protocol/wire_commands.rs @@ -1,4 +1,4 @@ -use crate::commands::*; +use super::commands::*; use byteorder::{BigEndian, ByteOrder, WriteBytesExt}; #[derive(PartialEq, Debug)] From da97390146f3fb9810f9f9b8eca3b6ff44195f11 Mon Sep 17 00:00:00 2001 From: Tom Kaitchuck Date: Fri, 20 Dec 2019 11:26:51 -0800 Subject: [PATCH 02/30] Move main method for controller --- Cargo.toml | 2 +- src/controller_client/client.rs | 38 +++++++++++++++++++++---- src/controller_client/client/main.rs | 31 -------------------- src/wire_protocol/connection_factory.rs | 1 - 4 files changed, 34 insertions(+), 38 deletions(-) delete mode 100644 src/controller_client/client/main.rs diff --git a/Cargo.toml b/Cargo.toml index 4caa012af..40a8f3200 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,5 +38,5 @@ tonic-build = "0.1.0-alpha.5" #Simplify running the client. This should be removed. [[bin]] name = "controller-client" -path = "src/controller_client/client/main.rs" +path = "src/controller_client/client.rs" diff --git a/src/controller_client/client.rs b/src/controller_client/client.rs index 79a24aaaf..13835a511 100644 --- a/src/controller_client/client.rs +++ b/src/controller_client/client.rs @@ -7,7 +7,6 @@ * * http://www.apache.org/licenses/LICENSE-2.0 */ -mod main; #[allow(non_camel_case_types)] mod controller { tonic::include_proto!("io.pravega.controller.stream.api.grpc.v1"); @@ -17,10 +16,10 @@ mod controller { #[cfg(test)] mod test; - -use controller::{ - client::ControllerServiceClient, scaling_policy::ScalingPolicyType, CreateScopeStatus, - CreateStreamStatus, ScalingPolicy, ScopeInfo, StreamConfig, StreamInfo, +pub use controller::{ + client::ControllerServiceClient as ControllerServiceClient, + scaling_policy::ScalingPolicyType as ScalingPolicyType, + CreateScopeStatus, CreateStreamStatus, ScalingPolicy, ScopeInfo, StreamConfig, StreamInfo, }; use tonic::transport::channel::Channel; @@ -57,3 +56,32 @@ pub async fn create_stream( op_status.into_inner() // return create Stream status } + +#[tokio::main] +async fn main() -> Result<(), Box> { + // start Pravega standalone before invoking this function. + let mut client = create_connection("http://[::1]:9090").await; + let request = ScopeInfo { + scope: "testScope123".into(), + }; + let response: CreateScopeStatus = create_scope(request, &mut client).await; + println!("Response for create_scope is {:?}", response); + + let request2 = StreamConfig { + stream_info: Some(StreamInfo { + scope: "testScope123".into(), + stream: "testStream".into(), + }), + scaling_policy: Some(ScalingPolicy { + scale_type: ScalingPolicyType::FixedNumSegments as i32, + target_rate: 0, + scale_factor: 0, + min_num_segments: 1, + }), + retention_policy: None, + }; + let response2: CreateStreamStatus = create_stream(request2, &mut client).await; + println!("Response 2 for create_stream is {:?}", response2); + + Ok(()) +} \ No newline at end of file diff --git a/src/controller_client/client/main.rs b/src/controller_client/client/main.rs deleted file mode 100644 index ca92f6816..000000000 --- a/src/controller_client/client/main.rs +++ /dev/null @@ -1,31 +0,0 @@ - -use super::*; - -#[tokio::main] -async fn main() -> Result<(), Box> { - // start Pravega standalone before invoking this function. - let mut client = create_connection("http://[::1]:9090").await; - let request = ScopeInfo { - scope: "testScope123".into(), - }; - let response: CreateScopeStatus = create_scope(request, &mut client).await; - println!("Response for create_scope is {:?}", response); - - let request2 = StreamConfig { - stream_info: Some(StreamInfo { - scope: "testScope123".into(), - stream: "testStream".into(), - }), - scaling_policy: Some(ScalingPolicy { - scale_type: ScalingPolicyType::FixedNumSegments as i32, - target_rate: 0, - scale_factor: 0, - min_num_segments: 1, - }), - retention_policy: None, - }; - let response2: CreateStreamStatus = create_stream(request2, &mut client).await; - println!("Response 2 for create_stream is {:?}", response2); - - Ok(()) -} \ No newline at end of file diff --git a/src/wire_protocol/connection_factory.rs b/src/wire_protocol/connection_factory.rs index a99802493..20be30218 100644 --- a/src/wire_protocol/connection_factory.rs +++ b/src/wire_protocol/connection_factory.rs @@ -180,7 +180,6 @@ impl Connection for TokioConnection { #[cfg(test)] mod tests { use super::*; - use crate::connection_factory::ConnectionFactory; use log::info; use std::io::Write; use std::net::{SocketAddr, TcpListener}; From 0ac4546d6bec0b4332663ed22cc9fdbe41538b5c Mon Sep 17 00:00:00 2001 From: Tom Kaitchuck Date: Fri, 20 Dec 2019 11:54:38 -0800 Subject: [PATCH 03/30] Upgrade versions and orginize deps --- Cargo.toml | 34 +++++++++++++++------------------ src/controller_client/client.rs | 2 +- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 40a8f3200..71fc98782 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,33 +7,29 @@ build = "build.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -serde = { version = "1.0", features = ["derive"] } -serde_repr = "0.1" +async-stream = "0.1.2" +async-trait = "0.1.17" bincode = "1.2" byteorder = "1.3" - +bytes = "0.4" +futures = "0.3" +http = "0.1" lazy_static = "1.4.0" - -async-trait = "0.1.17" log = "0.4" - -tonic = "0.1.0-alpha.5" # using a * as per tonic documentation does not work. -bytes = "0.4" prost = "0.5" -prost-derive = "0.5" - -tokio = "=0.2.0-alpha.6" -tokio-test = "0.2.0-alpha.6" - -futures-preview = { version = "=0.3.0-alpha.19", default-features = false, features = ["alloc", "async-await"]} -async-stream = "0.1.2" -http = "0.1" -tower = "=0.3.0-alpha.2" +serde = { version = "1.0", features = ["derive"] } +serde_repr = "0.1" snafu = "0.5.0" +tokio = { version = "0.2.6", features = ["full"] } +tonic = "0.1.0-beta.1" # using a * as per tonic documentation does not work. +tower = "0.3.0" + +[dev-dependencies] +tokio-test = "0.2" [build-dependencies] -# star indicates any version is > 0.0.0. -tonic-build = "0.1.0-alpha.5" +prost-derive = "0.5" +tonic-build = "0.1.0-beta.1" #Simplify running the client. This should be removed. [[bin]] diff --git a/src/controller_client/client.rs b/src/controller_client/client.rs index 13835a511..31fe2bb65 100644 --- a/src/controller_client/client.rs +++ b/src/controller_client/client.rs @@ -17,7 +17,7 @@ mod controller { mod test; pub use controller::{ - client::ControllerServiceClient as ControllerServiceClient, + controller_service_client::ControllerServiceClient as ControllerServiceClient, scaling_policy::ScalingPolicyType as ScalingPolicyType, CreateScopeStatus, CreateStreamStatus, ScalingPolicy, ScopeInfo, StreamConfig, StreamInfo, }; From 8b16a0e43b741134a29bd380161e5c82e316f8c8 Mon Sep 17 00:00:00 2001 From: Tom Kaitchuck Date: Fri, 20 Dec 2019 12:00:25 -0800 Subject: [PATCH 04/30] Adapt tests/docs to new tokio version --- src/controller_client/client/test.rs | 4 ++-- src/wire_protocol/connection_factory.rs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/controller_client/client/test.rs b/src/controller_client/client/test.rs index 1b5b8a854..eef2fbd67 100644 --- a/src/controller_client/client/test.rs +++ b/src/controller_client/client/test.rs @@ -6,7 +6,7 @@ use tokio::runtime::Runtime; #[test] #[should_panic] // since the controller is not running. fn test_create_scope_error() { - let rt = Runtime::new().unwrap(); + let mut rt = Runtime::new().unwrap(); let client_future = create_connection("http://[::1]:9090"); let mut client = rt.block_on(client_future); @@ -22,7 +22,7 @@ fn test_create_scope_error() { #[test] #[should_panic] // since the controller is not running. fn test_create_stream_error() { - let rt = Runtime::new().unwrap(); + let mut rt = Runtime::new().unwrap(); let client_future = create_connection("http://[::1]:9090"); let mut client = rt.block_on(client_future); diff --git a/src/wire_protocol/connection_factory.rs b/src/wire_protocol/connection_factory.rs index 20be30218..c942d7b17 100644 --- a/src/wire_protocol/connection_factory.rs +++ b/src/wire_protocol/connection_factory.rs @@ -94,7 +94,7 @@ pub trait Connection { /// use tokio::runtime::Runtime; /// /// fn main() { - /// let rt = Runtime::new().unwrap(); + /// let mut rt = Runtime::new().unwrap(); /// let endpoint: SocketAddr = "127.0.0.1:0".parse().expect("Unable to parse socket address"); /// let cf = connection_factory::ConnectionFactoryImpl {}; /// let connection_future = cf.establish_connection(connection_factory::ConnectionType::Tokio, endpoint); @@ -116,7 +116,7 @@ pub trait Connection { /// use tokio::runtime::Runtime; /// /// fn main() { - /// let rt = Runtime::new().unwrap(); + /// let mut rt = Runtime::new().unwrap(); /// let endpoint: SocketAddr = "127.0.0.1:0".parse().expect("Unable to parse socket address"); /// let cf = connection_factory::ConnectionFactoryImpl {}; /// let connection_future = cf.establish_connection(connection_factory::ConnectionType::Tokio, endpoint); @@ -210,7 +210,7 @@ mod tests { #[test] fn test_connection() { - let rt = Runtime::new().unwrap(); + let mut rt = Runtime::new().unwrap(); let mut server = Server::new(); From 42a803de8543f3f7a0622c00b737608c11a25027 Mon Sep 17 00:00:00 2001 From: Tom Kaitchuck Date: Fri, 20 Dec 2019 16:44:41 -0800 Subject: [PATCH 05/30] Move controller-client into a its own crate --- Cargo.toml | 16 +- controller-client/Cargo.toml | 22 + build.rs => controller-client/build.rs | 0 .../proto}/Controller.proto | 0 .../client.rs => controller-client/src/lib.rs | 30 +- controller-client/src/main.rs | 31 + .../client => controller-client/src}/test.rs | 0 src/lib.rs | 8 +- src/wire_protocol/connection_factory.rs | 6 +- src/wire_protocol/tests.rs | 769 +++++++++++++++++ tests/test_wirecommands.rs | 773 ------------------ 11 files changed, 835 insertions(+), 820 deletions(-) create mode 100644 controller-client/Cargo.toml rename build.rs => controller-client/build.rs (100%) rename {proto => controller-client/proto}/Controller.proto (100%) rename src/controller_client/client.rs => controller-client/src/lib.rs (66%) create mode 100644 controller-client/src/main.rs rename {src/controller_client/client => controller-client/src}/test.rs (100%) create mode 100644 src/wire_protocol/tests.rs delete mode 100644 tests/test_wirecommands.rs diff --git a/Cargo.toml b/Cargo.toml index 71fc98782..5e9060de9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,27 +2,26 @@ name = "pravega-client-rust" version = "0.1.0" edition = "2018" -build = "build.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[workspace] +members = [ + "controller-client", +] + [dependencies] async-stream = "0.1.2" async-trait = "0.1.17" bincode = "1.2" byteorder = "1.3" -bytes = "0.4" futures = "0.3" -http = "0.1" lazy_static = "1.4.0" log = "0.4" -prost = "0.5" serde = { version = "1.0", features = ["derive"] } serde_repr = "0.1" snafu = "0.5.0" tokio = { version = "0.2.6", features = ["full"] } -tonic = "0.1.0-beta.1" # using a * as per tonic documentation does not work. -tower = "0.3.0" [dev-dependencies] tokio-test = "0.2" @@ -31,8 +30,3 @@ tokio-test = "0.2" prost-derive = "0.5" tonic-build = "0.1.0-beta.1" -#Simplify running the client. This should be removed. -[[bin]] -name = "controller-client" -path = "src/controller_client/client.rs" - diff --git a/controller-client/Cargo.toml b/controller-client/Cargo.toml new file mode 100644 index 000000000..f9daea996 --- /dev/null +++ b/controller-client/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "pravega-controller-client" +version = "0.1.0" +edition = "2018" +build = "build.rs" + + +[dependencies] +tonic = "0.1.0-beta.1" # using a * as per tonic documentation does not work. +bytes = "0.4" +http = "0.1" +log = "0.4" +prost = "0.5" +snafu = "0.5.0" +tokio = { version = "0.2.6", features = ["full"] } + +[build-dependencies] +tonic-build = "0.1.0-beta.1" + +[[bin]] +name = "controller-cli" +path = "src/main.rs" \ No newline at end of file diff --git a/build.rs b/controller-client/build.rs similarity index 100% rename from build.rs rename to controller-client/build.rs diff --git a/proto/Controller.proto b/controller-client/proto/Controller.proto similarity index 100% rename from proto/Controller.proto rename to controller-client/proto/Controller.proto diff --git a/src/controller_client/client.rs b/controller-client/src/lib.rs similarity index 66% rename from src/controller_client/client.rs rename to controller-client/src/lib.rs index 31fe2bb65..9156675b7 100644 --- a/src/controller_client/client.rs +++ b/controller-client/src/lib.rs @@ -1,4 +1,4 @@ -/** +/* * Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -57,31 +57,3 @@ pub async fn create_stream( } -#[tokio::main] -async fn main() -> Result<(), Box> { - // start Pravega standalone before invoking this function. - let mut client = create_connection("http://[::1]:9090").await; - let request = ScopeInfo { - scope: "testScope123".into(), - }; - let response: CreateScopeStatus = create_scope(request, &mut client).await; - println!("Response for create_scope is {:?}", response); - - let request2 = StreamConfig { - stream_info: Some(StreamInfo { - scope: "testScope123".into(), - stream: "testStream".into(), - }), - scaling_policy: Some(ScalingPolicy { - scale_type: ScalingPolicyType::FixedNumSegments as i32, - target_rate: 0, - scale_factor: 0, - min_num_segments: 1, - }), - retention_policy: None, - }; - let response2: CreateStreamStatus = create_stream(request2, &mut client).await; - println!("Response 2 for create_stream is {:?}", response2); - - Ok(()) -} \ No newline at end of file diff --git a/controller-client/src/main.rs b/controller-client/src/main.rs new file mode 100644 index 000000000..aed4509fa --- /dev/null +++ b/controller-client/src/main.rs @@ -0,0 +1,31 @@ + +use pravega_controller_client::*; + +#[tokio::main] +async fn main() -> Result<(), Box> { + // start Pravega standalone before invoking this function. + let mut client = create_connection("http://[::1]:9090").await; + let request = ScopeInfo { + scope: "testScope123".into(), + }; + let response: CreateScopeStatus = create_scope(request, &mut client).await; + println!("Response for create_scope is {:?}", response); + + let request2 = StreamConfig { + stream_info: Some(StreamInfo { + scope: "testScope123".into(), + stream: "testStream".into(), + }), + scaling_policy: Some(ScalingPolicy { + scale_type: ScalingPolicyType::FixedNumSegments as i32, + target_rate: 0, + scale_factor: 0, + min_num_segments: 1, + }), + retention_policy: None, + }; + let response2: CreateStreamStatus = create_stream(request2, &mut client).await; + println!("Response 2 for create_stream is {:?}", response2); + + Ok(()) +} \ No newline at end of file diff --git a/src/controller_client/client/test.rs b/controller-client/src/test.rs similarity index 100% rename from src/controller_client/client/test.rs rename to controller-client/src/test.rs diff --git a/src/lib.rs b/src/lib.rs index f503db12b..4cb6bbd6c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,11 +10,11 @@ #[macro_use] extern crate lazy_static; + mod wire_protocol { mod commands; mod wire_commands; - mod connection_factory; + pub mod connection_factory; + #[cfg(test)] + mod tests; } -mod controller_client { - mod client; -} \ No newline at end of file diff --git a/src/wire_protocol/connection_factory.rs b/src/wire_protocol/connection_factory.rs index c942d7b17..c12182c0c 100644 --- a/src/wire_protocol/connection_factory.rs +++ b/src/wire_protocol/connection_factory.rs @@ -66,7 +66,7 @@ pub trait ConnectionFactory { /// use tokio::runtime::Runtime; /// /// fn main() { - /// let rt = Runtime::new().unwrap(); + /// let mut rt = Runtime::new().unwrap(); /// let endpoint: SocketAddr = "127.0.0.1:0".parse().expect("Unable to parse socket address"); /// let cf = connection_factory::ConnectionFactoryImpl {}; /// let connection_future = cf.establish_connection(connection_factory::ConnectionType::Tokio, endpoint); @@ -111,8 +111,8 @@ pub trait Connection { /// /// ```no_run /// use std::net::SocketAddr; - /// use pravega_client_rust::connection_factory; - /// use pravega_client_rust::connection_factory::ConnectionFactory; + /// use pravega_client_rust::wire_protocol::connection_factory; + /// use pravega_client_rust::wire_protocol::connection_factory::ConnectionFactory; /// use tokio::runtime::Runtime; /// /// fn main() { diff --git a/src/wire_protocol/tests.rs b/src/wire_protocol/tests.rs new file mode 100644 index 000000000..a389b9307 --- /dev/null +++ b/src/wire_protocol/tests.rs @@ -0,0 +1,769 @@ +use super::commands::*; +use super::wire_commands::*; + +#[test] +fn test_hello() { + let hello_command = WireCommands::Hello(HelloCommand { + high_version: 9, + low_version: 5, + }); + test_command(hello_command); +} + +#[test] +fn test_wrong_host() { + let correct_host_name = JavaString(String::from("foo")); + let segment_name = JavaString(String::from("segment-1")); + let stack_trace = JavaString(String::from("some exception")); + let wrong_host_command = WireCommands::WrongHost(WrongHostCommand { + request_id: 1, + segment: segment_name, + correct_host: correct_host_name, + server_stack_trace: stack_trace, + }); + test_command(wrong_host_command); +} + +#[test] +fn test_segment_is_sealed() { + let segment_name = JavaString(String::from("segment-1")); + let stack_trace = JavaString(String::from("some exception")); + let offset_pos = 100i64; + let segment_is_sealed_command = WireCommands::SegmentIsSealed(SegmentIsSealedCommand { + request_id: 1, + segment: segment_name, + server_stack_trace: stack_trace, + offset: offset_pos, + }); + test_command(segment_is_sealed_command); +} + +#[test] +fn test_segment_already_exists() { + let segment_name = JavaString(String::from("segment-1")); + let stack_trace = JavaString(String::from("some exception")); + let segment_already_exists_command = + WireCommands::SegmentAlreadyExists(SegmentAlreadyExistsCommand { + request_id: 1, + segment: segment_name, + server_stack_trace: stack_trace, + }); + test_command(segment_already_exists_command); +} + +#[test] +fn test_segment_is_truncated() { + let segment_name = JavaString(String::from("segment-1")); + let stack_trace = JavaString(String::from("some exception")); + let start_offset_pos = 0i64; + let offset_pos = 100i64; + let segment_is_truncated_command = + WireCommands::SegmentIsTruncated(SegmentIsTruncatedCommand { + request_id: 1, + segment: segment_name, + server_stack_trace: stack_trace, + start_offset: start_offset_pos, + offset: offset_pos, + }); + test_command(segment_is_truncated_command); +} + +#[test] +fn test_no_such_segment() { + let segment_name = JavaString(String::from("segment-1")); + let stack_trace = JavaString(String::from("some exception")); + let offset_pos = 100i64; + let no_such_segment_command = WireCommands::NoSuchSegment(NoSuchSegmentCommand { + request_id: 1, + segment: segment_name, + server_stack_trace: stack_trace, + offset: offset_pos, + }); + test_command(no_such_segment_command); +} + +#[test] +fn test_table_segment_not_empty() { + let segment_name = JavaString(String::from("segment-1")); + let stack_trace = JavaString(String::from("some exception")); + let table_segment_not_empty_command = + WireCommands::TableSegmentNotEmpty(TableSegmentNotEmptyCommand { + request_id: 1, + segment: segment_name, + server_stack_trace: stack_trace, + }); + test_command(table_segment_not_empty_command); +} + +#[test] +fn test_invalid_event_number() { + let writer_id_number: u128 = 123; + let event_num: i64 = 100; + let stack_trace = JavaString(String::from("some exception")); + let invalid_event_number_command = + WireCommands::InvalidEventNumber(InvalidEventNumberCommand { + writer_id: writer_id_number, + server_stack_trace: stack_trace, + event_number: event_num, + }); + test_command(invalid_event_number_command); +} + +#[test] +fn test_operation_unsupported() { + let name = JavaString(String::from("operation")); + let stack_trace = JavaString(String::from("some exception")); + let test_operation_unsupported_command = + WireCommands::OperationUnsupported(OperationUnsupportedCommand { + request_id: 1, + operation_name: name, + server_stack_trace: stack_trace, + }); + test_command(test_operation_unsupported_command); +} + +#[test] +fn test_padding() { + let length = 10; + let padding_command = WireCommands::Padding(PaddingCommand { length }); + let decoded = test_command(padding_command); + if let WireCommands::Padding(command) = decoded { + assert_eq!(command.length, 10); + } +} + +#[test] +fn test_partial_event() { + let data = String::from("event-1").into_bytes(); + let partial_event = WireCommands::PartialEvent(PartialEventCommand { data }); + test_command(partial_event); +} + +#[test] +fn test_event() { + let data = String::from("event-1").into_bytes(); + let event = WireCommands::Event(EventCommand { data }); + let decoded = test_command(event); + if let WireCommands::Event(event_struct) = decoded { + assert_eq!(String::from_utf8(event_struct.data).unwrap(), "event-1"); + } else { + panic!("test failed"); + } +} + +#[test] +fn test_setup_append() { + let writer_id_number: u128 = 123; + let segment_name = JavaString(String::from("segment-1")); + let token = JavaString(String::from("delegation_token")); + let setup_append_command = WireCommands::SetupAppend(SetupAppendCommand { + request_id: 1, + writer_id: writer_id_number, + segment: segment_name, + delegation_token: token, + }); + test_command(setup_append_command); +} + +#[test] +fn test_append_block() { + let writer_id_number: u128 = 123; + let data = String::from("event-1").into_bytes(); + let append_block_command = WireCommands::AppendBlock(AppendBlockCommand { + writer_id: writer_id_number, + data, + }); + test_command(append_block_command); +} + +#[test] +fn test_append_block_end() { + let writer_id_number: u128 = 123; + let data = String::from("event-1").into_bytes(); + let size_of_events = data.len() as i32; + let append_block_end_command = WireCommands::AppendBlockEnd(AppendBlockEndCommand { + writer_id: writer_id_number, + size_of_whole_events: size_of_events, + data, + num_event: 1, + last_event_number: 1, + request_id: 1, + }); + + test_command(append_block_end_command); +} + +#[test] +fn test_conditional_append() { + let writer_id_number: u128 = 123; + let data = String::from("event-1").into_bytes(); + let event = EventCommand { data }; + let conditional_append_command = + WireCommands::ConditionalAppend(ConditionalAppendCommand { + writer_id: writer_id_number, + event_number: 1, + expected_offset: 0, + event, + request_id: 1, + }); + + let decoded = test_command(conditional_append_command); + if let WireCommands::ConditionalAppend(command) = decoded { + let data = String::from("event-1").into_bytes(); + assert_eq!(command.event, EventCommand { data }); + } else { + panic!("test failed"); + } +} + +#[test] +fn test_append_setup() { + let writer_id_number: u128 = 123; + let segment_name = JavaString(String::from("segment-1")); + let append_setup_cmd = WireCommands::AppendSetup(AppendSetupCommand { + request_id: 1, + segment: segment_name, + writer_id: writer_id_number, + last_event_number: 1, + }); + test_command(append_setup_cmd); +} + +#[test] +fn test_data_appended() { + let writer_id_number: u128 = 123; + let data_appended_cmd = WireCommands::DataAppended(DataAppendedCommand { + writer_id: writer_id_number, + event_number: 1, + previous_event_number: 0, + request_id: 1, + current_segment_write_offset: 0, + }); + test_command(data_appended_cmd); +} + +#[test] +fn test_conditional_check_failed() { + let writer_id_number: u128 = 123; + let conditional_check_failed_cmd = + WireCommands::ConditionalCheckFailed(ConditionalCheckFailedCommand { + writer_id: writer_id_number, + event_number: 1, + request_id: 1, + }); + test_command(conditional_check_failed_cmd); +} + +#[test] +fn test_read_segment() { + let segment_name = JavaString(String::from("segment-1")); + let token = JavaString(String::from("delegation_token")); + let read_segment_command = WireCommands::ReadSegment(ReadSegmentCommand { + segment: segment_name, + offset: 0, + suggested_length: 10, + delegation_token: token, + request_id: 1, + }); + test_command(read_segment_command); +} + +#[test] +fn test_segment_read() { + let segment_name = JavaString(String::from("segment-1")); + let data = String::from("event-1").into_bytes(); + let segment_read_command = WireCommands::SegmentRead(SegmentReadCommand { + segment: segment_name, + offset: 0, + at_tail: true, + end_of_segment: true, + data, + request_id: 1, + }); + test_command(segment_read_command); +} + +#[test] +fn test_get_segment_attribute() { + let segment_name = JavaString(String::from("segment-1")); + let token = JavaString(String::from("delegation_token")); + let attribute_id: u128 = 123; + let get_segment_attribute_command = + WireCommands::GetSegmentAttribute(GetSegmentAttributeCommand { + request_id: 1, + segment_name, + attribute_id, + delegation_token: token, + }); + test_command(get_segment_attribute_command); +} + +#[test] +fn test_segment_attribute() { + let segment_attribute_command = WireCommands::SegmentAttribute(SegmentAttributeCommand { + request_id: 1, + value: 0, + }); + test_command(segment_attribute_command); +} + +#[test] +fn test_update_segment_attribute() { + let segment_name = JavaString(String::from("segment-1")); + let token = JavaString(String::from("delegation_token")); + let attribute_id: u128 = 123; + let update_segment_attribute = + WireCommands::UpdateSegmentAttribute(UpdateSegmentAttributeCommand { + request_id: 1, + segment_name, + attribute_id, + new_value: 2, + expected_value: 2, + delegation_token: token, + }); + test_command(update_segment_attribute); +} + +#[test] +fn test_segment_attribute_updated() { + let segment_attribute_updated = + WireCommands::SegmentAttributeUpdated(SegmentAttributeUpdatedCommand { + request_id: 1, + success: true, + }); + test_command(segment_attribute_updated); +} + +#[test] +fn test_get_stream_segment_info() { + let segment_name = JavaString(String::from("segment-1")); + let token = JavaString(String::from("delegation_token")); + let get_stream_segment_info = + WireCommands::GetStreamSegmentInfo(GetStreamSegmentInfoCommand { + request_id: 1, + segment_name, + delegation_token: token, + }); + test_command(get_stream_segment_info); +} + +#[test] +fn test_stream_segment_info() { + let segment_name = JavaString(String::from("segment-1")); + let stream_segment_info = WireCommands::StreamSegmentInfo(StreamSegmentInfoCommand { + request_id: 0, + segment_name, + exists: false, + is_sealed: false, + is_deleted: false, + last_modified: 0, + write_offset: 0, + start_offset: 0, + }); + test_command(stream_segment_info); +} + +#[test] +fn test_create_segment() { + let segment_name = JavaString(String::from("segment-1")); + let token = JavaString(String::from("delegation_token")); + let create_segment_command = WireCommands::CreateSegment(CreateSegmentCommand { + request_id: 1, + segment: segment_name, + target_rate: 1, + scale_type: 0, + delegation_token: token, + }); + test_command(create_segment_command); +} + +#[test] +fn test_create_table_segment() { + let segment_name = JavaString(String::from("segment-1")); + let token = JavaString(String::from("delegation_token")); + let create_table_segment_command = + WireCommands::CreateTableSegment(CreateTableSegmentCommand { + request_id: 1, + segment: segment_name, + delegation_token: token, + }); + test_command(create_table_segment_command); +} + +#[test] +fn test_segment_created() { + let segment_name = JavaString(String::from("segment-1")); + let segment_created_cmd = WireCommands::SegmentCreated(SegmentCreatedCommand { + request_id: 1, + segment: segment_name, + }); + test_command(segment_created_cmd); +} + +#[test] +fn test_update_segment_policy() { + let segment_name = JavaString(String::from("segment-1")); + let token = JavaString(String::from("delegation_token")); + let update_segment_policy_cmd = + WireCommands::UpdateSegmentPolicy(UpdateSegmentPolicyCommand { + request_id: 1, + segment: segment_name, + target_rate: 1, + scale_type: 0, + delegation_token: token, + }); + test_command(update_segment_policy_cmd); +} + +#[test] +fn test_segment_policy_updated() { + let segment_name = JavaString(String::from("segment-1")); + let segment_policy_updated = + WireCommands::SegmentPolicyUpdated(SegmentPolicyUpdatedCommand { + request_id: 0, + segment: segment_name, + }); + test_command(segment_policy_updated); +} + +#[test] +fn test_merge_segment() { + let target = JavaString(String::from("segment-1")); + let source = JavaString(String::from("segment-2")); + let token = JavaString(String::from("delegation_token")); + let merge_segment = WireCommands::MergeSegments(MergeSegmentsCommand { + request_id: 1, + target, + source, + delegation_token: token, + }); + test_command(merge_segment); +} + +#[test] +fn test_merge_table_segment() { + let target = JavaString(String::from("segment-1")); + let source = JavaString(String::from("segment-2")); + let token = JavaString(String::from("delegation_token")); + let merge_table_segment = WireCommands::MergeTableSegments(MergeTableSegmentsCommand { + request_id: 1, + target, + source, + delegation_token: token, + }); + test_command(merge_table_segment); +} + +#[test] +fn test_segment_merged() { + let target = JavaString(String::from("segment-1")); + let source = JavaString(String::from("segment-2")); + let segment_merged = WireCommands::SegmentsMerged(SegmentsMergedCommand { + request_id: 1, + target, + source, + new_target_write_offset: 10, + }); + test_command(segment_merged); +} + +#[test] +fn test_seal_segment() { + let segment_name = JavaString(String::from("segment-1")); + let token = JavaString(String::from("delegation_token")); + let seal_segment = WireCommands::SealSegment(SealSegmentCommand { + request_id: 1, + segment: segment_name, + delegation_token: token, + }); + test_command(seal_segment); +} + +#[test] +fn test_seal_table_segment() { + let segment_name = JavaString(String::from("segment-1")); + let token = JavaString(String::from("delegation_token")); + let seal_table_segment = WireCommands::SealTableSegment(SealTableSegmentCommand { + request_id: 1, + segment: segment_name, + delegation_token: token, + }); + test_command(seal_table_segment); +} + +#[test] +fn test_segment_sealed() { + let segment_name = JavaString(String::from("segment-1")); + let segment_sealed = WireCommands::SegmentSealed(SegmentSealedCommand { + request_id: 1, + segment: segment_name, + }); + test_command(segment_sealed); +} + +#[test] +fn test_truncate_segment() { + let segment_name = JavaString(String::from("segment-1")); + let token = JavaString(String::from("delegation_token")); + let truncate_segment = WireCommands::TruncateSegment(TruncateSegmentCommand { + request_id: 1, + segment: segment_name, + truncation_offset: 10, + delegation_token: token, + }); + test_command(truncate_segment); +} + +#[test] +fn test_segment_truncated() { + let segment_name = JavaString(String::from("segment-1")); + let segment_truncated = WireCommands::SegmentTruncated(SegmentTruncatedCommand { + request_id: 1, + segment: segment_name, + }); + test_command(segment_truncated); +} + +#[test] +fn test_delete_segment() { + let segment_name = JavaString(String::from("segment-1")); + let token = JavaString(String::from("delegation_token")); + let delete_segment_command = WireCommands::DeleteSegment(DeleteSegmentCommand { + request_id: 1, + segment: segment_name, + delegation_token: token, + }); + test_command(delete_segment_command); +} + +#[test] +fn test_segment_deleted() { + let segment = JavaString(String::from("segment-1")); + let segment_deleted = WireCommands::SegmentDeleted(SegmentDeletedCommand { + request_id: 1, + segment, + }); + test_command(segment_deleted); +} + +#[test] +fn test_delete_table_segment() { + let segment_name = JavaString(String::from("segment-1")); + let token = JavaString(String::from("delegation_token")); + let delete_table_segment = WireCommands::DeleteTableSegment(DeleteTableSegmentCommand { + request_id: 0, + segment: segment_name, + must_be_empty: true, + delegation_token: token, + }); + test_command(delete_table_segment); +} + +#[test] +fn test_keep_alive() { + let keep_alive = WireCommands::KeepAlive(KeepAliveCommand {}); + test_command(keep_alive); +} + +#[test] +fn test_auth_checked_failed() { + let stack_trace = JavaString(String::from("some exception")); + let auth_checked_failed = WireCommands::AuthTokenCheckFailed(AuthTokenCheckFailedCommand { + request_id: 1, + server_stack_trace: stack_trace, + error_code: -1, + }); + + let decode_command = test_command(auth_checked_failed); + if let WireCommands::AuthTokenCheckFailed(command) = decode_command { + assert_eq!(command.is_token_expired(), false); + assert_eq!(command.get_error_code(), ErrorCode::Unspecified); + } +} + +#[test] +fn test_update_table_entries() { + let mut entries = Vec::<(TableKey, TableValue)>::new(); + let key_data = String::from("key-1").into_bytes(); + let value_data = String::from("value-1").into_bytes(); + entries.push((TableKey::new(key_data, 1), TableValue::new(value_data))); + let table_entries = TableEntries { entries }; + let segment_name = JavaString(String::from("segment-1")); + let token = JavaString(String::from("delegation_token")); + let _size = table_entries.size(); + let update_table_entries = WireCommands::UpdateTableEntries(UpdateTableEntriesCommand { + request_id: 1, + segment: segment_name, + delegation_token: token, + table_entries, + }); + + test_command(update_table_entries); +} + +#[test] +fn test_table_entries_updated() { + let updated_versions: Vec = vec![1, 2, 3, 4]; + let table_entries_updated = WireCommands::TableEntriesUpdated(TableEntriesUpdatedCommand { + request_id: 1, + updated_versions, + }); + test_command(table_entries_updated); +} + +#[test] +fn test_remove_table_keys() { + let segment = JavaString(String::from("segment-1")); + let token = JavaString(String::from("delegation_token")); + let mut keys = Vec::<(TableKey)>::new(); + let key_data = String::from("key-1").into_bytes(); + keys.push(TableKey::new(key_data, 1)); + let remove_table_keys_command = WireCommands::RemoveTableKeys(RemoveTableKeysCommand { + request_id: 1, + segment, + delegation_token: token, + keys, + }); + test_command(remove_table_keys_command); +} + +#[test] +fn test_table_keys_removed() { + let segment = JavaString(String::from("segment-1")); + let table_key_removed = WireCommands::TableKeysRemoved(TableKeysRemovedCommand { + request_id: 1, + segment, + }); + test_command(table_key_removed); +} + +#[test] +fn test_read_table() { + let segment = JavaString(String::from("segment-1")); + let token = JavaString(String::from("delegation_token")); + let mut keys = Vec::<(TableKey)>::new(); + let key_data = String::from("key-1").into_bytes(); + keys.push(TableKey::new(key_data, 1)); + let read_table_command = WireCommands::ReadTable(ReadTableCommand { + request_id: 1, + segment, + delegation_token: token, + keys, + }); + + test_command(read_table_command); +} + +#[test] +fn test_table_read() { + let mut entries = Vec::<(TableKey, TableValue)>::new(); + let key_data = String::from("key-1").into_bytes(); + let value_data = String::from("value-1").into_bytes(); + entries.push((TableKey::new(key_data, 1), TableValue::new(value_data))); + let table_entries = TableEntries { entries }; + let segment_name = JavaString(String::from("segment-1")); + let table_read = WireCommands::TableRead(TableReadCommand { + request_id: 1, + segment: segment_name, + entries: table_entries, + }); + + test_command(table_read); +} + +#[test] +fn test_read_table_keys() { + let segment_name = JavaString(String::from("segment-1")); + let token = JavaString(String::from("delegation_token")); + let continuation_token: Vec = vec![1, 2, 3]; + let read_table_keys = WireCommands::ReadTableKeys(ReadTableKeysCommand { + request_id: 0, + segment: segment_name, + delegation_token: token, + suggested_key_count: 3, + continuation_token, + }); + test_command(read_table_keys); +} + +#[test] +fn test_table_keys_read() { + let segment = JavaString(String::from("segment-1")); + let mut keys = Vec::<(TableKey)>::new(); + let key_data = String::from("key-1").into_bytes(); + keys.push(TableKey::new(key_data, 1)); + let continuation_token: Vec = vec![1, 2, 3]; + let table_keys_read_command = WireCommands::TableKeysRead(TableKeysReadCommand { + request_id: 1, + segment, + keys, + continuation_token, + }); + test_command(table_keys_read_command); +} + +#[test] +fn test_read_table_entries() { + let segment_name = JavaString(String::from("segment-1")); + let token = JavaString(String::from("delegation_token")); + let continuation_token: Vec = vec![1, 2, 3]; + let read_table_entries = WireCommands::ReadTableEntries(ReadTableEntriesCommand { + request_id: 0, + segment: segment_name, + delegation_token: token, + suggested_entry_count: 3, + continuation_token, + }); + test_command(read_table_entries); +} + +#[test] +fn test_table_entries_read() { + let segment_name = JavaString(String::from("segment-1")); + let continuation_token: Vec = vec![1, 2, 3]; + let mut entries = Vec::<(TableKey, TableValue)>::new(); + let key_data = String::from("key-1").into_bytes(); + let value_data = String::from("value-1").into_bytes(); + entries.push((TableKey::new(key_data, 1), TableValue::new(value_data))); + let table_entries = TableEntries { entries }; + let table_entries_read = WireCommands::TableEntriesRead(TableEntriesReadCommand { + request_id: 1, + segment: segment_name, + entries: table_entries, + continuation_token, + }); + test_command(table_entries_read); +} + +#[test] +fn table_key_does_not_exist() { + let segment_name = JavaString(String::from("segment-1")); + let stack_trace = JavaString(String::from("some exception")); + let table_key_does_not_exist = + WireCommands::TableKeyDoesNotExist(TableKeyDoesNotExistCommand { + request_id: 0, + segment: segment_name, + server_stack_trace: stack_trace, + }); + test_command(table_key_does_not_exist); +} + +#[test] +fn table_key_bad_version() { + let segment_name = JavaString(String::from("segment-1")); + let stack_trace = JavaString(String::from("some exception")); + let table_key_bad_version = WireCommands::TableKeyBadVersion(TableKeyBadVersionCommand { + request_id: 0, + segment: segment_name, + server_stack_trace: stack_trace, + }); + test_command(table_key_bad_version); +} + +fn test_command(command: WireCommands) -> WireCommands { + let encoded: Vec = command.write_fields(); + let decoded = WireCommands::read_from(&encoded); + assert_eq!(command, decoded); + decoded +} + diff --git a/tests/test_wirecommands.rs b/tests/test_wirecommands.rs deleted file mode 100644 index eb2272a38..000000000 --- a/tests/test_wirecommands.rs +++ /dev/null @@ -1,773 +0,0 @@ -extern crate pravega_client_rust; - -#[cfg(test)] -mod tests { - use pravega_client_rust::commands::*; - use pravega_client_rust::wirecommands::*; - - #[test] - fn test_hello() { - let hello_command = WireCommands::Hello(HelloCommand { - high_version: 9, - low_version: 5, - }); - test_command(hello_command); - } - - #[test] - fn test_wrong_host() { - let correct_host_name = JavaString(String::from("foo")); - let segment_name = JavaString(String::from("segment-1")); - let stack_trace = JavaString(String::from("some exception")); - let wrong_host_command = WireCommands::WrongHost(WrongHostCommand { - request_id: 1, - segment: segment_name, - correct_host: correct_host_name, - server_stack_trace: stack_trace, - }); - test_command(wrong_host_command); - } - - #[test] - fn test_segment_is_sealed() { - let segment_name = JavaString(String::from("segment-1")); - let stack_trace = JavaString(String::from("some exception")); - let offset_pos = 100i64; - let segment_is_sealed_command = WireCommands::SegmentIsSealed(SegmentIsSealedCommand { - request_id: 1, - segment: segment_name, - server_stack_trace: stack_trace, - offset: offset_pos, - }); - test_command(segment_is_sealed_command); - } - - #[test] - fn test_segment_already_exists() { - let segment_name = JavaString(String::from("segment-1")); - let stack_trace = JavaString(String::from("some exception")); - let segment_already_exists_command = - WireCommands::SegmentAlreadyExists(SegmentAlreadyExistsCommand { - request_id: 1, - segment: segment_name, - server_stack_trace: stack_trace, - }); - test_command(segment_already_exists_command); - } - - #[test] - fn test_segment_is_truncated() { - let segment_name = JavaString(String::from("segment-1")); - let stack_trace = JavaString(String::from("some exception")); - let start_offset_pos = 0i64; - let offset_pos = 100i64; - let segment_is_truncated_command = - WireCommands::SegmentIsTruncated(SegmentIsTruncatedCommand { - request_id: 1, - segment: segment_name, - server_stack_trace: stack_trace, - start_offset: start_offset_pos, - offset: offset_pos, - }); - test_command(segment_is_truncated_command); - } - - #[test] - fn test_no_such_segment() { - let segment_name = JavaString(String::from("segment-1")); - let stack_trace = JavaString(String::from("some exception")); - let offset_pos = 100i64; - let no_such_segment_command = WireCommands::NoSuchSegment(NoSuchSegmentCommand { - request_id: 1, - segment: segment_name, - server_stack_trace: stack_trace, - offset: offset_pos, - }); - test_command(no_such_segment_command); - } - - #[test] - fn test_table_segment_not_empty() { - let segment_name = JavaString(String::from("segment-1")); - let stack_trace = JavaString(String::from("some exception")); - let table_segment_not_empty_command = - WireCommands::TableSegmentNotEmpty(TableSegmentNotEmptyCommand { - request_id: 1, - segment: segment_name, - server_stack_trace: stack_trace, - }); - test_command(table_segment_not_empty_command); - } - - #[test] - fn test_invalid_event_number() { - let writer_id_number: u128 = 123; - let event_num: i64 = 100; - let stack_trace = JavaString(String::from("some exception")); - let invalid_event_number_command = - WireCommands::InvalidEventNumber(InvalidEventNumberCommand { - writer_id: writer_id_number, - server_stack_trace: stack_trace, - event_number: event_num, - }); - test_command(invalid_event_number_command); - } - - #[test] - fn test_operation_unsupported() { - let name = JavaString(String::from("operation")); - let stack_trace = JavaString(String::from("some exception")); - let test_operation_unsupported_command = - WireCommands::OperationUnsupported(OperationUnsupportedCommand { - request_id: 1, - operation_name: name, - server_stack_trace: stack_trace, - }); - test_command(test_operation_unsupported_command); - } - - #[test] - fn test_padding() { - let length = 10; - let padding_command = WireCommands::Padding(PaddingCommand { length }); - let decoded = test_command(padding_command); - if let WireCommands::Padding(command) = decoded { - assert_eq!(command.length, 10); - } - } - - #[test] - fn test_partial_event() { - let data = String::from("event-1").into_bytes(); - let partial_event = WireCommands::PartialEvent(PartialEventCommand { data }); - test_command(partial_event); - } - - #[test] - fn test_event() { - let data = String::from("event-1").into_bytes(); - let event = WireCommands::Event(EventCommand { data }); - let decoded = test_command(event); - if let WireCommands::Event(event_struct) = decoded { - assert_eq!(String::from_utf8(event_struct.data).unwrap(), "event-1"); - } else { - panic!("test failed"); - } - } - - #[test] - fn test_setup_append() { - let writer_id_number: u128 = 123; - let segment_name = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); - let setup_append_command = WireCommands::SetupAppend(SetupAppendCommand { - request_id: 1, - writer_id: writer_id_number, - segment: segment_name, - delegation_token: token, - }); - test_command(setup_append_command); - } - - #[test] - fn test_append_block() { - let writer_id_number: u128 = 123; - let data = String::from("event-1").into_bytes(); - let append_block_command = WireCommands::AppendBlock(AppendBlockCommand { - writer_id: writer_id_number, - data, - }); - test_command(append_block_command); - } - - #[test] - fn test_append_block_end() { - let writer_id_number: u128 = 123; - let data = String::from("event-1").into_bytes(); - let size_of_events = data.len() as i32; - let append_block_end_command = WireCommands::AppendBlockEnd(AppendBlockEndCommand { - writer_id: writer_id_number, - size_of_whole_events: size_of_events, - data, - num_event: 1, - last_event_number: 1, - request_id: 1, - }); - - test_command(append_block_end_command); - } - - #[test] - fn test_conditional_append() { - let writer_id_number: u128 = 123; - let data = String::from("event-1").into_bytes(); - let event = EventCommand { data }; - let conditional_append_command = - WireCommands::ConditionalAppend(ConditionalAppendCommand { - writer_id: writer_id_number, - event_number: 1, - expected_offset: 0, - event, - request_id: 1, - }); - - let decoded = test_command(conditional_append_command); - if let WireCommands::ConditionalAppend(command) = decoded { - let data = String::from("event-1").into_bytes(); - assert_eq!(command.event, EventCommand { data }); - } else { - panic!("test failed"); - } - } - - #[test] - fn test_append_setup() { - let writer_id_number: u128 = 123; - let segment_name = JavaString(String::from("segment-1")); - let append_setup_cmd = WireCommands::AppendSetup(AppendSetupCommand { - request_id: 1, - segment: segment_name, - writer_id: writer_id_number, - last_event_number: 1, - }); - test_command(append_setup_cmd); - } - - #[test] - fn test_data_appended() { - let writer_id_number: u128 = 123; - let data_appended_cmd = WireCommands::DataAppended(DataAppendedCommand { - writer_id: writer_id_number, - event_number: 1, - previous_event_number: 0, - request_id: 1, - current_segment_write_offset: 0, - }); - test_command(data_appended_cmd); - } - - #[test] - fn test_conditional_check_failed() { - let writer_id_number: u128 = 123; - let conditional_check_failed_cmd = - WireCommands::ConditionalCheckFailed(ConditionalCheckFailedCommand { - writer_id: writer_id_number, - event_number: 1, - request_id: 1, - }); - test_command(conditional_check_failed_cmd); - } - - #[test] - fn test_read_segment() { - let segment_name = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); - let read_segment_command = WireCommands::ReadSegment(ReadSegmentCommand { - segment: segment_name, - offset: 0, - suggested_length: 10, - delegation_token: token, - request_id: 1, - }); - test_command(read_segment_command); - } - - #[test] - fn test_segment_read() { - let segment_name = JavaString(String::from("segment-1")); - let data = String::from("event-1").into_bytes(); - let segment_read_command = WireCommands::SegmentRead(SegmentReadCommand { - segment: segment_name, - offset: 0, - at_tail: true, - end_of_segment: true, - data, - request_id: 1, - }); - test_command(segment_read_command); - } - - #[test] - fn test_get_segment_attribute() { - let segment_name = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); - let attribute_id: u128 = 123; - let get_segment_attribute_command = - WireCommands::GetSegmentAttribute(GetSegmentAttributeCommand { - request_id: 1, - segment_name, - attribute_id, - delegation_token: token, - }); - test_command(get_segment_attribute_command); - } - - #[test] - fn test_segment_attribute() { - let segment_attribute_command = WireCommands::SegmentAttribute(SegmentAttributeCommand { - request_id: 1, - value: 0, - }); - test_command(segment_attribute_command); - } - - #[test] - fn test_update_segment_attribute() { - let segment_name = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); - let attribute_id: u128 = 123; - let update_segment_attribute = - WireCommands::UpdateSegmentAttribute(UpdateSegmentAttributeCommand { - request_id: 1, - segment_name, - attribute_id, - new_value: 2, - expected_value: 2, - delegation_token: token, - }); - test_command(update_segment_attribute); - } - - #[test] - fn test_segment_attribute_updated() { - let segment_attribute_updated = - WireCommands::SegmentAttributeUpdated(SegmentAttributeUpdatedCommand { - request_id: 1, - success: true, - }); - test_command(segment_attribute_updated); - } - - #[test] - fn test_get_stream_segment_info() { - let segment_name = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); - let get_stream_segment_info = - WireCommands::GetStreamSegmentInfo(GetStreamSegmentInfoCommand { - request_id: 1, - segment_name, - delegation_token: token, - }); - test_command(get_stream_segment_info); - } - - #[test] - fn test_stream_segment_info() { - let segment_name = JavaString(String::from("segment-1")); - let stream_segment_info = WireCommands::StreamSegmentInfo(StreamSegmentInfoCommand { - request_id: 0, - segment_name, - exists: false, - is_sealed: false, - is_deleted: false, - last_modified: 0, - write_offset: 0, - start_offset: 0, - }); - test_command(stream_segment_info); - } - - #[test] - fn test_create_segment() { - let segment_name = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); - let create_segment_command = WireCommands::CreateSegment(CreateSegmentCommand { - request_id: 1, - segment: segment_name, - target_rate: 1, - scale_type: 0, - delegation_token: token, - }); - test_command(create_segment_command); - } - - #[test] - fn test_create_table_segment() { - let segment_name = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); - let create_table_segment_command = - WireCommands::CreateTableSegment(CreateTableSegmentCommand { - request_id: 1, - segment: segment_name, - delegation_token: token, - }); - test_command(create_table_segment_command); - } - - #[test] - fn test_segment_created() { - let segment_name = JavaString(String::from("segment-1")); - let segment_created_cmd = WireCommands::SegmentCreated(SegmentCreatedCommand { - request_id: 1, - segment: segment_name, - }); - test_command(segment_created_cmd); - } - - #[test] - fn test_update_segment_policy() { - let segment_name = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); - let update_segment_policy_cmd = - WireCommands::UpdateSegmentPolicy(UpdateSegmentPolicyCommand { - request_id: 1, - segment: segment_name, - target_rate: 1, - scale_type: 0, - delegation_token: token, - }); - test_command(update_segment_policy_cmd); - } - - #[test] - fn test_segment_policy_updated() { - let segment_name = JavaString(String::from("segment-1")); - let segment_policy_updated = - WireCommands::SegmentPolicyUpdated(SegmentPolicyUpdatedCommand { - request_id: 0, - segment: segment_name, - }); - test_command(segment_policy_updated); - } - - #[test] - fn test_merge_segment() { - let target = JavaString(String::from("segment-1")); - let source = JavaString(String::from("segment-2")); - let token = JavaString(String::from("delegation_token")); - let merge_segment = WireCommands::MergeSegments(MergeSegmentsCommand { - request_id: 1, - target, - source, - delegation_token: token, - }); - test_command(merge_segment); - } - - #[test] - fn test_merge_table_segment() { - let target = JavaString(String::from("segment-1")); - let source = JavaString(String::from("segment-2")); - let token = JavaString(String::from("delegation_token")); - let merge_table_segment = WireCommands::MergeTableSegments(MergeTableSegmentsCommand { - request_id: 1, - target, - source, - delegation_token: token, - }); - test_command(merge_table_segment); - } - - #[test] - fn test_segment_merged() { - let target = JavaString(String::from("segment-1")); - let source = JavaString(String::from("segment-2")); - let segment_merged = WireCommands::SegmentsMerged(SegmentsMergedCommand { - request_id: 1, - target, - source, - new_target_write_offset: 10, - }); - test_command(segment_merged); - } - - #[test] - fn test_seal_segment() { - let segment_name = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); - let seal_segment = WireCommands::SealSegment(SealSegmentCommand { - request_id: 1, - segment: segment_name, - delegation_token: token, - }); - test_command(seal_segment); - } - - #[test] - fn test_seal_table_segment() { - let segment_name = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); - let seal_table_segment = WireCommands::SealTableSegment(SealTableSegmentCommand { - request_id: 1, - segment: segment_name, - delegation_token: token, - }); - test_command(seal_table_segment); - } - - #[test] - fn test_segment_sealed() { - let segment_name = JavaString(String::from("segment-1")); - let segment_sealed = WireCommands::SegmentSealed(SegmentSealedCommand { - request_id: 1, - segment: segment_name, - }); - test_command(segment_sealed); - } - - #[test] - fn test_truncate_segment() { - let segment_name = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); - let truncate_segment = WireCommands::TruncateSegment(TruncateSegmentCommand { - request_id: 1, - segment: segment_name, - truncation_offset: 10, - delegation_token: token, - }); - test_command(truncate_segment); - } - - #[test] - fn test_segment_truncated() { - let segment_name = JavaString(String::from("segment-1")); - let segment_truncated = WireCommands::SegmentTruncated(SegmentTruncatedCommand { - request_id: 1, - segment: segment_name, - }); - test_command(segment_truncated); - } - - #[test] - fn test_delete_segment() { - let segment_name = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); - let delete_segment_command = WireCommands::DeleteSegment(DeleteSegmentCommand { - request_id: 1, - segment: segment_name, - delegation_token: token, - }); - test_command(delete_segment_command); - } - - #[test] - fn test_segment_deleted() { - let segment = JavaString(String::from("segment-1")); - let segment_deleted = WireCommands::SegmentDeleted(SegmentDeletedCommand { - request_id: 1, - segment, - }); - test_command(segment_deleted); - } - - #[test] - fn test_delete_table_segment() { - let segment_name = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); - let delete_table_segment = WireCommands::DeleteTableSegment(DeleteTableSegmentCommand { - request_id: 0, - segment: segment_name, - must_be_empty: true, - delegation_token: token, - }); - test_command(delete_table_segment); - } - - #[test] - fn test_keep_alive() { - let keep_alive = WireCommands::KeepAlive(KeepAliveCommand {}); - test_command(keep_alive); - } - - #[test] - fn test_auth_checked_failed() { - let stack_trace = JavaString(String::from("some exception")); - let auth_checked_failed = WireCommands::AuthTokenCheckFailed(AuthTokenCheckFailedCommand { - request_id: 1, - server_stack_trace: stack_trace, - error_code: -1, - }); - - let decode_command = test_command(auth_checked_failed); - if let WireCommands::AuthTokenCheckFailed(command) = decode_command { - assert_eq!(command.is_token_expired(), false); - assert_eq!(command.get_error_code(), ErrorCode::Unspecified); - } - } - - #[test] - fn test_update_table_entries() { - let mut entries = Vec::<(TableKey, TableValue)>::new(); - let key_data = String::from("key-1").into_bytes(); - let value_data = String::from("value-1").into_bytes(); - entries.push((TableKey::new(key_data, 1), TableValue::new(value_data))); - let table_entries = TableEntries { entries }; - let segment_name = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); - let _size = table_entries.size(); - let update_table_entries = WireCommands::UpdateTableEntries(UpdateTableEntriesCommand { - request_id: 1, - segment: segment_name, - delegation_token: token, - table_entries, - }); - - test_command(update_table_entries); - } - - #[test] - fn test_table_entries_updated() { - let updated_versions: Vec = vec![1, 2, 3, 4]; - let table_entries_updated = WireCommands::TableEntriesUpdated(TableEntriesUpdatedCommand { - request_id: 1, - updated_versions, - }); - test_command(table_entries_updated); - } - - #[test] - fn test_remove_table_keys() { - let segment = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); - let mut keys = Vec::<(TableKey)>::new(); - let key_data = String::from("key-1").into_bytes(); - keys.push(TableKey::new(key_data, 1)); - let remove_table_keys_command = WireCommands::RemoveTableKeys(RemoveTableKeysCommand { - request_id: 1, - segment, - delegation_token: token, - keys, - }); - test_command(remove_table_keys_command); - } - - #[test] - fn test_table_keys_removed() { - let segment = JavaString(String::from("segment-1")); - let table_key_removed = WireCommands::TableKeysRemoved(TableKeysRemovedCommand { - request_id: 1, - segment, - }); - test_command(table_key_removed); - } - - #[test] - fn test_read_table() { - let segment = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); - let mut keys = Vec::<(TableKey)>::new(); - let key_data = String::from("key-1").into_bytes(); - keys.push(TableKey::new(key_data, 1)); - let read_table_command = WireCommands::ReadTable(ReadTableCommand { - request_id: 1, - segment, - delegation_token: token, - keys, - }); - - test_command(read_table_command); - } - - #[test] - fn test_table_read() { - let mut entries = Vec::<(TableKey, TableValue)>::new(); - let key_data = String::from("key-1").into_bytes(); - let value_data = String::from("value-1").into_bytes(); - entries.push((TableKey::new(key_data, 1), TableValue::new(value_data))); - let table_entries = TableEntries { entries }; - let segment_name = JavaString(String::from("segment-1")); - let table_read = WireCommands::TableRead(TableReadCommand { - request_id: 1, - segment: segment_name, - entries: table_entries, - }); - - test_command(table_read); - } - - #[test] - fn test_read_table_keys() { - let segment_name = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); - let continuation_token: Vec = vec![1, 2, 3]; - let read_table_keys = WireCommands::ReadTableKeys(ReadTableKeysCommand { - request_id: 0, - segment: segment_name, - delegation_token: token, - suggested_key_count: 3, - continuation_token, - }); - test_command(read_table_keys); - } - - #[test] - fn test_table_keys_read() { - let segment = JavaString(String::from("segment-1")); - let mut keys = Vec::<(TableKey)>::new(); - let key_data = String::from("key-1").into_bytes(); - keys.push(TableKey::new(key_data, 1)); - let continuation_token: Vec = vec![1, 2, 3]; - let table_keys_read_command = WireCommands::TableKeysRead(TableKeysReadCommand { - request_id: 1, - segment, - keys, - continuation_token, - }); - test_command(table_keys_read_command); - } - - #[test] - fn test_read_table_entries() { - let segment_name = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); - let continuation_token: Vec = vec![1, 2, 3]; - let read_table_entries = WireCommands::ReadTableEntries(ReadTableEntriesCommand { - request_id: 0, - segment: segment_name, - delegation_token: token, - suggested_entry_count: 3, - continuation_token, - }); - test_command(read_table_entries); - } - - #[test] - fn test_table_entries_read() { - let segment_name = JavaString(String::from("segment-1")); - let continuation_token: Vec = vec![1, 2, 3]; - let mut entries = Vec::<(TableKey, TableValue)>::new(); - let key_data = String::from("key-1").into_bytes(); - let value_data = String::from("value-1").into_bytes(); - entries.push((TableKey::new(key_data, 1), TableValue::new(value_data))); - let table_entries = TableEntries { entries }; - let table_entries_read = WireCommands::TableEntriesRead(TableEntriesReadCommand { - request_id: 1, - segment: segment_name, - entries: table_entries, - continuation_token, - }); - test_command(table_entries_read); - } - - #[test] - fn table_key_does_not_exist() { - let segment_name = JavaString(String::from("segment-1")); - let stack_trace = JavaString(String::from("some exception")); - let table_key_does_not_exist = - WireCommands::TableKeyDoesNotExist(TableKeyDoesNotExistCommand { - request_id: 0, - segment: segment_name, - server_stack_trace: stack_trace, - }); - test_command(table_key_does_not_exist); - } - - #[test] - fn table_key_bad_version() { - let segment_name = JavaString(String::from("segment-1")); - let stack_trace = JavaString(String::from("some exception")); - let table_key_bad_version = WireCommands::TableKeyBadVersion(TableKeyBadVersionCommand { - request_id: 0, - segment: segment_name, - server_stack_trace: stack_trace, - }); - test_command(table_key_bad_version); - } - - fn test_command(command: WireCommands) -> WireCommands { - let encoded: Vec = command.write_fields(); - let decoded = WireCommands::read_from(&encoded); - assert_eq!(command, decoded); - decoded - } -} From 6967b816740934ece4d3ede42751e2bd06266ef9 Mon Sep 17 00:00:00 2001 From: Tom Kaitchuck Date: Mon, 6 Jan 2020 09:46:14 -0800 Subject: [PATCH 06/30] Fix docs examples --- src/lib.rs | 2 +- src/wire_protocol/connection_factory.rs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4cb6bbd6c..56a74e685 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,7 +11,7 @@ #[macro_use] extern crate lazy_static; -mod wire_protocol { +pub mod wire_protocol { mod commands; mod wire_commands; pub mod connection_factory; diff --git a/src/wire_protocol/connection_factory.rs b/src/wire_protocol/connection_factory.rs index c12182c0c..d4e82b3a0 100644 --- a/src/wire_protocol/connection_factory.rs +++ b/src/wire_protocol/connection_factory.rs @@ -61,8 +61,8 @@ pub trait ConnectionFactory { /// /// ```no_run /// use std::net::SocketAddr; - /// use pravega_client_rust::connection_factory; - /// use pravega_client_rust::connection_factory::ConnectionFactory; + /// use pravega_client_rust::wire_protocol::connection_factory; + /// use pravega_client_rust::wire_protocol::connection_factory::ConnectionFactory; /// use tokio::runtime::Runtime; /// /// fn main() { @@ -89,8 +89,8 @@ pub trait Connection { /// /// ```no_run /// use std::net::SocketAddr; - /// use pravega_client_rust::connection_factory; - /// use pravega_client_rust::connection_factory::ConnectionFactory; + /// use pravega_client_rust::wire_protocol::connection_factory; + /// use pravega_client_rust::wire_protocol::connection_factory::ConnectionFactory; /// use tokio::runtime::Runtime; /// /// fn main() { From 3b243ebfeb90d1d4294c4a9edd137fb918a1a16b Mon Sep 17 00:00:00 2001 From: Tom Kaitchuck Date: Mon, 6 Jan 2020 11:58:18 -0800 Subject: [PATCH 07/30] Add comment --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index 56a74e685..e3697b9d7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,6 +14,7 @@ extern crate lazy_static; pub mod wire_protocol { mod commands; mod wire_commands; + //Public for docs to build. (TODO: Is there a better way to do this?) pub mod connection_factory; #[cfg(test)] mod tests; From e201118f804356e27b04647502edcc660497f59a Mon Sep 17 00:00:00 2001 From: Tom Kaitchuck Date: Mon, 6 Jan 2020 12:31:46 -0800 Subject: [PATCH 08/30] formating --- controller-client/src/lib.rs | 5 +--- controller-client/src/main.rs | 3 +-- controller-client/src/test.rs | 1 - src/wire_protocol/tests.rs | 51 ++++++++++++++++------------------- 4 files changed, 25 insertions(+), 35 deletions(-) diff --git a/controller-client/src/lib.rs b/controller-client/src/lib.rs index 9156675b7..8e69c9121 100644 --- a/controller-client/src/lib.rs +++ b/controller-client/src/lib.rs @@ -17,8 +17,7 @@ mod controller { mod test; pub use controller::{ - controller_service_client::ControllerServiceClient as ControllerServiceClient, - scaling_policy::ScalingPolicyType as ScalingPolicyType, + controller_service_client::ControllerServiceClient, scaling_policy::ScalingPolicyType, CreateScopeStatus, CreateStreamStatus, ScalingPolicy, ScopeInfo, StreamConfig, StreamInfo, }; use tonic::transport::channel::Channel; @@ -55,5 +54,3 @@ pub async fn create_stream( .expect("Failed to create Stream"); op_status.into_inner() // return create Stream status } - - diff --git a/controller-client/src/main.rs b/controller-client/src/main.rs index aed4509fa..ff46f0ebc 100644 --- a/controller-client/src/main.rs +++ b/controller-client/src/main.rs @@ -1,4 +1,3 @@ - use pravega_controller_client::*; #[tokio::main] @@ -28,4 +27,4 @@ async fn main() -> Result<(), Box> { println!("Response 2 for create_stream is {:?}", response2); Ok(()) -} \ No newline at end of file +} diff --git a/controller-client/src/test.rs b/controller-client/src/test.rs index eef2fbd67..fc569f259 100644 --- a/controller-client/src/test.rs +++ b/controller-client/src/test.rs @@ -1,4 +1,3 @@ - // Note this useful idiom: importing names from outer (for mod tests) scope. use super::*; use tokio::runtime::Runtime; diff --git a/src/wire_protocol/tests.rs b/src/wire_protocol/tests.rs index a389b9307..3f41829cd 100644 --- a/src/wire_protocol/tests.rs +++ b/src/wire_protocol/tests.rs @@ -198,14 +198,13 @@ fn test_conditional_append() { let writer_id_number: u128 = 123; let data = String::from("event-1").into_bytes(); let event = EventCommand { data }; - let conditional_append_command = - WireCommands::ConditionalAppend(ConditionalAppendCommand { - writer_id: writer_id_number, - event_number: 1, - expected_offset: 0, - event, - request_id: 1, - }); + let conditional_append_command = WireCommands::ConditionalAppend(ConditionalAppendCommand { + writer_id: writer_id_number, + event_number: 1, + expected_offset: 0, + event, + request_id: 1, + }); let decoded = test_command(conditional_append_command); if let WireCommands::ConditionalAppend(command) = decoded { @@ -338,12 +337,11 @@ fn test_segment_attribute_updated() { fn test_get_stream_segment_info() { let segment_name = JavaString(String::from("segment-1")); let token = JavaString(String::from("delegation_token")); - let get_stream_segment_info = - WireCommands::GetStreamSegmentInfo(GetStreamSegmentInfoCommand { - request_id: 1, - segment_name, - delegation_token: token, - }); + let get_stream_segment_info = WireCommands::GetStreamSegmentInfo(GetStreamSegmentInfoCommand { + request_id: 1, + segment_name, + delegation_token: token, + }); test_command(get_stream_segment_info); } @@ -404,25 +402,23 @@ fn test_segment_created() { fn test_update_segment_policy() { let segment_name = JavaString(String::from("segment-1")); let token = JavaString(String::from("delegation_token")); - let update_segment_policy_cmd = - WireCommands::UpdateSegmentPolicy(UpdateSegmentPolicyCommand { - request_id: 1, - segment: segment_name, - target_rate: 1, - scale_type: 0, - delegation_token: token, - }); + let update_segment_policy_cmd = WireCommands::UpdateSegmentPolicy(UpdateSegmentPolicyCommand { + request_id: 1, + segment: segment_name, + target_rate: 1, + scale_type: 0, + delegation_token: token, + }); test_command(update_segment_policy_cmd); } #[test] fn test_segment_policy_updated() { let segment_name = JavaString(String::from("segment-1")); - let segment_policy_updated = - WireCommands::SegmentPolicyUpdated(SegmentPolicyUpdatedCommand { - request_id: 0, - segment: segment_name, - }); + let segment_policy_updated = WireCommands::SegmentPolicyUpdated(SegmentPolicyUpdatedCommand { + request_id: 0, + segment: segment_name, + }); test_command(segment_policy_updated); } @@ -766,4 +762,3 @@ fn test_command(command: WireCommands) -> WireCommands { assert_eq!(command, decoded); decoded } - From 8e2ddbc077c41a41fedc2967b0209d923c399c68 Mon Sep 17 00:00:00 2001 From: Wenxiao Zhang Date: Tue, 7 Jan 2020 13:55:06 -0800 Subject: [PATCH 09/30] save the change --- src/commands.rs | 61 +++++++++++++++++++------------------- src/connection_factory.rs | 44 +++++++-------------------- src/error.rs | 49 ++++++++++++++++++++++++++++++ src/lib.rs | 4 ++- tests/test_wirecommands.rs | 6 ++-- 5 files changed, 97 insertions(+), 67 deletions(-) create mode 100644 src/error.rs diff --git a/src/commands.rs b/src/commands.rs index 9fcbaab05..1fa82e938 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -7,13 +7,14 @@ use std::fmt; use std::i64; use std::io::Cursor; use std::io::{Read, Write}; +use crate::error::SerializeError; /** * trait for Command. */ pub trait Command { const TYPE_CODE: i32; - fn write_fields(&self) -> Vec; + fn write_fields(&self) -> Result, SerializeError>; fn read_from(input: &[u8]) -> Self; } @@ -135,7 +136,7 @@ pub struct HelloCommand { impl Command for HelloCommand { const TYPE_CODE: i32 = -127; - fn write_fields(&self) -> Vec { + fn write_fields(&self) -> Result, SerializeError> { let encoded = CONFIG.serialize(&self).unwrap(); encoded } @@ -171,7 +172,7 @@ pub struct WrongHostCommand { impl Command for WrongHostCommand { const TYPE_CODE: i32 = 50; - fn write_fields(&self) -> Vec { + fn write_fields(&self) -> Result, SerializeError> { let encoded = CONFIG.serialize(&self).unwrap(); encoded } @@ -203,7 +204,7 @@ pub struct SegmentIsSealedCommand { impl Command for SegmentIsSealedCommand { const TYPE_CODE: i32 = 51; - fn write_fields(&self) -> Vec { + fn write_fields(&self) -> Result, SerializeError> { let encoded = CONFIG.serialize(&self).unwrap(); encoded } @@ -237,7 +238,7 @@ pub struct SegmentIsTruncatedCommand { impl Command for SegmentIsTruncatedCommand { const TYPE_CODE: i32 = 56; - fn write_fields(&self) -> Vec { + fn write_fields(&self) -> Result, SerializeError> { let encoded = CONFIG.serialize(&self).unwrap(); encoded } @@ -269,7 +270,7 @@ pub struct SegmentAlreadyExistsCommand { impl Command for SegmentAlreadyExistsCommand { const TYPE_CODE: i32 = 52; - fn write_fields(&self) -> Vec { + fn write_fields(&self) -> Result, SerializeError> { let encoded = CONFIG.serialize(&self).unwrap(); encoded } @@ -308,7 +309,7 @@ pub struct NoSuchSegmentCommand { impl Command for NoSuchSegmentCommand { const TYPE_CODE: i32 = 53; - fn write_fields(&self) -> Vec { + fn write_fields(&self) -> Result, SerializeError> { let encoded = CONFIG.serialize(&self).unwrap(); encoded } @@ -346,7 +347,7 @@ pub struct TableSegmentNotEmptyCommand { impl Command for TableSegmentNotEmptyCommand { const TYPE_CODE: i32 = 80; - fn write_fields(&self) -> Vec { + fn write_fields(&self) -> Result, SerializeError> { let encoded = CONFIG.serialize(&self).unwrap(); encoded } @@ -384,7 +385,7 @@ pub struct InvalidEventNumberCommand { impl Command for InvalidEventNumberCommand { const TYPE_CODE: i32 = 55; - fn write_fields(&self) -> Vec { + fn write_fields(&self) -> Result, SerializeError> { let encoded = CONFIG.serialize(&self).unwrap(); encoded } @@ -426,7 +427,7 @@ pub struct OperationUnsupportedCommand { impl Command for OperationUnsupportedCommand { const TYPE_CODE: i32 = 57; - fn write_fields(&self) -> Vec { + fn write_fields(&self) -> Result, SerializeError> { let encoded = CONFIG.serialize(&self).unwrap(); encoded } @@ -457,7 +458,7 @@ pub struct PaddingCommand { impl Command for PaddingCommand { const TYPE_CODE: i32 = -1; - fn write_fields(&self) -> Vec { + fn write_fields(&self) -> Result, SerializeError> { let mut res = Vec::new(); for _i in 0..(self.length / 8) { res.write_i64::(0).unwrap(); @@ -487,7 +488,7 @@ pub struct PartialEventCommand { impl Command for PartialEventCommand { const TYPE_CODE: i32 = -2; - fn write_fields(&self) -> Vec { + fn write_fields(&self) -> Result, SerializeError> { //FIXME: In java, we use data.getBytes(data.readerIndex(), (OutputStream) out, data.readableBytes()); // which means the result would not contain the prefix length; // so in rust we can directly return data. @@ -511,7 +512,7 @@ pub struct EventCommand { impl Command for EventCommand { const TYPE_CODE: i32 = 0; - fn write_fields(&self) -> Vec { + fn write_fields(&self) -> Result, SerializeError> { let mut res = Vec::new(); res.write_i32::(EventCommand::TYPE_CODE).unwrap(); let encoded = CONFIG.serialize(&self).unwrap(); @@ -540,7 +541,7 @@ pub struct SetupAppendCommand { impl Command for SetupAppendCommand { const TYPE_CODE: i32 = 1; - fn write_fields(&self) -> Vec { + fn write_fields(&self) -> Result, SerializeError> { let encoded = CONFIG.serialize(&self).unwrap(); encoded } @@ -570,7 +571,7 @@ impl Command for AppendBlockCommand { const TYPE_CODE: i32 = 3; //FIXME: The serialize and deserialize method need to customize; // In JAVA, it doesn't write data(because it'empty), but here it will write the prefix length(0). - fn write_fields(&self) -> Vec { + fn write_fields(&self) -> Result, SerializeError> { let encoded = CONFIG.serialize(&self).unwrap(); encoded } @@ -597,7 +598,7 @@ pub struct AppendBlockEndCommand { impl Command for AppendBlockEndCommand { const TYPE_CODE: i32 = 4; - fn write_fields(&self) -> Vec { + fn write_fields(&self) -> Result, SerializeError> { let encoded = CONFIG.serialize(&self).unwrap(); encoded } @@ -635,7 +636,7 @@ impl Command for ConditionalAppendCommand { const TYPE_CODE: i32 = 5; // Customize the serialize and deserialize method. // Because in CondtionalAppend the event should be serialize as |type_code|length|data| - fn write_fields(&self) -> Vec { + fn write_fields(&self) -> Result, SerializeError> { let mut res = Vec::new(); res.write_u128::(self.writer_id).unwrap(); res.write_i64::(self.event_number).unwrap(); @@ -682,7 +683,7 @@ pub struct AppendSetupCommand { impl Command for AppendSetupCommand { const TYPE_CODE: i32 = 2; - fn write_fields(&self) -> Vec { + fn write_fields(&self) -> Result, SerializeError> { let encoded = CONFIG.serialize(&self).unwrap(); encoded } @@ -714,7 +715,7 @@ pub struct DataAppendedCommand { impl Command for DataAppendedCommand { const TYPE_CODE: i32 = 7; - fn write_fields(&self) -> Vec { + fn write_fields(&self) -> Result, SerializeError> { let encoded = CONFIG.serialize(&self).unwrap(); encoded } @@ -744,7 +745,7 @@ pub struct ConditionalCheckFailedCommand { impl Command for ConditionalCheckFailedCommand { const TYPE_CODE: i32 = 8; - fn write_fields(&self) -> Vec { + fn write_fields(&self) -> Result, SerializeError> { let encoded = CONFIG.serialize(&self).unwrap(); encoded } @@ -776,7 +777,7 @@ pub struct ReadSegmentCommand { impl Command for ReadSegmentCommand { const TYPE_CODE: i32 = 9; - fn write_fields(&self) -> Vec { + fn write_fields(&self) -> Result, SerializeError> { let encoded = CONFIG.serialize(&self).unwrap(); encoded } @@ -809,7 +810,7 @@ pub struct SegmentReadCommand { impl Command for SegmentReadCommand { const TYPE_CODE: i32 = 10; - fn write_fields(&self) -> Vec { + fn write_fields(&self) -> Result, SerializeError> { let encoded = CONFIG.serialize(&self).unwrap(); encoded } @@ -840,7 +841,7 @@ pub struct GetSegmentAttributeCommand { impl Command for GetSegmentAttributeCommand { const TYPE_CODE: i32 = 34; - fn write_fields(&self) -> Vec { + fn write_fields(&self) -> Result, SerializeError> { let encoded = CONFIG.serialize(&self).unwrap(); encoded } @@ -869,7 +870,7 @@ pub struct SegmentAttributeCommand { impl Command for SegmentAttributeCommand { const TYPE_CODE: i32 = 35; - fn write_fields(&self) -> Vec { + fn write_fields(&self) -> Result, SerializeError> { let encoded = CONFIG.serialize(&self).unwrap(); encoded } @@ -902,7 +903,7 @@ pub struct UpdateSegmentAttributeCommand { impl Command for UpdateSegmentAttributeCommand { const TYPE_CODE: i32 = 36; - fn write_fields(&self) -> Vec { + fn write_fields(&self) -> Result, SerializeError> { let encoded = CONFIG.serialize(&self).unwrap(); encoded } @@ -931,7 +932,7 @@ pub struct SegmentAttributeUpdatedCommand { impl Command for SegmentAttributeUpdatedCommand { const TYPE_CODE: i32 = 37; - fn write_fields(&self) -> Vec { + fn write_fields(&self) -> Result, SerializeError> { let encoded = CONFIG.serialize(&self).unwrap(); encoded } @@ -961,7 +962,7 @@ pub struct GetStreamSegmentInfoCommand { impl Command for GetStreamSegmentInfoCommand { const TYPE_CODE: i32 = 11; - fn write_fields(&self) -> Vec { + fn write_fields(&self) -> Result, SerializeError> { let encoded = CONFIG.serialize(&self).unwrap(); encoded } @@ -996,7 +997,7 @@ pub struct StreamSegmentInfoCommand { impl Command for StreamSegmentInfoCommand { const TYPE_CODE: i32 = 12; - fn write_fields(&self) -> Vec { + fn write_fields(&self) -> Result, SerializeError> { let encoded = CONFIG.serialize(&self).unwrap(); encoded } @@ -1028,7 +1029,7 @@ pub struct CreateSegmentCommand { impl Command for CreateSegmentCommand { const TYPE_CODE: i32 = 20; - fn write_fields(&self) -> Vec { + fn write_fields(&self) -> Result, SerializeError> { let encoded = CONFIG.serialize(&self).unwrap(); encoded } @@ -1058,7 +1059,7 @@ pub struct CreateTableSegmentCommand { impl Command for CreateTableSegmentCommand { const TYPE_CODE: i32 = 70; - fn write_fields(&self) -> Vec { + fn write_fields(&self) -> Result, SerializeError> { let encoded = CONFIG.serialize(&self).unwrap(); encoded } diff --git a/src/connection_factory.rs b/src/connection_factory.rs index a99802493..981c523ad 100644 --- a/src/connection_factory.rs +++ b/src/connection_factory.rs @@ -8,11 +8,15 @@ // http://www.apache.org/licenses/LICENSE-2.0 // use async_trait::async_trait; -use snafu::{ResultExt, Snafu}; +use snafu::{ResultExt}; use std::fmt; use std::net::SocketAddr; use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::net::TcpStream; +use crate::error::ConnectionError; +use crate::error::Connect; +use crate::error::SendData; +use crate::error::ReadData; #[allow(dead_code)] #[derive(Debug)] @@ -26,32 +30,6 @@ impl fmt::Display for ConnectionType { } } -#[derive(Debug, Snafu)] -pub enum Error { - #[snafu(display( - "Could not connect to endpoint {} using connection type {}", - endpoint, - connection_type - ))] - Connect { - connection_type: ConnectionType, - endpoint: SocketAddr, - source: std::io::Error, - }, - #[snafu(display("Could not send data to {} asynchronously", endpoint))] - SendData { - endpoint: SocketAddr, - source: std::io::Error, - }, - #[snafu(display("Could not read data from {} asynchronously", endpoint))] - ReadData { - endpoint: SocketAddr, - source: std::io::Error, - }, -} - -type Result = std::result::Result; - /// ConnectionFactory trait is the factory used to establish the TCP connection with remote servers. #[async_trait] pub trait ConnectionFactory { @@ -77,7 +55,7 @@ pub trait ConnectionFactory { &self, connection_type: ConnectionType, endpoint: SocketAddr, - ) -> Result>; + ) -> Result, ConnectionError>; } /// Connection can send and read data using a TCP connection @@ -103,7 +81,7 @@ pub trait Connection { /// let fut = connection.send_async(&payload); /// } /// ``` - async fn send_async(&mut self, payload: &[u8]) -> Result<()>; + async fn send_async(&mut self, payload: &[u8]) -> Result<(), ConnectionError>; /// read_async will read exactly the amount of data needed to fill the provided buffer asynchronously. /// @@ -125,7 +103,7 @@ pub trait Connection { /// let fut = connection.read_async(&mut buf); /// } /// ``` - async fn read_async(&mut self, buf: &mut [u8]) -> Result<()>; + async fn read_async(&mut self, buf: &mut [u8]) -> Result<(), ConnectionError>; } pub struct ConnectionFactoryImpl {} @@ -136,7 +114,7 @@ impl ConnectionFactory for ConnectionFactoryImpl { &self, connection_type: ConnectionType, endpoint: SocketAddr, - ) -> Result> { + ) -> Result, ConnectionError> { match connection_type { ConnectionType::Tokio => { let stream = TcpStream::connect(endpoint).await.context(Connect { @@ -158,7 +136,7 @@ pub struct TokioConnection { #[async_trait] impl Connection for TokioConnection { - async fn send_async(&mut self, payload: &[u8]) -> Result<()> { + async fn send_async(&mut self, payload: &[u8]) -> Result<(), ConnectionError> { let endpoint = self.endpoint; self.stream .write_all(payload) @@ -167,7 +145,7 @@ impl Connection for TokioConnection { Ok(()) } - async fn read_async(&mut self, buf: &mut [u8]) -> Result<()> { + async fn read_async(&mut self, buf: &mut [u8]) -> Result<(), ConnectionError> { let endpoint = self.endpoint; self.stream .read_exact(buf) diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 000000000..9a5b11df8 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,49 @@ +use std::net::SocketAddr; +use bincode::Error as bincodeError; +use std::io::Error as IOError; +use snafu::{Snafu}; +use crate::commands; +use crate::connection_factory::ConnectionType; + + +/// This kind of error that can be produced during Pravega client connecting to server. +#[derive(Debug, Snafu)] +#[snafu(visibility = "pub(crate)")] +pub enum ConnectionError { + #[snafu(display( + "Could not connect to endpoint {} using connection type {}", + endpoint, + connection_type + ))] + Connect { + connection_type: ConnectionType, + endpoint: SocketAddr, + source: std::io::Error, + }, + #[snafu(display("Could not send data to {} asynchronously", endpoint))] + SendData { + endpoint: SocketAddr, + source: std::io::Error, + }, + #[snafu(display("Could not read data from {} asynchronously", endpoint))] + ReadData { + endpoint: SocketAddr, + source: std::io::Error, + } +} + +/// This kind of error that can be produced during Pravega serialize the wire commands. +#[derive(Debug, Snafu)] +pub enum SerializeError { + #[snafu(display("Could not serialize command {} because of: {}", commandType, source))] + InvalidData { + command_type: u8, + source: bincodeError + }, + #[snafu(display("Could not serialize command {} because of: {}", commandType, source))] + Io { + command_type: u8, + source: IOError + } +} + diff --git a/src/lib.rs b/src/lib.rs index 53fb7256f..2dd146d0a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,5 +11,7 @@ #[macro_use] extern crate lazy_static; pub mod commands; -pub mod connection_factory; pub mod wirecommands; +mod error; +pub mod connection_factory; + diff --git a/tests/test_wirecommands.rs b/tests/test_wirecommands.rs index eb2272a38..d062c3856 100644 --- a/tests/test_wirecommands.rs +++ b/tests/test_wirecommands.rs @@ -619,7 +619,7 @@ mod tests { fn test_remove_table_keys() { let segment = JavaString(String::from("segment-1")); let token = JavaString(String::from("delegation_token")); - let mut keys = Vec::<(TableKey)>::new(); + let mut keys = Vec::::new(); let key_data = String::from("key-1").into_bytes(); keys.push(TableKey::new(key_data, 1)); let remove_table_keys_command = WireCommands::RemoveTableKeys(RemoveTableKeysCommand { @@ -645,7 +645,7 @@ mod tests { fn test_read_table() { let segment = JavaString(String::from("segment-1")); let token = JavaString(String::from("delegation_token")); - let mut keys = Vec::<(TableKey)>::new(); + let mut keys = Vec::::new(); let key_data = String::from("key-1").into_bytes(); keys.push(TableKey::new(key_data, 1)); let read_table_command = WireCommands::ReadTable(ReadTableCommand { @@ -693,7 +693,7 @@ mod tests { #[test] fn test_table_keys_read() { let segment = JavaString(String::from("segment-1")); - let mut keys = Vec::<(TableKey)>::new(); + let mut keys = Vec::::new(); let key_data = String::from("key-1").into_bytes(); keys.push(TableKey::new(key_data, 1)); let continuation_token: Vec = vec![1, 2, 3]; From a5186db20046c5884b52a02b60c50e2d3a6df4d5 Mon Sep 17 00:00:00 2001 From: Wenxiao Zhang Date: Wed, 8 Jan 2020 15:14:43 -0800 Subject: [PATCH 10/30] finish serialize error handling --- Cargo.toml | 2 +- controller-client/Cargo.toml | 2 +- src/lib.rs | 1 + src/wire_protocol/commands.rs | 123 +++---- src/wire_protocol/connection_factory.rs | 1 - src/wire_protocol/error.rs | 20 +- src/wire_protocol/tests.rs | 8 +- src/wire_protocol/wire_commands.rs | 408 +++++++++++------------- 8 files changed, 257 insertions(+), 308 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5e9060de9..fbe7c2199 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ log = "0.4" serde = { version = "1.0", features = ["derive"] } serde_repr = "0.1" snafu = "0.5.0" -tokio = { version = "0.2.6", features = ["full"] } +tokio = { version = "0.2.8", features = ["full"] } [dev-dependencies] tokio-test = "0.2" diff --git a/controller-client/Cargo.toml b/controller-client/Cargo.toml index f9daea996..1a0edb103 100644 --- a/controller-client/Cargo.toml +++ b/controller-client/Cargo.toml @@ -12,7 +12,7 @@ http = "0.1" log = "0.4" prost = "0.5" snafu = "0.5.0" -tokio = { version = "0.2.6", features = ["full"] } +tokio = { version = "0.2.8", features = ["full"] } [build-dependencies] tonic-build = "0.1.0-beta.1" diff --git a/src/lib.rs b/src/lib.rs index 7b2e1a3ba..fa599e2fb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,7 @@ #[macro_use] extern crate lazy_static; +#[allow(dead_code)] pub mod wire_protocol { mod commands; mod wire_commands; diff --git a/src/wire_protocol/commands.rs b/src/wire_protocol/commands.rs index 708fa8d19..a2724dfd8 100644 --- a/src/wire_protocol/commands.rs +++ b/src/wire_protocol/commands.rs @@ -10,6 +10,7 @@ use std::io::{Read, Write}; use snafu::ResultExt; use super::error::SerializeError; use super::error::InvalidData; +use super::error::Io; /** * trait for Command. @@ -139,7 +140,7 @@ pub struct HelloCommand { impl Command for HelloCommand { const TYPE_CODE: i32 = -127; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -175,7 +176,7 @@ pub struct WrongHostCommand { impl Command for WrongHostCommand { const TYPE_CODE: i32 = 50; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } fn read_from(input: &[u8]) -> WrongHostCommand { @@ -207,7 +208,7 @@ pub struct SegmentIsSealedCommand { impl Command for SegmentIsSealedCommand { const TYPE_CODE: i32 = 51; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -241,7 +242,7 @@ pub struct SegmentIsTruncatedCommand { impl Command for SegmentIsTruncatedCommand { const TYPE_CODE: i32 = 56; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -273,7 +274,7 @@ pub struct SegmentAlreadyExistsCommand { impl Command for SegmentAlreadyExistsCommand { const TYPE_CODE: i32 = 52; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -312,7 +313,7 @@ pub struct NoSuchSegmentCommand { impl Command for NoSuchSegmentCommand { const TYPE_CODE: i32 = 53; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -350,7 +351,7 @@ pub struct TableSegmentNotEmptyCommand { impl Command for TableSegmentNotEmptyCommand { const TYPE_CODE: i32 = 80; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -388,7 +389,7 @@ pub struct InvalidEventNumberCommand { impl Command for InvalidEventNumberCommand { const TYPE_CODE: i32 = 55; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -430,7 +431,7 @@ pub struct OperationUnsupportedCommand { impl Command for OperationUnsupportedCommand { const TYPE_CODE: i32 = 57; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -463,10 +464,10 @@ impl Command for PaddingCommand { fn write_fields(&self) -> Result, SerializeError> { let mut res = Vec::new(); for _i in 0..(self.length / 8) { - res.write_i64::(0).unwrap(); + res.write_i64::(0).context(Io{command_type: Self::TYPE_CODE})?; } for _i in 0..(self.length % 8) { - res.write_u8(0).unwrap(); + res.write_u8(0).context(Io{command_type: Self::TYPE_CODE})?; } Ok(res) } @@ -516,8 +517,8 @@ impl Command for EventCommand { const TYPE_CODE: i32 = 0; fn write_fields(&self) -> Result, SerializeError> { let mut res = Vec::new(); - res.write_i32::(EventCommand::TYPE_CODE).unwrap(); - let encoded = CONFIG.serialize(&self).unwrap(); + res.write_i32::(EventCommand::TYPE_CODE).context(Io{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; res.extend(encoded); Ok(res) } @@ -544,7 +545,7 @@ pub struct SetupAppendCommand { impl Command for SetupAppendCommand { const TYPE_CODE: i32 = 1; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -574,7 +575,7 @@ impl Command for AppendBlockCommand { //FIXME: The serialize and deserialize method need to customize; // In JAVA, it doesn't write data(because it'empty), but here it will write the prefix length(0). fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -601,7 +602,7 @@ impl Command for AppendBlockEndCommand { const TYPE_CODE: i32 = 4; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -640,11 +641,11 @@ impl Command for ConditionalAppendCommand { // Because in CondtionalAppend the event should be serialize as |type_code|length|data| fn write_fields(&self) -> Result, SerializeError> { let mut res = Vec::new(); - res.write_u128::(self.writer_id).unwrap(); - res.write_i64::(self.event_number).unwrap(); - res.write_i64::(self.expected_offset).unwrap(); - res.write_all(&self.event.write_fields()).unwrap(); - res.write_i64::(self.request_id).unwrap(); + res.write_u128::(self.writer_id).context(Io{command_type: Self::TYPE_CODE})?; + res.write_i64::(self.event_number).context(Io{command_type: Self::TYPE_CODE})?; + res.write_i64::(self.expected_offset).context(Io{command_type: Self::TYPE_CODE})?; + res.write_all(&self.event.write_fields()?).context(Io{command_type: Self::TYPE_CODE})?; + res.write_i64::(self.request_id).context(Io{command_type: Self::TYPE_CODE})?; Ok(res) } @@ -686,7 +687,7 @@ impl Command for AppendSetupCommand { const TYPE_CODE: i32 = 2; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -718,7 +719,7 @@ impl Command for DataAppendedCommand { const TYPE_CODE: i32 = 7; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -748,7 +749,7 @@ impl Command for ConditionalCheckFailedCommand { const TYPE_CODE: i32 = 8; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -780,7 +781,7 @@ impl Command for ReadSegmentCommand { const TYPE_CODE: i32 = 9; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -813,7 +814,7 @@ impl Command for SegmentReadCommand { const TYPE_CODE: i32 = 10; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -844,7 +845,7 @@ impl Command for GetSegmentAttributeCommand { const TYPE_CODE: i32 = 34; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -873,7 +874,7 @@ impl Command for SegmentAttributeCommand { const TYPE_CODE: i32 = 35; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -906,7 +907,7 @@ impl Command for UpdateSegmentAttributeCommand { const TYPE_CODE: i32 = 36; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -935,7 +936,7 @@ impl Command for SegmentAttributeUpdatedCommand { const TYPE_CODE: i32 = 37; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -965,7 +966,7 @@ impl Command for GetStreamSegmentInfoCommand { const TYPE_CODE: i32 = 11; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -1000,7 +1001,7 @@ impl Command for StreamSegmentInfoCommand { const TYPE_CODE: i32 = 12; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -1032,7 +1033,7 @@ impl Command for CreateSegmentCommand { const TYPE_CODE: i32 = 20; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -1062,7 +1063,7 @@ impl Command for CreateTableSegmentCommand { const TYPE_CODE: i32 = 70; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -1091,7 +1092,7 @@ impl Command for SegmentCreatedCommand { const TYPE_CODE: i32 = 21; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -1123,7 +1124,7 @@ impl Command for UpdateSegmentPolicyCommand { const TYPE_CODE: i32 = 32; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -1152,7 +1153,7 @@ impl Command for SegmentPolicyUpdatedCommand { const TYPE_CODE: i32 = 33; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -1183,7 +1184,7 @@ impl Command for MergeSegmentsCommand { const TYPE_CODE: i32 = 58; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -1213,7 +1214,7 @@ impl Command for MergeTableSegmentsCommand { const TYPE_CODE: i32 = 72; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -1244,7 +1245,7 @@ impl Command for SegmentsMergedCommand { const TYPE_CODE: i32 = 59; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -1274,7 +1275,7 @@ impl Command for SealSegmentCommand { const TYPE_CODE: i32 = 28; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -1304,7 +1305,7 @@ impl Command for SealTableSegmentCommand { const TYPE_CODE: i32 = 73; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -1333,7 +1334,7 @@ impl Command for SegmentSealedCommand { const TYPE_CODE: i32 = 29; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -1364,7 +1365,7 @@ impl Command for TruncateSegmentCommand { const TYPE_CODE: i32 = 38; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -1393,7 +1394,7 @@ impl Command for SegmentTruncatedCommand { const TYPE_CODE: i32 = 39; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -1423,7 +1424,7 @@ impl Command for DeleteSegmentCommand { const TYPE_CODE: i32 = 30; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -1453,7 +1454,7 @@ pub struct DeleteTableSegmentCommand { impl Command for DeleteTableSegmentCommand { const TYPE_CODE: i32 = 71; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -1482,7 +1483,7 @@ impl Command for SegmentDeletedCommand { const TYPE_CODE: i32 = 31; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -1555,7 +1556,7 @@ impl Command for AuthTokenCheckFailedCommand { const TYPE_CODE: i32 = 60; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -1611,7 +1612,7 @@ impl Command for UpdateTableEntriesCommand { const TYPE_CODE: i32 = 74; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -1640,7 +1641,7 @@ impl Command for TableEntriesUpdatedCommand { const TYPE_CODE: i32 = 75; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -1671,7 +1672,7 @@ impl Command for RemoveTableKeysCommand { const TYPE_CODE: i32 = 76; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -1700,7 +1701,7 @@ impl Command for TableKeysRemovedCommand { const TYPE_CODE: i32 = 77; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -1731,7 +1732,7 @@ impl Command for ReadTableCommand { const TYPE_CODE: i32 = 78; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -1761,7 +1762,7 @@ impl Command for TableReadCommand { const TYPE_CODE: i32 = 79; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -1793,7 +1794,7 @@ impl Command for ReadTableKeysCommand { const TYPE_CODE: i32 = 83; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -1824,7 +1825,7 @@ impl Command for TableKeysReadCommand { const TYPE_CODE: i32 = 84; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -1856,7 +1857,7 @@ impl Command for ReadTableEntriesCommand { const TYPE_CODE: i32 = 85; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -1887,7 +1888,7 @@ impl Command for TableEntriesReadCommand { const TYPE_CODE: i32 = 86; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -1917,7 +1918,7 @@ impl Command for TableKeyDoesNotExistCommand { const TYPE_CODE: i32 = 81; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } @@ -1961,7 +1962,7 @@ impl Command for TableKeyBadVersionCommand { const TYPE_CODE: i32 = 82; fn write_fields(&self) -> Result, SerializeError> { - let encoded = CONFIG.serialize(&self).unwrap(); + let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } diff --git a/src/wire_protocol/connection_factory.rs b/src/wire_protocol/connection_factory.rs index 004bbedf7..b29ee6c8e 100644 --- a/src/wire_protocol/connection_factory.rs +++ b/src/wire_protocol/connection_factory.rs @@ -18,7 +18,6 @@ use super::error::Connect; use super::error::SendData; use super::error::ReadData; -#[allow(dead_code)] #[derive(Debug)] pub enum ConnectionType { Tokio, diff --git a/src/wire_protocol/error.rs b/src/wire_protocol/error.rs index 9a5b11df8..3eafa1766 100644 --- a/src/wire_protocol/error.rs +++ b/src/wire_protocol/error.rs @@ -1,9 +1,8 @@ use std::net::SocketAddr; -use bincode::Error as bincodeError; -use std::io::Error as IOError; +use bincode::Error as BincodeError; +use std::io::Error as IoError; use snafu::{Snafu}; -use crate::commands; -use crate::connection_factory::ConnectionType; +use super::connection_factory::ConnectionType; /// This kind of error that can be produced during Pravega client connecting to server. @@ -34,16 +33,17 @@ pub enum ConnectionError { /// This kind of error that can be produced during Pravega serialize the wire commands. #[derive(Debug, Snafu)] +#[snafu(visibility = "pub(crate)")] pub enum SerializeError { - #[snafu(display("Could not serialize command {} because of: {}", commandType, source))] + #[snafu(display("Could not serialize command {} because of: {}", command_type, source))] InvalidData { - command_type: u8, - source: bincodeError + command_type: i32, + source: BincodeError, }, - #[snafu(display("Could not serialize command {} because of: {}", commandType, source))] + #[snafu(display("Could not serialize command {} because of: {}", command_type, source))] Io { - command_type: u8, - source: IOError + command_type: i32, + source: IoError, } } diff --git a/src/wire_protocol/tests.rs b/src/wire_protocol/tests.rs index 3f41829cd..e02325b96 100644 --- a/src/wire_protocol/tests.rs +++ b/src/wire_protocol/tests.rs @@ -611,7 +611,7 @@ fn test_table_entries_updated() { fn test_remove_table_keys() { let segment = JavaString(String::from("segment-1")); let token = JavaString(String::from("delegation_token")); - let mut keys = Vec::<(TableKey)>::new(); + let mut keys = Vec::::new(); let key_data = String::from("key-1").into_bytes(); keys.push(TableKey::new(key_data, 1)); let remove_table_keys_command = WireCommands::RemoveTableKeys(RemoveTableKeysCommand { @@ -637,7 +637,7 @@ fn test_table_keys_removed() { fn test_read_table() { let segment = JavaString(String::from("segment-1")); let token = JavaString(String::from("delegation_token")); - let mut keys = Vec::<(TableKey)>::new(); + let mut keys = Vec::::new(); let key_data = String::from("key-1").into_bytes(); keys.push(TableKey::new(key_data, 1)); let read_table_command = WireCommands::ReadTable(ReadTableCommand { @@ -685,7 +685,7 @@ fn test_read_table_keys() { #[test] fn test_table_keys_read() { let segment = JavaString(String::from("segment-1")); - let mut keys = Vec::<(TableKey)>::new(); + let mut keys = Vec::::new(); let key_data = String::from("key-1").into_bytes(); keys.push(TableKey::new(key_data, 1)); let continuation_token: Vec = vec![1, 2, 3]; @@ -757,7 +757,7 @@ fn table_key_bad_version() { } fn test_command(command: WireCommands) -> WireCommands { - let encoded: Vec = command.write_fields(); + let encoded: Vec = command.write_fields().unwrap(); let decoded = WireCommands::read_from(&encoded); assert_eq!(command, decoded); decoded diff --git a/src/wire_protocol/wire_commands.rs b/src/wire_protocol/wire_commands.rs index 45ad980b2..e9b762792 100644 --- a/src/wire_protocol/wire_commands.rs +++ b/src/wire_protocol/wire_commands.rs @@ -1,4 +1,5 @@ use super::commands::*; +use super::error::SerializeError; use byteorder::{BigEndian, ByteOrder, WriteBytesExt}; #[derive(PartialEq, Debug)] @@ -64,7 +65,7 @@ pub enum WireCommands { } pub trait Encode { - fn write_fields(&self) -> Vec; + fn write_fields(&self) -> Result, SerializeError>; } pub trait Decode { @@ -72,409 +73,356 @@ pub trait Decode { } impl Encode for WireCommands { - fn write_fields(&self) -> Vec { + fn write_fields(&self) -> Result, SerializeError> { let mut res = Vec::new(); match self { WireCommands::Hello(hello_cmd) => { - res.write_i32::(HelloCommand::TYPE_CODE).unwrap(); - let se = hello_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(HelloCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); + let se = hello_cmd.write_fields()?; + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::WrongHost(wrong_host_cmd) => { - res.write_i32::(WrongHostCommand::TYPE_CODE) - .unwrap(); - let se = wrong_host_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(WrongHostCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = wrong_host_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::SegmentIsSealed(seg_is_sealed_cmd) => { - res.write_i32::(SegmentIsSealedCommand::TYPE_CODE) - .unwrap(); - let se = seg_is_sealed_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(SegmentIsSealedCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = seg_is_sealed_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::SegmentAlreadyExists(seg_already_exists_cmd) => { - res.write_i32::(SegmentAlreadyExistsCommand::TYPE_CODE) - .unwrap(); - let se = seg_already_exists_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(SegmentAlreadyExistsCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = seg_already_exists_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::SegmentIsTruncated(seg_is_truncated_cmd) => { - res.write_i32::(SegmentIsTruncatedCommand::TYPE_CODE) - .unwrap(); - let se = seg_is_truncated_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(SegmentIsTruncatedCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = seg_is_truncated_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::NoSuchSegment(no_such_seg_cmd) => { - res.write_i32::(NoSuchSegmentCommand::TYPE_CODE) - .unwrap(); - let se = no_such_seg_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(NoSuchSegmentCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = no_such_seg_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::TableSegmentNotEmpty(table_seg_not_empty_cmd) => { - res.write_i32::(TableSegmentNotEmptyCommand::TYPE_CODE) - .unwrap(); - let se = table_seg_not_empty_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(TableSegmentNotEmptyCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = table_seg_not_empty_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::InvalidEventNumber(invalid_event_num_cmd) => { - res.write_i32::(InvalidEventNumberCommand::TYPE_CODE) - .unwrap(); - let se = invalid_event_num_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(InvalidEventNumberCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = invalid_event_num_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::OperationUnsupported(operation_unsupported_cmd) => { - res.write_i32::(OperationUnsupportedCommand::TYPE_CODE) - .unwrap(); - let se = operation_unsupported_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(OperationUnsupportedCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = operation_unsupported_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::Padding(padding_command) => { - res.write_i32::(PaddingCommand::TYPE_CODE) - .unwrap(); - let se = padding_command.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(PaddingCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = padding_command.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::PartialEvent(partial_event_cmd) => { - res.write_i32::(PartialEventCommand::TYPE_CODE) - .unwrap(); - let se = partial_event_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(PartialEventCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = partial_event_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::Event(event_cmd) => { - res.write_i32::(EventCommand::TYPE_CODE).unwrap(); - let se = event_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(EventCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = event_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::SetupAppend(setup_append_cmd) => { - res.write_i32::(SetupAppendCommand::TYPE_CODE) - .unwrap(); - let se = setup_append_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(SetupAppendCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = setup_append_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::AppendBlock(append_block_cmd) => { - res.write_i32::(AppendBlockCommand::TYPE_CODE) - .unwrap(); - let se = append_block_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(AppendBlockCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = append_block_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::AppendBlockEnd(append_block_end_cmd) => { - res.write_i32::(AppendBlockEndCommand::TYPE_CODE) - .unwrap(); - let se = append_block_end_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(AppendBlockEndCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = append_block_end_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::ConditionalAppend(conditional_append_cmd) => { - res.write_i32::(ConditionalAppendCommand::TYPE_CODE) - .unwrap(); - let se = conditional_append_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(ConditionalAppendCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = conditional_append_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::AppendSetup(append_setup_cmd) => { - res.write_i32::(AppendSetupCommand::TYPE_CODE) - .unwrap(); - let se = append_setup_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(AppendSetupCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = append_setup_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::DataAppended(data_appended_cmd) => { - res.write_i32::(DataAppendedCommand::TYPE_CODE) - .unwrap(); - let se = data_appended_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(DataAppendedCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = data_appended_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::ConditionalCheckFailed(conditional_check_failed_cmd) => { - res.write_i32::(ConditionalCheckFailedCommand::TYPE_CODE) - .unwrap(); - let se = conditional_check_failed_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(ConditionalCheckFailedCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = conditional_check_failed_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::ReadSegment(read_segment_cmd) => { - res.write_i32::(ReadSegmentCommand::TYPE_CODE) - .unwrap(); - let se = read_segment_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(ReadSegmentCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = read_segment_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::SegmentRead(segment_read_cmd) => { - res.write_i32::(SegmentReadCommand::TYPE_CODE) - .unwrap(); - let se = segment_read_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(SegmentReadCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = segment_read_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::GetSegmentAttribute(get_segment_attribute_cmd) => { - res.write_i32::(GetSegmentAttributeCommand::TYPE_CODE) - .unwrap(); - let se = get_segment_attribute_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(GetSegmentAttributeCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = get_segment_attribute_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::SegmentAttribute(segment_attribute_cmd) => { - res.write_i32::(SegmentAttributeCommand::TYPE_CODE) - .unwrap(); - let se = segment_attribute_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(SegmentAttributeCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = segment_attribute_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::UpdateSegmentAttribute(update_segment_attribute_cmd) => { - res.write_i32::(UpdateSegmentAttributeCommand::TYPE_CODE) - .unwrap(); - let se = update_segment_attribute_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(UpdateSegmentAttributeCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = update_segment_attribute_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::SegmentAttributeUpdated(segment_attribute_updated_cmd) => { - res.write_i32::(SegmentAttributeUpdatedCommand::TYPE_CODE) - .unwrap(); - let se = segment_attribute_updated_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(SegmentAttributeUpdatedCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = segment_attribute_updated_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::GetStreamSegmentInfo(get_stream_segment_info_cmd) => { - res.write_i32::(GetStreamSegmentInfoCommand::TYPE_CODE) - .unwrap(); - let se = get_stream_segment_info_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(GetStreamSegmentInfoCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = get_stream_segment_info_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::StreamSegmentInfo(stream_segment_info_cmd) => { - res.write_i32::(StreamSegmentInfoCommand::TYPE_CODE) - .unwrap(); - let se = stream_segment_info_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(StreamSegmentInfoCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = stream_segment_info_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::CreateSegment(create_segment_cmd) => { - res.write_i32::(CreateSegmentCommand::TYPE_CODE) - .unwrap(); - let se = create_segment_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(CreateSegmentCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = create_segment_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::CreateTableSegment(create_table_segment_command) => { - res.write_i32::(CreateTableSegmentCommand::TYPE_CODE) - .unwrap(); - let se = create_table_segment_command.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(CreateTableSegmentCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = create_table_segment_command.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::SegmentCreated(segment_created_cmd) => { - res.write_i32::(SegmentCreatedCommand::TYPE_CODE) - .unwrap(); - let se = segment_created_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(SegmentCreatedCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = segment_created_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::UpdateSegmentPolicy(update_segment_policy_cmd) => { - res.write_i32::(UpdateSegmentPolicyCommand::TYPE_CODE) - .unwrap(); - let se = update_segment_policy_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(UpdateSegmentPolicyCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = update_segment_policy_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::SegmentPolicyUpdated(segment_policy_updated_cmd) => { - res.write_i32::(SegmentPolicyUpdatedCommand::TYPE_CODE) - .unwrap(); - let se = segment_policy_updated_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); - res.extend(se); + res.write_i32::(SegmentPolicyUpdatedCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = segment_policy_updated_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.extend(se) } WireCommands::MergeSegments(merge_segments_cmd) => { - res.write_i32::(MergeSegmentsCommand::TYPE_CODE) - .unwrap(); - let se = merge_segments_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(MergeSegmentsCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = merge_segments_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::MergeTableSegments(merge_table_segments_cmd) => { - res.write_i32::(MergeTableSegmentsCommand::TYPE_CODE) - .unwrap(); - let se = merge_table_segments_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(MergeTableSegmentsCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = merge_table_segments_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::SegmentsMerged(segments_merged_cmd) => { - res.write_i32::(SegmentsMergedCommand::TYPE_CODE) - .unwrap(); - let se = segments_merged_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(SegmentsMergedCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = segments_merged_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::SealSegment(seal_segment_cmd) => { - res.write_i32::(SealSegmentCommand::TYPE_CODE) - .unwrap(); - let se = seal_segment_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(SealSegmentCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = seal_segment_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::SealTableSegment(seal_table_segment_cmd) => { - res.write_i32::(SealTableSegmentCommand::TYPE_CODE) - .unwrap(); - let se = seal_table_segment_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(SealTableSegmentCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = seal_table_segment_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::SegmentSealed(segment_sealed_cmd) => { - res.write_i32::(SegmentSealedCommand::TYPE_CODE) - .unwrap(); - let se = segment_sealed_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(SegmentSealedCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = segment_sealed_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::TruncateSegment(truncate_segment_cmd) => { - res.write_i32::(TruncateSegmentCommand::TYPE_CODE) - .unwrap(); - let se = truncate_segment_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(TruncateSegmentCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = truncate_segment_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::SegmentTruncated(segment_truncated_cmd) => { - res.write_i32::(SegmentTruncatedCommand::TYPE_CODE) - .unwrap(); - let se = segment_truncated_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(SegmentTruncatedCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = segment_truncated_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::DeleteSegment(delete_segment_cmd) => { - res.write_i32::(DeleteSegmentCommand::TYPE_CODE) - .unwrap(); - let se = delete_segment_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(DeleteSegmentCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = delete_segment_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::DeleteTableSegment(delete_table_segment_cmd) => { - res.write_i32::(DeleteTableSegmentCommand::TYPE_CODE) - .unwrap(); - let se = delete_table_segment_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(DeleteTableSegmentCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = delete_table_segment_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::SegmentDeleted(segment_deleted_cmd) => { - res.write_i32::(SegmentDeletedCommand::TYPE_CODE) - .unwrap(); - let se = segment_deleted_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(SegmentDeletedCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = segment_deleted_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::KeepAlive(keep_alive_cmd) => { - res.write_i32::(KeepAliveCommand::TYPE_CODE) - .unwrap(); - let se = keep_alive_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(KeepAliveCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = keep_alive_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::AuthTokenCheckFailed(auth_token_check_failed_cmd) => { - res.write_i32::(AuthTokenCheckFailedCommand::TYPE_CODE) - .unwrap(); - let se = auth_token_check_failed_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(AuthTokenCheckFailedCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = auth_token_check_failed_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::UpdateTableEntries(update_table_entries_cmd) => { - res.write_i32::(UpdateTableEntriesCommand::TYPE_CODE) - .unwrap(); - let se = update_table_entries_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(UpdateTableEntriesCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = update_table_entries_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::TableEntriesUpdated(table_entries_updated_cmd) => { - res.write_i32::(TableEntriesUpdatedCommand::TYPE_CODE) - .unwrap(); - let se = table_entries_updated_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(TableEntriesUpdatedCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = table_entries_updated_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::RemoveTableKeys(remove_table_keys_cmd) => { - res.write_i32::(RemoveTableKeysCommand::TYPE_CODE) - .unwrap(); - let se = remove_table_keys_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(RemoveTableKeysCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = remove_table_keys_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::TableKeysRemoved(table_key_removed_cmd) => { - res.write_i32::(TableKeysRemovedCommand::TYPE_CODE) - .unwrap(); - let se = table_key_removed_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(TableKeysRemovedCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = table_key_removed_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::ReadTable(read_table_cmd) => { - res.write_i32::(ReadTableCommand::TYPE_CODE) - .unwrap(); - let se = read_table_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(ReadTableCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = read_table_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::TableRead(table_read_cmd) => { - res.write_i32::(TableReadCommand::TYPE_CODE) - .unwrap(); - let se = table_read_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(TableReadCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = table_read_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::ReadTableKeys(read_table_keys_cmd) => { - res.write_i32::(ReadTableKeysCommand::TYPE_CODE) - .unwrap(); - let se = read_table_keys_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(ReadTableKeysCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = read_table_keys_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::TableKeysRead(table_keys_read_cmd) => { - res.write_i32::(TableKeysReadCommand::TYPE_CODE) - .unwrap(); - let se = table_keys_read_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(TableKeysReadCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = table_keys_read_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::ReadTableEntries(read_table_entries_cmd) => { - res.write_i32::(ReadTableEntriesCommand::TYPE_CODE) - .unwrap(); - let se = read_table_entries_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(ReadTableEntriesCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = read_table_entries_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::TableEntriesRead(table_entries_read_cmd) => { - res.write_i32::(TableEntriesReadCommand::TYPE_CODE) - .unwrap(); - let se = table_entries_read_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(TableEntriesReadCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = table_entries_read_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::TableKeyDoesNotExist(table_key_does_not_exist_cmd) => { - res.write_i32::(TableKeyDoesNotExistCommand::TYPE_CODE) - .unwrap(); - let se = table_key_does_not_exist_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(TableKeyDoesNotExistCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = table_key_does_not_exist_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } WireCommands::TableKeyBadVersion(table_key_bad_version_cmd) => { - res.write_i32::(TableKeyBadVersionCommand::TYPE_CODE) - .unwrap(); - let se = table_key_bad_version_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.write_i32::(TableKeyBadVersionCommand::TYPE_CODE).expect("Writing to an in memory vec"); + let se = table_key_bad_version_cmd.write_fields()?; + res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); res.extend(se); } _ => panic!("Unknown WireCommands"), } - res + Ok(res) } } From 7cc11e5f10b1859e5a5746c11d5064ce805af6bb Mon Sep 17 00:00:00 2001 From: Wenxiao Zhang Date: Wed, 8 Jan 2020 16:32:05 -0800 Subject: [PATCH 11/30] add deserialize error handling --- src/wire_protocol/commands.rs | 356 ++++++++++++++--------------- src/wire_protocol/error.rs | 7 +- src/wire_protocol/tests.rs | 2 +- src/wire_protocol/wire_commands.rs | 142 ++++++------ 4 files changed, 256 insertions(+), 251 deletions(-) diff --git a/src/wire_protocol/commands.rs b/src/wire_protocol/commands.rs index a2724dfd8..8caee797d 100644 --- a/src/wire_protocol/commands.rs +++ b/src/wire_protocol/commands.rs @@ -8,7 +8,7 @@ use std::i64; use std::io::Cursor; use std::io::{Read, Write}; use snafu::ResultExt; -use super::error::SerializeError; +use super::error::CommandError; use super::error::InvalidData; use super::error::Io; @@ -17,8 +17,8 @@ use super::error::Io; */ pub trait Command { const TYPE_CODE: i32; - fn write_fields(&self) -> Result, SerializeError>; - fn read_from(input: &[u8]) -> Self; + fn write_fields(&self) -> Result, CommandError>; + fn read_from(input: &[u8]) -> Result where Self: Sized; } /** @@ -139,14 +139,14 @@ pub struct HelloCommand { impl Command for HelloCommand { const TYPE_CODE: i32 = -127; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> HelloCommand { - let decoded: HelloCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + fn read_from(input: &[u8]) -> Result { + let decoded: HelloCommand = CONFIG.deserialize(&input[..]).context(InvalidData{command_type: Self::TYPE_CODE})?; + Ok(decoded) } } @@ -175,13 +175,13 @@ pub struct WrongHostCommand { impl Command for WrongHostCommand { const TYPE_CODE: i32 = 50; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> WrongHostCommand { + fn read_from(input: &[u8]) -> Result { let decoded: WrongHostCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -207,14 +207,14 @@ pub struct SegmentIsSealedCommand { impl Command for SegmentIsSealedCommand { const TYPE_CODE: i32 = 51; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> SegmentIsSealedCommand { + fn read_from(input: &[u8]) -> Result { let decoded: SegmentIsSealedCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -241,14 +241,14 @@ pub struct SegmentIsTruncatedCommand { impl Command for SegmentIsTruncatedCommand { const TYPE_CODE: i32 = 56; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> SegmentIsTruncatedCommand { + fn read_from(input: &[u8]) -> Result { let decoded: SegmentIsTruncatedCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -273,14 +273,14 @@ pub struct SegmentAlreadyExistsCommand { impl Command for SegmentAlreadyExistsCommand { const TYPE_CODE: i32 = 52; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> SegmentAlreadyExistsCommand { + fn read_from(input: &[u8]) -> Result { let decoded: SegmentAlreadyExistsCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -312,14 +312,14 @@ pub struct NoSuchSegmentCommand { impl Command for NoSuchSegmentCommand { const TYPE_CODE: i32 = 53; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> NoSuchSegmentCommand { + fn read_from(input: &[u8]) -> Result { let decoded: NoSuchSegmentCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -350,14 +350,14 @@ pub struct TableSegmentNotEmptyCommand { impl Command for TableSegmentNotEmptyCommand { const TYPE_CODE: i32 = 80; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> TableSegmentNotEmptyCommand { + fn read_from(input: &[u8]) -> Result { let decoded: TableSegmentNotEmptyCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -388,14 +388,14 @@ pub struct InvalidEventNumberCommand { impl Command for InvalidEventNumberCommand { const TYPE_CODE: i32 = 55; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> InvalidEventNumberCommand { + fn read_from(input: &[u8]) -> Result { let decoded: InvalidEventNumberCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -430,14 +430,14 @@ pub struct OperationUnsupportedCommand { impl Command for OperationUnsupportedCommand { const TYPE_CODE: i32 = 57; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> OperationUnsupportedCommand { + fn read_from(input: &[u8]) -> Result { let decoded: OperationUnsupportedCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -461,7 +461,7 @@ pub struct PaddingCommand { impl Command for PaddingCommand { const TYPE_CODE: i32 = -1; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let mut res = Vec::new(); for _i in 0..(self.length / 8) { res.write_i64::(0).context(Io{command_type: Self::TYPE_CODE})?; @@ -472,12 +472,12 @@ impl Command for PaddingCommand { Ok(res) } - fn read_from(input: &[u8]) -> PaddingCommand { + fn read_from(input: &[u8]) -> Result { //FIXME: In java we use skipBytes to remove these padding bytes. //FIXME: I think we don't need to do in rust. - PaddingCommand { + Ok(PaddingCommand { length: input.len() as i32, - } + }) } } @@ -491,17 +491,17 @@ pub struct PartialEventCommand { impl Command for PartialEventCommand { const TYPE_CODE: i32 = -2; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { //FIXME: In java, we use data.getBytes(data.readerIndex(), (OutputStream) out, data.readableBytes()); // which means the result would not contain the prefix length; // so in rust we can directly return data. Ok(self.data.clone()) } - fn read_from(input: &[u8]) -> PartialEventCommand { - PartialEventCommand { + fn read_from(input: &[u8]) -> Result { + Ok(PartialEventCommand { data: input.to_vec(), - } + }) } } @@ -515,7 +515,7 @@ pub struct EventCommand { impl Command for EventCommand { const TYPE_CODE: i32 = 0; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let mut res = Vec::new(); res.write_i32::(EventCommand::TYPE_CODE).context(Io{command_type: Self::TYPE_CODE})?; let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; @@ -523,11 +523,11 @@ impl Command for EventCommand { Ok(res) } - fn read_from(input: &[u8]) -> EventCommand { + fn read_from(input: &[u8]) -> Result { //read the type_code. let _type_code = BigEndian::read_i32(input); let decoded: EventCommand = CONFIG.deserialize(&input[4..]).unwrap(); - decoded + Ok(decoded) } } @@ -544,14 +544,14 @@ pub struct SetupAppendCommand { impl Command for SetupAppendCommand { const TYPE_CODE: i32 = 1; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> SetupAppendCommand { + fn read_from(input: &[u8]) -> Result { let decoded: SetupAppendCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -574,14 +574,14 @@ impl Command for AppendBlockCommand { const TYPE_CODE: i32 = 3; //FIXME: The serialize and deserialize method need to customize; // In JAVA, it doesn't write data(because it'empty), but here it will write the prefix length(0). - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> AppendBlockCommand { + fn read_from(input: &[u8]) -> Result { let decoded: AppendBlockCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -601,14 +601,14 @@ pub struct AppendBlockEndCommand { impl Command for AppendBlockEndCommand { const TYPE_CODE: i32 = 4; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> AppendBlockEndCommand { + fn read_from(input: &[u8]) -> Result { let decoded: AppendBlockEndCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -639,7 +639,7 @@ impl Command for ConditionalAppendCommand { const TYPE_CODE: i32 = 5; // Customize the serialize and deserialize method. // Because in CondtionalAppend the event should be serialize as |type_code|length|data| - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let mut res = Vec::new(); res.write_u128::(self.writer_id).context(Io{command_type: Self::TYPE_CODE})?; res.write_i64::(self.event_number).context(Io{command_type: Self::TYPE_CODE})?; @@ -649,20 +649,20 @@ impl Command for ConditionalAppendCommand { Ok(res) } - fn read_from(input: &[u8]) -> ConditionalAppendCommand { + fn read_from(input: &[u8]) -> Result { let mut rdr = Cursor::new(input); let writer_id = rdr.read_u128::().unwrap(); let event_number = rdr.read_i64::().unwrap(); let expected_offset = rdr.read_i64::().unwrap(); let event = ConditionalAppendCommand::read_event(&mut rdr); let request_id = rdr.read_i64::().unwrap(); - ConditionalAppendCommand { + Ok(ConditionalAppendCommand { writer_id, event_number, expected_offset, event, request_id, - } + }) } } @@ -686,14 +686,14 @@ pub struct AppendSetupCommand { impl Command for AppendSetupCommand { const TYPE_CODE: i32 = 2; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> Self { + fn read_from(input: &[u8]) -> Result { let decoded: AppendSetupCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -718,14 +718,14 @@ pub struct DataAppendedCommand { impl Command for DataAppendedCommand { const TYPE_CODE: i32 = 7; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> DataAppendedCommand { + fn read_from(input: &[u8]) -> Result { let decoded: DataAppendedCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -748,14 +748,14 @@ pub struct ConditionalCheckFailedCommand { impl Command for ConditionalCheckFailedCommand { const TYPE_CODE: i32 = 8; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> ConditionalCheckFailedCommand { + fn read_from(input: &[u8]) -> Result { let decoded: ConditionalCheckFailedCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -780,14 +780,14 @@ pub struct ReadSegmentCommand { impl Command for ReadSegmentCommand { const TYPE_CODE: i32 = 9; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> ReadSegmentCommand { + fn read_from(input: &[u8]) -> Result { let decoded: ReadSegmentCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -813,14 +813,14 @@ pub struct SegmentReadCommand { impl Command for SegmentReadCommand { const TYPE_CODE: i32 = 10; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> SegmentReadCommand { + fn read_from(input: &[u8]) -> Result { let decoded: SegmentReadCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -844,14 +844,14 @@ pub struct GetSegmentAttributeCommand { impl Command for GetSegmentAttributeCommand { const TYPE_CODE: i32 = 34; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> GetSegmentAttributeCommand { + fn read_from(input: &[u8]) -> Result { let decoded: GetSegmentAttributeCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -873,14 +873,14 @@ pub struct SegmentAttributeCommand { impl Command for SegmentAttributeCommand { const TYPE_CODE: i32 = 35; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> SegmentAttributeCommand { + fn read_from(input: &[u8]) -> Result { let decoded: SegmentAttributeCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -906,14 +906,14 @@ pub struct UpdateSegmentAttributeCommand { impl Command for UpdateSegmentAttributeCommand { const TYPE_CODE: i32 = 36; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> UpdateSegmentAttributeCommand { + fn read_from(input: &[u8]) -> Result { let decoded: UpdateSegmentAttributeCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -935,14 +935,14 @@ pub struct SegmentAttributeUpdatedCommand { impl Command for SegmentAttributeUpdatedCommand { const TYPE_CODE: i32 = 37; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> SegmentAttributeUpdatedCommand { + fn read_from(input: &[u8]) -> Result { let decoded: SegmentAttributeUpdatedCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -965,14 +965,14 @@ pub struct GetStreamSegmentInfoCommand { impl Command for GetStreamSegmentInfoCommand { const TYPE_CODE: i32 = 11; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> GetStreamSegmentInfoCommand { + fn read_from(input: &[u8]) -> Result { let decoded: GetStreamSegmentInfoCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -1000,14 +1000,14 @@ pub struct StreamSegmentInfoCommand { impl Command for StreamSegmentInfoCommand { const TYPE_CODE: i32 = 12; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> StreamSegmentInfoCommand { + fn read_from(input: &[u8]) -> Result { let decoded: StreamSegmentInfoCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -1032,14 +1032,14 @@ pub struct CreateSegmentCommand { impl Command for CreateSegmentCommand { const TYPE_CODE: i32 = 20; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> CreateSegmentCommand { + fn read_from(input: &[u8]) -> Result { let decoded: CreateSegmentCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -1062,14 +1062,14 @@ pub struct CreateTableSegmentCommand { impl Command for CreateTableSegmentCommand { const TYPE_CODE: i32 = 70; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> CreateTableSegmentCommand { + fn read_from(input: &[u8]) -> Result { let decoded: CreateTableSegmentCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -1091,14 +1091,14 @@ pub struct SegmentCreatedCommand { impl Command for SegmentCreatedCommand { const TYPE_CODE: i32 = 21; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> SegmentCreatedCommand { + fn read_from(input: &[u8]) -> Result { let decoded: SegmentCreatedCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -1123,14 +1123,14 @@ pub struct UpdateSegmentPolicyCommand { impl Command for UpdateSegmentPolicyCommand { const TYPE_CODE: i32 = 32; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> UpdateSegmentPolicyCommand { + fn read_from(input: &[u8]) -> Result { let decoded: UpdateSegmentPolicyCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -1152,14 +1152,14 @@ pub struct SegmentPolicyUpdatedCommand { impl Command for SegmentPolicyUpdatedCommand { const TYPE_CODE: i32 = 33; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> SegmentPolicyUpdatedCommand { + fn read_from(input: &[u8]) -> Result { let decoded: SegmentPolicyUpdatedCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -1183,14 +1183,14 @@ pub struct MergeSegmentsCommand { impl Command for MergeSegmentsCommand { const TYPE_CODE: i32 = 58; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> MergeSegmentsCommand { + fn read_from(input: &[u8]) -> Result { let decoded: MergeSegmentsCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -1213,14 +1213,14 @@ pub struct MergeTableSegmentsCommand { impl Command for MergeTableSegmentsCommand { const TYPE_CODE: i32 = 72; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> MergeTableSegmentsCommand { + fn read_from(input: &[u8]) -> Result { let decoded: MergeTableSegmentsCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -1244,14 +1244,14 @@ pub struct SegmentsMergedCommand { impl Command for SegmentsMergedCommand { const TYPE_CODE: i32 = 59; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> SegmentsMergedCommand { + fn read_from(input: &[u8]) -> Result { let decoded: SegmentsMergedCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -1274,14 +1274,14 @@ pub struct SealSegmentCommand { impl Command for SealSegmentCommand { const TYPE_CODE: i32 = 28; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> SealSegmentCommand { + fn read_from(input: &[u8]) -> Result { let decoded: SealSegmentCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -1304,14 +1304,14 @@ pub struct SealTableSegmentCommand { impl Command for SealTableSegmentCommand { const TYPE_CODE: i32 = 73; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> SealTableSegmentCommand { + fn read_from(input: &[u8]) -> Result { let decoded: SealTableSegmentCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -1333,14 +1333,14 @@ pub struct SegmentSealedCommand { impl Command for SegmentSealedCommand { const TYPE_CODE: i32 = 29; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> SegmentSealedCommand { + fn read_from(input: &[u8]) -> Result { let decoded: SegmentSealedCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -1364,14 +1364,14 @@ pub struct TruncateSegmentCommand { impl Command for TruncateSegmentCommand { const TYPE_CODE: i32 = 38; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> TruncateSegmentCommand { + fn read_from(input: &[u8]) -> Result { let decoded: TruncateSegmentCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -1393,14 +1393,14 @@ pub struct SegmentTruncatedCommand { impl Command for SegmentTruncatedCommand { const TYPE_CODE: i32 = 39; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> SegmentTruncatedCommand { + fn read_from(input: &[u8]) -> Result { let decoded: SegmentTruncatedCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -1423,14 +1423,14 @@ pub struct DeleteSegmentCommand { impl Command for DeleteSegmentCommand { const TYPE_CODE: i32 = 30; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> DeleteSegmentCommand { + fn read_from(input: &[u8]) -> Result { let decoded: DeleteSegmentCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -1453,14 +1453,14 @@ pub struct DeleteTableSegmentCommand { impl Command for DeleteTableSegmentCommand { const TYPE_CODE: i32 = 71; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> DeleteTableSegmentCommand { + fn read_from(input: &[u8]) -> Result { let decoded: DeleteTableSegmentCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -1482,14 +1482,14 @@ pub struct SegmentDeletedCommand { impl Command for SegmentDeletedCommand { const TYPE_CODE: i32 = 31; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> SegmentDeletedCommand { + fn read_from(input: &[u8]) -> Result { let decoded: SegmentDeletedCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -1507,13 +1507,13 @@ pub struct KeepAliveCommand {} impl Command for KeepAliveCommand { const TYPE_CODE: i32 = 100; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let res: Vec = Vec::new(); Ok(res) } - fn read_from(_input: &[u8]) -> KeepAliveCommand { - KeepAliveCommand {} + fn read_from(_input: &[u8]) -> Result { + Ok(KeepAliveCommand {}) } } @@ -1555,14 +1555,14 @@ impl AuthTokenCheckFailedCommand { impl Command for AuthTokenCheckFailedCommand { const TYPE_CODE: i32 = 60; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> AuthTokenCheckFailedCommand { + fn read_from(input: &[u8]) -> Result { let decoded: AuthTokenCheckFailedCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -1611,14 +1611,14 @@ pub struct UpdateTableEntriesCommand { impl Command for UpdateTableEntriesCommand { const TYPE_CODE: i32 = 74; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> UpdateTableEntriesCommand { + fn read_from(input: &[u8]) -> Result { let decoded: UpdateTableEntriesCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -1640,14 +1640,14 @@ pub struct TableEntriesUpdatedCommand { impl Command for TableEntriesUpdatedCommand { const TYPE_CODE: i32 = 75; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> TableEntriesUpdatedCommand { + fn read_from(input: &[u8]) -> Result { let decoded: TableEntriesUpdatedCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -1671,14 +1671,14 @@ pub struct RemoveTableKeysCommand { impl Command for RemoveTableKeysCommand { const TYPE_CODE: i32 = 76; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> RemoveTableKeysCommand { + fn read_from(input: &[u8]) -> Result { let decoded: RemoveTableKeysCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -1700,14 +1700,14 @@ pub struct TableKeysRemovedCommand { impl Command for TableKeysRemovedCommand { const TYPE_CODE: i32 = 77; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> TableKeysRemovedCommand { + fn read_from(input: &[u8]) -> Result { let decoded: TableKeysRemovedCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -1731,14 +1731,14 @@ pub struct ReadTableCommand { impl Command for ReadTableCommand { const TYPE_CODE: i32 = 78; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> ReadTableCommand { + fn read_from(input: &[u8]) -> Result { let decoded: ReadTableCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -1761,14 +1761,14 @@ pub struct TableReadCommand { impl Command for TableReadCommand { const TYPE_CODE: i32 = 79; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> TableReadCommand { + fn read_from(input: &[u8]) -> Result { let decoded: TableReadCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -1793,14 +1793,14 @@ pub struct ReadTableKeysCommand { impl Command for ReadTableKeysCommand { const TYPE_CODE: i32 = 83; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> ReadTableKeysCommand { + fn read_from(input: &[u8]) -> Result { let decoded: ReadTableKeysCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -1824,14 +1824,14 @@ pub struct TableKeysReadCommand { impl Command for TableKeysReadCommand { const TYPE_CODE: i32 = 84; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> TableKeysReadCommand { + fn read_from(input: &[u8]) -> Result { let decoded: TableKeysReadCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -1856,14 +1856,14 @@ pub struct ReadTableEntriesCommand { impl Command for ReadTableEntriesCommand { const TYPE_CODE: i32 = 85; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> ReadTableEntriesCommand { + fn read_from(input: &[u8]) -> Result { let decoded: ReadTableEntriesCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -1887,14 +1887,14 @@ pub struct TableEntriesReadCommand { impl Command for TableEntriesReadCommand { const TYPE_CODE: i32 = 86; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> TableEntriesReadCommand { + fn read_from(input: &[u8]) -> Result { let decoded: TableEntriesReadCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -1917,14 +1917,14 @@ pub struct TableKeyDoesNotExistCommand { impl Command for TableKeyDoesNotExistCommand { const TYPE_CODE: i32 = 81; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> TableKeyDoesNotExistCommand { + fn read_from(input: &[u8]) -> Result { let decoded: TableKeyDoesNotExistCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } @@ -1961,14 +1961,14 @@ pub struct TableKeyBadVersionCommand { impl Command for TableKeyBadVersionCommand { const TYPE_CODE: i32 = 82; - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; Ok(encoded) } - fn read_from(input: &[u8]) -> TableKeyBadVersionCommand { + fn read_from(input: &[u8]) -> Result { let decoded: TableKeyBadVersionCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + Ok(decoded) } } diff --git a/src/wire_protocol/error.rs b/src/wire_protocol/error.rs index 3eafa1766..c7a1b4e82 100644 --- a/src/wire_protocol/error.rs +++ b/src/wire_protocol/error.rs @@ -31,10 +31,10 @@ pub enum ConnectionError { } } -/// This kind of error that can be produced during Pravega serialize the wire commands. +/// This kind of error that can be produced during Pravega serialize and deserialize the wire commands. #[derive(Debug, Snafu)] #[snafu(visibility = "pub(crate)")] -pub enum SerializeError { +pub enum CommandError { #[snafu(display("Could not serialize command {} because of: {}", command_type, source))] InvalidData { command_type: i32, @@ -47,3 +47,6 @@ pub enum SerializeError { } } + + + diff --git a/src/wire_protocol/tests.rs b/src/wire_protocol/tests.rs index e02325b96..44c217595 100644 --- a/src/wire_protocol/tests.rs +++ b/src/wire_protocol/tests.rs @@ -758,7 +758,7 @@ fn table_key_bad_version() { fn test_command(command: WireCommands) -> WireCommands { let encoded: Vec = command.write_fields().unwrap(); - let decoded = WireCommands::read_from(&encoded); + let decoded = WireCommands::read_from(&encoded).unwrap(); assert_eq!(command, decoded); decoded } diff --git a/src/wire_protocol/wire_commands.rs b/src/wire_protocol/wire_commands.rs index e9b762792..e8ec7b82e 100644 --- a/src/wire_protocol/wire_commands.rs +++ b/src/wire_protocol/wire_commands.rs @@ -1,5 +1,5 @@ use super::commands::*; -use super::error::SerializeError; +use super::error::CommandError; use byteorder::{BigEndian, ByteOrder, WriteBytesExt}; #[derive(PartialEq, Debug)] @@ -65,15 +65,15 @@ pub enum WireCommands { } pub trait Encode { - fn write_fields(&self) -> Result, SerializeError>; + fn write_fields(&self) -> Result, CommandError>; } pub trait Decode { - fn read_from(raw_input: &Vec) -> WireCommands; + fn read_from(raw_input: &Vec) -> Result; } impl Encode for WireCommands { - fn write_fields(&self) -> Result, SerializeError> { + fn write_fields(&self) -> Result, CommandError> { let mut res = Vec::new(); match self { WireCommands::Hello(hello_cmd) => { @@ -427,178 +427,180 @@ impl Encode for WireCommands { } impl Decode for WireCommands { - fn read_from(raw_input: &Vec) -> WireCommands { + fn read_from(raw_input: &Vec) -> Result { let type_code = BigEndian::read_i32(raw_input); let _length = BigEndian::read_i32(&raw_input[4..]); let input = &raw_input[8..]; match type_code { - HelloCommand::TYPE_CODE => WireCommands::Hello(HelloCommand::read_from(input)), + HelloCommand::TYPE_CODE => Ok(WireCommands::Hello(HelloCommand::read_from(input)?)), + WrongHostCommand::TYPE_CODE => { - WireCommands::WrongHost(WrongHostCommand::read_from(input)) + Ok(WireCommands::WrongHost(WrongHostCommand::read_from(input)?)) } SegmentIsSealedCommand::TYPE_CODE => { - WireCommands::SegmentIsSealed(SegmentIsSealedCommand::read_from(input)) + Ok(WireCommands::SegmentIsSealed(SegmentIsSealedCommand::read_from(input)?)) } SegmentAlreadyExistsCommand::TYPE_CODE => { - WireCommands::SegmentAlreadyExists(SegmentAlreadyExistsCommand::read_from(input)) + Ok(WireCommands::SegmentAlreadyExists(SegmentAlreadyExistsCommand::read_from(input)?)) } SegmentIsTruncatedCommand::TYPE_CODE => { - WireCommands::SegmentIsTruncated(SegmentIsTruncatedCommand::read_from(input)) + Ok(WireCommands::SegmentIsTruncated(SegmentIsTruncatedCommand::read_from(input)?)) } NoSuchSegmentCommand::TYPE_CODE => { - WireCommands::NoSuchSegment(NoSuchSegmentCommand::read_from(input)) + Ok(WireCommands::NoSuchSegment(NoSuchSegmentCommand::read_from(input)?)) } TableSegmentNotEmptyCommand::TYPE_CODE => { - WireCommands::TableSegmentNotEmpty(TableSegmentNotEmptyCommand::read_from(input)) + Ok(WireCommands::TableSegmentNotEmpty(TableSegmentNotEmptyCommand::read_from(input)?)) } InvalidEventNumberCommand::TYPE_CODE => { - WireCommands::InvalidEventNumber(InvalidEventNumberCommand::read_from(input)) + Ok(WireCommands::InvalidEventNumber(InvalidEventNumberCommand::read_from(input)?)) } OperationUnsupportedCommand::TYPE_CODE => { - WireCommands::OperationUnsupported(OperationUnsupportedCommand::read_from(input)) + Ok(WireCommands::OperationUnsupported(OperationUnsupportedCommand::read_from(input)?)) } - PaddingCommand::TYPE_CODE => WireCommands::Padding(PaddingCommand::read_from(input)), + PaddingCommand::TYPE_CODE => Ok(WireCommands::Padding(PaddingCommand::read_from(input)?)), PartialEventCommand::TYPE_CODE => { - WireCommands::PartialEvent(PartialEventCommand::read_from(input)) + Ok(WireCommands::PartialEvent(PartialEventCommand::read_from(input)?)) } - EventCommand::TYPE_CODE => WireCommands::Event(EventCommand::read_from(input)), + EventCommand::TYPE_CODE => Ok(WireCommands::Event(EventCommand::read_from(input)?)), + SetupAppendCommand::TYPE_CODE => { - WireCommands::SetupAppend(SetupAppendCommand::read_from(input)) + Ok(WireCommands::SetupAppend(SetupAppendCommand::read_from(input)?)) } AppendBlockCommand::TYPE_CODE => { - WireCommands::AppendBlock(AppendBlockCommand::read_from(input)) + Ok(WireCommands::AppendBlock(AppendBlockCommand::read_from(input)?)) } AppendBlockEndCommand::TYPE_CODE => { - WireCommands::AppendBlockEnd(AppendBlockEndCommand::read_from(input)) + Ok(WireCommands::AppendBlockEnd(AppendBlockEndCommand::read_from(input)?)) } ConditionalAppendCommand::TYPE_CODE => { - WireCommands::ConditionalAppend(ConditionalAppendCommand::read_from(input)) + Ok(WireCommands::ConditionalAppend(ConditionalAppendCommand::read_from(input)?)) } AppendSetupCommand::TYPE_CODE => { - WireCommands::AppendSetup(AppendSetupCommand::read_from(input)) + Ok(WireCommands::AppendSetup(AppendSetupCommand::read_from(input)?)) } DataAppendedCommand::TYPE_CODE => { - WireCommands::DataAppended(DataAppendedCommand::read_from(input)) + Ok(WireCommands::DataAppended(DataAppendedCommand::read_from(input)?)) } - ConditionalCheckFailedCommand::TYPE_CODE => WireCommands::ConditionalCheckFailed( - ConditionalCheckFailedCommand::read_from(input), - ), + ConditionalCheckFailedCommand::TYPE_CODE => Ok(WireCommands::ConditionalCheckFailed( + ConditionalCheckFailedCommand::read_from(input)?)), + ReadSegmentCommand::TYPE_CODE => { - WireCommands::ReadSegment(ReadSegmentCommand::read_from(input)) + Ok(WireCommands::ReadSegment(ReadSegmentCommand::read_from(input)?)) } SegmentReadCommand::TYPE_CODE => { - WireCommands::SegmentRead(SegmentReadCommand::read_from(input)) + Ok(WireCommands::SegmentRead(SegmentReadCommand::read_from(input)?)) } GetSegmentAttributeCommand::TYPE_CODE => { - WireCommands::GetSegmentAttribute(GetSegmentAttributeCommand::read_from(input)) + Ok(WireCommands::GetSegmentAttribute(GetSegmentAttributeCommand::read_from(input)?)) } SegmentAttributeCommand::TYPE_CODE => { - WireCommands::SegmentAttribute(SegmentAttributeCommand::read_from(input)) - } - UpdateSegmentAttributeCommand::TYPE_CODE => WireCommands::UpdateSegmentAttribute( - UpdateSegmentAttributeCommand::read_from(input), - ), - SegmentAttributeUpdatedCommand::TYPE_CODE => WireCommands::SegmentAttributeUpdated( - SegmentAttributeUpdatedCommand::read_from(input), - ), + Ok(WireCommands::SegmentAttribute(SegmentAttributeCommand::read_from(input)?)) + } + UpdateSegmentAttributeCommand::TYPE_CODE => Ok(WireCommands::UpdateSegmentAttribute( + UpdateSegmentAttributeCommand::read_from(input)?)), + + SegmentAttributeUpdatedCommand::TYPE_CODE => Ok(WireCommands::SegmentAttributeUpdated( + SegmentAttributeUpdatedCommand::read_from(input)?)), + GetStreamSegmentInfoCommand::TYPE_CODE => { - WireCommands::GetStreamSegmentInfo(GetStreamSegmentInfoCommand::read_from(input)) + Ok(WireCommands::GetStreamSegmentInfo(GetStreamSegmentInfoCommand::read_from(input)?)) } StreamSegmentInfoCommand::TYPE_CODE => { - WireCommands::StreamSegmentInfo(StreamSegmentInfoCommand::read_from(input)) + Ok(WireCommands::StreamSegmentInfo(StreamSegmentInfoCommand::read_from(input)?)) } CreateSegmentCommand::TYPE_CODE => { - WireCommands::CreateSegment(CreateSegmentCommand::read_from(input)) + Ok(WireCommands::CreateSegment(CreateSegmentCommand::read_from(input)?)) } CreateTableSegmentCommand::TYPE_CODE => { - WireCommands::CreateTableSegment(CreateTableSegmentCommand::read_from(input)) + Ok(WireCommands::CreateTableSegment(CreateTableSegmentCommand::read_from(input)?)) } SegmentCreatedCommand::TYPE_CODE => { - WireCommands::SegmentCreated(SegmentCreatedCommand::read_from(input)) + Ok(WireCommands::SegmentCreated(SegmentCreatedCommand::read_from(input)?)) } UpdateSegmentPolicyCommand::TYPE_CODE => { - WireCommands::UpdateSegmentPolicy(UpdateSegmentPolicyCommand::read_from(input)) + Ok(WireCommands::UpdateSegmentPolicy(UpdateSegmentPolicyCommand::read_from(input)?)) } SegmentPolicyUpdatedCommand::TYPE_CODE => { - WireCommands::SegmentPolicyUpdated(SegmentPolicyUpdatedCommand::read_from(input)) + Ok(WireCommands::SegmentPolicyUpdated(SegmentPolicyUpdatedCommand::read_from(input)?)) } MergeSegmentsCommand::TYPE_CODE => { - WireCommands::MergeSegments(MergeSegmentsCommand::read_from(input)) + Ok(WireCommands::MergeSegments(MergeSegmentsCommand::read_from(input)?)) } MergeTableSegmentsCommand::TYPE_CODE => { - WireCommands::MergeTableSegments(MergeTableSegmentsCommand::read_from(input)) + Ok(WireCommands::MergeTableSegments(MergeTableSegmentsCommand::read_from(input)?)) } SegmentsMergedCommand::TYPE_CODE => { - WireCommands::SegmentsMerged(SegmentsMergedCommand::read_from(input)) + Ok(WireCommands::SegmentsMerged(SegmentsMergedCommand::read_from(input)?)) } SealSegmentCommand::TYPE_CODE => { - WireCommands::SealSegment(SealSegmentCommand::read_from(input)) + Ok(WireCommands::SealSegment(SealSegmentCommand::read_from(input)?)) } SealTableSegmentCommand::TYPE_CODE => { - WireCommands::SealTableSegment(SealTableSegmentCommand::read_from(input)) + Ok(WireCommands::SealTableSegment(SealTableSegmentCommand::read_from(input)?)) } SegmentSealedCommand::TYPE_CODE => { - WireCommands::SegmentSealed(SegmentSealedCommand::read_from(input)) + Ok(WireCommands::SegmentSealed(SegmentSealedCommand::read_from(input)?)) } TruncateSegmentCommand::TYPE_CODE => { - WireCommands::TruncateSegment(TruncateSegmentCommand::read_from(input)) + Ok(WireCommands::TruncateSegment(TruncateSegmentCommand::read_from(input)?)) } SegmentTruncatedCommand::TYPE_CODE => { - WireCommands::SegmentTruncated(SegmentTruncatedCommand::read_from(input)) + Ok(WireCommands::SegmentTruncated(SegmentTruncatedCommand::read_from(input)?)) } DeleteSegmentCommand::TYPE_CODE => { - WireCommands::DeleteSegment(DeleteSegmentCommand::read_from(input)) + Ok(WireCommands::DeleteSegment(DeleteSegmentCommand::read_from(input)?)) } DeleteTableSegmentCommand::TYPE_CODE => { - WireCommands::DeleteTableSegment(DeleteTableSegmentCommand::read_from(input)) + Ok(WireCommands::DeleteTableSegment(DeleteTableSegmentCommand::read_from(input)?)) } SegmentDeletedCommand::TYPE_CODE => { - WireCommands::SegmentDeleted(SegmentDeletedCommand::read_from(input)) + Ok(WireCommands::SegmentDeleted(SegmentDeletedCommand::read_from(input)?)) } KeepAliveCommand::TYPE_CODE => { - WireCommands::KeepAlive(KeepAliveCommand::read_from(input)) + Ok(WireCommands::KeepAlive(KeepAliveCommand::read_from(input)?)) } AuthTokenCheckFailedCommand::TYPE_CODE => { - WireCommands::AuthTokenCheckFailed(AuthTokenCheckFailedCommand::read_from(input)) + Ok(WireCommands::AuthTokenCheckFailed(AuthTokenCheckFailedCommand::read_from(input)?)) } UpdateTableEntriesCommand::TYPE_CODE => { - WireCommands::UpdateTableEntries(UpdateTableEntriesCommand::read_from(input)) + Ok(WireCommands::UpdateTableEntries(UpdateTableEntriesCommand::read_from(input)?)) } TableEntriesUpdatedCommand::TYPE_CODE => { - WireCommands::TableEntriesUpdated(TableEntriesUpdatedCommand::read_from(input)) + Ok(WireCommands::TableEntriesUpdated(TableEntriesUpdatedCommand::read_from(input)?)) } RemoveTableKeysCommand::TYPE_CODE => { - WireCommands::RemoveTableKeys(RemoveTableKeysCommand::read_from(input)) + Ok(WireCommands::RemoveTableKeys(RemoveTableKeysCommand::read_from(input)?)) } TableKeysRemovedCommand::TYPE_CODE => { - WireCommands::TableKeysRemoved(TableKeysRemovedCommand::read_from(input)) + Ok(WireCommands::TableKeysRemoved(TableKeysRemovedCommand::read_from(input)?)) } ReadTableCommand::TYPE_CODE => { - WireCommands::ReadTable(ReadTableCommand::read_from(input)) + Ok(WireCommands::ReadTable(ReadTableCommand::read_from(input)?)) } TableReadCommand::TYPE_CODE => { - WireCommands::TableRead(TableReadCommand::read_from(input)) + Ok(WireCommands::TableRead(TableReadCommand::read_from(input)?)) } ReadTableKeysCommand::TYPE_CODE => { - WireCommands::ReadTableKeys(ReadTableKeysCommand::read_from(input)) + Ok(WireCommands::ReadTableKeys(ReadTableKeysCommand::read_from(input)?)) } TableKeysReadCommand::TYPE_CODE => { - WireCommands::TableKeysRead(TableKeysReadCommand::read_from(input)) + Ok(WireCommands::TableKeysRead(TableKeysReadCommand::read_from(input)?)) } ReadTableEntriesCommand::TYPE_CODE => { - WireCommands::ReadTableEntries(ReadTableEntriesCommand::read_from(input)) + Ok(WireCommands::ReadTableEntries(ReadTableEntriesCommand::read_from(input)?)) } TableEntriesReadCommand::TYPE_CODE => { - WireCommands::TableEntriesRead(TableEntriesReadCommand::read_from(input)) + Ok(WireCommands::TableEntriesRead(TableEntriesReadCommand::read_from(input)?)) } TableKeyDoesNotExistCommand::TYPE_CODE => { - WireCommands::TableKeyDoesNotExist(TableKeyDoesNotExistCommand::read_from(input)) + Ok(WireCommands::TableKeyDoesNotExist(TableKeyDoesNotExistCommand::read_from(input)?)) } TableKeyBadVersionCommand::TYPE_CODE => { - WireCommands::TableKeyBadVersion(TableKeyBadVersionCommand::read_from(input)) + Ok(WireCommands::TableKeyBadVersion(TableKeyBadVersionCommand::read_from(input)?)) } - _ => WireCommands::UnknownCommand, + _ => Ok(WireCommands::UnknownCommand), } } } From 08a25fbf6374230c4905a62ecc78dc5748982000 Mon Sep 17 00:00:00 2001 From: Wenxiao Zhang Date: Thu, 9 Jan 2020 13:54:14 -0800 Subject: [PATCH 12/30] add deserialize error handling and fix fmt error --- src/lib.rs | 2 +- src/wire_protocol/commands.rs | 527 +++++++++++++++----- src/wire_protocol/connection_factory.rs | 11 +- src/wire_protocol/error.rs | 24 +- src/wire_protocol/wire_commands.rs | 634 ++++++++++++++---------- 5 files changed, 793 insertions(+), 405 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index fa599e2fb..d74ecab5c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,8 +14,8 @@ extern crate lazy_static; #[allow(dead_code)] pub mod wire_protocol { mod commands; - mod wire_commands; mod error; + mod wire_commands; //Public for docs to build. (TODO: Is there a better way to do this?) pub mod connection_factory; #[cfg(test)] diff --git a/src/wire_protocol/commands.rs b/src/wire_protocol/commands.rs index 8caee797d..5eeddd852 100644 --- a/src/wire_protocol/commands.rs +++ b/src/wire_protocol/commands.rs @@ -1,16 +1,16 @@ +use super::error::CommandError; +use super::error::InvalidData; +use super::error::Io; use bincode::Config; use byteorder::{BigEndian, ByteOrder, ReadBytesExt, WriteBytesExt}; use serde::de::{self, Deserializer, Unexpected, Visitor}; use serde::ser::Serializer; use serde::{Deserialize, Serialize}; +use snafu::ResultExt; use std::fmt; use std::i64; use std::io::Cursor; use std::io::{Read, Write}; -use snafu::ResultExt; -use super::error::CommandError; -use super::error::InvalidData; -use super::error::Io; /** * trait for Command. @@ -18,7 +18,9 @@ use super::error::Io; pub trait Command { const TYPE_CODE: i32; fn write_fields(&self) -> Result, CommandError>; - fn read_from(input: &[u8]) -> Result where Self: Sized; + fn read_from(input: &[u8]) -> Result + where + Self: Sized; } /** @@ -140,12 +142,16 @@ pub struct HelloCommand { impl Command for HelloCommand { const TYPE_CODE: i32 = -127; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: HelloCommand = CONFIG.deserialize(&input[..]).context(InvalidData{command_type: Self::TYPE_CODE})?; + let decoded: HelloCommand = CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -176,11 +182,15 @@ pub struct WrongHostCommand { impl Command for WrongHostCommand { const TYPE_CODE: i32 = 50; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: WrongHostCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: WrongHostCommand = CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -208,12 +218,17 @@ pub struct SegmentIsSealedCommand { impl Command for SegmentIsSealedCommand { const TYPE_CODE: i32 = 51; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: SegmentIsSealedCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: SegmentIsSealedCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -242,12 +257,17 @@ pub struct SegmentIsTruncatedCommand { impl Command for SegmentIsTruncatedCommand { const TYPE_CODE: i32 = 56; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: SegmentIsTruncatedCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: SegmentIsTruncatedCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -274,12 +294,17 @@ pub struct SegmentAlreadyExistsCommand { impl Command for SegmentAlreadyExistsCommand { const TYPE_CODE: i32 = 52; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: SegmentAlreadyExistsCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: SegmentAlreadyExistsCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -313,12 +338,17 @@ pub struct NoSuchSegmentCommand { impl Command for NoSuchSegmentCommand { const TYPE_CODE: i32 = 53; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: NoSuchSegmentCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: NoSuchSegmentCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -351,12 +381,17 @@ pub struct TableSegmentNotEmptyCommand { impl Command for TableSegmentNotEmptyCommand { const TYPE_CODE: i32 = 80; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: TableSegmentNotEmptyCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: TableSegmentNotEmptyCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -389,12 +424,17 @@ pub struct InvalidEventNumberCommand { impl Command for InvalidEventNumberCommand { const TYPE_CODE: i32 = 55; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: InvalidEventNumberCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: InvalidEventNumberCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -431,12 +471,17 @@ pub struct OperationUnsupportedCommand { impl Command for OperationUnsupportedCommand { const TYPE_CODE: i32 = 57; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: OperationUnsupportedCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: OperationUnsupportedCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -464,10 +509,14 @@ impl Command for PaddingCommand { fn write_fields(&self) -> Result, CommandError> { let mut res = Vec::new(); for _i in 0..(self.length / 8) { - res.write_i64::(0).context(Io{command_type: Self::TYPE_CODE})?; + res.write_i64::(0).context(Io { + command_type: Self::TYPE_CODE, + })?; } for _i in 0..(self.length % 8) { - res.write_u8(0).context(Io{command_type: Self::TYPE_CODE})?; + res.write_u8(0).context(Io { + command_type: Self::TYPE_CODE, + })?; } Ok(res) } @@ -517,8 +566,13 @@ impl Command for EventCommand { const TYPE_CODE: i32 = 0; fn write_fields(&self) -> Result, CommandError> { let mut res = Vec::new(); - res.write_i32::(EventCommand::TYPE_CODE).context(Io{command_type: Self::TYPE_CODE})?; - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + res.write_i32::(EventCommand::TYPE_CODE) + .context(Io { + command_type: Self::TYPE_CODE, + })?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; res.extend(encoded); Ok(res) } @@ -526,7 +580,9 @@ impl Command for EventCommand { fn read_from(input: &[u8]) -> Result { //read the type_code. let _type_code = BigEndian::read_i32(input); - let decoded: EventCommand = CONFIG.deserialize(&input[4..]).unwrap(); + let decoded: EventCommand = CONFIG.deserialize(&input[4..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -545,12 +601,16 @@ pub struct SetupAppendCommand { impl Command for SetupAppendCommand { const TYPE_CODE: i32 = 1; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: SetupAppendCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: SetupAppendCommand = CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -575,12 +635,16 @@ impl Command for AppendBlockCommand { //FIXME: The serialize and deserialize method need to customize; // In JAVA, it doesn't write data(because it'empty), but here it will write the prefix length(0). fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: AppendBlockCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: AppendBlockCommand = CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -602,12 +666,17 @@ impl Command for AppendBlockEndCommand { const TYPE_CODE: i32 = 4; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: AppendBlockEndCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: AppendBlockEndCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -641,21 +710,40 @@ impl Command for ConditionalAppendCommand { // Because in CondtionalAppend the event should be serialize as |type_code|length|data| fn write_fields(&self) -> Result, CommandError> { let mut res = Vec::new(); - res.write_u128::(self.writer_id).context(Io{command_type: Self::TYPE_CODE})?; - res.write_i64::(self.event_number).context(Io{command_type: Self::TYPE_CODE})?; - res.write_i64::(self.expected_offset).context(Io{command_type: Self::TYPE_CODE})?; - res.write_all(&self.event.write_fields()?).context(Io{command_type: Self::TYPE_CODE})?; - res.write_i64::(self.request_id).context(Io{command_type: Self::TYPE_CODE})?; + res.write_u128::(self.writer_id).context(Io { + command_type: Self::TYPE_CODE, + })?; + res.write_i64::(self.event_number).context(Io { + command_type: Self::TYPE_CODE, + })?; + res.write_i64::(self.expected_offset) + .context(Io { + command_type: Self::TYPE_CODE, + })?; + res.write_all(&self.event.write_fields()?).context(Io { + command_type: Self::TYPE_CODE, + })?; + res.write_i64::(self.request_id).context(Io { + command_type: Self::TYPE_CODE, + })?; Ok(res) } fn read_from(input: &[u8]) -> Result { let mut rdr = Cursor::new(input); - let writer_id = rdr.read_u128::().unwrap(); - let event_number = rdr.read_i64::().unwrap(); - let expected_offset = rdr.read_i64::().unwrap(); + let writer_id = rdr.read_u128::().context(Io { + command_type: Self::TYPE_CODE, + })?; + let event_number = rdr.read_i64::().context(Io { + command_type: Self::TYPE_CODE, + })?; + let expected_offset = rdr.read_i64::().context(Io { + command_type: Self::TYPE_CODE, + })?; let event = ConditionalAppendCommand::read_event(&mut rdr); - let request_id = rdr.read_i64::().unwrap(); + let request_id = rdr.read_i64::().context(Io { + command_type: Self::TYPE_CODE, + })?; Ok(ConditionalAppendCommand { writer_id, event_number, @@ -687,12 +775,16 @@ impl Command for AppendSetupCommand { const TYPE_CODE: i32 = 2; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: AppendSetupCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: AppendSetupCommand = CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -719,12 +811,16 @@ impl Command for DataAppendedCommand { const TYPE_CODE: i32 = 7; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: DataAppendedCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: DataAppendedCommand = CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -749,12 +845,17 @@ impl Command for ConditionalCheckFailedCommand { const TYPE_CODE: i32 = 8; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: ConditionalCheckFailedCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: ConditionalCheckFailedCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -781,12 +882,16 @@ impl Command for ReadSegmentCommand { const TYPE_CODE: i32 = 9; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: ReadSegmentCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: ReadSegmentCommand = CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -814,12 +919,16 @@ impl Command for SegmentReadCommand { const TYPE_CODE: i32 = 10; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: SegmentReadCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: SegmentReadCommand = CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -845,12 +954,17 @@ impl Command for GetSegmentAttributeCommand { const TYPE_CODE: i32 = 34; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: GetSegmentAttributeCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: GetSegmentAttributeCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -874,12 +988,17 @@ impl Command for SegmentAttributeCommand { const TYPE_CODE: i32 = 35; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: SegmentAttributeCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: SegmentAttributeCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -907,12 +1026,17 @@ impl Command for UpdateSegmentAttributeCommand { const TYPE_CODE: i32 = 36; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: UpdateSegmentAttributeCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: UpdateSegmentAttributeCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -936,12 +1060,17 @@ impl Command for SegmentAttributeUpdatedCommand { const TYPE_CODE: i32 = 37; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: SegmentAttributeUpdatedCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: SegmentAttributeUpdatedCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -966,12 +1095,17 @@ impl Command for GetStreamSegmentInfoCommand { const TYPE_CODE: i32 = 11; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: GetStreamSegmentInfoCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: GetStreamSegmentInfoCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -1001,12 +1135,17 @@ impl Command for StreamSegmentInfoCommand { const TYPE_CODE: i32 = 12; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: StreamSegmentInfoCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: StreamSegmentInfoCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -1033,12 +1172,17 @@ impl Command for CreateSegmentCommand { const TYPE_CODE: i32 = 20; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: CreateSegmentCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: CreateSegmentCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -1063,12 +1207,17 @@ impl Command for CreateTableSegmentCommand { const TYPE_CODE: i32 = 70; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: CreateTableSegmentCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: CreateTableSegmentCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -1092,12 +1241,17 @@ impl Command for SegmentCreatedCommand { const TYPE_CODE: i32 = 21; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: SegmentCreatedCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: SegmentCreatedCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -1124,12 +1278,17 @@ impl Command for UpdateSegmentPolicyCommand { const TYPE_CODE: i32 = 32; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: UpdateSegmentPolicyCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: UpdateSegmentPolicyCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -1153,12 +1312,17 @@ impl Command for SegmentPolicyUpdatedCommand { const TYPE_CODE: i32 = 33; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: SegmentPolicyUpdatedCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: SegmentPolicyUpdatedCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -1184,12 +1348,17 @@ impl Command for MergeSegmentsCommand { const TYPE_CODE: i32 = 58; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: MergeSegmentsCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: MergeSegmentsCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -1214,12 +1383,17 @@ impl Command for MergeTableSegmentsCommand { const TYPE_CODE: i32 = 72; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: MergeTableSegmentsCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: MergeTableSegmentsCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -1245,12 +1419,17 @@ impl Command for SegmentsMergedCommand { const TYPE_CODE: i32 = 59; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: SegmentsMergedCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: SegmentsMergedCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -1275,12 +1454,16 @@ impl Command for SealSegmentCommand { const TYPE_CODE: i32 = 28; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: SealSegmentCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: SealSegmentCommand = CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -1305,12 +1488,17 @@ impl Command for SealTableSegmentCommand { const TYPE_CODE: i32 = 73; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: SealTableSegmentCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: SealTableSegmentCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -1334,12 +1522,17 @@ impl Command for SegmentSealedCommand { const TYPE_CODE: i32 = 29; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: SegmentSealedCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: SegmentSealedCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -1365,12 +1558,17 @@ impl Command for TruncateSegmentCommand { const TYPE_CODE: i32 = 38; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: TruncateSegmentCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: TruncateSegmentCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -1394,12 +1592,17 @@ impl Command for SegmentTruncatedCommand { const TYPE_CODE: i32 = 39; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: SegmentTruncatedCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: SegmentTruncatedCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -1424,12 +1627,17 @@ impl Command for DeleteSegmentCommand { const TYPE_CODE: i32 = 30; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: DeleteSegmentCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: DeleteSegmentCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -1454,12 +1662,17 @@ pub struct DeleteTableSegmentCommand { impl Command for DeleteTableSegmentCommand { const TYPE_CODE: i32 = 71; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: DeleteTableSegmentCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: DeleteTableSegmentCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -1483,12 +1696,17 @@ impl Command for SegmentDeletedCommand { const TYPE_CODE: i32 = 31; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: SegmentDeletedCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: SegmentDeletedCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -1556,12 +1774,17 @@ impl Command for AuthTokenCheckFailedCommand { const TYPE_CODE: i32 = 60; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: AuthTokenCheckFailedCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: AuthTokenCheckFailedCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -1612,12 +1835,17 @@ impl Command for UpdateTableEntriesCommand { const TYPE_CODE: i32 = 74; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: UpdateTableEntriesCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: UpdateTableEntriesCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -1641,12 +1869,17 @@ impl Command for TableEntriesUpdatedCommand { const TYPE_CODE: i32 = 75; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: TableEntriesUpdatedCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: TableEntriesUpdatedCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -1672,12 +1905,17 @@ impl Command for RemoveTableKeysCommand { const TYPE_CODE: i32 = 76; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: RemoveTableKeysCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: RemoveTableKeysCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -1701,12 +1939,17 @@ impl Command for TableKeysRemovedCommand { const TYPE_CODE: i32 = 77; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: TableKeysRemovedCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: TableKeysRemovedCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -1732,12 +1975,16 @@ impl Command for ReadTableCommand { const TYPE_CODE: i32 = 78; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: ReadTableCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: ReadTableCommand = CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -1762,12 +2009,16 @@ impl Command for TableReadCommand { const TYPE_CODE: i32 = 79; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: TableReadCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: TableReadCommand = CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -1794,12 +2045,17 @@ impl Command for ReadTableKeysCommand { const TYPE_CODE: i32 = 83; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: ReadTableKeysCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: ReadTableKeysCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -1825,12 +2081,17 @@ impl Command for TableKeysReadCommand { const TYPE_CODE: i32 = 84; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: TableKeysReadCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: TableKeysReadCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -1857,12 +2118,17 @@ impl Command for ReadTableEntriesCommand { const TYPE_CODE: i32 = 85; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: ReadTableEntriesCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: ReadTableEntriesCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -1888,12 +2154,17 @@ impl Command for TableEntriesReadCommand { const TYPE_CODE: i32 = 86; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: TableEntriesReadCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: TableEntriesReadCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -1918,12 +2189,17 @@ impl Command for TableKeyDoesNotExistCommand { const TYPE_CODE: i32 = 81; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: TableKeyDoesNotExistCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: TableKeyDoesNotExistCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } @@ -1962,12 +2238,17 @@ impl Command for TableKeyBadVersionCommand { const TYPE_CODE: i32 = 82; fn write_fields(&self) -> Result, CommandError> { - let encoded = CONFIG.serialize(&self).context(InvalidData{command_type: Self::TYPE_CODE})?; + let encoded = CONFIG.serialize(&self).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(encoded) } fn read_from(input: &[u8]) -> Result { - let decoded: TableKeyBadVersionCommand = CONFIG.deserialize(&input[..]).unwrap(); + let decoded: TableKeyBadVersionCommand = + CONFIG.deserialize(&input[..]).context(InvalidData { + command_type: Self::TYPE_CODE, + })?; Ok(decoded) } } diff --git a/src/wire_protocol/connection_factory.rs b/src/wire_protocol/connection_factory.rs index b29ee6c8e..7532ef2a5 100644 --- a/src/wire_protocol/connection_factory.rs +++ b/src/wire_protocol/connection_factory.rs @@ -7,16 +7,17 @@ // // http://www.apache.org/licenses/LICENSE-2.0 // + +use super::error::Connect; +use super::error::ConnectionError; +use super::error::ReadData; +use super::error::SendData; use async_trait::async_trait; -use snafu::{ResultExt}; +use snafu::ResultExt; use std::fmt; use std::net::SocketAddr; use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::net::TcpStream; -use super::error::ConnectionError; -use super::error::Connect; -use super::error::SendData; -use super::error::ReadData; #[derive(Debug)] pub enum ConnectionType { diff --git a/src/wire_protocol/error.rs b/src/wire_protocol/error.rs index c7a1b4e82..db4f2eb79 100644 --- a/src/wire_protocol/error.rs +++ b/src/wire_protocol/error.rs @@ -1,18 +1,17 @@ -use std::net::SocketAddr; +use super::connection_factory::ConnectionType; use bincode::Error as BincodeError; +use snafu::Snafu; use std::io::Error as IoError; -use snafu::{Snafu}; -use super::connection_factory::ConnectionType; - +use std::net::SocketAddr; /// This kind of error that can be produced during Pravega client connecting to server. #[derive(Debug, Snafu)] #[snafu(visibility = "pub(crate)")] pub enum ConnectionError { #[snafu(display( - "Could not connect to endpoint {} using connection type {}", - endpoint, - connection_type + "Could not connect to endpoint {} using connection type {}", + endpoint, + connection_type ))] Connect { connection_type: ConnectionType, @@ -28,7 +27,7 @@ pub enum ConnectionError { ReadData { endpoint: SocketAddr, source: std::io::Error, - } + }, } /// This kind of error that can be produced during Pravega serialize and deserialize the wire commands. @@ -41,12 +40,5 @@ pub enum CommandError { source: BincodeError, }, #[snafu(display("Could not serialize command {} because of: {}", command_type, source))] - Io { - command_type: i32, - source: IoError, - } + Io { command_type: i32, source: IoError }, } - - - - diff --git a/src/wire_protocol/wire_commands.rs b/src/wire_protocol/wire_commands.rs index e8ec7b82e..9b344b855 100644 --- a/src/wire_protocol/wire_commands.rs +++ b/src/wire_protocol/wire_commands.rs @@ -85,339 +85,451 @@ impl Encode for WireCommands { res.extend(se); } WireCommands::WrongHost(wrong_host_cmd) => { - res.write_i32::(WrongHostCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(WrongHostCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = wrong_host_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::SegmentIsSealed(seg_is_sealed_cmd) => { - res.write_i32::(SegmentIsSealedCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(SegmentIsSealedCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = seg_is_sealed_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::SegmentAlreadyExists(seg_already_exists_cmd) => { - res.write_i32::(SegmentAlreadyExistsCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(SegmentAlreadyExistsCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = seg_already_exists_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::SegmentIsTruncated(seg_is_truncated_cmd) => { - res.write_i32::(SegmentIsTruncatedCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(SegmentIsTruncatedCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = seg_is_truncated_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::NoSuchSegment(no_such_seg_cmd) => { - res.write_i32::(NoSuchSegmentCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(NoSuchSegmentCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = no_such_seg_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::TableSegmentNotEmpty(table_seg_not_empty_cmd) => { - res.write_i32::(TableSegmentNotEmptyCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(TableSegmentNotEmptyCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = table_seg_not_empty_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::InvalidEventNumber(invalid_event_num_cmd) => { - res.write_i32::(InvalidEventNumberCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(InvalidEventNumberCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = invalid_event_num_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::OperationUnsupported(operation_unsupported_cmd) => { - res.write_i32::(OperationUnsupportedCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(OperationUnsupportedCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = operation_unsupported_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::Padding(padding_command) => { - res.write_i32::(PaddingCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(PaddingCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = padding_command.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::PartialEvent(partial_event_cmd) => { - res.write_i32::(PartialEventCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(PartialEventCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = partial_event_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::Event(event_cmd) => { - res.write_i32::(EventCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(EventCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = event_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::SetupAppend(setup_append_cmd) => { - res.write_i32::(SetupAppendCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(SetupAppendCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = setup_append_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::AppendBlock(append_block_cmd) => { - res.write_i32::(AppendBlockCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(AppendBlockCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = append_block_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::AppendBlockEnd(append_block_end_cmd) => { - res.write_i32::(AppendBlockEndCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(AppendBlockEndCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = append_block_end_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::ConditionalAppend(conditional_append_cmd) => { - res.write_i32::(ConditionalAppendCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(ConditionalAppendCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = conditional_append_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::AppendSetup(append_setup_cmd) => { - res.write_i32::(AppendSetupCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(AppendSetupCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = append_setup_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::DataAppended(data_appended_cmd) => { - res.write_i32::(DataAppendedCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(DataAppendedCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = data_appended_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::ConditionalCheckFailed(conditional_check_failed_cmd) => { - res.write_i32::(ConditionalCheckFailedCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(ConditionalCheckFailedCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = conditional_check_failed_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::ReadSegment(read_segment_cmd) => { - res.write_i32::(ReadSegmentCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(ReadSegmentCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = read_segment_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::SegmentRead(segment_read_cmd) => { - res.write_i32::(SegmentReadCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(SegmentReadCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = segment_read_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::GetSegmentAttribute(get_segment_attribute_cmd) => { - res.write_i32::(GetSegmentAttributeCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(GetSegmentAttributeCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = get_segment_attribute_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::SegmentAttribute(segment_attribute_cmd) => { - res.write_i32::(SegmentAttributeCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(SegmentAttributeCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = segment_attribute_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::UpdateSegmentAttribute(update_segment_attribute_cmd) => { - res.write_i32::(UpdateSegmentAttributeCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(UpdateSegmentAttributeCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = update_segment_attribute_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::SegmentAttributeUpdated(segment_attribute_updated_cmd) => { - res.write_i32::(SegmentAttributeUpdatedCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(SegmentAttributeUpdatedCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = segment_attribute_updated_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::GetStreamSegmentInfo(get_stream_segment_info_cmd) => { - res.write_i32::(GetStreamSegmentInfoCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(GetStreamSegmentInfoCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = get_stream_segment_info_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::StreamSegmentInfo(stream_segment_info_cmd) => { - res.write_i32::(StreamSegmentInfoCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(StreamSegmentInfoCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = stream_segment_info_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::CreateSegment(create_segment_cmd) => { - res.write_i32::(CreateSegmentCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(CreateSegmentCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = create_segment_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::CreateTableSegment(create_table_segment_command) => { - res.write_i32::(CreateTableSegmentCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(CreateTableSegmentCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = create_table_segment_command.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::SegmentCreated(segment_created_cmd) => { - res.write_i32::(SegmentCreatedCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(SegmentCreatedCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = segment_created_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::UpdateSegmentPolicy(update_segment_policy_cmd) => { - res.write_i32::(UpdateSegmentPolicyCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(UpdateSegmentPolicyCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = update_segment_policy_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::SegmentPolicyUpdated(segment_policy_updated_cmd) => { - res.write_i32::(SegmentPolicyUpdatedCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(SegmentPolicyUpdatedCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = segment_policy_updated_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se) } WireCommands::MergeSegments(merge_segments_cmd) => { - res.write_i32::(MergeSegmentsCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(MergeSegmentsCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = merge_segments_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::MergeTableSegments(merge_table_segments_cmd) => { - res.write_i32::(MergeTableSegmentsCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(MergeTableSegmentsCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = merge_table_segments_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::SegmentsMerged(segments_merged_cmd) => { - res.write_i32::(SegmentsMergedCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(SegmentsMergedCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = segments_merged_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::SealSegment(seal_segment_cmd) => { - res.write_i32::(SealSegmentCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(SealSegmentCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = seal_segment_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::SealTableSegment(seal_table_segment_cmd) => { - res.write_i32::(SealTableSegmentCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(SealTableSegmentCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = seal_table_segment_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::SegmentSealed(segment_sealed_cmd) => { - res.write_i32::(SegmentSealedCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(SegmentSealedCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = segment_sealed_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::TruncateSegment(truncate_segment_cmd) => { - res.write_i32::(TruncateSegmentCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(TruncateSegmentCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = truncate_segment_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::SegmentTruncated(segment_truncated_cmd) => { - res.write_i32::(SegmentTruncatedCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(SegmentTruncatedCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = segment_truncated_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::DeleteSegment(delete_segment_cmd) => { - res.write_i32::(DeleteSegmentCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(DeleteSegmentCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = delete_segment_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::DeleteTableSegment(delete_table_segment_cmd) => { - res.write_i32::(DeleteTableSegmentCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(DeleteTableSegmentCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = delete_table_segment_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::SegmentDeleted(segment_deleted_cmd) => { - res.write_i32::(SegmentDeletedCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(SegmentDeletedCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = segment_deleted_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::KeepAlive(keep_alive_cmd) => { - res.write_i32::(KeepAliveCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(KeepAliveCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = keep_alive_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::AuthTokenCheckFailed(auth_token_check_failed_cmd) => { - res.write_i32::(AuthTokenCheckFailedCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(AuthTokenCheckFailedCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = auth_token_check_failed_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::UpdateTableEntries(update_table_entries_cmd) => { - res.write_i32::(UpdateTableEntriesCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(UpdateTableEntriesCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = update_table_entries_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::TableEntriesUpdated(table_entries_updated_cmd) => { - res.write_i32::(TableEntriesUpdatedCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(TableEntriesUpdatedCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = table_entries_updated_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::RemoveTableKeys(remove_table_keys_cmd) => { - res.write_i32::(RemoveTableKeysCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(RemoveTableKeysCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = remove_table_keys_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::TableKeysRemoved(table_key_removed_cmd) => { - res.write_i32::(TableKeysRemovedCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(TableKeysRemovedCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = table_key_removed_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::ReadTable(read_table_cmd) => { - res.write_i32::(ReadTableCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(ReadTableCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = read_table_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::TableRead(table_read_cmd) => { - res.write_i32::(TableReadCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(TableReadCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = table_read_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::ReadTableKeys(read_table_keys_cmd) => { - res.write_i32::(ReadTableKeysCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(ReadTableKeysCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = read_table_keys_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::TableKeysRead(table_keys_read_cmd) => { - res.write_i32::(TableKeysReadCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(TableKeysReadCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = table_keys_read_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::ReadTableEntries(read_table_entries_cmd) => { - res.write_i32::(ReadTableEntriesCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(ReadTableEntriesCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = read_table_entries_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::TableEntriesRead(table_entries_read_cmd) => { - res.write_i32::(TableEntriesReadCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(TableEntriesReadCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = table_entries_read_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::TableKeyDoesNotExist(table_key_does_not_exist_cmd) => { - res.write_i32::(TableKeyDoesNotExistCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(TableKeyDoesNotExistCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = table_key_does_not_exist_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } WireCommands::TableKeyBadVersion(table_key_bad_version_cmd) => { - res.write_i32::(TableKeyBadVersionCommand::TYPE_CODE).expect("Writing to an in memory vec"); + res.write_i32::(TableKeyBadVersionCommand::TYPE_CODE) + .expect("Writing to an in memory vec"); let se = table_key_bad_version_cmd.write_fields()?; - res.write_i32::(se.len() as i32).expect("Writing to an in memory vec"); + res.write_i32::(se.len() as i32) + .expect("Writing to an in memory vec"); res.extend(se); } _ => panic!("Unknown WireCommands"), @@ -433,173 +545,175 @@ impl Decode for WireCommands { let input = &raw_input[8..]; match type_code { HelloCommand::TYPE_CODE => Ok(WireCommands::Hello(HelloCommand::read_from(input)?)), - WrongHostCommand::TYPE_CODE => { Ok(WireCommands::WrongHost(WrongHostCommand::read_from(input)?)) } - SegmentIsSealedCommand::TYPE_CODE => { - Ok(WireCommands::SegmentIsSealed(SegmentIsSealedCommand::read_from(input)?)) - } - SegmentAlreadyExistsCommand::TYPE_CODE => { - Ok(WireCommands::SegmentAlreadyExists(SegmentAlreadyExistsCommand::read_from(input)?)) - } - SegmentIsTruncatedCommand::TYPE_CODE => { - Ok(WireCommands::SegmentIsTruncated(SegmentIsTruncatedCommand::read_from(input)?)) - } - NoSuchSegmentCommand::TYPE_CODE => { - Ok(WireCommands::NoSuchSegment(NoSuchSegmentCommand::read_from(input)?)) + SegmentIsSealedCommand::TYPE_CODE => Ok(WireCommands::SegmentIsSealed( + SegmentIsSealedCommand::read_from(input)?, + )), + SegmentAlreadyExistsCommand::TYPE_CODE => Ok(WireCommands::SegmentAlreadyExists( + SegmentAlreadyExistsCommand::read_from(input)?, + )), + SegmentIsTruncatedCommand::TYPE_CODE => Ok(WireCommands::SegmentIsTruncated( + SegmentIsTruncatedCommand::read_from(input)?, + )), + NoSuchSegmentCommand::TYPE_CODE => Ok(WireCommands::NoSuchSegment( + NoSuchSegmentCommand::read_from(input)?, + )), + TableSegmentNotEmptyCommand::TYPE_CODE => Ok(WireCommands::TableSegmentNotEmpty( + TableSegmentNotEmptyCommand::read_from(input)?, + )), + InvalidEventNumberCommand::TYPE_CODE => Ok(WireCommands::InvalidEventNumber( + InvalidEventNumberCommand::read_from(input)?, + )), + OperationUnsupportedCommand::TYPE_CODE => Ok(WireCommands::OperationUnsupported( + OperationUnsupportedCommand::read_from(input)?, + )), + PaddingCommand::TYPE_CODE => { + Ok(WireCommands::Padding(PaddingCommand::read_from(input)?)) } - TableSegmentNotEmptyCommand::TYPE_CODE => { - Ok(WireCommands::TableSegmentNotEmpty(TableSegmentNotEmptyCommand::read_from(input)?)) - } - InvalidEventNumberCommand::TYPE_CODE => { - Ok(WireCommands::InvalidEventNumber(InvalidEventNumberCommand::read_from(input)?)) - } - OperationUnsupportedCommand::TYPE_CODE => { - Ok(WireCommands::OperationUnsupported(OperationUnsupportedCommand::read_from(input)?)) - } - PaddingCommand::TYPE_CODE => Ok(WireCommands::Padding(PaddingCommand::read_from(input)?)), - PartialEventCommand::TYPE_CODE => { - Ok(WireCommands::PartialEvent(PartialEventCommand::read_from(input)?)) - } + PartialEventCommand::TYPE_CODE => Ok(WireCommands::PartialEvent( + PartialEventCommand::read_from(input)?, + )), + EventCommand::TYPE_CODE => Ok(WireCommands::Event(EventCommand::read_from(input)?)), - SetupAppendCommand::TYPE_CODE => { - Ok(WireCommands::SetupAppend(SetupAppendCommand::read_from(input)?)) - } - AppendBlockCommand::TYPE_CODE => { - Ok(WireCommands::AppendBlock(AppendBlockCommand::read_from(input)?)) - } - AppendBlockEndCommand::TYPE_CODE => { - Ok(WireCommands::AppendBlockEnd(AppendBlockEndCommand::read_from(input)?)) - } - ConditionalAppendCommand::TYPE_CODE => { - Ok(WireCommands::ConditionalAppend(ConditionalAppendCommand::read_from(input)?)) - } - AppendSetupCommand::TYPE_CODE => { - Ok(WireCommands::AppendSetup(AppendSetupCommand::read_from(input)?)) - } - DataAppendedCommand::TYPE_CODE => { - Ok(WireCommands::DataAppended(DataAppendedCommand::read_from(input)?)) - } + SetupAppendCommand::TYPE_CODE => Ok(WireCommands::SetupAppend( + SetupAppendCommand::read_from(input)?, + )), + AppendBlockCommand::TYPE_CODE => Ok(WireCommands::AppendBlock( + AppendBlockCommand::read_from(input)?, + )), + AppendBlockEndCommand::TYPE_CODE => Ok(WireCommands::AppendBlockEnd( + AppendBlockEndCommand::read_from(input)?, + )), + ConditionalAppendCommand::TYPE_CODE => Ok(WireCommands::ConditionalAppend( + ConditionalAppendCommand::read_from(input)?, + )), + AppendSetupCommand::TYPE_CODE => Ok(WireCommands::AppendSetup( + AppendSetupCommand::read_from(input)?, + )), + DataAppendedCommand::TYPE_CODE => Ok(WireCommands::DataAppended( + DataAppendedCommand::read_from(input)?, + )), ConditionalCheckFailedCommand::TYPE_CODE => Ok(WireCommands::ConditionalCheckFailed( - ConditionalCheckFailedCommand::read_from(input)?)), - - ReadSegmentCommand::TYPE_CODE => { - Ok(WireCommands::ReadSegment(ReadSegmentCommand::read_from(input)?)) - } - SegmentReadCommand::TYPE_CODE => { - Ok(WireCommands::SegmentRead(SegmentReadCommand::read_from(input)?)) - } - GetSegmentAttributeCommand::TYPE_CODE => { - Ok(WireCommands::GetSegmentAttribute(GetSegmentAttributeCommand::read_from(input)?)) - } - SegmentAttributeCommand::TYPE_CODE => { - Ok(WireCommands::SegmentAttribute(SegmentAttributeCommand::read_from(input)?)) - } + ConditionalCheckFailedCommand::read_from(input)?, + )), + ReadSegmentCommand::TYPE_CODE => Ok(WireCommands::ReadSegment( + ReadSegmentCommand::read_from(input)?, + )), + SegmentReadCommand::TYPE_CODE => Ok(WireCommands::SegmentRead( + SegmentReadCommand::read_from(input)?, + )), + GetSegmentAttributeCommand::TYPE_CODE => Ok(WireCommands::GetSegmentAttribute( + GetSegmentAttributeCommand::read_from(input)?, + )), + SegmentAttributeCommand::TYPE_CODE => Ok(WireCommands::SegmentAttribute( + SegmentAttributeCommand::read_from(input)?, + )), UpdateSegmentAttributeCommand::TYPE_CODE => Ok(WireCommands::UpdateSegmentAttribute( - UpdateSegmentAttributeCommand::read_from(input)?)), - + UpdateSegmentAttributeCommand::read_from(input)?, + )), SegmentAttributeUpdatedCommand::TYPE_CODE => Ok(WireCommands::SegmentAttributeUpdated( - SegmentAttributeUpdatedCommand::read_from(input)?)), - - GetStreamSegmentInfoCommand::TYPE_CODE => { - Ok(WireCommands::GetStreamSegmentInfo(GetStreamSegmentInfoCommand::read_from(input)?)) - } - StreamSegmentInfoCommand::TYPE_CODE => { - Ok(WireCommands::StreamSegmentInfo(StreamSegmentInfoCommand::read_from(input)?)) - } - CreateSegmentCommand::TYPE_CODE => { - Ok(WireCommands::CreateSegment(CreateSegmentCommand::read_from(input)?)) - } - CreateTableSegmentCommand::TYPE_CODE => { - Ok(WireCommands::CreateTableSegment(CreateTableSegmentCommand::read_from(input)?)) - } - SegmentCreatedCommand::TYPE_CODE => { - Ok(WireCommands::SegmentCreated(SegmentCreatedCommand::read_from(input)?)) - } - UpdateSegmentPolicyCommand::TYPE_CODE => { - Ok(WireCommands::UpdateSegmentPolicy(UpdateSegmentPolicyCommand::read_from(input)?)) - } - SegmentPolicyUpdatedCommand::TYPE_CODE => { - Ok(WireCommands::SegmentPolicyUpdated(SegmentPolicyUpdatedCommand::read_from(input)?)) - } - MergeSegmentsCommand::TYPE_CODE => { - Ok(WireCommands::MergeSegments(MergeSegmentsCommand::read_from(input)?)) - } - MergeTableSegmentsCommand::TYPE_CODE => { - Ok(WireCommands::MergeTableSegments(MergeTableSegmentsCommand::read_from(input)?)) - } - SegmentsMergedCommand::TYPE_CODE => { - Ok(WireCommands::SegmentsMerged(SegmentsMergedCommand::read_from(input)?)) - } - SealSegmentCommand::TYPE_CODE => { - Ok(WireCommands::SealSegment(SealSegmentCommand::read_from(input)?)) - } - SealTableSegmentCommand::TYPE_CODE => { - Ok(WireCommands::SealTableSegment(SealTableSegmentCommand::read_from(input)?)) - } - SegmentSealedCommand::TYPE_CODE => { - Ok(WireCommands::SegmentSealed(SegmentSealedCommand::read_from(input)?)) - } - TruncateSegmentCommand::TYPE_CODE => { - Ok(WireCommands::TruncateSegment(TruncateSegmentCommand::read_from(input)?)) - } - SegmentTruncatedCommand::TYPE_CODE => { - Ok(WireCommands::SegmentTruncated(SegmentTruncatedCommand::read_from(input)?)) - } - DeleteSegmentCommand::TYPE_CODE => { - Ok(WireCommands::DeleteSegment(DeleteSegmentCommand::read_from(input)?)) - } - DeleteTableSegmentCommand::TYPE_CODE => { - Ok(WireCommands::DeleteTableSegment(DeleteTableSegmentCommand::read_from(input)?)) - } - SegmentDeletedCommand::TYPE_CODE => { - Ok(WireCommands::SegmentDeleted(SegmentDeletedCommand::read_from(input)?)) - } + SegmentAttributeUpdatedCommand::read_from(input)?, + )), + GetStreamSegmentInfoCommand::TYPE_CODE => Ok(WireCommands::GetStreamSegmentInfo( + GetStreamSegmentInfoCommand::read_from(input)?, + )), + StreamSegmentInfoCommand::TYPE_CODE => Ok(WireCommands::StreamSegmentInfo( + StreamSegmentInfoCommand::read_from(input)?, + )), + CreateSegmentCommand::TYPE_CODE => Ok(WireCommands::CreateSegment( + CreateSegmentCommand::read_from(input)?, + )), + CreateTableSegmentCommand::TYPE_CODE => Ok(WireCommands::CreateTableSegment( + CreateTableSegmentCommand::read_from(input)?, + )), + SegmentCreatedCommand::TYPE_CODE => Ok(WireCommands::SegmentCreated( + SegmentCreatedCommand::read_from(input)?, + )), + UpdateSegmentPolicyCommand::TYPE_CODE => Ok(WireCommands::UpdateSegmentPolicy( + UpdateSegmentPolicyCommand::read_from(input)?, + )), + SegmentPolicyUpdatedCommand::TYPE_CODE => Ok(WireCommands::SegmentPolicyUpdated( + SegmentPolicyUpdatedCommand::read_from(input)?, + )), + MergeSegmentsCommand::TYPE_CODE => Ok(WireCommands::MergeSegments( + MergeSegmentsCommand::read_from(input)?, + )), + MergeTableSegmentsCommand::TYPE_CODE => Ok(WireCommands::MergeTableSegments( + MergeTableSegmentsCommand::read_from(input)?, + )), + SegmentsMergedCommand::TYPE_CODE => Ok(WireCommands::SegmentsMerged( + SegmentsMergedCommand::read_from(input)?, + )), + SealSegmentCommand::TYPE_CODE => Ok(WireCommands::SealSegment( + SealSegmentCommand::read_from(input)?, + )), + SealTableSegmentCommand::TYPE_CODE => Ok(WireCommands::SealTableSegment( + SealTableSegmentCommand::read_from(input)?, + )), + SegmentSealedCommand::TYPE_CODE => Ok(WireCommands::SegmentSealed( + SegmentSealedCommand::read_from(input)?, + )), + TruncateSegmentCommand::TYPE_CODE => Ok(WireCommands::TruncateSegment( + TruncateSegmentCommand::read_from(input)?, + )), + SegmentTruncatedCommand::TYPE_CODE => Ok(WireCommands::SegmentTruncated( + SegmentTruncatedCommand::read_from(input)?, + )), + DeleteSegmentCommand::TYPE_CODE => Ok(WireCommands::DeleteSegment( + DeleteSegmentCommand::read_from(input)?, + )), + DeleteTableSegmentCommand::TYPE_CODE => Ok(WireCommands::DeleteTableSegment( + DeleteTableSegmentCommand::read_from(input)?, + )), + SegmentDeletedCommand::TYPE_CODE => Ok(WireCommands::SegmentDeleted( + SegmentDeletedCommand::read_from(input)?, + )), KeepAliveCommand::TYPE_CODE => { Ok(WireCommands::KeepAlive(KeepAliveCommand::read_from(input)?)) } - AuthTokenCheckFailedCommand::TYPE_CODE => { - Ok(WireCommands::AuthTokenCheckFailed(AuthTokenCheckFailedCommand::read_from(input)?)) - } - UpdateTableEntriesCommand::TYPE_CODE => { - Ok(WireCommands::UpdateTableEntries(UpdateTableEntriesCommand::read_from(input)?)) - } - TableEntriesUpdatedCommand::TYPE_CODE => { - Ok(WireCommands::TableEntriesUpdated(TableEntriesUpdatedCommand::read_from(input)?)) - } - RemoveTableKeysCommand::TYPE_CODE => { - Ok(WireCommands::RemoveTableKeys(RemoveTableKeysCommand::read_from(input)?)) - } - TableKeysRemovedCommand::TYPE_CODE => { - Ok(WireCommands::TableKeysRemoved(TableKeysRemovedCommand::read_from(input)?)) - } + AuthTokenCheckFailedCommand::TYPE_CODE => Ok(WireCommands::AuthTokenCheckFailed( + AuthTokenCheckFailedCommand::read_from(input)?, + )), + UpdateTableEntriesCommand::TYPE_CODE => Ok(WireCommands::UpdateTableEntries( + UpdateTableEntriesCommand::read_from(input)?, + )), + TableEntriesUpdatedCommand::TYPE_CODE => Ok(WireCommands::TableEntriesUpdated( + TableEntriesUpdatedCommand::read_from(input)?, + )), + RemoveTableKeysCommand::TYPE_CODE => Ok(WireCommands::RemoveTableKeys( + RemoveTableKeysCommand::read_from(input)?, + )), + TableKeysRemovedCommand::TYPE_CODE => Ok(WireCommands::TableKeysRemoved( + TableKeysRemovedCommand::read_from(input)?, + )), ReadTableCommand::TYPE_CODE => { Ok(WireCommands::ReadTable(ReadTableCommand::read_from(input)?)) } TableReadCommand::TYPE_CODE => { Ok(WireCommands::TableRead(TableReadCommand::read_from(input)?)) } - ReadTableKeysCommand::TYPE_CODE => { - Ok(WireCommands::ReadTableKeys(ReadTableKeysCommand::read_from(input)?)) - } - TableKeysReadCommand::TYPE_CODE => { - Ok(WireCommands::TableKeysRead(TableKeysReadCommand::read_from(input)?)) - } - ReadTableEntriesCommand::TYPE_CODE => { - Ok(WireCommands::ReadTableEntries(ReadTableEntriesCommand::read_from(input)?)) - } - TableEntriesReadCommand::TYPE_CODE => { - Ok(WireCommands::TableEntriesRead(TableEntriesReadCommand::read_from(input)?)) - } - TableKeyDoesNotExistCommand::TYPE_CODE => { - Ok(WireCommands::TableKeyDoesNotExist(TableKeyDoesNotExistCommand::read_from(input)?)) - } - TableKeyBadVersionCommand::TYPE_CODE => { - Ok(WireCommands::TableKeyBadVersion(TableKeyBadVersionCommand::read_from(input)?)) - } + ReadTableKeysCommand::TYPE_CODE => Ok(WireCommands::ReadTableKeys( + ReadTableKeysCommand::read_from(input)?, + )), + TableKeysReadCommand::TYPE_CODE => Ok(WireCommands::TableKeysRead( + TableKeysReadCommand::read_from(input)?, + )), + ReadTableEntriesCommand::TYPE_CODE => Ok(WireCommands::ReadTableEntries( + ReadTableEntriesCommand::read_from(input)?, + )), + TableEntriesReadCommand::TYPE_CODE => Ok(WireCommands::TableEntriesRead( + TableEntriesReadCommand::read_from(input)?, + )), + TableKeyDoesNotExistCommand::TYPE_CODE => Ok(WireCommands::TableKeyDoesNotExist( + TableKeyDoesNotExistCommand::read_from(input)?, + )), + TableKeyBadVersionCommand::TYPE_CODE => Ok(WireCommands::TableKeyBadVersion( + TableKeyBadVersionCommand::read_from(input)?, + )), _ => Ok(WireCommands::UnknownCommand), } } From 5954d01235550126df19cc2859637f9916c820ef Mon Sep 17 00:00:00 2001 From: Wenxiao Zhang Date: Thu, 9 Jan 2020 14:01:14 -0800 Subject: [PATCH 13/30] change snafu description --- src/wire_protocol/error.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/wire_protocol/error.rs b/src/wire_protocol/error.rs index db4f2eb79..d5a40d98a 100644 --- a/src/wire_protocol/error.rs +++ b/src/wire_protocol/error.rs @@ -34,11 +34,19 @@ pub enum ConnectionError { #[derive(Debug, Snafu)] #[snafu(visibility = "pub(crate)")] pub enum CommandError { - #[snafu(display("Could not serialize command {} because of: {}", command_type, source))] + #[snafu(display( + "Could not serialize/deserialize command {} because of: {}", + command_type, + source + ))] InvalidData { command_type: i32, source: BincodeError, }, - #[snafu(display("Could not serialize command {} because of: {}", command_type, source))] + #[snafu(display( + "Could not serialize/deserialize command {} because of: {}", + command_type, + source + ))] Io { command_type: i32, source: IoError }, } From 1e8c0a54a17c975b978ce51364c0a1bd083627c6 Mon Sep 17 00:00:00 2001 From: Wenxiao Zhang Date: Thu, 9 Jan 2020 14:14:48 -0800 Subject: [PATCH 14/30] fix conflict --- src/wire_protocol/error.rs | 20 ++++++++++++++++++ src/wire_protocol/wirecommand_reader.rs | 28 +++++-------------------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/wire_protocol/error.rs b/src/wire_protocol/error.rs index d5a40d98a..7e75fb576 100644 --- a/src/wire_protocol/error.rs +++ b/src/wire_protocol/error.rs @@ -50,3 +50,23 @@ pub enum CommandError { ))] Io { command_type: i32, source: IoError }, } + +/// This kind of error that can be produced during Pravega read Wire Commands. +#[derive(Debug, Snafu)] +#[snafu(visibility = "pub(crate)")] +pub enum ReaderError { + #[snafu(display("Failed to read wirecommand {}", part))] + ReadWirecommand { + part: String, + source: ConnectionError, + }, + #[snafu(display( + "The payload size {} exceeds the max wirecommand size {}", + payload_size, + max_wirecommand_size + ))] + PayloadLengthTooLong { + payload_size: u32, + max_wirecommand_size: u32, + }, +} diff --git a/src/wire_protocol/wirecommand_reader.rs b/src/wire_protocol/wirecommand_reader.rs index f56f2b7a7..5bd65c4f0 100644 --- a/src/wire_protocol/wirecommand_reader.rs +++ b/src/wire_protocol/wirecommand_reader.rs @@ -9,42 +9,24 @@ // extern crate byteorder; -use crate::wire_protocol::connection_factory; +use super::error::PayloadLengthTooLong; +use super::error::ReadWirecommand; +use super::error::ReaderError; use crate::wire_protocol::connection_factory::Connection; use byteorder::{BigEndian, ReadBytesExt}; -use snafu::{ensure, ResultExt, Snafu}; +use snafu::{ensure, ResultExt}; use std::io::Cursor; pub const MAX_WIRECOMMAND_SIZE: u32 = 0x007FFFFF; pub const LENGTH_FIELD_OFFSET: u32 = 4; pub const LENGTH_FIELD_LENGTH: u32 = 4; -#[derive(Debug, Snafu)] -pub enum Error { - #[snafu(display("Failed to read wirecommand {}", part))] - ReadWirecommand { - part: String, - source: connection_factory::Error, - }, - #[snafu(display( - "The payload size {} exceeds the max wirecommand size {}", - payload_size, - max_wirecommand_size - ))] - PayloadLengthTooLong { - payload_size: u32, - max_wirecommand_size: u32, - }, -} - -type Result = std::result::Result; - pub struct WireCommandReader { pub connection: Box, } impl WireCommandReader { - async fn read(&mut self) -> Result> { + async fn read(&mut self) -> Result, ReaderError> { let mut header: Vec = vec![0; LENGTH_FIELD_OFFSET as usize + LENGTH_FIELD_LENGTH as usize]; self.connection From d500acc77840a66700798f1da9600a898115492b Mon Sep 17 00:00:00 2001 From: Tom Kaitchuck Date: Thu, 9 Jan 2020 15:21:55 -0800 Subject: [PATCH 15/30] Add additional lints from clippy. (and fix some hilighted problems) --- src/lib.rs | 20 ++ src/wire_protocol/commands.rs | 315 ++++++++++------------------- src/wire_protocol/wire_commands.rs | 4 +- 3 files changed, 127 insertions(+), 212 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e3697b9d7..14f5bcf2f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,26 @@ // http://www.apache.org/licenses/LICENSE-2.0 // +#![allow(dead_code)] +#![deny( + clippy::all, + clippy::cargo, + clippy::else_if_without_else, + clippy::empty_line_after_outer_attr, + clippy::multiple_inherent_impl, + clippy::mut_mut, + clippy::path_buf_push_overwrite +)] +#![warn( + clippy::cargo_common_metadata, + clippy::multiple_crate_versions, + clippy::mutex_integer, + clippy::needless_borrow, + clippy::option_unwrap_used, + clippy::result_unwrap_used, + clippy::similar_names +)] + #[macro_use] extern crate lazy_static; diff --git a/src/wire_protocol/commands.rs b/src/wire_protocol/commands.rs index 9fcbaab05..c32701333 100644 --- a/src/wire_protocol/commands.rs +++ b/src/wire_protocol/commands.rs @@ -136,13 +136,11 @@ pub struct HelloCommand { impl Command for HelloCommand { const TYPE_CODE: i32 = -127; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> HelloCommand { - let decoded: HelloCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -172,12 +170,10 @@ pub struct WrongHostCommand { impl Command for WrongHostCommand { const TYPE_CODE: i32 = 50; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> WrongHostCommand { - let decoded: WrongHostCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -204,13 +200,11 @@ pub struct SegmentIsSealedCommand { impl Command for SegmentIsSealedCommand { const TYPE_CODE: i32 = 51; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> SegmentIsSealedCommand { - let decoded: SegmentIsSealedCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -238,13 +232,11 @@ pub struct SegmentIsTruncatedCommand { impl Command for SegmentIsTruncatedCommand { const TYPE_CODE: i32 = 56; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> SegmentIsTruncatedCommand { - let decoded: SegmentIsTruncatedCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -270,13 +262,11 @@ pub struct SegmentAlreadyExistsCommand { impl Command for SegmentAlreadyExistsCommand { const TYPE_CODE: i32 = 52; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> SegmentAlreadyExistsCommand { - let decoded: SegmentAlreadyExistsCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -309,13 +299,11 @@ pub struct NoSuchSegmentCommand { impl Command for NoSuchSegmentCommand { const TYPE_CODE: i32 = 53; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> NoSuchSegmentCommand { - let decoded: NoSuchSegmentCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -347,13 +335,11 @@ pub struct TableSegmentNotEmptyCommand { impl Command for TableSegmentNotEmptyCommand { const TYPE_CODE: i32 = 80; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> TableSegmentNotEmptyCommand { - let decoded: TableSegmentNotEmptyCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -385,13 +371,11 @@ pub struct InvalidEventNumberCommand { impl Command for InvalidEventNumberCommand { const TYPE_CODE: i32 = 55; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> InvalidEventNumberCommand { - let decoded: InvalidEventNumberCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -427,13 +411,11 @@ pub struct OperationUnsupportedCommand { impl Command for OperationUnsupportedCommand { const TYPE_CODE: i32 = 57; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> OperationUnsupportedCommand { - let decoded: OperationUnsupportedCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -522,8 +504,7 @@ impl Command for EventCommand { fn read_from(input: &[u8]) -> EventCommand { //read the type_code. let _type_code = BigEndian::read_i32(input); - let decoded: EventCommand = CONFIG.deserialize(&input[4..]).unwrap(); - decoded + CONFIG.deserialize(&input[4..]).unwrap() } } @@ -541,13 +522,11 @@ pub struct SetupAppendCommand { impl Command for SetupAppendCommand { const TYPE_CODE: i32 = 1; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> SetupAppendCommand { - let decoded: SetupAppendCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -571,13 +550,11 @@ impl Command for AppendBlockCommand { //FIXME: The serialize and deserialize method need to customize; // In JAVA, it doesn't write data(because it'empty), but here it will write the prefix length(0). fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> AppendBlockCommand { - let decoded: AppendBlockCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -598,13 +575,11 @@ impl Command for AppendBlockEndCommand { const TYPE_CODE: i32 = 4; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> AppendBlockEndCommand { - let decoded: AppendBlockEndCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -683,13 +658,11 @@ impl Command for AppendSetupCommand { const TYPE_CODE: i32 = 2; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> Self { - let decoded: AppendSetupCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -715,13 +688,11 @@ impl Command for DataAppendedCommand { const TYPE_CODE: i32 = 7; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> DataAppendedCommand { - let decoded: DataAppendedCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -745,13 +716,11 @@ impl Command for ConditionalCheckFailedCommand { const TYPE_CODE: i32 = 8; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> ConditionalCheckFailedCommand { - let decoded: ConditionalCheckFailedCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -777,13 +746,11 @@ impl Command for ReadSegmentCommand { const TYPE_CODE: i32 = 9; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> ReadSegmentCommand { - let decoded: ReadSegmentCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -810,13 +777,11 @@ impl Command for SegmentReadCommand { const TYPE_CODE: i32 = 10; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> SegmentReadCommand { - let decoded: SegmentReadCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -841,13 +806,11 @@ impl Command for GetSegmentAttributeCommand { const TYPE_CODE: i32 = 34; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> GetSegmentAttributeCommand { - let decoded: GetSegmentAttributeCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -870,13 +833,11 @@ impl Command for SegmentAttributeCommand { const TYPE_CODE: i32 = 35; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> SegmentAttributeCommand { - let decoded: SegmentAttributeCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -903,13 +864,11 @@ impl Command for UpdateSegmentAttributeCommand { const TYPE_CODE: i32 = 36; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> UpdateSegmentAttributeCommand { - let decoded: UpdateSegmentAttributeCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -932,13 +891,11 @@ impl Command for SegmentAttributeUpdatedCommand { const TYPE_CODE: i32 = 37; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> SegmentAttributeUpdatedCommand { - let decoded: SegmentAttributeUpdatedCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -962,13 +919,11 @@ impl Command for GetStreamSegmentInfoCommand { const TYPE_CODE: i32 = 11; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> GetStreamSegmentInfoCommand { - let decoded: GetStreamSegmentInfoCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -997,13 +952,11 @@ impl Command for StreamSegmentInfoCommand { const TYPE_CODE: i32 = 12; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> StreamSegmentInfoCommand { - let decoded: StreamSegmentInfoCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -1029,13 +982,11 @@ impl Command for CreateSegmentCommand { const TYPE_CODE: i32 = 20; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> CreateSegmentCommand { - let decoded: CreateSegmentCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -1059,13 +1010,11 @@ impl Command for CreateTableSegmentCommand { const TYPE_CODE: i32 = 70; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> CreateTableSegmentCommand { - let decoded: CreateTableSegmentCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -1088,13 +1037,11 @@ impl Command for SegmentCreatedCommand { const TYPE_CODE: i32 = 21; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> SegmentCreatedCommand { - let decoded: SegmentCreatedCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -1120,13 +1067,11 @@ impl Command for UpdateSegmentPolicyCommand { const TYPE_CODE: i32 = 32; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> UpdateSegmentPolicyCommand { - let decoded: UpdateSegmentPolicyCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -1149,13 +1094,11 @@ impl Command for SegmentPolicyUpdatedCommand { const TYPE_CODE: i32 = 33; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> SegmentPolicyUpdatedCommand { - let decoded: SegmentPolicyUpdatedCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -1180,13 +1123,11 @@ impl Command for MergeSegmentsCommand { const TYPE_CODE: i32 = 58; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> MergeSegmentsCommand { - let decoded: MergeSegmentsCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -1210,13 +1151,11 @@ impl Command for MergeTableSegmentsCommand { const TYPE_CODE: i32 = 72; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> MergeTableSegmentsCommand { - let decoded: MergeTableSegmentsCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -1241,13 +1180,11 @@ impl Command for SegmentsMergedCommand { const TYPE_CODE: i32 = 59; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> SegmentsMergedCommand { - let decoded: SegmentsMergedCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -1271,13 +1208,11 @@ impl Command for SealSegmentCommand { const TYPE_CODE: i32 = 28; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> SealSegmentCommand { - let decoded: SealSegmentCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -1301,13 +1236,11 @@ impl Command for SealTableSegmentCommand { const TYPE_CODE: i32 = 73; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> SealTableSegmentCommand { - let decoded: SealTableSegmentCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -1330,13 +1263,11 @@ impl Command for SegmentSealedCommand { const TYPE_CODE: i32 = 29; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> SegmentSealedCommand { - let decoded: SegmentSealedCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -1361,13 +1292,11 @@ impl Command for TruncateSegmentCommand { const TYPE_CODE: i32 = 38; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> TruncateSegmentCommand { - let decoded: TruncateSegmentCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -1390,13 +1319,11 @@ impl Command for SegmentTruncatedCommand { const TYPE_CODE: i32 = 39; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> SegmentTruncatedCommand { - let decoded: SegmentTruncatedCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -1420,13 +1347,11 @@ impl Command for DeleteSegmentCommand { const TYPE_CODE: i32 = 30; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> DeleteSegmentCommand { - let decoded: DeleteSegmentCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -1450,13 +1375,11 @@ pub struct DeleteTableSegmentCommand { impl Command for DeleteTableSegmentCommand { const TYPE_CODE: i32 = 71; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> DeleteTableSegmentCommand { - let decoded: DeleteTableSegmentCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -1479,13 +1402,11 @@ impl Command for SegmentDeletedCommand { const TYPE_CODE: i32 = 31; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> SegmentDeletedCommand { - let decoded: SegmentDeletedCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -1552,13 +1473,11 @@ impl Command for AuthTokenCheckFailedCommand { const TYPE_CODE: i32 = 60; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> AuthTokenCheckFailedCommand { - let decoded: AuthTokenCheckFailedCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -1608,13 +1527,11 @@ impl Command for UpdateTableEntriesCommand { const TYPE_CODE: i32 = 74; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> UpdateTableEntriesCommand { - let decoded: UpdateTableEntriesCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -1637,13 +1554,11 @@ impl Command for TableEntriesUpdatedCommand { const TYPE_CODE: i32 = 75; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> TableEntriesUpdatedCommand { - let decoded: TableEntriesUpdatedCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -1668,13 +1583,11 @@ impl Command for RemoveTableKeysCommand { const TYPE_CODE: i32 = 76; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> RemoveTableKeysCommand { - let decoded: RemoveTableKeysCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -1697,13 +1610,11 @@ impl Command for TableKeysRemovedCommand { const TYPE_CODE: i32 = 77; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> TableKeysRemovedCommand { - let decoded: TableKeysRemovedCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -1728,13 +1639,11 @@ impl Command for ReadTableCommand { const TYPE_CODE: i32 = 78; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> ReadTableCommand { - let decoded: ReadTableCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -1758,13 +1667,11 @@ impl Command for TableReadCommand { const TYPE_CODE: i32 = 79; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> TableReadCommand { - let decoded: TableReadCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -1790,13 +1697,11 @@ impl Command for ReadTableKeysCommand { const TYPE_CODE: i32 = 83; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> ReadTableKeysCommand { - let decoded: ReadTableKeysCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -1821,13 +1726,11 @@ impl Command for TableKeysReadCommand { const TYPE_CODE: i32 = 84; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> TableKeysReadCommand { - let decoded: TableKeysReadCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -1853,13 +1756,11 @@ impl Command for ReadTableEntriesCommand { const TYPE_CODE: i32 = 85; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> ReadTableEntriesCommand { - let decoded: ReadTableEntriesCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -1884,13 +1785,11 @@ impl Command for TableEntriesReadCommand { const TYPE_CODE: i32 = 86; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> TableEntriesReadCommand { - let decoded: TableEntriesReadCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -1914,13 +1813,11 @@ impl Command for TableKeyDoesNotExistCommand { const TYPE_CODE: i32 = 81; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> TableKeyDoesNotExistCommand { - let decoded: TableKeyDoesNotExistCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } @@ -1958,13 +1855,11 @@ impl Command for TableKeyBadVersionCommand { const TYPE_CODE: i32 = 82; fn write_fields(&self) -> Vec { - let encoded = CONFIG.serialize(&self).unwrap(); - encoded + CONFIG.serialize(&self).unwrap() } fn read_from(input: &[u8]) -> TableKeyBadVersionCommand { - let decoded: TableKeyBadVersionCommand = CONFIG.deserialize(&input[..]).unwrap(); - decoded + CONFIG.deserialize(&input[..]).unwrap() } } diff --git a/src/wire_protocol/wire_commands.rs b/src/wire_protocol/wire_commands.rs index 45ad980b2..d0b39366b 100644 --- a/src/wire_protocol/wire_commands.rs +++ b/src/wire_protocol/wire_commands.rs @@ -68,7 +68,7 @@ pub trait Encode { } pub trait Decode { - fn read_from(raw_input: &Vec) -> WireCommands; + fn read_from(raw_input: &[u8]) -> WireCommands; } impl Encode for WireCommands { @@ -479,7 +479,7 @@ impl Encode for WireCommands { } impl Decode for WireCommands { - fn read_from(raw_input: &Vec) -> WireCommands { + fn read_from(raw_input: &[u8]) -> WireCommands { let type_code = BigEndian::read_i32(raw_input); let _length = BigEndian::read_i32(&raw_input[4..]); let input = &raw_input[8..]; From 6d75581dce995bf4bc76c667de83645434a4b3aa Mon Sep 17 00:00:00 2001 From: Tom Kaitchuck Date: Fri, 10 Jan 2020 13:20:31 -0800 Subject: [PATCH 16/30] Avoid some clippy warnings --- src/wire_protocol/wire_commands.rs | 287 ++++++++++-------------- src/wire_protocol/wirecommand_reader.rs | 4 +- 2 files changed, 118 insertions(+), 173 deletions(-) diff --git a/src/wire_protocol/wire_commands.rs b/src/wire_protocol/wire_commands.rs index d0b39366b..7e969fe72 100644 --- a/src/wire_protocol/wire_commands.rs +++ b/src/wire_protocol/wire_commands.rs @@ -1,5 +1,5 @@ use super::commands::*; -use byteorder::{BigEndian, ByteOrder, WriteBytesExt}; +use byteorder::{BigEndian, ByteOrder}; #[derive(PartialEq, Debug)] pub enum WireCommands { @@ -75,404 +75,349 @@ impl Encode for WireCommands { fn write_fields(&self) -> Vec { let mut res = Vec::new(); match self { + WireCommands::UnknownCommand => panic!("Sending unknown command?"), WireCommands::Hello(hello_cmd) => { - res.write_i32::(HelloCommand::TYPE_CODE).unwrap(); + res.extend_from_slice(&HelloCommand::TYPE_CODE.to_be_bytes()); let se = hello_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::WrongHost(wrong_host_cmd) => { - res.write_i32::(WrongHostCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&WrongHostCommand::TYPE_CODE.to_be_bytes()); let se = wrong_host_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::SegmentIsSealed(seg_is_sealed_cmd) => { - res.write_i32::(SegmentIsSealedCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&SegmentIsSealedCommand::TYPE_CODE.to_be_bytes()); let se = seg_is_sealed_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::SegmentAlreadyExists(seg_already_exists_cmd) => { - res.write_i32::(SegmentAlreadyExistsCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&SegmentAlreadyExistsCommand::TYPE_CODE.to_be_bytes()); let se = seg_already_exists_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::SegmentIsTruncated(seg_is_truncated_cmd) => { - res.write_i32::(SegmentIsTruncatedCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&SegmentIsTruncatedCommand::TYPE_CODE.to_be_bytes()); let se = seg_is_truncated_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::NoSuchSegment(no_such_seg_cmd) => { - res.write_i32::(NoSuchSegmentCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&NoSuchSegmentCommand::TYPE_CODE.to_be_bytes()); let se = no_such_seg_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::TableSegmentNotEmpty(table_seg_not_empty_cmd) => { - res.write_i32::(TableSegmentNotEmptyCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&TableSegmentNotEmptyCommand::TYPE_CODE.to_be_bytes()); let se = table_seg_not_empty_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::InvalidEventNumber(invalid_event_num_cmd) => { - res.write_i32::(InvalidEventNumberCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&InvalidEventNumberCommand::TYPE_CODE.to_be_bytes()); let se = invalid_event_num_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::OperationUnsupported(operation_unsupported_cmd) => { - res.write_i32::(OperationUnsupportedCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&OperationUnsupportedCommand::TYPE_CODE.to_be_bytes()); let se = operation_unsupported_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::Padding(padding_command) => { - res.write_i32::(PaddingCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&PaddingCommand::TYPE_CODE.to_be_bytes()); let se = padding_command.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::PartialEvent(partial_event_cmd) => { - res.write_i32::(PartialEventCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&PartialEventCommand::TYPE_CODE.to_be_bytes()); let se = partial_event_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::Event(event_cmd) => { - res.write_i32::(EventCommand::TYPE_CODE).unwrap(); + res.extend_from_slice(&(EventCommand::TYPE_CODE).to_be_bytes()); let se = event_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::SetupAppend(setup_append_cmd) => { - res.write_i32::(SetupAppendCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&SetupAppendCommand::TYPE_CODE.to_be_bytes()); let se = setup_append_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::AppendBlock(append_block_cmd) => { - res.write_i32::(AppendBlockCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&AppendBlockCommand::TYPE_CODE.to_be_bytes()); let se = append_block_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::AppendBlockEnd(append_block_end_cmd) => { - res.write_i32::(AppendBlockEndCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&AppendBlockEndCommand::TYPE_CODE.to_be_bytes()); let se = append_block_end_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::ConditionalAppend(conditional_append_cmd) => { - res.write_i32::(ConditionalAppendCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&ConditionalAppendCommand::TYPE_CODE.to_be_bytes()); let se = conditional_append_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::AppendSetup(append_setup_cmd) => { - res.write_i32::(AppendSetupCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&AppendSetupCommand::TYPE_CODE.to_be_bytes()); let se = append_setup_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::DataAppended(data_appended_cmd) => { - res.write_i32::(DataAppendedCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&DataAppendedCommand::TYPE_CODE.to_be_bytes()); let se = data_appended_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::ConditionalCheckFailed(conditional_check_failed_cmd) => { - res.write_i32::(ConditionalCheckFailedCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&ConditionalCheckFailedCommand::TYPE_CODE.to_be_bytes()); let se = conditional_check_failed_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::ReadSegment(read_segment_cmd) => { - res.write_i32::(ReadSegmentCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&ReadSegmentCommand::TYPE_CODE.to_be_bytes()); let se = read_segment_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::SegmentRead(segment_read_cmd) => { - res.write_i32::(SegmentReadCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&SegmentReadCommand::TYPE_CODE.to_be_bytes()); let se = segment_read_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::GetSegmentAttribute(get_segment_attribute_cmd) => { - res.write_i32::(GetSegmentAttributeCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&GetSegmentAttributeCommand::TYPE_CODE.to_be_bytes()); let se = get_segment_attribute_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::SegmentAttribute(segment_attribute_cmd) => { - res.write_i32::(SegmentAttributeCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&SegmentAttributeCommand::TYPE_CODE.to_be_bytes()); let se = segment_attribute_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::UpdateSegmentAttribute(update_segment_attribute_cmd) => { - res.write_i32::(UpdateSegmentAttributeCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&UpdateSegmentAttributeCommand::TYPE_CODE.to_be_bytes()); let se = update_segment_attribute_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::SegmentAttributeUpdated(segment_attribute_updated_cmd) => { - res.write_i32::(SegmentAttributeUpdatedCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&SegmentAttributeUpdatedCommand::TYPE_CODE.to_be_bytes()); let se = segment_attribute_updated_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::GetStreamSegmentInfo(get_stream_segment_info_cmd) => { - res.write_i32::(GetStreamSegmentInfoCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&GetStreamSegmentInfoCommand::TYPE_CODE.to_be_bytes()); let se = get_stream_segment_info_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::StreamSegmentInfo(stream_segment_info_cmd) => { - res.write_i32::(StreamSegmentInfoCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&StreamSegmentInfoCommand::TYPE_CODE.to_be_bytes()); let se = stream_segment_info_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::CreateSegment(create_segment_cmd) => { - res.write_i32::(CreateSegmentCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&CreateSegmentCommand::TYPE_CODE.to_be_bytes()); let se = create_segment_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::CreateTableSegment(create_table_segment_command) => { - res.write_i32::(CreateTableSegmentCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&CreateTableSegmentCommand::TYPE_CODE.to_be_bytes()); let se = create_table_segment_command.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::SegmentCreated(segment_created_cmd) => { - res.write_i32::(SegmentCreatedCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&SegmentCreatedCommand::TYPE_CODE.to_be_bytes()); let se = segment_created_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::UpdateSegmentPolicy(update_segment_policy_cmd) => { - res.write_i32::(UpdateSegmentPolicyCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&UpdateSegmentPolicyCommand::TYPE_CODE.to_be_bytes()); let se = update_segment_policy_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::SegmentPolicyUpdated(segment_policy_updated_cmd) => { - res.write_i32::(SegmentPolicyUpdatedCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&SegmentPolicyUpdatedCommand::TYPE_CODE.to_be_bytes()); let se = segment_policy_updated_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::MergeSegments(merge_segments_cmd) => { - res.write_i32::(MergeSegmentsCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&MergeSegmentsCommand::TYPE_CODE.to_be_bytes()); let se = merge_segments_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::MergeTableSegments(merge_table_segments_cmd) => { - res.write_i32::(MergeTableSegmentsCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&MergeTableSegmentsCommand::TYPE_CODE.to_be_bytes()); let se = merge_table_segments_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::SegmentsMerged(segments_merged_cmd) => { - res.write_i32::(SegmentsMergedCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&SegmentsMergedCommand::TYPE_CODE.to_be_bytes()); let se = segments_merged_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::SealSegment(seal_segment_cmd) => { - res.write_i32::(SealSegmentCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&SealSegmentCommand::TYPE_CODE.to_be_bytes()); let se = seal_segment_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::SealTableSegment(seal_table_segment_cmd) => { - res.write_i32::(SealTableSegmentCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&SealTableSegmentCommand::TYPE_CODE.to_be_bytes()); let se = seal_table_segment_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::SegmentSealed(segment_sealed_cmd) => { - res.write_i32::(SegmentSealedCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&SegmentSealedCommand::TYPE_CODE.to_be_bytes()); let se = segment_sealed_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::TruncateSegment(truncate_segment_cmd) => { - res.write_i32::(TruncateSegmentCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&TruncateSegmentCommand::TYPE_CODE.to_be_bytes()); let se = truncate_segment_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::SegmentTruncated(segment_truncated_cmd) => { - res.write_i32::(SegmentTruncatedCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&SegmentTruncatedCommand::TYPE_CODE.to_be_bytes()); let se = segment_truncated_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::DeleteSegment(delete_segment_cmd) => { - res.write_i32::(DeleteSegmentCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&DeleteSegmentCommand::TYPE_CODE.to_be_bytes()); let se = delete_segment_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::DeleteTableSegment(delete_table_segment_cmd) => { - res.write_i32::(DeleteTableSegmentCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&DeleteTableSegmentCommand::TYPE_CODE.to_be_bytes()); let se = delete_table_segment_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::SegmentDeleted(segment_deleted_cmd) => { - res.write_i32::(SegmentDeletedCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&SegmentDeletedCommand::TYPE_CODE.to_be_bytes()); let se = segment_deleted_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::KeepAlive(keep_alive_cmd) => { - res.write_i32::(KeepAliveCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&KeepAliveCommand::TYPE_CODE.to_be_bytes()); let se = keep_alive_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::AuthTokenCheckFailed(auth_token_check_failed_cmd) => { - res.write_i32::(AuthTokenCheckFailedCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&AuthTokenCheckFailedCommand::TYPE_CODE.to_be_bytes()); let se = auth_token_check_failed_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::UpdateTableEntries(update_table_entries_cmd) => { - res.write_i32::(UpdateTableEntriesCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&UpdateTableEntriesCommand::TYPE_CODE.to_be_bytes()); let se = update_table_entries_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::TableEntriesUpdated(table_entries_updated_cmd) => { - res.write_i32::(TableEntriesUpdatedCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&TableEntriesUpdatedCommand::TYPE_CODE.to_be_bytes()); let se = table_entries_updated_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::RemoveTableKeys(remove_table_keys_cmd) => { - res.write_i32::(RemoveTableKeysCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&RemoveTableKeysCommand::TYPE_CODE.to_be_bytes()); let se = remove_table_keys_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::TableKeysRemoved(table_key_removed_cmd) => { - res.write_i32::(TableKeysRemovedCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&TableKeysRemovedCommand::TYPE_CODE.to_be_bytes()); let se = table_key_removed_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::ReadTable(read_table_cmd) => { - res.write_i32::(ReadTableCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&ReadTableCommand::TYPE_CODE.to_be_bytes()); let se = read_table_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::TableRead(table_read_cmd) => { - res.write_i32::(TableReadCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&TableReadCommand::TYPE_CODE.to_be_bytes()); let se = table_read_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::ReadTableKeys(read_table_keys_cmd) => { - res.write_i32::(ReadTableKeysCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&ReadTableKeysCommand::TYPE_CODE.to_be_bytes()); let se = read_table_keys_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::TableKeysRead(table_keys_read_cmd) => { - res.write_i32::(TableKeysReadCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&TableKeysReadCommand::TYPE_CODE.to_be_bytes()); let se = table_keys_read_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::ReadTableEntries(read_table_entries_cmd) => { - res.write_i32::(ReadTableEntriesCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&ReadTableEntriesCommand::TYPE_CODE.to_be_bytes()); let se = read_table_entries_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::TableEntriesRead(table_entries_read_cmd) => { - res.write_i32::(TableEntriesReadCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&TableEntriesReadCommand::TYPE_CODE.to_be_bytes()); let se = table_entries_read_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::TableKeyDoesNotExist(table_key_does_not_exist_cmd) => { - res.write_i32::(TableKeyDoesNotExistCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&TableKeyDoesNotExistCommand::TYPE_CODE.to_be_bytes()); let se = table_key_does_not_exist_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::TableKeyBadVersion(table_key_bad_version_cmd) => { - res.write_i32::(TableKeyBadVersionCommand::TYPE_CODE) - .unwrap(); + res.extend_from_slice(&TableKeyBadVersionCommand::TYPE_CODE.to_be_bytes()); let se = table_key_bad_version_cmd.write_fields(); - res.write_i32::(se.len() as i32).unwrap(); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } - _ => panic!("Unknown WireCommands"), } res } diff --git a/src/wire_protocol/wirecommand_reader.rs b/src/wire_protocol/wirecommand_reader.rs index f56f2b7a7..92f436837 100644 --- a/src/wire_protocol/wirecommand_reader.rs +++ b/src/wire_protocol/wirecommand_reader.rs @@ -15,7 +15,7 @@ use byteorder::{BigEndian, ReadBytesExt}; use snafu::{ensure, ResultExt, Snafu}; use std::io::Cursor; -pub const MAX_WIRECOMMAND_SIZE: u32 = 0x007FFFFF; +pub const MAX_WIRECOMMAND_SIZE: u32 = 0x007F_FFFF; pub const LENGTH_FIELD_OFFSET: u32 = 4; pub const LENGTH_FIELD_LENGTH: u32 = 4; @@ -55,7 +55,7 @@ impl WireCommandReader { })?; let mut rdr = Cursor::new(&header[4..8]); - let payload_length = rdr.read_u32::().unwrap(); + let payload_length = rdr.read_u32::().expect("Exact size"); ensure!( payload_length <= MAX_WIRECOMMAND_SIZE, From 3d99e4eaa9e3d27afa81fe693ec95b046cbe57e8 Mon Sep 17 00:00:00 2001 From: Tom Kaitchuck Date: Fri, 10 Jan 2020 13:31:36 -0800 Subject: [PATCH 17/30] Avoid calling expect when not needed. --- src/wire_protocol/wire_commands.rs | 344 ++++++++++------------------- 1 file changed, 115 insertions(+), 229 deletions(-) diff --git a/src/wire_protocol/wire_commands.rs b/src/wire_protocol/wire_commands.rs index 9b344b855..fb307c9bc 100644 --- a/src/wire_protocol/wire_commands.rs +++ b/src/wire_protocol/wire_commands.rs @@ -1,6 +1,6 @@ use super::commands::*; use super::error::CommandError; -use byteorder::{BigEndian, ByteOrder, WriteBytesExt}; +use byteorder::{BigEndian, ByteOrder}; #[derive(PartialEq, Debug)] pub enum WireCommands { @@ -77,459 +77,345 @@ impl Encode for WireCommands { let mut res = Vec::new(); match self { WireCommands::Hello(hello_cmd) => { - res.write_i32::(HelloCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&HelloCommand::TYPE_CODE.to_be_bytes()); let se = hello_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::WrongHost(wrong_host_cmd) => { - res.write_i32::(WrongHostCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&WrongHostCommand::TYPE_CODE.to_be_bytes()); let se = wrong_host_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::SegmentIsSealed(seg_is_sealed_cmd) => { - res.write_i32::(SegmentIsSealedCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&SegmentIsSealedCommand::TYPE_CODE.to_be_bytes()); let se = seg_is_sealed_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::SegmentAlreadyExists(seg_already_exists_cmd) => { - res.write_i32::(SegmentAlreadyExistsCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&SegmentAlreadyExistsCommand::TYPE_CODE.to_be_bytes()); let se = seg_already_exists_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::SegmentIsTruncated(seg_is_truncated_cmd) => { - res.write_i32::(SegmentIsTruncatedCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&SegmentIsTruncatedCommand::TYPE_CODE.to_be_bytes()); let se = seg_is_truncated_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::NoSuchSegment(no_such_seg_cmd) => { - res.write_i32::(NoSuchSegmentCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&NoSuchSegmentCommand::TYPE_CODE.to_be_bytes()); let se = no_such_seg_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::TableSegmentNotEmpty(table_seg_not_empty_cmd) => { - res.write_i32::(TableSegmentNotEmptyCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&TableSegmentNotEmptyCommand::TYPE_CODE.to_be_bytes()); let se = table_seg_not_empty_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::InvalidEventNumber(invalid_event_num_cmd) => { - res.write_i32::(InvalidEventNumberCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&InvalidEventNumberCommand::TYPE_CODE.to_be_bytes()); let se = invalid_event_num_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::OperationUnsupported(operation_unsupported_cmd) => { - res.write_i32::(OperationUnsupportedCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&OperationUnsupportedCommand::TYPE_CODE.to_be_bytes()); let se = operation_unsupported_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::Padding(padding_command) => { - res.write_i32::(PaddingCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&PaddingCommand::TYPE_CODE.to_be_bytes()); let se = padding_command.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::PartialEvent(partial_event_cmd) => { - res.write_i32::(PartialEventCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&PartialEventCommand::TYPE_CODE.to_be_bytes()); let se = partial_event_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::Event(event_cmd) => { - res.write_i32::(EventCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&EventCommand::TYPE_CODE.to_be_bytes()); let se = event_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::SetupAppend(setup_append_cmd) => { - res.write_i32::(SetupAppendCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&SetupAppendCommand::TYPE_CODE.to_be_bytes()); let se = setup_append_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::AppendBlock(append_block_cmd) => { - res.write_i32::(AppendBlockCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&AppendBlockCommand::TYPE_CODE.to_be_bytes()); let se = append_block_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::AppendBlockEnd(append_block_end_cmd) => { - res.write_i32::(AppendBlockEndCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&AppendBlockEndCommand::TYPE_CODE.to_be_bytes()); let se = append_block_end_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::ConditionalAppend(conditional_append_cmd) => { - res.write_i32::(ConditionalAppendCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&ConditionalAppendCommand::TYPE_CODE.to_be_bytes()); let se = conditional_append_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::AppendSetup(append_setup_cmd) => { - res.write_i32::(AppendSetupCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&AppendSetupCommand::TYPE_CODE.to_be_bytes()); let se = append_setup_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::DataAppended(data_appended_cmd) => { - res.write_i32::(DataAppendedCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&DataAppendedCommand::TYPE_CODE.to_be_bytes()); let se = data_appended_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::ConditionalCheckFailed(conditional_check_failed_cmd) => { - res.write_i32::(ConditionalCheckFailedCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&ConditionalCheckFailedCommand::TYPE_CODE.to_be_bytes()); let se = conditional_check_failed_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::ReadSegment(read_segment_cmd) => { - res.write_i32::(ReadSegmentCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&ReadSegmentCommand::TYPE_CODE.to_be_bytes()); let se = read_segment_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::SegmentRead(segment_read_cmd) => { - res.write_i32::(SegmentReadCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&SegmentReadCommand::TYPE_CODE.to_be_bytes()); let se = segment_read_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::GetSegmentAttribute(get_segment_attribute_cmd) => { - res.write_i32::(GetSegmentAttributeCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&GetSegmentAttributeCommand::TYPE_CODE.to_be_bytes()); let se = get_segment_attribute_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::SegmentAttribute(segment_attribute_cmd) => { - res.write_i32::(SegmentAttributeCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&SegmentAttributeCommand::TYPE_CODE.to_be_bytes()); let se = segment_attribute_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::UpdateSegmentAttribute(update_segment_attribute_cmd) => { - res.write_i32::(UpdateSegmentAttributeCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&UpdateSegmentAttributeCommand::TYPE_CODE.to_be_bytes()); let se = update_segment_attribute_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::SegmentAttributeUpdated(segment_attribute_updated_cmd) => { - res.write_i32::(SegmentAttributeUpdatedCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&SegmentAttributeUpdatedCommand::TYPE_CODE.to_be_bytes()); let se = segment_attribute_updated_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::GetStreamSegmentInfo(get_stream_segment_info_cmd) => { - res.write_i32::(GetStreamSegmentInfoCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&GetStreamSegmentInfoCommand::TYPE_CODE.to_be_bytes()); let se = get_stream_segment_info_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::StreamSegmentInfo(stream_segment_info_cmd) => { - res.write_i32::(StreamSegmentInfoCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&StreamSegmentInfoCommand::TYPE_CODE.to_be_bytes()); let se = stream_segment_info_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::CreateSegment(create_segment_cmd) => { - res.write_i32::(CreateSegmentCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&CreateSegmentCommand::TYPE_CODE.to_be_bytes()); let se = create_segment_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::CreateTableSegment(create_table_segment_command) => { - res.write_i32::(CreateTableSegmentCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&CreateTableSegmentCommand::TYPE_CODE.to_be_bytes()); let se = create_table_segment_command.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::SegmentCreated(segment_created_cmd) => { - res.write_i32::(SegmentCreatedCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&SegmentCreatedCommand::TYPE_CODE.to_be_bytes()); let se = segment_created_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::UpdateSegmentPolicy(update_segment_policy_cmd) => { - res.write_i32::(UpdateSegmentPolicyCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&UpdateSegmentPolicyCommand::TYPE_CODE.to_be_bytes()); let se = update_segment_policy_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::SegmentPolicyUpdated(segment_policy_updated_cmd) => { - res.write_i32::(SegmentPolicyUpdatedCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&SegmentPolicyUpdatedCommand::TYPE_CODE.to_be_bytes()); let se = segment_policy_updated_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se) } WireCommands::MergeSegments(merge_segments_cmd) => { - res.write_i32::(MergeSegmentsCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&MergeSegmentsCommand::TYPE_CODE.to_be_bytes()); let se = merge_segments_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::MergeTableSegments(merge_table_segments_cmd) => { - res.write_i32::(MergeTableSegmentsCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&MergeTableSegmentsCommand::TYPE_CODE.to_be_bytes()); let se = merge_table_segments_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::SegmentsMerged(segments_merged_cmd) => { - res.write_i32::(SegmentsMergedCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&SegmentsMergedCommand::TYPE_CODE.to_be_bytes()); let se = segments_merged_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::SealSegment(seal_segment_cmd) => { - res.write_i32::(SealSegmentCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&SealSegmentCommand::TYPE_CODE.to_be_bytes()); let se = seal_segment_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::SealTableSegment(seal_table_segment_cmd) => { - res.write_i32::(SealTableSegmentCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&SealTableSegmentCommand::TYPE_CODE.to_be_bytes()); let se = seal_table_segment_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::SegmentSealed(segment_sealed_cmd) => { - res.write_i32::(SegmentSealedCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&SegmentSealedCommand::TYPE_CODE.to_be_bytes()); let se = segment_sealed_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::TruncateSegment(truncate_segment_cmd) => { - res.write_i32::(TruncateSegmentCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&TruncateSegmentCommand::TYPE_CODE.to_be_bytes()); let se = truncate_segment_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::SegmentTruncated(segment_truncated_cmd) => { - res.write_i32::(SegmentTruncatedCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&SegmentTruncatedCommand::TYPE_CODE.to_be_bytes()); let se = segment_truncated_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::DeleteSegment(delete_segment_cmd) => { - res.write_i32::(DeleteSegmentCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&DeleteSegmentCommand::TYPE_CODE.to_be_bytes()); let se = delete_segment_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::DeleteTableSegment(delete_table_segment_cmd) => { - res.write_i32::(DeleteTableSegmentCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&DeleteTableSegmentCommand::TYPE_CODE.to_be_bytes()); let se = delete_table_segment_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::SegmentDeleted(segment_deleted_cmd) => { - res.write_i32::(SegmentDeletedCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&SegmentDeletedCommand::TYPE_CODE.to_be_bytes()); let se = segment_deleted_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::KeepAlive(keep_alive_cmd) => { - res.write_i32::(KeepAliveCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&KeepAliveCommand::TYPE_CODE.to_be_bytes()); let se = keep_alive_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::AuthTokenCheckFailed(auth_token_check_failed_cmd) => { - res.write_i32::(AuthTokenCheckFailedCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&AuthTokenCheckFailedCommand::TYPE_CODE.to_be_bytes()); let se = auth_token_check_failed_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::UpdateTableEntries(update_table_entries_cmd) => { - res.write_i32::(UpdateTableEntriesCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&UpdateTableEntriesCommand::TYPE_CODE.to_be_bytes()); let se = update_table_entries_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::TableEntriesUpdated(table_entries_updated_cmd) => { - res.write_i32::(TableEntriesUpdatedCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&TableEntriesUpdatedCommand::TYPE_CODE.to_be_bytes()); let se = table_entries_updated_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::RemoveTableKeys(remove_table_keys_cmd) => { - res.write_i32::(RemoveTableKeysCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&RemoveTableKeysCommand::TYPE_CODE.to_be_bytes()); let se = remove_table_keys_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::TableKeysRemoved(table_key_removed_cmd) => { - res.write_i32::(TableKeysRemovedCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&TableKeysRemovedCommand::TYPE_CODE.to_be_bytes()); let se = table_key_removed_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::ReadTable(read_table_cmd) => { - res.write_i32::(ReadTableCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&ReadTableCommand::TYPE_CODE.to_be_bytes()); let se = read_table_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::TableRead(table_read_cmd) => { - res.write_i32::(TableReadCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&TableReadCommand::TYPE_CODE.to_be_bytes()); let se = table_read_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::ReadTableKeys(read_table_keys_cmd) => { - res.write_i32::(ReadTableKeysCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&ReadTableKeysCommand::TYPE_CODE.to_be_bytes()); let se = read_table_keys_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::TableKeysRead(table_keys_read_cmd) => { - res.write_i32::(TableKeysReadCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&TableKeysReadCommand::TYPE_CODE.to_be_bytes()); let se = table_keys_read_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::ReadTableEntries(read_table_entries_cmd) => { - res.write_i32::(ReadTableEntriesCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&ReadTableEntriesCommand::TYPE_CODE.to_be_bytes()); let se = read_table_entries_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::TableEntriesRead(table_entries_read_cmd) => { - res.write_i32::(TableEntriesReadCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&TableEntriesReadCommand::TYPE_CODE.to_be_bytes()); let se = table_entries_read_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::TableKeyDoesNotExist(table_key_does_not_exist_cmd) => { - res.write_i32::(TableKeyDoesNotExistCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&TableKeyDoesNotExistCommand::TYPE_CODE.to_be_bytes()); let se = table_key_does_not_exist_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } WireCommands::TableKeyBadVersion(table_key_bad_version_cmd) => { - res.write_i32::(TableKeyBadVersionCommand::TYPE_CODE) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&TableKeyBadVersionCommand::TYPE_CODE.to_be_bytes()); let se = table_key_bad_version_cmd.write_fields()?; - res.write_i32::(se.len() as i32) - .expect("Writing to an in memory vec"); + res.extend_from_slice(&(se.len() as i32).to_be_bytes()); res.extend(se); } _ => panic!("Unknown WireCommands"), From d4a8302758d835e007418223c4f0191b2427e7ad Mon Sep 17 00:00:00 2001 From: Tom Kaitchuck Date: Fri, 10 Jan 2020 14:01:41 -0800 Subject: [PATCH 18/30] Remove more unneeded error cases. Signed-off-by: Tom Kaitchuck --- src/wire_protocol/commands.rs | 28 +++++++--------------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/src/wire_protocol/commands.rs b/src/wire_protocol/commands.rs index 5eeddd852..e8dfb8b79 100644 --- a/src/wire_protocol/commands.rs +++ b/src/wire_protocol/commands.rs @@ -78,7 +78,7 @@ impl Serialize for JavaString { let binary = self.0.as_bytes(); // Serialize let mut content = vec![]; - content.write_u16::(length).unwrap(); + content.extend_from_slice(&length.to_be_bytes()); content.extend(binary); serializer.serialize_bytes(&content) } @@ -509,9 +509,7 @@ impl Command for PaddingCommand { fn write_fields(&self) -> Result, CommandError> { let mut res = Vec::new(); for _i in 0..(self.length / 8) { - res.write_i64::(0).context(Io { - command_type: Self::TYPE_CODE, - })?; + res.extend_from_slice(&0_i64.to_be_bytes()); } for _i in 0..(self.length % 8) { res.write_u8(0).context(Io { @@ -566,10 +564,7 @@ impl Command for EventCommand { const TYPE_CODE: i32 = 0; fn write_fields(&self) -> Result, CommandError> { let mut res = Vec::new(); - res.write_i32::(EventCommand::TYPE_CODE) - .context(Io { - command_type: Self::TYPE_CODE, - })?; + res.extend_from_slice(&EventCommand::TYPE_CODE.to_be_bytes()); let encoded = CONFIG.serialize(&self).context(InvalidData { command_type: Self::TYPE_CODE, })?; @@ -710,22 +705,13 @@ impl Command for ConditionalAppendCommand { // Because in CondtionalAppend the event should be serialize as |type_code|length|data| fn write_fields(&self) -> Result, CommandError> { let mut res = Vec::new(); - res.write_u128::(self.writer_id).context(Io { - command_type: Self::TYPE_CODE, - })?; - res.write_i64::(self.event_number).context(Io { - command_type: Self::TYPE_CODE, - })?; - res.write_i64::(self.expected_offset) - .context(Io { - command_type: Self::TYPE_CODE, - })?; + res.extend_from_slice(&self.writer_id.to_be_bytes()); + res.extend_from_slice(&self.event_number.to_be_bytes()); + res.extend_from_slice(&self.expected_offset.to_be_bytes()); res.write_all(&self.event.write_fields()?).context(Io { command_type: Self::TYPE_CODE, })?; - res.write_i64::(self.request_id).context(Io { - command_type: Self::TYPE_CODE, - })?; + res.extend_from_slice(&self.request_id.to_be_bytes()); Ok(res) } From b7953130ca3599d4874862635967270eada82ae1 Mon Sep 17 00:00:00 2001 From: Tom Kaitchuck Date: Fri, 10 Jan 2020 14:07:42 -0800 Subject: [PATCH 19/30] More idomatic way to allocate an array of zeros. Signed-off-by: Tom Kaitchuck --- src/wire_protocol/commands.rs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/wire_protocol/commands.rs b/src/wire_protocol/commands.rs index e8dfb8b79..e6c1c073d 100644 --- a/src/wire_protocol/commands.rs +++ b/src/wire_protocol/commands.rs @@ -2,7 +2,7 @@ use super::error::CommandError; use super::error::InvalidData; use super::error::Io; use bincode::Config; -use byteorder::{BigEndian, ByteOrder, ReadBytesExt, WriteBytesExt}; +use byteorder::{BigEndian, ByteOrder, ReadBytesExt}; use serde::de::{self, Deserializer, Unexpected, Visitor}; use serde::ser::Serializer; use serde::{Deserialize, Serialize}; @@ -507,15 +507,7 @@ impl Command for PaddingCommand { const TYPE_CODE: i32 = -1; fn write_fields(&self) -> Result, CommandError> { - let mut res = Vec::new(); - for _i in 0..(self.length / 8) { - res.extend_from_slice(&0_i64.to_be_bytes()); - } - for _i in 0..(self.length % 8) { - res.write_u8(0).context(Io { - command_type: Self::TYPE_CODE, - })?; - } + let res = vec![0; self.length as usize]; Ok(res) } From 3890e3b428aa06d5fba7ac0c99fb8a510f896ad5 Mon Sep 17 00:00:00 2001 From: Tom Kaitchuck Date: Fri, 10 Jan 2020 15:13:05 -0800 Subject: [PATCH 20/30] Create local variable Signed-off-by: Tom Kaitchuck --- src/wire_protocol/commands.rs | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/wire_protocol/commands.rs b/src/wire_protocol/commands.rs index e6c1c073d..7dc260507 100644 --- a/src/wire_protocol/commands.rs +++ b/src/wire_protocol/commands.rs @@ -709,19 +709,14 @@ impl Command for ConditionalAppendCommand { fn read_from(input: &[u8]) -> Result { let mut rdr = Cursor::new(input); - let writer_id = rdr.read_u128::().context(Io { + let ctx = Io { command_type: Self::TYPE_CODE, - })?; - let event_number = rdr.read_i64::().context(Io { - command_type: Self::TYPE_CODE, - })?; - let expected_offset = rdr.read_i64::().context(Io { - command_type: Self::TYPE_CODE, - })?; + }; + let writer_id = rdr.read_u128::().context(ctx)?; + let event_number = rdr.read_i64::().context(ctx)?; + let expected_offset = rdr.read_i64::().context(ctx)?; let event = ConditionalAppendCommand::read_event(&mut rdr); - let request_id = rdr.read_i64::().context(Io { - command_type: Self::TYPE_CODE, - })?; + let request_id = rdr.read_i64::().context(ctx)?; Ok(ConditionalAppendCommand { writer_id, event_number, From 9fad598014d993b7ca3197298279bb287f3ec6f9 Mon Sep 17 00:00:00 2001 From: Tom Kaitchuck Date: Fri, 10 Jan 2020 16:33:30 -0800 Subject: [PATCH 21/30] Propigate errors properly Signed-off-by: Tom Kaitchuck --- src/wire_protocol/commands.rs | 12 ++++++------ src/wire_protocol/wire_commands.rs | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/wire_protocol/commands.rs b/src/wire_protocol/commands.rs index 7dc260507..60ae4b825 100644 --- a/src/wire_protocol/commands.rs +++ b/src/wire_protocol/commands.rs @@ -681,13 +681,13 @@ pub struct ConditionalAppendCommand { } impl ConditionalAppendCommand { - fn read_event(rdr: &mut Cursor<&[u8]>) -> EventCommand { - let _type_code = rdr.read_i32::().unwrap(); - let event_length = rdr.read_u64::().unwrap(); + fn read_event(rdr: &mut Cursor<&[u8]>) -> Result { + let _type_code = rdr.read_i32::()?; + let event_length = rdr.read_u64::()?; // read the data in event let mut msg: Vec = vec![0; event_length as usize]; - rdr.read_exact(&mut msg).unwrap(); - EventCommand { data: msg } + rdr.read_exact(&mut msg)?; + Ok(EventCommand { data: msg }) } } @@ -715,7 +715,7 @@ impl Command for ConditionalAppendCommand { let writer_id = rdr.read_u128::().context(ctx)?; let event_number = rdr.read_i64::().context(ctx)?; let expected_offset = rdr.read_i64::().context(ctx)?; - let event = ConditionalAppendCommand::read_event(&mut rdr); + let event = ConditionalAppendCommand::read_event(&mut rdr).context(ctx)?; let request_id = rdr.read_i64::().context(ctx)?; Ok(ConditionalAppendCommand { writer_id, diff --git a/src/wire_protocol/wire_commands.rs b/src/wire_protocol/wire_commands.rs index fb307c9bc..6c4304390 100644 --- a/src/wire_protocol/wire_commands.rs +++ b/src/wire_protocol/wire_commands.rs @@ -69,7 +69,7 @@ pub trait Encode { } pub trait Decode { - fn read_from(raw_input: &Vec) -> Result; + fn read_from(raw_input: &[u8]) -> Result; } impl Encode for WireCommands { @@ -425,7 +425,7 @@ impl Encode for WireCommands { } impl Decode for WireCommands { - fn read_from(raw_input: &Vec) -> Result { + fn read_from(raw_input: &[u8]) -> Result { let type_code = BigEndian::read_i32(raw_input); let _length = BigEndian::read_i32(&raw_input[4..]); let input = &raw_input[8..]; From eb29d50a83c8b9e47fea98084215c2f546daec02 Mon Sep 17 00:00:00 2001 From: Tom Kaitchuck Date: Fri, 10 Jan 2020 17:32:26 -0800 Subject: [PATCH 22/30] Add some additional information for Cargo Signed-off-by: Tom Kaitchuck --- Cargo.toml | 9 +++++++++ controller-client/Cargo.toml | 9 ++++++++- controller-client/Readme.md | 3 +++ src/lib.rs | 2 +- 4 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 controller-client/Readme.md diff --git a/Cargo.toml b/Cargo.toml index fbe7c2199..2507bac06 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,15 @@ name = "pravega-client-rust" version = "0.1.0" edition = "2018" +categories = ["Network programming"] +keywords = ["streaming", "client", "pravega"] +readme = "Readme.md" +repository = "https://github.com/pravega/pravega-client-rust" +license = "Apache-2.0" +license-file = "LICENSE" +description = "A Rust client for Pravega. (Pravega.io)" +authors = ["Tom Kaitchuck ", "Wenqi Mou ", + "Sandeep Shridhar ", "Wenxiao Zhang "] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/controller-client/Cargo.toml b/controller-client/Cargo.toml index 1a0edb103..0edcdb127 100644 --- a/controller-client/Cargo.toml +++ b/controller-client/Cargo.toml @@ -3,7 +3,14 @@ name = "pravega-controller-client" version = "0.1.0" edition = "2018" build = "build.rs" - +categories = ["Network programming"] +keywords = ["streaming", "client", "pravega"] +readme = "Readme.md" +repository = "https://github.com/pravega/pravega-client-rust" +license = "Apache-2.0" +description = "An internal library used by the Rust client for Pravega to talk to the Pravega controller." +authors = ["Tom Kaitchuck ", "Wenqi Mou ", + "Sandeep Shridhar ", "Wenxiao Zhang "] [dependencies] tonic = "0.1.0-beta.1" # using a * as per tonic documentation does not work. diff --git a/controller-client/Readme.md b/controller-client/Readme.md new file mode 100644 index 000000000..b5f715eb7 --- /dev/null +++ b/controller-client/Readme.md @@ -0,0 +1,3 @@ +# Controller client + +This provides a client to access the controller's APIs via GRPC. \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 07e0bf1fa..07ee067d1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,13 +20,13 @@ )] #![warn( clippy::cargo_common_metadata, - clippy::multiple_crate_versions, clippy::mutex_integer, clippy::needless_borrow, clippy::option_unwrap_used, clippy::result_unwrap_used, clippy::similar_names )] +#![allow(clippy::multiple_crate_versions)] #[macro_use] extern crate lazy_static; From 86c21a8f9ed8a70d64279ef14b2233b2f5e7bc07 Mon Sep 17 00:00:00 2001 From: Tom Kaitchuck Date: Fri, 10 Jan 2020 18:06:29 -0800 Subject: [PATCH 23/30] Enable clippy checks on our PRs Signed-off-by: Tom Kaitchuck --- .github/workflows/cibuild.yml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/cibuild.yml b/.github/workflows/cibuild.yml index 40d003e96..15025af6b 100644 --- a/.github/workflows/cibuild.yml +++ b/.github/workflows/cibuild.yml @@ -20,7 +20,6 @@ jobs: - name: Run cargo check uses: actions-rs/cargo@v1 - continue-on-error: true # WARNING: only for this example, remove it! with: command: check @@ -83,9 +82,13 @@ jobs: - name: Install clippy run: rustup component add clippy + - name: Run cargo clean + uses: actions-rs/cargo@v1 + with: + command: clean + - name: Run cargo clippy uses: actions-rs/cargo@v1 - continue-on-error: true # WARNING: only for this example, remove it! with: command: clippy args: -- -D warnings From 2f77e8761bb2bdb0bfbede9a46876e1529f83129 Mon Sep 17 00:00:00 2001 From: Tom Kaitchuck Date: Fri, 10 Jan 2020 18:12:25 -0800 Subject: [PATCH 24/30] Remove redundant licence declaration Signed-off-by: Tom Kaitchuck --- Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 2507bac06..8ce420efd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,6 @@ keywords = ["streaming", "client", "pravega"] readme = "Readme.md" repository = "https://github.com/pravega/pravega-client-rust" license = "Apache-2.0" -license-file = "LICENSE" description = "A Rust client for Pravega. (Pravega.io)" authors = ["Tom Kaitchuck ", "Wenqi Mou ", "Sandeep Shridhar ", "Wenxiao Zhang "] From cf537d13557874dae02accaca9dd07b0ea08ca52 Mon Sep 17 00:00:00 2001 From: Tom Kaitchuck Date: Fri, 10 Jan 2020 18:12:50 -0800 Subject: [PATCH 25/30] Disallow unsafe code Signed-off-by: Tom Kaitchuck --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index 07ee067d1..93caa454e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,6 +31,7 @@ #[macro_use] extern crate lazy_static; +#[forbid(unsafe_code)] #[allow(dead_code)] pub mod wire_protocol { mod commands; From 796c1265fb58feafff1272a98d9877556a5db0a0 Mon Sep 17 00:00:00 2001 From: Tom Kaitchuck Date: Mon, 20 Jan 2020 11:14:23 -0800 Subject: [PATCH 26/30] Add output dir to gitignore Signed-off-by: Tom Kaitchuck --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 927e6b51b..64463d300 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # Generated by Cargo # will have compiled files and executables /target/ +/controller-client/ # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html From f3d60858dec661e56908f755008e88f1f29aaf83 Mon Sep 17 00:00:00 2001 From: Tom Kaitchuck Date: Mon, 20 Jan 2020 11:23:18 -0800 Subject: [PATCH 27/30] Switch to bincode Signed-off-by: Tom Kaitchuck --- Cargo.toml | 2 +- src/wire_protocol/commands.rs | 4 ++-- src/wire_protocol/error.rs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8ce420efd..a954891a1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ members = [ [dependencies] async-stream = "0.1.2" async-trait = "0.1.17" -bincode = "1.2" +bincode2 = "2.0.1" byteorder = "1.3" futures = "0.3" lazy_static = "1.4.0" diff --git a/src/wire_protocol/commands.rs b/src/wire_protocol/commands.rs index 82816ddda..8e1571011 100644 --- a/src/wire_protocol/commands.rs +++ b/src/wire_protocol/commands.rs @@ -1,7 +1,7 @@ use super::error::CommandError; use super::error::InvalidData; use super::error::Io; -use bincode::Config; +use bincode2::Config; use byteorder::{BigEndian, ByteOrder, ReadBytesExt}; use serde::de::{self, Deserializer, Unexpected, Visitor}; use serde::ser::Serializer; @@ -124,7 +124,7 @@ impl<'de> Deserialize<'de> for JavaString { */ lazy_static! { static ref CONFIG: Config = { - let mut config = bincode::config(); + let mut config = bincode2::config(); config.big_endian(); config }; diff --git a/src/wire_protocol/error.rs b/src/wire_protocol/error.rs index 5428bfc81..d0b357894 100644 --- a/src/wire_protocol/error.rs +++ b/src/wire_protocol/error.rs @@ -1,5 +1,5 @@ use super::connection_factory::ConnectionType; -use bincode::Error as BincodeError; +use bincode2::Error as BincodeError; use snafu::Snafu; use std::io::Error as IoError; use std::net::SocketAddr; From 01dffebd623917d39b863fa2e8841e08321356bf Mon Sep 17 00:00:00 2001 From: Tom Kaitchuck Date: Mon, 20 Jan 2020 11:23:53 -0800 Subject: [PATCH 28/30] Use results in unit tests. Signed-off-by: Tom Kaitchuck --- controller-client/src/test.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/controller-client/src/test.rs b/controller-client/src/test.rs index fc569f259..134674b41 100644 --- a/controller-client/src/test.rs +++ b/controller-client/src/test.rs @@ -15,7 +15,7 @@ fn test_create_scope_error() { }; let fut = create_scope(request, &mut client); - rt.block_on(fut); + rt.block_on(fut).unwrap(); } #[test] @@ -41,5 +41,5 @@ fn test_create_stream_error() { }; let fut = create_stream(request, &mut client); - rt.block_on(fut); + rt.block_on(fut).unwrap(); } From 76172bc75261526ce7edfdd6d9458426b427db38 Mon Sep 17 00:00:00 2001 From: Tom Kaitchuck Date: Mon, 20 Jan 2020 12:14:21 -0800 Subject: [PATCH 29/30] Remove JavaString and use Bincode2's custom length. Signed-off-by: Tom Kaitchuck --- src/wire_protocol/commands.rs | 238 ++++++++++++---------------------- src/wire_protocol/tests.rs | 154 +++++++++++----------- 2 files changed, 159 insertions(+), 233 deletions(-) diff --git a/src/wire_protocol/commands.rs b/src/wire_protocol/commands.rs index 8e1571011..96c4d12bf 100644 --- a/src/wire_protocol/commands.rs +++ b/src/wire_protocol/commands.rs @@ -2,9 +2,8 @@ use super::error::CommandError; use super::error::InvalidData; use super::error::Io; use bincode2::Config; +use bincode2::LengthOption; use byteorder::{BigEndian, ByteOrder, ReadBytesExt}; -use serde::de::{self, Deserializer, Unexpected, Visitor}; -use serde::ser::Serializer; use serde::{Deserialize, Serialize}; use snafu::ResultExt; use std::fmt; @@ -43,82 +42,6 @@ pub trait Reply { } } -/** - * Wrap String to follow Java Serialize/Deserialize Style. - * - */ -pub struct JavaString(pub String); - -impl fmt::Debug for JavaString { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.0) - } -} - -impl fmt::Display for JavaString { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.0) - } -} - -impl PartialEq for JavaString { - fn eq(&self, other: &Self) -> bool { - self.0 == other.0 - } -} - -impl Serialize for JavaString { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - // get the length of the String. - let length = self.0.len() as u16; - // get the content of the String. - let binary = self.0.as_bytes(); - // Serialize - let mut content = vec![]; - content.extend_from_slice(&length.to_be_bytes()); - content.extend(binary); - serializer.serialize_bytes(&content) - } -} - -struct JavaStringVisitor; - -impl<'de> Visitor<'de> for JavaStringVisitor { - type Value = JavaString; - - fn expecting(&self, formatter: &mut std::fmt::Formatter) -> fmt::Result { - formatter.write_str("A Byte buffer which contains length and content") - } - - fn visit_byte_buf(self, value: Vec) -> Result - where - E: de::Error, - { - // get the length - let _length = ((value[0] as u16) << 8) | value[1] as u16; - // construct the JavaString - let content = String::from_utf8_lossy(&value[2..]).into_owned(); - - if _length == content.len() as u16 { - Ok(JavaString(content)) - } else { - Err(de::Error::invalid_value(Unexpected::Bytes(&value), &self)) - } - } -} - -impl<'de> Deserialize<'de> for JavaString { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_byte_buf(JavaStringVisitor) - } -} - /* * bincode serialize and deserialize config */ @@ -126,6 +49,9 @@ lazy_static! { static ref CONFIG: Config = { let mut config = bincode2::config(); config.big_endian(); + config.limit(0x007f_ffff); + config.array_length(LengthOption::U32); + config.string_length(LengthOption::U16); config }; } @@ -174,9 +100,9 @@ impl Reply for HelloCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct WrongHostCommand { pub request_id: i64, - pub segment: JavaString, - pub correct_host: JavaString, - pub server_stack_trace: JavaString, + pub segment: String, + pub correct_host: String, + pub server_stack_trace: String, } impl Command for WrongHostCommand { @@ -210,8 +136,8 @@ impl Reply for WrongHostCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct SegmentIsSealedCommand { pub request_id: i64, - pub segment: JavaString, - pub server_stack_trace: JavaString, + pub segment: String, + pub server_stack_trace: String, pub offset: i64, } @@ -248,9 +174,9 @@ impl Reply for SegmentIsSealedCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct SegmentIsTruncatedCommand { pub request_id: i64, - pub segment: JavaString, + pub segment: String, pub start_offset: i64, - pub server_stack_trace: JavaString, + pub server_stack_trace: String, pub offset: i64, } @@ -287,8 +213,8 @@ impl Reply for SegmentIsTruncatedCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct SegmentAlreadyExistsCommand { pub request_id: i64, - pub segment: JavaString, - pub server_stack_trace: JavaString, + pub segment: String, + pub server_stack_trace: String, } impl Command for SegmentAlreadyExistsCommand { @@ -330,8 +256,8 @@ impl fmt::Display for SegmentAlreadyExistsCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct NoSuchSegmentCommand { pub request_id: i64, - pub segment: JavaString, - pub server_stack_trace: JavaString, + pub segment: String, + pub server_stack_trace: String, pub offset: i64, } @@ -374,8 +300,8 @@ impl fmt::Display for NoSuchSegmentCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct TableSegmentNotEmptyCommand { pub request_id: i64, - pub segment: JavaString, - pub server_stack_trace: JavaString, + pub segment: String, + pub server_stack_trace: String, } impl Command for TableSegmentNotEmptyCommand { @@ -418,7 +344,7 @@ impl fmt::Display for TableSegmentNotEmptyCommand { pub struct InvalidEventNumberCommand { pub writer_id: u128, pub event_number: i64, - pub server_stack_trace: JavaString, + pub server_stack_trace: String, } impl Command for InvalidEventNumberCommand { @@ -464,8 +390,8 @@ impl fmt::Display for InvalidEventNumberCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct OperationUnsupportedCommand { pub request_id: i64, - pub operation_name: JavaString, - pub server_stack_trace: JavaString, + pub operation_name: String, + pub server_stack_trace: String, } impl Command for OperationUnsupportedCommand { @@ -581,8 +507,8 @@ impl Command for EventCommand { pub struct SetupAppendCommand { pub request_id: i64, pub writer_id: u128, - pub segment: JavaString, - pub delegation_token: JavaString, + pub segment: String, + pub delegation_token: String, } impl Command for SetupAppendCommand { @@ -683,7 +609,7 @@ pub struct ConditionalAppendCommand { impl ConditionalAppendCommand { fn read_event(rdr: &mut Cursor<&[u8]>) -> Result { let _type_code = rdr.read_i32::()?; - let event_length = rdr.read_u64::()?; + let event_length = rdr.read_u32::()?; // read the data in event let mut msg: Vec = vec![0; event_length as usize]; rdr.read_exact(&mut msg)?; @@ -739,7 +665,7 @@ impl Request for ConditionalAppendCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct AppendSetupCommand { pub request_id: i64, - pub segment: JavaString, + pub segment: String, pub writer_id: u128, pub last_event_number: i64, } @@ -844,10 +770,10 @@ impl Reply for ConditionalAppendCommand { */ #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct ReadSegmentCommand { - pub segment: JavaString, + pub segment: String, pub offset: i64, pub suggested_length: i64, - pub delegation_token: JavaString, + pub delegation_token: String, pub request_id: i64, } @@ -880,7 +806,7 @@ impl Request for ReadSegmentCommand { */ #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct SegmentReadCommand { - pub segment: JavaString, + pub segment: String, pub offset: i64, pub at_tail: bool, pub end_of_segment: bool, @@ -918,9 +844,9 @@ impl Reply for SegmentReadCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct GetSegmentAttributeCommand { pub request_id: i64, - pub segment_name: JavaString, + pub segment_name: String, pub attribute_id: u128, - pub delegation_token: JavaString, + pub delegation_token: String, } impl Command for GetSegmentAttributeCommand { @@ -988,11 +914,11 @@ impl Reply for SegmentAttributeCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct UpdateSegmentAttributeCommand { pub request_id: i64, - pub segment_name: JavaString, + pub segment_name: String, pub attribute_id: u128, pub new_value: i64, pub expected_value: i64, - pub delegation_token: JavaString, + pub delegation_token: String, } impl Command for UpdateSegmentAttributeCommand { @@ -1060,8 +986,8 @@ impl Reply for SegmentAttributeUpdatedCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct GetStreamSegmentInfoCommand { pub request_id: i64, - pub segment_name: JavaString, - pub delegation_token: JavaString, + pub segment_name: String, + pub delegation_token: String, } impl Command for GetStreamSegmentInfoCommand { @@ -1095,7 +1021,7 @@ impl Request for GetStreamSegmentInfoCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct StreamSegmentInfoCommand { pub request_id: i64, - pub segment_name: JavaString, + pub segment_name: String, pub exists: bool, pub is_sealed: bool, pub is_deleted: bool, @@ -1135,10 +1061,10 @@ impl Reply for StreamSegmentInfoCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct CreateSegmentCommand { pub request_id: i64, - pub segment: JavaString, + pub segment: String, pub target_rate: i32, pub scale_type: u8, - pub delegation_token: JavaString, + pub delegation_token: String, } impl Command for CreateSegmentCommand { @@ -1172,8 +1098,8 @@ impl Request for CreateSegmentCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct CreateTableSegmentCommand { pub request_id: i64, - pub segment: JavaString, - pub delegation_token: JavaString, + pub segment: String, + pub delegation_token: String, } impl Command for CreateTableSegmentCommand { @@ -1207,7 +1133,7 @@ impl Request for CreateTableSegmentCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct SegmentCreatedCommand { pub request_id: i64, - pub segment: JavaString, + pub segment: String, } impl Command for SegmentCreatedCommand { @@ -1241,10 +1167,10 @@ impl Reply for SegmentCreatedCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct UpdateSegmentPolicyCommand { pub request_id: i64, - pub segment: JavaString, + pub segment: String, pub target_rate: i32, pub scale_type: u8, - pub delegation_token: JavaString, + pub delegation_token: String, } impl Command for UpdateSegmentPolicyCommand { @@ -1278,7 +1204,7 @@ impl Request for UpdateSegmentPolicyCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct SegmentPolicyUpdatedCommand { pub request_id: i64, - pub segment: JavaString, + pub segment: String, } impl Command for SegmentPolicyUpdatedCommand { @@ -1312,9 +1238,9 @@ impl Reply for SegmentPolicyUpdatedCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct MergeSegmentsCommand { pub request_id: i64, - pub target: JavaString, - pub source: JavaString, - pub delegation_token: JavaString, + pub target: String, + pub source: String, + pub delegation_token: String, } impl Command for MergeSegmentsCommand { @@ -1347,9 +1273,9 @@ impl Request for MergeSegmentsCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct MergeTableSegmentsCommand { pub request_id: i64, - pub target: JavaString, - pub source: JavaString, - pub delegation_token: JavaString, + pub target: String, + pub source: String, + pub delegation_token: String, } impl Command for MergeTableSegmentsCommand { @@ -1383,8 +1309,8 @@ impl Request for MergeTableSegmentsCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct SegmentsMergedCommand { pub request_id: i64, - pub target: JavaString, - pub source: JavaString, + pub target: String, + pub source: String, pub new_target_write_offset: i64, } @@ -1419,8 +1345,8 @@ impl Reply for SegmentsMergedCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct SealSegmentCommand { pub request_id: i64, - pub segment: JavaString, - pub delegation_token: JavaString, + pub segment: String, + pub delegation_token: String, } impl Command for SealSegmentCommand { @@ -1453,8 +1379,8 @@ impl Request for SealSegmentCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct SealTableSegmentCommand { pub request_id: i64, - pub segment: JavaString, - pub delegation_token: JavaString, + pub segment: String, + pub delegation_token: String, } impl Command for SealTableSegmentCommand { @@ -1488,7 +1414,7 @@ impl Request for SealTableSegmentCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct SegmentSealedCommand { pub request_id: i64, - pub segment: JavaString, + pub segment: String, } impl Command for SegmentSealedCommand { @@ -1522,9 +1448,9 @@ impl Reply for SegmentSealedCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct TruncateSegmentCommand { pub request_id: i64, - pub segment: JavaString, + pub segment: String, pub truncation_offset: i64, - pub delegation_token: JavaString, + pub delegation_token: String, } impl Command for TruncateSegmentCommand { @@ -1558,7 +1484,7 @@ impl Request for TruncateSegmentCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct SegmentTruncatedCommand { pub request_id: i64, - pub segment: JavaString, + pub segment: String, } impl Command for SegmentTruncatedCommand { @@ -1592,8 +1518,8 @@ impl Reply for SegmentTruncatedCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct DeleteSegmentCommand { pub request_id: i64, - pub segment: JavaString, - pub delegation_token: JavaString, + pub segment: String, + pub delegation_token: String, } impl Command for DeleteSegmentCommand { @@ -1627,9 +1553,9 @@ impl Request for DeleteSegmentCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct DeleteTableSegmentCommand { pub request_id: i64, - pub segment: JavaString, + pub segment: String, pub must_be_empty: bool, // If true, the Table Segment will only be deleted if it is empty (contains no keys) - pub delegation_token: JavaString, + pub delegation_token: String, } impl Command for DeleteTableSegmentCommand { @@ -1662,7 +1588,7 @@ impl Request for DeleteTableSegmentCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct SegmentDeletedCommand { pub request_id: i64, - pub segment: JavaString, + pub segment: String, } impl Command for SegmentDeletedCommand { @@ -1729,7 +1655,7 @@ impl Reply for KeepAliveCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct AuthTokenCheckFailedCommand { pub request_id: i64, - pub server_stack_trace: JavaString, + pub server_stack_trace: String, pub error_code: i32, } @@ -1799,8 +1725,8 @@ impl ErrorCode { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct UpdateTableEntriesCommand { pub request_id: i64, - pub segment: JavaString, - pub delegation_token: JavaString, + pub segment: String, + pub delegation_token: String, pub table_entries: TableEntries, } @@ -1869,8 +1795,8 @@ impl Reply for TableEntriesUpdatedCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct RemoveTableKeysCommand { pub request_id: i64, - pub segment: JavaString, - pub delegation_token: JavaString, + pub segment: String, + pub delegation_token: String, pub keys: Vec, } @@ -1905,7 +1831,7 @@ impl Request for RemoveTableKeysCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct TableKeysRemovedCommand { pub request_id: i64, - pub segment: JavaString, + pub segment: String, } impl Command for TableKeysRemovedCommand { @@ -1939,8 +1865,8 @@ impl Reply for TableKeysRemovedCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct ReadTableCommand { pub request_id: i64, - pub segment: JavaString, - pub delegation_token: JavaString, + pub segment: String, + pub delegation_token: String, pub keys: Vec, } @@ -1974,7 +1900,7 @@ impl Request for ReadTableCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct TableReadCommand { pub request_id: i64, - pub segment: JavaString, + pub segment: String, pub entries: TableEntries, } @@ -2008,8 +1934,8 @@ impl Reply for TableReadCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct ReadTableKeysCommand { pub request_id: i64, - pub segment: JavaString, - pub delegation_token: JavaString, + pub segment: String, + pub delegation_token: String, pub suggested_key_count: i32, pub continuation_token: Vec, } @@ -2045,7 +1971,7 @@ impl Request for ReadTableKeysCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct TableKeysReadCommand { pub request_id: i64, - pub segment: JavaString, + pub segment: String, pub keys: Vec, pub continuation_token: Vec, // this is used to indicate the point from which the next keys should be fetched. } @@ -2081,8 +2007,8 @@ impl Reply for TableKeysReadCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct ReadTableEntriesCommand { pub request_id: i64, - pub segment: JavaString, - pub delegation_token: JavaString, + pub segment: String, + pub delegation_token: String, pub suggested_entry_count: i32, pub continuation_token: Vec, } @@ -2118,7 +2044,7 @@ impl Request for ReadTableEntriesCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct TableEntriesReadCommand { pub request_id: i64, - pub segment: JavaString, + pub segment: String, pub entries: TableEntries, pub continuation_token: Vec, } @@ -2154,8 +2080,8 @@ impl Reply for TableEntriesReadCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct TableKeyDoesNotExistCommand { pub request_id: i64, - pub segment: JavaString, - pub server_stack_trace: JavaString, + pub segment: String, + pub server_stack_trace: String, } impl Command for TableKeyDoesNotExistCommand { @@ -2203,8 +2129,8 @@ impl fmt::Display for TableKeyDoesNotExistCommand { #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct TableKeyBadVersionCommand { pub request_id: i64, - pub segment: JavaString, - pub server_stack_trace: JavaString, + pub segment: String, + pub server_stack_trace: String, } impl Command for TableKeyBadVersionCommand { diff --git a/src/wire_protocol/tests.rs b/src/wire_protocol/tests.rs index 44c217595..3ae65ff66 100644 --- a/src/wire_protocol/tests.rs +++ b/src/wire_protocol/tests.rs @@ -12,9 +12,9 @@ fn test_hello() { #[test] fn test_wrong_host() { - let correct_host_name = JavaString(String::from("foo")); - let segment_name = JavaString(String::from("segment-1")); - let stack_trace = JavaString(String::from("some exception")); + let correct_host_name = String::from("foo"); + let segment_name = String::from("segment-1"); + let stack_trace = String::from("some exception"); let wrong_host_command = WireCommands::WrongHost(WrongHostCommand { request_id: 1, segment: segment_name, @@ -26,8 +26,8 @@ fn test_wrong_host() { #[test] fn test_segment_is_sealed() { - let segment_name = JavaString(String::from("segment-1")); - let stack_trace = JavaString(String::from("some exception")); + let segment_name = String::from("segment-1"); + let stack_trace = String::from("some exception"); let offset_pos = 100i64; let segment_is_sealed_command = WireCommands::SegmentIsSealed(SegmentIsSealedCommand { request_id: 1, @@ -40,8 +40,8 @@ fn test_segment_is_sealed() { #[test] fn test_segment_already_exists() { - let segment_name = JavaString(String::from("segment-1")); - let stack_trace = JavaString(String::from("some exception")); + let segment_name = String::from("segment-1"); + let stack_trace = String::from("some exception"); let segment_already_exists_command = WireCommands::SegmentAlreadyExists(SegmentAlreadyExistsCommand { request_id: 1, @@ -53,8 +53,8 @@ fn test_segment_already_exists() { #[test] fn test_segment_is_truncated() { - let segment_name = JavaString(String::from("segment-1")); - let stack_trace = JavaString(String::from("some exception")); + let segment_name = String::from("segment-1"); + let stack_trace = String::from("some exception"); let start_offset_pos = 0i64; let offset_pos = 100i64; let segment_is_truncated_command = @@ -70,8 +70,8 @@ fn test_segment_is_truncated() { #[test] fn test_no_such_segment() { - let segment_name = JavaString(String::from("segment-1")); - let stack_trace = JavaString(String::from("some exception")); + let segment_name = String::from("segment-1"); + let stack_trace = String::from("some exception"); let offset_pos = 100i64; let no_such_segment_command = WireCommands::NoSuchSegment(NoSuchSegmentCommand { request_id: 1, @@ -84,8 +84,8 @@ fn test_no_such_segment() { #[test] fn test_table_segment_not_empty() { - let segment_name = JavaString(String::from("segment-1")); - let stack_trace = JavaString(String::from("some exception")); + let segment_name = String::from("segment-1"); + let stack_trace = String::from("some exception"); let table_segment_not_empty_command = WireCommands::TableSegmentNotEmpty(TableSegmentNotEmptyCommand { request_id: 1, @@ -99,7 +99,7 @@ fn test_table_segment_not_empty() { fn test_invalid_event_number() { let writer_id_number: u128 = 123; let event_num: i64 = 100; - let stack_trace = JavaString(String::from("some exception")); + let stack_trace = String::from("some exception"); let invalid_event_number_command = WireCommands::InvalidEventNumber(InvalidEventNumberCommand { writer_id: writer_id_number, @@ -111,8 +111,8 @@ fn test_invalid_event_number() { #[test] fn test_operation_unsupported() { - let name = JavaString(String::from("operation")); - let stack_trace = JavaString(String::from("some exception")); + let name = String::from("operation"); + let stack_trace = String::from("some exception"); let test_operation_unsupported_command = WireCommands::OperationUnsupported(OperationUnsupportedCommand { request_id: 1, @@ -154,8 +154,8 @@ fn test_event() { #[test] fn test_setup_append() { let writer_id_number: u128 = 123; - let segment_name = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); + let segment_name = String::from("segment-1"); + let token = String::from("delegation_token"); let setup_append_command = WireCommands::SetupAppend(SetupAppendCommand { request_id: 1, writer_id: writer_id_number, @@ -218,7 +218,7 @@ fn test_conditional_append() { #[test] fn test_append_setup() { let writer_id_number: u128 = 123; - let segment_name = JavaString(String::from("segment-1")); + let segment_name = String::from("segment-1"); let append_setup_cmd = WireCommands::AppendSetup(AppendSetupCommand { request_id: 1, segment: segment_name, @@ -255,8 +255,8 @@ fn test_conditional_check_failed() { #[test] fn test_read_segment() { - let segment_name = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); + let segment_name = String::from("segment-1"); + let token = String::from("delegation_token"); let read_segment_command = WireCommands::ReadSegment(ReadSegmentCommand { segment: segment_name, offset: 0, @@ -269,7 +269,7 @@ fn test_read_segment() { #[test] fn test_segment_read() { - let segment_name = JavaString(String::from("segment-1")); + let segment_name = String::from("segment-1"); let data = String::from("event-1").into_bytes(); let segment_read_command = WireCommands::SegmentRead(SegmentReadCommand { segment: segment_name, @@ -284,8 +284,8 @@ fn test_segment_read() { #[test] fn test_get_segment_attribute() { - let segment_name = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); + let segment_name = String::from("segment-1"); + let token = String::from("delegation_token"); let attribute_id: u128 = 123; let get_segment_attribute_command = WireCommands::GetSegmentAttribute(GetSegmentAttributeCommand { @@ -308,8 +308,8 @@ fn test_segment_attribute() { #[test] fn test_update_segment_attribute() { - let segment_name = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); + let segment_name = String::from("segment-1"); + let token = String::from("delegation_token"); let attribute_id: u128 = 123; let update_segment_attribute = WireCommands::UpdateSegmentAttribute(UpdateSegmentAttributeCommand { @@ -335,8 +335,8 @@ fn test_segment_attribute_updated() { #[test] fn test_get_stream_segment_info() { - let segment_name = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); + let segment_name = String::from("segment-1"); + let token = String::from("delegation_token"); let get_stream_segment_info = WireCommands::GetStreamSegmentInfo(GetStreamSegmentInfoCommand { request_id: 1, segment_name, @@ -347,7 +347,7 @@ fn test_get_stream_segment_info() { #[test] fn test_stream_segment_info() { - let segment_name = JavaString(String::from("segment-1")); + let segment_name = String::from("segment-1"); let stream_segment_info = WireCommands::StreamSegmentInfo(StreamSegmentInfoCommand { request_id: 0, segment_name, @@ -363,8 +363,8 @@ fn test_stream_segment_info() { #[test] fn test_create_segment() { - let segment_name = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); + let segment_name = String::from("segment-1"); + let token = String::from("delegation_token"); let create_segment_command = WireCommands::CreateSegment(CreateSegmentCommand { request_id: 1, segment: segment_name, @@ -377,8 +377,8 @@ fn test_create_segment() { #[test] fn test_create_table_segment() { - let segment_name = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); + let segment_name = String::from("segment-1"); + let token = String::from("delegation_token"); let create_table_segment_command = WireCommands::CreateTableSegment(CreateTableSegmentCommand { request_id: 1, @@ -390,7 +390,7 @@ fn test_create_table_segment() { #[test] fn test_segment_created() { - let segment_name = JavaString(String::from("segment-1")); + let segment_name = String::from("segment-1"); let segment_created_cmd = WireCommands::SegmentCreated(SegmentCreatedCommand { request_id: 1, segment: segment_name, @@ -400,8 +400,8 @@ fn test_segment_created() { #[test] fn test_update_segment_policy() { - let segment_name = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); + let segment_name = String::from("segment-1"); + let token = String::from("delegation_token"); let update_segment_policy_cmd = WireCommands::UpdateSegmentPolicy(UpdateSegmentPolicyCommand { request_id: 1, segment: segment_name, @@ -414,7 +414,7 @@ fn test_update_segment_policy() { #[test] fn test_segment_policy_updated() { - let segment_name = JavaString(String::from("segment-1")); + let segment_name = String::from("segment-1"); let segment_policy_updated = WireCommands::SegmentPolicyUpdated(SegmentPolicyUpdatedCommand { request_id: 0, segment: segment_name, @@ -424,9 +424,9 @@ fn test_segment_policy_updated() { #[test] fn test_merge_segment() { - let target = JavaString(String::from("segment-1")); - let source = JavaString(String::from("segment-2")); - let token = JavaString(String::from("delegation_token")); + let target = String::from("segment-1"); + let source = String::from("segment-2"); + let token = String::from("delegation_token"); let merge_segment = WireCommands::MergeSegments(MergeSegmentsCommand { request_id: 1, target, @@ -438,9 +438,9 @@ fn test_merge_segment() { #[test] fn test_merge_table_segment() { - let target = JavaString(String::from("segment-1")); - let source = JavaString(String::from("segment-2")); - let token = JavaString(String::from("delegation_token")); + let target = String::from("segment-1"); + let source = String::from("segment-2"); + let token = String::from("delegation_token"); let merge_table_segment = WireCommands::MergeTableSegments(MergeTableSegmentsCommand { request_id: 1, target, @@ -452,8 +452,8 @@ fn test_merge_table_segment() { #[test] fn test_segment_merged() { - let target = JavaString(String::from("segment-1")); - let source = JavaString(String::from("segment-2")); + let target = String::from("segment-1"); + let source = String::from("segment-2"); let segment_merged = WireCommands::SegmentsMerged(SegmentsMergedCommand { request_id: 1, target, @@ -465,8 +465,8 @@ fn test_segment_merged() { #[test] fn test_seal_segment() { - let segment_name = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); + let segment_name = String::from("segment-1"); + let token = String::from("delegation_token"); let seal_segment = WireCommands::SealSegment(SealSegmentCommand { request_id: 1, segment: segment_name, @@ -477,8 +477,8 @@ fn test_seal_segment() { #[test] fn test_seal_table_segment() { - let segment_name = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); + let segment_name = String::from("segment-1"); + let token = String::from("delegation_token"); let seal_table_segment = WireCommands::SealTableSegment(SealTableSegmentCommand { request_id: 1, segment: segment_name, @@ -489,7 +489,7 @@ fn test_seal_table_segment() { #[test] fn test_segment_sealed() { - let segment_name = JavaString(String::from("segment-1")); + let segment_name = String::from("segment-1"); let segment_sealed = WireCommands::SegmentSealed(SegmentSealedCommand { request_id: 1, segment: segment_name, @@ -499,8 +499,8 @@ fn test_segment_sealed() { #[test] fn test_truncate_segment() { - let segment_name = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); + let segment_name = String::from("segment-1"); + let token = String::from("delegation_token"); let truncate_segment = WireCommands::TruncateSegment(TruncateSegmentCommand { request_id: 1, segment: segment_name, @@ -512,7 +512,7 @@ fn test_truncate_segment() { #[test] fn test_segment_truncated() { - let segment_name = JavaString(String::from("segment-1")); + let segment_name = String::from("segment-1"); let segment_truncated = WireCommands::SegmentTruncated(SegmentTruncatedCommand { request_id: 1, segment: segment_name, @@ -522,8 +522,8 @@ fn test_segment_truncated() { #[test] fn test_delete_segment() { - let segment_name = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); + let segment_name = String::from("segment-1"); + let token = String::from("delegation_token"); let delete_segment_command = WireCommands::DeleteSegment(DeleteSegmentCommand { request_id: 1, segment: segment_name, @@ -534,7 +534,7 @@ fn test_delete_segment() { #[test] fn test_segment_deleted() { - let segment = JavaString(String::from("segment-1")); + let segment = String::from("segment-1"); let segment_deleted = WireCommands::SegmentDeleted(SegmentDeletedCommand { request_id: 1, segment, @@ -544,8 +544,8 @@ fn test_segment_deleted() { #[test] fn test_delete_table_segment() { - let segment_name = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); + let segment_name = String::from("segment-1"); + let token = String::from("delegation_token"); let delete_table_segment = WireCommands::DeleteTableSegment(DeleteTableSegmentCommand { request_id: 0, segment: segment_name, @@ -563,7 +563,7 @@ fn test_keep_alive() { #[test] fn test_auth_checked_failed() { - let stack_trace = JavaString(String::from("some exception")); + let stack_trace = String::from("some exception"); let auth_checked_failed = WireCommands::AuthTokenCheckFailed(AuthTokenCheckFailedCommand { request_id: 1, server_stack_trace: stack_trace, @@ -584,8 +584,8 @@ fn test_update_table_entries() { let value_data = String::from("value-1").into_bytes(); entries.push((TableKey::new(key_data, 1), TableValue::new(value_data))); let table_entries = TableEntries { entries }; - let segment_name = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); + let segment_name = String::from("segment-1"); + let token = String::from("delegation_token"); let _size = table_entries.size(); let update_table_entries = WireCommands::UpdateTableEntries(UpdateTableEntriesCommand { request_id: 1, @@ -609,8 +609,8 @@ fn test_table_entries_updated() { #[test] fn test_remove_table_keys() { - let segment = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); + let segment = String::from("segment-1"); + let token = String::from("delegation_token"); let mut keys = Vec::::new(); let key_data = String::from("key-1").into_bytes(); keys.push(TableKey::new(key_data, 1)); @@ -625,7 +625,7 @@ fn test_remove_table_keys() { #[test] fn test_table_keys_removed() { - let segment = JavaString(String::from("segment-1")); + let segment = String::from("segment-1"); let table_key_removed = WireCommands::TableKeysRemoved(TableKeysRemovedCommand { request_id: 1, segment, @@ -635,8 +635,8 @@ fn test_table_keys_removed() { #[test] fn test_read_table() { - let segment = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); + let segment = String::from("segment-1"); + let token = String::from("delegation_token"); let mut keys = Vec::::new(); let key_data = String::from("key-1").into_bytes(); keys.push(TableKey::new(key_data, 1)); @@ -657,7 +657,7 @@ fn test_table_read() { let value_data = String::from("value-1").into_bytes(); entries.push((TableKey::new(key_data, 1), TableValue::new(value_data))); let table_entries = TableEntries { entries }; - let segment_name = JavaString(String::from("segment-1")); + let segment_name = String::from("segment-1"); let table_read = WireCommands::TableRead(TableReadCommand { request_id: 1, segment: segment_name, @@ -669,8 +669,8 @@ fn test_table_read() { #[test] fn test_read_table_keys() { - let segment_name = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); + let segment_name = String::from("segment-1"); + let token = String::from("delegation_token"); let continuation_token: Vec = vec![1, 2, 3]; let read_table_keys = WireCommands::ReadTableKeys(ReadTableKeysCommand { request_id: 0, @@ -684,7 +684,7 @@ fn test_read_table_keys() { #[test] fn test_table_keys_read() { - let segment = JavaString(String::from("segment-1")); + let segment = String::from("segment-1"); let mut keys = Vec::::new(); let key_data = String::from("key-1").into_bytes(); keys.push(TableKey::new(key_data, 1)); @@ -700,8 +700,8 @@ fn test_table_keys_read() { #[test] fn test_read_table_entries() { - let segment_name = JavaString(String::from("segment-1")); - let token = JavaString(String::from("delegation_token")); + let segment_name = String::from("segment-1"); + let token = String::from("delegation_token"); let continuation_token: Vec = vec![1, 2, 3]; let read_table_entries = WireCommands::ReadTableEntries(ReadTableEntriesCommand { request_id: 0, @@ -715,7 +715,7 @@ fn test_read_table_entries() { #[test] fn test_table_entries_read() { - let segment_name = JavaString(String::from("segment-1")); + let segment_name = String::from("segment-1"); let continuation_token: Vec = vec![1, 2, 3]; let mut entries = Vec::<(TableKey, TableValue)>::new(); let key_data = String::from("key-1").into_bytes(); @@ -733,8 +733,8 @@ fn test_table_entries_read() { #[test] fn table_key_does_not_exist() { - let segment_name = JavaString(String::from("segment-1")); - let stack_trace = JavaString(String::from("some exception")); + let segment_name = String::from("segment-1"); + let stack_trace = String::from("some exception"); let table_key_does_not_exist = WireCommands::TableKeyDoesNotExist(TableKeyDoesNotExistCommand { request_id: 0, @@ -746,8 +746,8 @@ fn table_key_does_not_exist() { #[test] fn table_key_bad_version() { - let segment_name = JavaString(String::from("segment-1")); - let stack_trace = JavaString(String::from("some exception")); + let segment_name = String::from("segment-1"); + let stack_trace = String::from("some exception"); let table_key_bad_version = WireCommands::TableKeyBadVersion(TableKeyBadVersionCommand { request_id: 0, segment: segment_name, From b719c2efe41a7fa640eb6317b6e6f7a9687bc252 Mon Sep 17 00:00:00 2001 From: Tom Kaitchuck Date: Mon, 20 Jan 2020 14:14:36 -0800 Subject: [PATCH 30/30] Add backtraces to errors. Signed-off-by: Tom Kaitchuck --- Cargo.toml | 2 +- controller-client/src/lib.rs | 4 +++- src/wire_protocol/error.rs | 19 ++++++++++++++++--- src/wire_protocol/wire_commands.rs | 2 ++ 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a954891a1..a8b2b7e7a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,7 +28,7 @@ lazy_static = "1.4.0" log = "0.4" serde = { version = "1.0", features = ["derive"] } serde_repr = "0.1" -snafu = "0.5.0" +snafu = "0.6.2" tokio = { version = "0.2.8", features = ["full"] } [dev-dependencies] diff --git a/controller-client/src/lib.rs b/controller-client/src/lib.rs index 02a4c6df3..d8cb69f56 100644 --- a/controller-client/src/lib.rs +++ b/controller-client/src/lib.rs @@ -21,7 +21,7 @@ pub use controller::{ scaling_policy::ScalingPolicyType, CreateScopeStatus, CreateStreamStatus, ScalingPolicy, ScopeInfo, StreamConfig, StreamInfo, }; -use snafu::Snafu; +use snafu::{Backtrace, Snafu}; use tonic::transport::channel::Channel; use tonic::{Code, Status}; @@ -36,12 +36,14 @@ pub enum ControllerError { can_retry: bool, operation: String, error_msg: String, + backtrace: Backtrace, }, #[snafu(display("Could not connect to controller {}", endpoint))] ConnectionError { can_retry: bool, endpoint: String, error_msg: String, + backtrace: Backtrace, }, } diff --git a/src/wire_protocol/error.rs b/src/wire_protocol/error.rs index d0b357894..08f20116d 100644 --- a/src/wire_protocol/error.rs +++ b/src/wire_protocol/error.rs @@ -1,6 +1,6 @@ use super::connection_factory::ConnectionType; use bincode2::Error as BincodeError; -use snafu::Snafu; +use snafu::{Backtrace, Snafu}; use std::io::Error as IoError; use std::net::SocketAddr; @@ -17,16 +17,19 @@ pub enum ConnectionError { connection_type: ConnectionType, endpoint: SocketAddr, source: std::io::Error, + backtrace: Backtrace, }, #[snafu(display("Could not send data to {} asynchronously", endpoint))] SendData { endpoint: SocketAddr, source: std::io::Error, + backtrace: Backtrace, }, #[snafu(display("Could not read data from {} asynchronously", endpoint))] ReadData { endpoint: SocketAddr, source: std::io::Error, + backtrace: Backtrace, }, } @@ -42,18 +45,26 @@ pub enum CommandError { InvalidData { command_type: i32, source: BincodeError, + backtrace: Backtrace, }, #[snafu(display( "Could not serialize/deserialize command {} because of: {}", command_type, source ))] - Io { command_type: i32, source: IoError }, + Io { + command_type: i32, + source: IoError, + backtrace: Backtrace, + }, #[snafu(display( "Could not serialize/deserialize command {} because of: Unknown Command", command_type ))] - InvalidType { command_type: i32 }, + InvalidType { + command_type: i32, + backtrace: Backtrace, + }, } /// This kind of error that can be produced during Pravega read Wire Commands. @@ -64,6 +75,7 @@ pub enum ReaderError { ReadWirecommand { part: String, source: ConnectionError, + backtrace: Backtrace, }, #[snafu(display( "The payload size {} exceeds the max wirecommand size {}", @@ -73,5 +85,6 @@ pub enum ReaderError { PayloadLengthTooLong { payload_size: u32, max_wirecommand_size: u32, + backtrace: Backtrace, }, } diff --git a/src/wire_protocol/wire_commands.rs b/src/wire_protocol/wire_commands.rs index 8d1e14aee..6c9c06823 100644 --- a/src/wire_protocol/wire_commands.rs +++ b/src/wire_protocol/wire_commands.rs @@ -1,6 +1,7 @@ use super::commands::*; use super::error::CommandError; use byteorder::{BigEndian, ByteOrder}; +use snafu::{Backtrace, GenerateBacktrace}; #[derive(PartialEq, Debug)] pub enum WireCommands { @@ -600,6 +601,7 @@ impl Decode for WireCommands { )), _ => Err(CommandError::InvalidType { command_type: type_code, + backtrace: Backtrace::generate(), }), } }