mirror of
https://github.com/microsoft/vscode-extension-samples.git
synced 2026-04-27 16:55:44 +08:00
26 lines
908 B
Rust
26 lines
908 B
Rust
// Use a procedural macro to generate bindings for the world we specified in
|
|
// `host.wit`
|
|
wit_bindgen::generate!({
|
|
// the name of the world in the `*.wit` input file
|
|
world: "calculator",
|
|
});
|
|
|
|
struct Calculator;
|
|
|
|
impl Guest for Calculator {
|
|
|
|
fn calc(op: Operation) -> Result<u32, ErrorCode> {
|
|
log(&format!("Starting calculation: {:?}", op));
|
|
let result: Result<u32, ErrorCode> = match op {
|
|
Operation::Add(operands) => Result::Ok(operands.left + operands.right),
|
|
Operation::Sub(operands) => Result::Ok(operands.left - operands.right),
|
|
Operation::Mul(operands) => Result::Ok(operands.left * operands.right),
|
|
Operation::Div(operands) => if operands.right == 0 { Result::Err(ErrorCode::DivideByZero) } else { Result::Ok(operands.left / operands.right) }
|
|
};
|
|
log(&format!("Finished calculation: {:?}", op));
|
|
result
|
|
}
|
|
}
|
|
|
|
// Export the Calculator to the extension code.
|
|
export!(Calculator); |