diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-07-02 09:57:35 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-07-02 09:57:35 +0100 |
| commit | 066af00eebe662e5a589a4e274e1613201b36982 (patch) | |
| tree | 985cf23c7288ca11c51611b744fd5dbde2031fa7 /examples | |
| parent | 3d6034f51eba69de538ce2b7ccdd59654870db1d (diff) | |
Support translating sums into Rust types
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/02-sum/hobgoblin.json5 | 4 | ||||
| -rw-r--r-- | examples/02-sum/output/rust/.gitignore | 1 | ||||
| -rw-r--r-- | examples/02-sum/output/rust/Cargo.lock | 7 | ||||
| -rw-r--r-- | examples/02-sum/output/rust/Cargo.toml | 6 | ||||
| -rw-r--r-- | examples/02-sum/output/rust/rust-toolchain.toml | 2 | ||||
| -rw-r--r-- | examples/02-sum/output/rust/src/main.rs | 16 | ||||
| -rw-r--r-- | examples/02-sum/output/rust/src/shapes.rs | 15 |
7 files changed, 51 insertions, 0 deletions
diff --git a/examples/02-sum/hobgoblin.json5 b/examples/02-sum/hobgoblin.json5 index 1ec9568..2bf59c3 100644 --- a/examples/02-sum/hobgoblin.json5 +++ b/examples/02-sum/hobgoblin.json5 @@ -5,5 +5,9 @@ path: "output/java/src/gen/java", packageName: "org.zwobble.example.types" }, + { + generator: "rust-types", + path: "output/rust/src", + }, ], } diff --git a/examples/02-sum/output/rust/.gitignore b/examples/02-sum/output/rust/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/examples/02-sum/output/rust/.gitignore @@ -0,0 +1 @@ +/target diff --git a/examples/02-sum/output/rust/Cargo.lock b/examples/02-sum/output/rust/Cargo.lock new file mode 100644 index 0000000..9dc6e3e --- /dev/null +++ b/examples/02-sum/output/rust/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "example" +version = "0.1.0" diff --git a/examples/02-sum/output/rust/Cargo.toml b/examples/02-sum/output/rust/Cargo.toml new file mode 100644 index 0000000..5d4c622 --- /dev/null +++ b/examples/02-sum/output/rust/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "example" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/examples/02-sum/output/rust/rust-toolchain.toml b/examples/02-sum/output/rust/rust-toolchain.toml new file mode 100644 index 0000000..4fedb95 --- /dev/null +++ b/examples/02-sum/output/rust/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "1.96" diff --git a/examples/02-sum/output/rust/src/main.rs b/examples/02-sum/output/rust/src/main.rs new file mode 100644 index 0000000..010ea2a --- /dev/null +++ b/examples/02-sum/output/rust/src/main.rs @@ -0,0 +1,16 @@ +mod shapes; + +use shapes::{Rectangle, Shape, Triangle}; + +fn main() { + let shape = create_shape(); + + let area = match shape { + Shape::Rectangle(rectangle) => rectangle.width * rectangle.height, + Shape::Triangle(triangle) => triangle.width * triangle.height / 2, + }; +} + +fn create_shape() -> Shape { + Shape::Rectangle(Rectangle { width: 10, height: 25 }) +} diff --git a/examples/02-sum/output/rust/src/shapes.rs b/examples/02-sum/output/rust/src/shapes.rs new file mode 100644 index 0000000..9bb32f4 --- /dev/null +++ b/examples/02-sum/output/rust/src/shapes.rs @@ -0,0 +1,15 @@ +/// A 2D shape. +pub enum Shape { + Rectangle(crate::shapes::Rectangle), + Triangle(crate::shapes::Triangle), +} + +pub struct Rectangle { + pub width: i32, + pub height: i32, +} + +pub struct Triangle { + pub width: i32, + pub height: i32, +} |
