summaryrefslogtreecommitdiff
path: root/tutorial
diff options
context:
space:
mode:
authorAllen George <allengeorge@apache.org>2021-03-01 14:47:04 -0500
committerGitHub <noreply@github.com>2021-03-01 14:47:04 -0500
commit2e90ef569c1b38f6e0f1279e3f25d2a7f6b5ff99 (patch)
tree21744c04e50cf73a19ce2e9b858d65409d5501af /tutorial
parent4f6439b30953865fd1ec23729cbf17292c0ad17d (diff)
downloadthrift-2e90ef569c1b38f6e0f1279e3f25d2a7f6b5ff99.tar.gz
[THRIFT-5314][THRIFT-4101] Generate enums that don't error on unexpected values (#2337)
Client: rs
Diffstat (limited to 'tutorial')
-rw-r--r--tutorial/rs/src/bin/tutorial_client.rs4
-rw-r--r--tutorial/rs/src/bin/tutorial_server.rs21
2 files changed, 16 insertions, 9 deletions
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)),
+ })
}
}
}