THRIFT-2945 Add Rust support
Client: Rust
Patch: Allen George <allen.george@gmail.com>
This closes #1147
diff --git a/lib/rs/test/src/bin/kitchen_sink_client.rs b/lib/rs/test/src/bin/kitchen_sink_client.rs
new file mode 100644
index 0000000..27171be
--- /dev/null
+++ b/lib/rs/test/src/bin/kitchen_sink_client.rs
@@ -0,0 +1,142 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you 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
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#[macro_use]
+extern crate clap;
+
+extern crate kitchen_sink;
+extern crate thrift;
+
+use std::cell::RefCell;
+use std::rc::Rc;
+
+use kitchen_sink::base_two::{TNapkinServiceSyncClient, TRamenServiceSyncClient};
+use kitchen_sink::midlayer::{MealServiceSyncClient, TMealServiceSyncClient};
+use kitchen_sink::ultimate::{FullMealServiceSyncClient, TFullMealServiceSyncClient};
+use thrift::transport::{TFramedTransport, TTcpTransport, TTransport};
+use thrift::protocol::{TBinaryInputProtocol, TBinaryOutputProtocol, TCompactInputProtocol,
+ TCompactOutputProtocol, TInputProtocol, TOutputProtocol};
+
+fn main() {
+ match run() {
+ Ok(()) => println!("kitchen sink client completed successfully"),
+ Err(e) => {
+ println!("kitchen sink client failed with error {:?}", e);
+ std::process::exit(1);
+ }
+ }
+}
+
+fn run() -> thrift::Result<()> {
+ let matches = clap_app!(rust_kitchen_sink_client =>
+ (version: "0.1.0")
+ (author: "Apache Thrift Developers <dev@thrift.apache.org>")
+ (about: "Thrift Rust kitchen sink client")
+ (@arg host: --host +takes_value "Host on which the Thrift test server is located")
+ (@arg port: --port +takes_value "Port on which the Thrift test server is listening")
+ (@arg protocol: --protocol +takes_value "Thrift protocol implementation to use (\"binary\", \"compact\")")
+ (@arg service: --service +takes_value "Service type to contact (\"part\", \"full\")")
+ ).get_matches();
+
+ let host = matches.value_of("host").unwrap_or("127.0.0.1");
+ let port = value_t!(matches, "port", u16).unwrap_or(9090);
+ let protocol = matches.value_of("protocol").unwrap_or("compact");
+ let service = matches.value_of("service").unwrap_or("part");
+
+ let t = open_tcp_transport(host, port)?;
+ let t = Rc::new(RefCell::new(Box::new(TFramedTransport::new(t)) as Box<TTransport>));
+
+ let (i_prot, o_prot): (Box<TInputProtocol>, Box<TOutputProtocol>) = match protocol {
+ "binary" => {
+ (Box::new(TBinaryInputProtocol::new(t.clone(), true)),
+ Box::new(TBinaryOutputProtocol::new(t.clone(), true)))
+ }
+ "compact" => {
+ (Box::new(TCompactInputProtocol::new(t.clone())),
+ Box::new(TCompactOutputProtocol::new(t.clone())))
+ }
+ unmatched => return Err(format!("unsupported protocol {}", unmatched).into()),
+ };
+
+ run_client(service, i_prot, o_prot)
+}
+
+fn run_client(service: &str,
+ i_prot: Box<TInputProtocol>,
+ o_prot: Box<TOutputProtocol>)
+ -> thrift::Result<()> {
+ match service {
+ "full" => run_full_meal_service(i_prot, o_prot),
+ "part" => run_meal_service(i_prot, o_prot),
+ _ => Err(thrift::Error::from(format!("unknown service type {}", service))),
+ }
+}
+
+fn open_tcp_transport(host: &str, port: u16) -> thrift::Result<Rc<RefCell<Box<TTransport>>>> {
+ let mut t = TTcpTransport::new();
+ match t.open(&format!("{}:{}", host, port)) {
+ Ok(()) => Ok(Rc::new(RefCell::new(Box::new(t) as Box<TTransport>))),
+ Err(e) => Err(e),
+ }
+}
+
+fn run_meal_service(i_prot: Box<TInputProtocol>,
+ o_prot: Box<TOutputProtocol>)
+ -> thrift::Result<()> {
+ let mut client = MealServiceSyncClient::new(i_prot, o_prot);
+
+ // client.full_meal(); // <-- IMPORTANT: if you uncomment this, compilation *should* fail
+ // this is because the MealService struct does not contain the appropriate service marker
+
+ // only the following three calls work
+ execute_call("part", "ramen", || client.ramen(50))?;
+ execute_call("part", "meal", || client.meal())?;
+ execute_call("part", "napkin", || client.napkin())?;
+
+ Ok(())
+}
+
+fn run_full_meal_service(i_prot: Box<TInputProtocol>,
+ o_prot: Box<TOutputProtocol>)
+ -> thrift::Result<()> {
+ let mut client = FullMealServiceSyncClient::new(i_prot, o_prot);
+
+ execute_call("full", "ramen", || client.ramen(100))?;
+ execute_call("full", "meal", || client.meal())?;
+ execute_call("full", "napkin", || client.napkin())?;
+ execute_call("full", "full meal", || client.full_meal())?;
+
+ Ok(())
+}
+
+fn execute_call<F, R>(service_type: &str, call_name: &str, mut f: F) -> thrift::Result<()>
+ where F: FnMut() -> thrift::Result<R>
+{
+ let res = f();
+
+ match res {
+ Ok(_) => println!("{}: completed {} call", service_type, call_name),
+ Err(ref e) => {
+ println!("{}: failed {} call with error {:?}",
+ service_type,
+ call_name,
+ e)
+ }
+ }
+
+ res.map(|_| ())
+}
diff --git a/lib/rs/test/src/bin/kitchen_sink_server.rs b/lib/rs/test/src/bin/kitchen_sink_server.rs
new file mode 100644
index 0000000..4ce4fa3
--- /dev/null
+++ b/lib/rs/test/src/bin/kitchen_sink_server.rs
@@ -0,0 +1,225 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you 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
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#[macro_use]
+extern crate clap;
+
+extern crate kitchen_sink;
+extern crate thrift;
+
+use kitchen_sink::base_one::Noodle;
+use kitchen_sink::base_two::{Napkin, Ramen, NapkinServiceSyncHandler, RamenServiceSyncHandler};
+use kitchen_sink::midlayer::{Dessert, Meal, MealServiceSyncHandler, MealServiceSyncProcessor};
+use kitchen_sink::ultimate::{Drink, FullMeal, FullMealAndDrinks,
+ FullMealAndDrinksServiceSyncProcessor, FullMealServiceSyncHandler};
+use kitchen_sink::ultimate::FullMealAndDrinksServiceSyncHandler;
+use thrift::protocol::{TBinaryInputProtocolFactory, TBinaryOutputProtocolFactory,
+ TCompactInputProtocolFactory, TCompactOutputProtocolFactory,
+ TInputProtocolFactory, TOutputProtocolFactory};
+use thrift::transport::{TFramedTransportFactory, TTransportFactory};
+use thrift::server::TSimpleServer;
+
+fn main() {
+ match run() {
+ Ok(()) => println!("kitchen sink server completed successfully"),
+ Err(e) => {
+ println!("kitchen sink server failed with error {:?}", e);
+ std::process::exit(1);
+ }
+ }
+}
+
+fn run() -> thrift::Result<()> {
+
+ let matches = clap_app!(rust_kitchen_sink_server =>
+ (version: "0.1.0")
+ (author: "Apache Thrift Developers <dev@thrift.apache.org>")
+ (about: "Thrift Rust kitchen sink test server")
+ (@arg port: --port +takes_value "port on which the test server listens")
+ (@arg protocol: --protocol +takes_value "Thrift protocol implementation to use (\"binary\", \"compact\")")
+ (@arg service: --service +takes_value "Service type to contact (\"part\", \"full\")")
+ ).get_matches();
+
+ let port = value_t!(matches, "port", u16).unwrap_or(9090);
+ let protocol = matches.value_of("protocol").unwrap_or("compact");
+ let service = matches.value_of("service").unwrap_or("part");
+ let listen_address = format!("127.0.0.1:{}", port);
+
+ println!("binding to {}", listen_address);
+
+ let (i_transport_factory, o_transport_factory): (Box<TTransportFactory>,
+ Box<TTransportFactory>) =
+ (Box::new(TFramedTransportFactory {}), Box::new(TFramedTransportFactory {}));
+
+ let (i_protocol_factory, o_protocol_factory): (Box<TInputProtocolFactory>,
+ Box<TOutputProtocolFactory>) =
+ match &*protocol {
+ "binary" => {
+ (Box::new(TBinaryInputProtocolFactory::new()),
+ Box::new(TBinaryOutputProtocolFactory::new()))
+ }
+ "compact" => {
+ (Box::new(TCompactInputProtocolFactory::new()),
+ Box::new(TCompactOutputProtocolFactory::new()))
+ }
+ unknown => {
+ return Err(format!("unsupported transport type {}", unknown).into());
+ }
+ };
+
+ // FIXME: should processor be boxed as well?
+ //
+ // [sigh] I hate Rust generics implementation
+ //
+ // I would have preferred to build a server here, return it, and then do
+ // the common listen-and-handle stuff, but since the server doesn't have a
+ // common type (because each match arm instantiates a server with a
+ // different processor) this isn't possible.
+ //
+ // Since what I'm doing is uncommon I'm just going to duplicate the code
+ match &*service {
+ "part" => {
+ run_meal_server(&listen_address,
+ i_transport_factory,
+ i_protocol_factory,
+ o_transport_factory,
+ o_protocol_factory)
+ }
+ "full" => {
+ run_full_meal_server(&listen_address,
+ i_transport_factory,
+ i_protocol_factory,
+ o_transport_factory,
+ o_protocol_factory)
+ }
+ unknown => Err(format!("unsupported service type {}", unknown).into()),
+ }
+}
+
+fn run_meal_server(listen_address: &str,
+ i_transport_factory: Box<TTransportFactory>,
+ i_protocol_factory: Box<TInputProtocolFactory>,
+ o_transport_factory: Box<TTransportFactory>,
+ o_protocol_factory: Box<TOutputProtocolFactory>)
+ -> thrift::Result<()> {
+ let processor = MealServiceSyncProcessor::new(PartHandler {});
+ let mut server = TSimpleServer::new(i_transport_factory,
+ i_protocol_factory,
+ o_transport_factory,
+ o_protocol_factory,
+ processor);
+
+ server.listen(listen_address)
+}
+
+fn run_full_meal_server(listen_address: &str,
+ i_transport_factory: Box<TTransportFactory>,
+ i_protocol_factory: Box<TInputProtocolFactory>,
+ o_transport_factory: Box<TTransportFactory>,
+ o_protocol_factory: Box<TOutputProtocolFactory>)
+ -> thrift::Result<()> {
+ let processor = FullMealAndDrinksServiceSyncProcessor::new(FullHandler {});
+ let mut server = TSimpleServer::new(i_transport_factory,
+ i_protocol_factory,
+ o_transport_factory,
+ o_protocol_factory,
+ processor);
+
+ server.listen(listen_address)
+}
+
+struct PartHandler;
+
+impl MealServiceSyncHandler for PartHandler {
+ fn handle_meal(&mut self) -> thrift::Result<Meal> {
+ println!("part: handling meal call");
+ Ok(meal())
+ }
+}
+
+impl RamenServiceSyncHandler for PartHandler {
+ fn handle_ramen(&mut self, _: i32) -> thrift::Result<Ramen> {
+ println!("part: handling ramen call");
+ Ok(ramen())
+ }
+}
+
+impl NapkinServiceSyncHandler for PartHandler {
+ fn handle_napkin(&mut self) -> thrift::Result<Napkin> {
+ println!("part: handling napkin call");
+ Ok(napkin())
+ }
+}
+
+// full service
+//
+
+struct FullHandler;
+
+impl FullMealAndDrinksServiceSyncHandler for FullHandler {
+ fn handle_full_meal_and_drinks(&mut self) -> thrift::Result<FullMealAndDrinks> {
+ Ok(FullMealAndDrinks::new(full_meal(), Drink::WHISKEY))
+ }
+}
+
+impl FullMealServiceSyncHandler for FullHandler {
+ fn handle_full_meal(&mut self) -> thrift::Result<FullMeal> {
+ println!("full: handling full meal call");
+ Ok(full_meal())
+ }
+}
+
+impl MealServiceSyncHandler for FullHandler {
+ fn handle_meal(&mut self) -> thrift::Result<Meal> {
+ println!("full: handling meal call");
+ Ok(meal())
+ }
+}
+
+impl RamenServiceSyncHandler for FullHandler {
+ fn handle_ramen(&mut self, _: i32) -> thrift::Result<Ramen> {
+ println!("full: handling ramen call");
+ Ok(ramen())
+ }
+}
+
+impl NapkinServiceSyncHandler for FullHandler {
+ fn handle_napkin(&mut self) -> thrift::Result<Napkin> {
+ println!("full: handling napkin call");
+ Ok(napkin())
+ }
+}
+
+fn full_meal() -> FullMeal {
+ FullMeal::new(meal(), Dessert::Port("Graham's Tawny".to_owned()))
+}
+
+fn meal() -> Meal {
+ Meal::new(noodle(), ramen())
+}
+
+fn noodle() -> Noodle {
+ Noodle::new("spelt".to_owned(), 100)
+}
+
+fn ramen() -> Ramen {
+ Ramen::new("Mr Ramen".to_owned(), 72)
+}
+
+fn napkin() -> Napkin {
+ Napkin {}
+}
diff --git a/lib/rs/test/src/lib.rs b/lib/rs/test/src/lib.rs
new file mode 100644
index 0000000..8a7ccd0
--- /dev/null
+++ b/lib/rs/test/src/lib.rs
@@ -0,0 +1,53 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you 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
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+extern crate ordered_float;
+extern crate thrift;
+extern crate try_from;
+
+pub mod base_one;
+pub mod base_two;
+pub mod midlayer;
+pub mod ultimate;
+
+#[cfg(test)]
+mod tests {
+
+ use std::default::Default;
+
+ use super::*;
+
+ #[test]
+ fn must_be_able_to_use_constructor() {
+ let _ = midlayer::Meal::new(Some(base_one::Noodle::default()), None);
+ }
+
+ #[test]
+ fn must_be_able_to_use_constructor_with_no_fields() {
+ let _ = midlayer::Meal::new(None, None);
+ }
+
+ #[test]
+ fn must_be_able_to_use_constructor_without_option_wrap() {
+ let _ = midlayer::Meal::new(base_one::Noodle::default(), None);
+ }
+
+ #[test]
+ fn must_be_able_to_use_defaults() {
+ let _ = midlayer::Meal { noodle: Some(base_one::Noodle::default()), ..Default::default() };
+ }
+}