Last active
July 9, 2021 18:14
-
-
Save metamemelord/3fec16e42981ed5d938078db6fd76fb5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use fdk::{Function, RuntimeContext, Result}; | |
use serde::{Deserialize, Serialize}; | |
use std::error::Error; | |
#[derive(serde::Deserialize)] | |
enum Operation { | |
Add, | |
Sub, | |
Mul, | |
Div, | |
} | |
#[derive(Deserialize)] | |
struct CalculatorInput { | |
operand1: f64, | |
operand2: f64, | |
operation: Operation, | |
} | |
#[derive(Serialize)] | |
struct CalculatorResult { | |
result: f64, | |
} | |
impl CalculatorResult { | |
fn new(result: f64) -> Self { | |
Self { result } | |
} | |
} | |
fn simple_calc( | |
_: &mut RuntimeContext, | |
input: CalculatorInput, | |
) -> Result<CalculatorResult> { | |
Ok(match input.operation { | |
Operation::Add => CalculatorResult::new(input.operand1 + input.operand2), | |
Operation::Sub => CalculatorResult::new(input.operand1 - input.operand2), | |
Operation::Mul => CalculatorResult::new(input.operand1 * input.operand2), | |
Operation::Div => CalculatorResult::new(input.operand1 / input.operand2), | |
}) | |
} | |
#[tokio::main] | |
async fn main() { | |
let function = Function::run(simple_calc); | |
if let Err(e) = function.await { | |
eprintln!("{}", e); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment