From 2e90ef569c1b38f6e0f1279e3f25d2a7f6b5ff99 Mon Sep 17 00:00:00 2001 From: Allen George Date: Mon, 1 Mar 2021 14:47:04 -0500 Subject: [THRIFT-5314][THRIFT-4101] Generate enums that don't error on unexpected values (#2337) Client: rs --- tutorial/rs/src/bin/tutorial_client.rs | 4 ++-- tutorial/rs/src/bin/tutorial_server.rs | 21 ++++++++++++++------- 2 files changed, 16 insertions(+), 9 deletions(-) (limited to 'tutorial') diff --git a/tutorial/rs/src/bin/tutorial_client.rs b/tutorial/rs/src/bin/tutorial_client.rs index f7de23f06..4bf2ec098 100644 --- a/tutorial/rs/src/bin/tutorial_client.rs +++ b/tutorial/rs/src/bin/tutorial_client.rs @@ -67,7 +67,7 @@ fn run() -> thrift::Result<()> { let logid = 32; // let's do...a multiply! - let res = client.calculate(logid, Work::new(7, 8, Operation::Multiply, None))?; + let res = client.calculate(logid, Work::new(7, 8, Operation::MULTIPLY, None))?; println!("multiplied 7 and 8 and got {}", res); // let's get the log for it @@ -77,7 +77,7 @@ fn run() -> thrift::Result<()> { // ok - let's be bad :( // do a divide by 0 // logid doesn't matter; won't be recorded - let res = client.calculate(77, Work::new(2, 0, Operation::Divide, "we bad".to_owned())); + let res = client.calculate(77, Work::new(2, 0, Operation::DIVIDE, "we bad".to_owned())); // we should have gotten an exception back match res { diff --git a/tutorial/rs/src/bin/tutorial_server.rs b/tutorial/rs/src/bin/tutorial_server.rs index fbccb69c9..b6ed7a123 100644 --- a/tutorial/rs/src/bin/tutorial_server.rs +++ b/tutorial/rs/src/bin/tutorial_server.rs @@ -123,7 +123,7 @@ impl CalculatorSyncHandler for CalculatorServer { let res = if let Some(ref op) = w.op { if w.num1.is_none() || w.num2.is_none() { Err(InvalidOperation { - what_op: Some(*op as i32), + what_op: Some(op.into()), why: Some("no operands specified".to_owned()), }) } else { @@ -131,19 +131,26 @@ impl CalculatorSyncHandler for CalculatorServer { let num1 = w.num1.as_ref().expect("operands checked"); let num2 = w.num2.as_ref().expect("operands checked"); - match *op { - Operation::Add => Ok(num1 + num2), - Operation::Subtract => Ok(num1 - num2), - Operation::Multiply => Ok(num1 * num2), - Operation::Divide => { + match op { + &Operation::ADD => Ok(num1 + num2), + &Operation::SUBTRACT => Ok(num1 - num2), + &Operation::MULTIPLY => Ok(num1 * num2), + &Operation::DIVIDE => { if *num2 == 0 { Err(InvalidOperation { - what_op: Some(*op as i32), + what_op: Some(op.into()), why: Some("divide by 0".to_owned()), }) } else { Ok(num1 / num2) } + }, + _ => { + let op_val: i32 = op.into(); + Err(InvalidOperation { + what_op: Some(op_val), + why: Some(format!("unsupported operation type '{}'", op_val)), + }) } } } -- cgit v1.2.1