From 8455bb19b7d4235e667cab9112aa276557969dcc Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 5 Aug 2026 18:44:26 +0100 Subject: Type check generic native types --- examples/11-native-type-generic/hobgoblin.json5 | 33 ++ .../11-native-type-generic/output/java/.gitignore | 2 + .../11-native-type-generic/output/java/pom.xml | 1 + .../example/types/map/HobgoblinNativeMap.java | 9 + .../test/java/org/zwobble/example/MapTests.java | 13 + .../11-native-type-generic/output/rust/.gitignore | 1 + .../11-native-type-generic/output/rust/Cargo.lock | 7 + .../11-native-type-generic/output/rust/Cargo.toml | 6 + .../output/rust/rust-toolchain.toml | 2 + .../output/rust/src/gen/map.rs | 6 + .../11-native-type-generic/output/rust/src/lib.rs | 12 + .../11-native-type-generic/output/rust/src/map.rs | 1 + examples/11-native-type-generic/src/map.hob | 6 + examples/11-shared/hobgoblin.json5 | 23 -- examples/11-shared/output/java/.gitignore | 2 - examples/11-shared/output/java/pom.xml | 1 - .../test/java/org/zwobble/example/PointTests.java | 15 - examples/11-shared/output/rust/.gitignore | 1 - examples/11-shared/output/rust/Cargo.lock | 7 - examples/11-shared/output/rust/Cargo.toml | 6 - examples/11-shared/output/rust/rust-toolchain.toml | 2 - examples/11-shared/output/rust/src/lib.rs | 14 - examples/11-shared/output/rust/src/point.rs | 15 - examples/11-shared/src/point.hob | 11 - examples/12-custom-code/hobgoblin.json5 | 19 - examples/12-custom-code/output/java/.gitignore | 2 - examples/12-custom-code/output/java/pom.xml | 1 - .../org/zwobble/example/types/shapes/Square.java | 37 -- .../test/java/org/zwobble/example/ShapesTests.java | 13 - examples/12-custom-code/output/rust/.gitignore | 1 - examples/12-custom-code/output/rust/Cargo.lock | 7 - examples/12-custom-code/output/rust/Cargo.toml | 6 - .../12-custom-code/output/rust/rust-toolchain.toml | 2 - .../12-custom-code/output/rust/src/gen/shapes.rs | 6 - examples/12-custom-code/output/rust/src/lib.rs | 12 - examples/12-custom-code/output/rust/src/shapes.rs | 7 - examples/12-custom-code/src/shapes.hob | 3 - examples/12-shared/hobgoblin.json5 | 23 ++ examples/12-shared/output/java/.gitignore | 2 + examples/12-shared/output/java/pom.xml | 1 + .../test/java/org/zwobble/example/PointTests.java | 15 + examples/12-shared/output/rust/.gitignore | 1 + examples/12-shared/output/rust/Cargo.lock | 7 + examples/12-shared/output/rust/Cargo.toml | 6 + examples/12-shared/output/rust/rust-toolchain.toml | 2 + examples/12-shared/output/rust/src/lib.rs | 14 + examples/12-shared/output/rust/src/point.rs | 15 + examples/12-shared/src/point.hob | 11 + examples/13-custom-code/hobgoblin.json5 | 19 + examples/13-custom-code/output/java/.gitignore | 2 + examples/13-custom-code/output/java/pom.xml | 1 + .../org/zwobble/example/types/shapes/Square.java | 37 ++ .../test/java/org/zwobble/example/ShapesTests.java | 13 + examples/13-custom-code/output/rust/.gitignore | 1 + examples/13-custom-code/output/rust/Cargo.lock | 7 + examples/13-custom-code/output/rust/Cargo.toml | 6 + .../13-custom-code/output/rust/rust-toolchain.toml | 2 + .../13-custom-code/output/rust/src/gen/shapes.rs | 6 + examples/13-custom-code/output/rust/src/lib.rs | 12 + examples/13-custom-code/output/rust/src/shapes.rs | 7 + examples/13-custom-code/src/shapes.hob | 3 + examples/13-imports/hobgoblin.json5 | 19 - examples/13-imports/output/java/.gitignore | 2 - examples/13-imports/output/java/pom.xml | 1 - .../test/java/org/zwobble/example/LineTests.java | 15 - examples/13-imports/output/rust/.gitignore | 1 - examples/13-imports/output/rust/Cargo.lock | 7 - examples/13-imports/output/rust/Cargo.toml | 6 - .../13-imports/output/rust/rust-toolchain.toml | 2 - examples/13-imports/output/rust/src/lib.rs | 15 - examples/13-imports/output/rust/src/line.rs | 8 - examples/13-imports/output/rust/src/point.rs | 8 - examples/13-imports/src/line.hob | 7 - examples/13-imports/src/point.hob | 5 - examples/14-imports/hobgoblin.json5 | 19 + examples/14-imports/output/java/.gitignore | 2 + examples/14-imports/output/java/pom.xml | 1 + .../test/java/org/zwobble/example/LineTests.java | 15 + examples/14-imports/output/rust/.gitignore | 1 + examples/14-imports/output/rust/Cargo.lock | 7 + examples/14-imports/output/rust/Cargo.toml | 6 + .../14-imports/output/rust/rust-toolchain.toml | 2 + examples/14-imports/output/rust/src/lib.rs | 15 + examples/14-imports/output/rust/src/line.rs | 8 + examples/14-imports/output/rust/src/point.rs | 8 + examples/14-imports/src/line.hob | 7 + examples/14-imports/src/point.hob | 5 + examples/14-transient-0/hobgoblin.json5 | 27 -- examples/14-transient-0/output/java/.gitignore | 3 - examples/14-transient-0/output/java/pom.xml | 1 - .../java/org/zwobble/example/Transient0Tests.java | 353 ---------------- examples/14-transient-0/output/rust/.gitignore | 2 - examples/14-transient-0/output/rust/Cargo.lock | 7 - examples/14-transient-0/output/rust/Cargo.toml | 6 - .../14-transient-0/output/rust/rust-toolchain.toml | 2 - examples/14-transient-0/output/rust/src/data.rs | 5 - .../14-transient-0/output/rust/src/gen/data.rs | 140 ------- .../output/rust/src/gen/data/transient_0.rs | 455 --------------------- .../output/rust/src/gen/transient_0.rs | 84 ---- examples/14-transient-0/output/rust/src/lib.rs | 318 -------------- .../14-transient-0/output/rust/src/transient_0.rs | 1 - examples/14-transient-0/src/data.hob | 96 ----- examples/15-transient-0/hobgoblin.json5 | 27 ++ examples/15-transient-0/output/java/.gitignore | 3 + examples/15-transient-0/output/java/pom.xml | 1 + .../java/org/zwobble/example/Transient0Tests.java | 353 ++++++++++++++++ examples/15-transient-0/output/rust/.gitignore | 2 + examples/15-transient-0/output/rust/Cargo.lock | 7 + examples/15-transient-0/output/rust/Cargo.toml | 6 + .../15-transient-0/output/rust/rust-toolchain.toml | 2 + examples/15-transient-0/output/rust/src/data.rs | 5 + .../15-transient-0/output/rust/src/gen/data.rs | 140 +++++++ .../output/rust/src/gen/data/transient_0.rs | 455 +++++++++++++++++++++ .../output/rust/src/gen/transient_0.rs | 84 ++++ examples/15-transient-0/output/rust/src/lib.rs | 318 ++++++++++++++ .../15-transient-0/output/rust/src/transient_0.rs | 1 + examples/15-transient-0/src/data.hob | 96 +++++ .../ast/typed/TypedNativeTypeDefinitionNode.java | 5 +- .../compiler/typechecker/TypeChecker.java | 26 +- .../hobgoblin/compiler/types/TypeConstructor.java | 4 + .../compiler/types/TypeOrConstructor.java | 20 + .../TypeCheckerNativeTypeDefinitionTests.java | 7 +- 122 files changed, 1962 insertions(+), 1817 deletions(-) create mode 100644 examples/11-native-type-generic/hobgoblin.json5 create mode 100644 examples/11-native-type-generic/output/java/.gitignore create mode 120000 examples/11-native-type-generic/output/java/pom.xml create mode 100644 examples/11-native-type-generic/output/java/src/main/java/org/zwobble/example/types/map/HobgoblinNativeMap.java create mode 100644 examples/11-native-type-generic/output/java/src/test/java/org/zwobble/example/MapTests.java create mode 100644 examples/11-native-type-generic/output/rust/.gitignore create mode 100644 examples/11-native-type-generic/output/rust/Cargo.lock create mode 100644 examples/11-native-type-generic/output/rust/Cargo.toml create mode 100644 examples/11-native-type-generic/output/rust/rust-toolchain.toml create mode 100644 examples/11-native-type-generic/output/rust/src/gen/map.rs create mode 100644 examples/11-native-type-generic/output/rust/src/lib.rs create mode 100644 examples/11-native-type-generic/output/rust/src/map.rs create mode 100644 examples/11-native-type-generic/src/map.hob delete mode 100644 examples/11-shared/hobgoblin.json5 delete mode 100644 examples/11-shared/output/java/.gitignore delete mode 120000 examples/11-shared/output/java/pom.xml delete mode 100644 examples/11-shared/output/java/src/test/java/org/zwobble/example/PointTests.java delete mode 100644 examples/11-shared/output/rust/.gitignore delete mode 100644 examples/11-shared/output/rust/Cargo.lock delete mode 100644 examples/11-shared/output/rust/Cargo.toml delete mode 100644 examples/11-shared/output/rust/rust-toolchain.toml delete mode 100644 examples/11-shared/output/rust/src/lib.rs delete mode 100644 examples/11-shared/output/rust/src/point.rs delete mode 100644 examples/11-shared/src/point.hob delete mode 100644 examples/12-custom-code/hobgoblin.json5 delete mode 100644 examples/12-custom-code/output/java/.gitignore delete mode 120000 examples/12-custom-code/output/java/pom.xml delete mode 100644 examples/12-custom-code/output/java/src/main/java/org/zwobble/example/types/shapes/Square.java delete mode 100644 examples/12-custom-code/output/java/src/test/java/org/zwobble/example/ShapesTests.java delete mode 100644 examples/12-custom-code/output/rust/.gitignore delete mode 100644 examples/12-custom-code/output/rust/Cargo.lock delete mode 100644 examples/12-custom-code/output/rust/Cargo.toml delete mode 100644 examples/12-custom-code/output/rust/rust-toolchain.toml delete mode 100644 examples/12-custom-code/output/rust/src/gen/shapes.rs delete mode 100644 examples/12-custom-code/output/rust/src/lib.rs delete mode 100644 examples/12-custom-code/output/rust/src/shapes.rs delete mode 100644 examples/12-custom-code/src/shapes.hob create mode 100644 examples/12-shared/hobgoblin.json5 create mode 100644 examples/12-shared/output/java/.gitignore create mode 120000 examples/12-shared/output/java/pom.xml create mode 100644 examples/12-shared/output/java/src/test/java/org/zwobble/example/PointTests.java create mode 100644 examples/12-shared/output/rust/.gitignore create mode 100644 examples/12-shared/output/rust/Cargo.lock create mode 100644 examples/12-shared/output/rust/Cargo.toml create mode 100644 examples/12-shared/output/rust/rust-toolchain.toml create mode 100644 examples/12-shared/output/rust/src/lib.rs create mode 100644 examples/12-shared/output/rust/src/point.rs create mode 100644 examples/12-shared/src/point.hob create mode 100644 examples/13-custom-code/hobgoblin.json5 create mode 100644 examples/13-custom-code/output/java/.gitignore create mode 120000 examples/13-custom-code/output/java/pom.xml create mode 100644 examples/13-custom-code/output/java/src/main/java/org/zwobble/example/types/shapes/Square.java create mode 100644 examples/13-custom-code/output/java/src/test/java/org/zwobble/example/ShapesTests.java create mode 100644 examples/13-custom-code/output/rust/.gitignore create mode 100644 examples/13-custom-code/output/rust/Cargo.lock create mode 100644 examples/13-custom-code/output/rust/Cargo.toml create mode 100644 examples/13-custom-code/output/rust/rust-toolchain.toml create mode 100644 examples/13-custom-code/output/rust/src/gen/shapes.rs create mode 100644 examples/13-custom-code/output/rust/src/lib.rs create mode 100644 examples/13-custom-code/output/rust/src/shapes.rs create mode 100644 examples/13-custom-code/src/shapes.hob delete mode 100644 examples/13-imports/hobgoblin.json5 delete mode 100644 examples/13-imports/output/java/.gitignore delete mode 120000 examples/13-imports/output/java/pom.xml delete mode 100644 examples/13-imports/output/java/src/test/java/org/zwobble/example/LineTests.java delete mode 100644 examples/13-imports/output/rust/.gitignore delete mode 100644 examples/13-imports/output/rust/Cargo.lock delete mode 100644 examples/13-imports/output/rust/Cargo.toml delete mode 100644 examples/13-imports/output/rust/rust-toolchain.toml delete mode 100644 examples/13-imports/output/rust/src/lib.rs delete mode 100644 examples/13-imports/output/rust/src/line.rs delete mode 100644 examples/13-imports/output/rust/src/point.rs delete mode 100644 examples/13-imports/src/line.hob delete mode 100644 examples/13-imports/src/point.hob create mode 100644 examples/14-imports/hobgoblin.json5 create mode 100644 examples/14-imports/output/java/.gitignore create mode 120000 examples/14-imports/output/java/pom.xml create mode 100644 examples/14-imports/output/java/src/test/java/org/zwobble/example/LineTests.java create mode 100644 examples/14-imports/output/rust/.gitignore create mode 100644 examples/14-imports/output/rust/Cargo.lock create mode 100644 examples/14-imports/output/rust/Cargo.toml create mode 100644 examples/14-imports/output/rust/rust-toolchain.toml create mode 100644 examples/14-imports/output/rust/src/lib.rs create mode 100644 examples/14-imports/output/rust/src/line.rs create mode 100644 examples/14-imports/output/rust/src/point.rs create mode 100644 examples/14-imports/src/line.hob create mode 100644 examples/14-imports/src/point.hob delete mode 100644 examples/14-transient-0/hobgoblin.json5 delete mode 100644 examples/14-transient-0/output/java/.gitignore delete mode 120000 examples/14-transient-0/output/java/pom.xml delete mode 100644 examples/14-transient-0/output/java/src/test/java/org/zwobble/example/Transient0Tests.java delete mode 100644 examples/14-transient-0/output/rust/.gitignore delete mode 100644 examples/14-transient-0/output/rust/Cargo.lock delete mode 100644 examples/14-transient-0/output/rust/Cargo.toml delete mode 100644 examples/14-transient-0/output/rust/rust-toolchain.toml delete mode 100644 examples/14-transient-0/output/rust/src/data.rs delete mode 100644 examples/14-transient-0/output/rust/src/gen/data.rs delete mode 100644 examples/14-transient-0/output/rust/src/gen/data/transient_0.rs delete mode 100644 examples/14-transient-0/output/rust/src/gen/transient_0.rs delete mode 100644 examples/14-transient-0/output/rust/src/lib.rs delete mode 100644 examples/14-transient-0/output/rust/src/transient_0.rs delete mode 100644 examples/14-transient-0/src/data.hob create mode 100644 examples/15-transient-0/hobgoblin.json5 create mode 100644 examples/15-transient-0/output/java/.gitignore create mode 120000 examples/15-transient-0/output/java/pom.xml create mode 100644 examples/15-transient-0/output/java/src/test/java/org/zwobble/example/Transient0Tests.java create mode 100644 examples/15-transient-0/output/rust/.gitignore create mode 100644 examples/15-transient-0/output/rust/Cargo.lock create mode 100644 examples/15-transient-0/output/rust/Cargo.toml create mode 100644 examples/15-transient-0/output/rust/rust-toolchain.toml create mode 100644 examples/15-transient-0/output/rust/src/data.rs create mode 100644 examples/15-transient-0/output/rust/src/gen/data.rs create mode 100644 examples/15-transient-0/output/rust/src/gen/data/transient_0.rs create mode 100644 examples/15-transient-0/output/rust/src/gen/transient_0.rs create mode 100644 examples/15-transient-0/output/rust/src/lib.rs create mode 100644 examples/15-transient-0/output/rust/src/transient_0.rs create mode 100644 examples/15-transient-0/src/data.hob create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/types/TypeOrConstructor.java diff --git a/examples/11-native-type-generic/hobgoblin.json5 b/examples/11-native-type-generic/hobgoblin.json5 new file mode 100644 index 0000000..ca81993 --- /dev/null +++ b/examples/11-native-type-generic/hobgoblin.json5 @@ -0,0 +1,33 @@ +{ + outputs: { + langs: { + java: { + packageName: "org.zwobble.example.types", + nativeTypes: [ + { + hobgoblin: "map.NativeMap", + java: "java.util.Map", + }, + ], + }, + rust: { + nativeTypes: [ + { + hobgoblin: "map.NativeMap", + rust: "::std::collections::BTreeMap", + }, + ], + }, + }, + generators: [ + { + generator: "java-types", + path: "output/java/src/gen/java", + }, + { + generator: "rust-types", + path: "output/rust/src/gen", + }, + ], + }, +} diff --git a/examples/11-native-type-generic/output/java/.gitignore b/examples/11-native-type-generic/output/java/.gitignore new file mode 100644 index 0000000..ff511d4 --- /dev/null +++ b/examples/11-native-type-generic/output/java/.gitignore @@ -0,0 +1,2 @@ +/src/gen/ +/target/ diff --git a/examples/11-native-type-generic/output/java/pom.xml b/examples/11-native-type-generic/output/java/pom.xml new file mode 120000 index 0000000..445fa65 --- /dev/null +++ b/examples/11-native-type-generic/output/java/pom.xml @@ -0,0 +1 @@ +../../../templates/java-junit/pom.xml \ No newline at end of file diff --git a/examples/11-native-type-generic/output/java/src/main/java/org/zwobble/example/types/map/HobgoblinNativeMap.java b/examples/11-native-type-generic/output/java/src/main/java/org/zwobble/example/types/map/HobgoblinNativeMap.java new file mode 100644 index 0000000..309ba0b --- /dev/null +++ b/examples/11-native-type-generic/output/java/src/main/java/org/zwobble/example/types/map/HobgoblinNativeMap.java @@ -0,0 +1,9 @@ +package org.zwobble.example.types.map; + +public class HobgoblinNativeMap { + private HobgoblinNativeMap() {} + + public static java.util.Map arbitraryNativeMap() { + return new java.util.HashMap<>(); + } +} diff --git a/examples/11-native-type-generic/output/java/src/test/java/org/zwobble/example/MapTests.java b/examples/11-native-type-generic/output/java/src/test/java/org/zwobble/example/MapTests.java new file mode 100644 index 0000000..5b6a764 --- /dev/null +++ b/examples/11-native-type-generic/output/java/src/test/java/org/zwobble/example/MapTests.java @@ -0,0 +1,13 @@ +package org.zwobble.example; + +import java.util.Map; +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; +import org.zwobble.example.types.map.X; + +public class MapTests { + @Test + public void canUseGenericNativeTypes() { + var x = new X(Map.ofEntries(Map.entry("a", 1), Map.entry("b", 2))); + } +} diff --git a/examples/11-native-type-generic/output/rust/.gitignore b/examples/11-native-type-generic/output/rust/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/examples/11-native-type-generic/output/rust/.gitignore @@ -0,0 +1 @@ +/target diff --git a/examples/11-native-type-generic/output/rust/Cargo.lock b/examples/11-native-type-generic/output/rust/Cargo.lock new file mode 100644 index 0000000..9dc6e3e --- /dev/null +++ b/examples/11-native-type-generic/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/11-native-type-generic/output/rust/Cargo.toml b/examples/11-native-type-generic/output/rust/Cargo.toml new file mode 100644 index 0000000..5d4c622 --- /dev/null +++ b/examples/11-native-type-generic/output/rust/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "example" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/examples/11-native-type-generic/output/rust/rust-toolchain.toml b/examples/11-native-type-generic/output/rust/rust-toolchain.toml new file mode 100644 index 0000000..4fedb95 --- /dev/null +++ b/examples/11-native-type-generic/output/rust/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "1.96" diff --git a/examples/11-native-type-generic/output/rust/src/gen/map.rs b/examples/11-native-type-generic/output/rust/src/gen/map.rs new file mode 100644 index 0000000..5d9c837 --- /dev/null +++ b/examples/11-native-type-generic/output/rust/src/gen/map.rs @@ -0,0 +1,6 @@ +// Generated by hobgoblin. + +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct X { + pub map: ::std::collections::BTreeMap::<::std::string::String, ::core::primitive::i32>, +} diff --git a/examples/11-native-type-generic/output/rust/src/lib.rs b/examples/11-native-type-generic/output/rust/src/lib.rs new file mode 100644 index 0000000..b0adf47 --- /dev/null +++ b/examples/11-native-type-generic/output/rust/src/lib.rs @@ -0,0 +1,12 @@ +mod map; + +#[cfg(test)] +mod test { + use std::collections::BTreeMap; + use super::map::X; + + #[test] + fn can_use_generic_native_types() { + let x = X { map: BTreeMap::from([("a".to_string(), 1), ("b".to_string(), 2)]) }; + } +} diff --git a/examples/11-native-type-generic/output/rust/src/map.rs b/examples/11-native-type-generic/output/rust/src/map.rs new file mode 100644 index 0000000..3dd0a72 --- /dev/null +++ b/examples/11-native-type-generic/output/rust/src/map.rs @@ -0,0 +1 @@ +include!("gen/map.rs"); diff --git a/examples/11-native-type-generic/src/map.hob b/examples/11-native-type-generic/src/map.hob new file mode 100644 index 0000000..0118889 --- /dev/null +++ b/examples/11-native-type-generic/src/map.hob @@ -0,0 +1,6 @@ +native NativeMap[K, V] { +} + +struct X { + field map: NativeMap[String, Int32]; +} diff --git a/examples/11-shared/hobgoblin.json5 b/examples/11-shared/hobgoblin.json5 deleted file mode 100644 index 19e0f93..0000000 --- a/examples/11-shared/hobgoblin.json5 +++ /dev/null @@ -1,23 +0,0 @@ -{ - outputs: { - langs: { - java: { - packageName: "org.zwobble.example.types", - }, - }, - generators: [ - { - generator: "java-types", - path: "output/java/src/gen/java", - }, - { - generator: "java-precisely-matchers", - path: "output/java/src/gen/java", - }, - { - generator: "rust-types", - path: "output/rust/src", - }, - ], - } -} diff --git a/examples/11-shared/output/java/.gitignore b/examples/11-shared/output/java/.gitignore deleted file mode 100644 index ff511d4..0000000 --- a/examples/11-shared/output/java/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -/src/gen/ -/target/ diff --git a/examples/11-shared/output/java/pom.xml b/examples/11-shared/output/java/pom.xml deleted file mode 120000 index 445fa65..0000000 --- a/examples/11-shared/output/java/pom.xml +++ /dev/null @@ -1 +0,0 @@ -../../../templates/java-junit/pom.xml \ No newline at end of file diff --git a/examples/11-shared/output/java/src/test/java/org/zwobble/example/PointTests.java b/examples/11-shared/output/java/src/test/java/org/zwobble/example/PointTests.java deleted file mode 100644 index b4c140d..0000000 --- a/examples/11-shared/output/java/src/test/java/org/zwobble/example/PointTests.java +++ /dev/null @@ -1,15 +0,0 @@ -package org.zwobble.example; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import org.junit.jupiter.api.Test; -import org.zwobble.example.types.point.Line; -import org.zwobble.example.types.point.Point; - -public class PointTests { - @Test - public void line() { - var point = new Point(10, 25); - var line = new Line(point, point); - assertEquals(line.start().x(), 10); - } -} diff --git a/examples/11-shared/output/rust/.gitignore b/examples/11-shared/output/rust/.gitignore deleted file mode 100644 index ea8c4bf..0000000 --- a/examples/11-shared/output/rust/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target diff --git a/examples/11-shared/output/rust/Cargo.lock b/examples/11-shared/output/rust/Cargo.lock deleted file mode 100644 index 9dc6e3e..0000000 --- a/examples/11-shared/output/rust/Cargo.lock +++ /dev/null @@ -1,7 +0,0 @@ -# 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/11-shared/output/rust/Cargo.toml b/examples/11-shared/output/rust/Cargo.toml deleted file mode 100644 index 5d4c622..0000000 --- a/examples/11-shared/output/rust/Cargo.toml +++ /dev/null @@ -1,6 +0,0 @@ -[package] -name = "example" -version = "0.1.0" -edition = "2024" - -[dependencies] diff --git a/examples/11-shared/output/rust/rust-toolchain.toml b/examples/11-shared/output/rust/rust-toolchain.toml deleted file mode 100644 index 4fedb95..0000000 --- a/examples/11-shared/output/rust/rust-toolchain.toml +++ /dev/null @@ -1,2 +0,0 @@ -[toolchain] -channel = "1.96" diff --git a/examples/11-shared/output/rust/src/lib.rs b/examples/11-shared/output/rust/src/lib.rs deleted file mode 100644 index af2c87f..0000000 --- a/examples/11-shared/output/rust/src/lib.rs +++ /dev/null @@ -1,14 +0,0 @@ -mod point; - -#[cfg(test)] -mod test { - use std::sync::Arc; - use super::point::{Line, Point}; - - #[test] - fn line() { - let point = Arc::new(Point { x: 10, y: 25 }); - let line = Line { start: Arc::clone(&point), end: point }; - assert_eq!(line.start.x, 10); - } -} diff --git a/examples/11-shared/output/rust/src/point.rs b/examples/11-shared/output/rust/src/point.rs deleted file mode 100644 index 7bc470a..0000000 --- a/examples/11-shared/output/rust/src/point.rs +++ /dev/null @@ -1,15 +0,0 @@ -// Generated by hobgoblin. - -/// A straight line from one point to another. -#[derive(Clone, Debug, Hash, PartialEq)] -pub struct Line { - pub start: ::std::sync::Arc::, - pub end: ::std::sync::Arc::, -} - -/// A 2D point. -#[derive(Clone, Debug, Hash, PartialEq)] -pub struct Point { - pub x: ::core::primitive::i32, - pub y: ::core::primitive::i32, -} diff --git a/examples/11-shared/src/point.hob b/examples/11-shared/src/point.hob deleted file mode 100644 index 7b9a8e6..0000000 --- a/examples/11-shared/src/point.hob +++ /dev/null @@ -1,11 +0,0 @@ -/// A straight line from one point to another. -struct Line { - field start: Shared[Point]; - field end: Shared[Point]; -} - -/// A 2D point. -struct Point { - field x: Int32; - field y: Int32; -} diff --git a/examples/12-custom-code/hobgoblin.json5 b/examples/12-custom-code/hobgoblin.json5 deleted file mode 100644 index 1d08a40..0000000 --- a/examples/12-custom-code/hobgoblin.json5 +++ /dev/null @@ -1,19 +0,0 @@ -{ - outputs: { - langs: { - java: { - packageName: "org.zwobble.example.types", - }, - }, - generators: [ - { - generator: "java-types", - path: "output/java/src/main/java", - }, - { - generator: "rust-types", - path: "output/rust/src/gen", - }, - ], - }, -} diff --git a/examples/12-custom-code/output/java/.gitignore b/examples/12-custom-code/output/java/.gitignore deleted file mode 100644 index ff511d4..0000000 --- a/examples/12-custom-code/output/java/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -/src/gen/ -/target/ diff --git a/examples/12-custom-code/output/java/pom.xml b/examples/12-custom-code/output/java/pom.xml deleted file mode 120000 index 445fa65..0000000 --- a/examples/12-custom-code/output/java/pom.xml +++ /dev/null @@ -1 +0,0 @@ -../../../templates/java-junit/pom.xml \ No newline at end of file diff --git a/examples/12-custom-code/output/java/src/main/java/org/zwobble/example/types/shapes/Square.java b/examples/12-custom-code/output/java/src/main/java/org/zwobble/example/types/shapes/Square.java deleted file mode 100644 index 69a1ab1..0000000 --- a/examples/12-custom-code/output/java/src/main/java/org/zwobble/example/types/shapes/Square.java +++ /dev/null @@ -1,37 +0,0 @@ -// Generated by hobgoblin. - -package org.zwobble.example.types.shapes; - -// Custom area start: org.zwobble.example.types.shapes.Square imports -// Custom area end: org.zwobble.example.types.shapes.Square imports - -public record Square( - int width -) { - public static org.zwobble.example.types.shapes.Square.Builder arbitrary() { - return new org.zwobble.example.types.shapes.Square.Builder(0); - } - - public record Builder( - int width - ) { - public org.zwobble.example.types.shapes.Square build() { - return new org.zwobble.example.types.shapes.Square(width); - } - - public org.zwobble.example.types.shapes.Square.Builder withWidth(int width) { - return new org.zwobble.example.types.shapes.Square.Builder(width); - } - - // Custom area start: org.zwobble.example.types.shapes.Square.Builder body - // Custom area end: org.zwobble.example.types.shapes.Square.Builder body - } - - // Custom area start: org.zwobble.example.types.shapes.Square body - - public int area() { - return this.width * this.width; - } - - // Custom area end: org.zwobble.example.types.shapes.Square body -} diff --git a/examples/12-custom-code/output/java/src/test/java/org/zwobble/example/ShapesTests.java b/examples/12-custom-code/output/java/src/test/java/org/zwobble/example/ShapesTests.java deleted file mode 100644 index fba8e6c..0000000 --- a/examples/12-custom-code/output/java/src/test/java/org/zwobble/example/ShapesTests.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.zwobble.example; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import org.junit.jupiter.api.Test; -import org.zwobble.example.types.shapes.Square; - -public class ShapesTests { - @Test - public void canUseCustomCode() { - Square square = new Square(10); - assertEquals(square.area(), 100); - } -} diff --git a/examples/12-custom-code/output/rust/.gitignore b/examples/12-custom-code/output/rust/.gitignore deleted file mode 100644 index ea8c4bf..0000000 --- a/examples/12-custom-code/output/rust/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target diff --git a/examples/12-custom-code/output/rust/Cargo.lock b/examples/12-custom-code/output/rust/Cargo.lock deleted file mode 100644 index 9dc6e3e..0000000 --- a/examples/12-custom-code/output/rust/Cargo.lock +++ /dev/null @@ -1,7 +0,0 @@ -# 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/12-custom-code/output/rust/Cargo.toml b/examples/12-custom-code/output/rust/Cargo.toml deleted file mode 100644 index 5d4c622..0000000 --- a/examples/12-custom-code/output/rust/Cargo.toml +++ /dev/null @@ -1,6 +0,0 @@ -[package] -name = "example" -version = "0.1.0" -edition = "2024" - -[dependencies] diff --git a/examples/12-custom-code/output/rust/rust-toolchain.toml b/examples/12-custom-code/output/rust/rust-toolchain.toml deleted file mode 100644 index 4fedb95..0000000 --- a/examples/12-custom-code/output/rust/rust-toolchain.toml +++ /dev/null @@ -1,2 +0,0 @@ -[toolchain] -channel = "1.96" diff --git a/examples/12-custom-code/output/rust/src/gen/shapes.rs b/examples/12-custom-code/output/rust/src/gen/shapes.rs deleted file mode 100644 index d1582ad..0000000 --- a/examples/12-custom-code/output/rust/src/gen/shapes.rs +++ /dev/null @@ -1,6 +0,0 @@ -// Generated by hobgoblin. - -#[derive(Clone, Debug, Hash, PartialEq)] -pub struct Square { - pub width: ::core::primitive::i32, -} diff --git a/examples/12-custom-code/output/rust/src/lib.rs b/examples/12-custom-code/output/rust/src/lib.rs deleted file mode 100644 index 1825d72..0000000 --- a/examples/12-custom-code/output/rust/src/lib.rs +++ /dev/null @@ -1,12 +0,0 @@ -mod shapes; - -#[cfg(test)] -mod test { - use super::shapes::Square; - - #[test] - fn can_use_custom_code() { - let square = Square { width: 10 }; - assert_eq!(square.area(), 100); - } -} diff --git a/examples/12-custom-code/output/rust/src/shapes.rs b/examples/12-custom-code/output/rust/src/shapes.rs deleted file mode 100644 index f806bbb..0000000 --- a/examples/12-custom-code/output/rust/src/shapes.rs +++ /dev/null @@ -1,7 +0,0 @@ -include!("gen/shapes.rs"); - -impl Square { - pub fn area(&self) -> i32 { - self.width * self.width - } -} diff --git a/examples/12-custom-code/src/shapes.hob b/examples/12-custom-code/src/shapes.hob deleted file mode 100644 index 5dfe833..0000000 --- a/examples/12-custom-code/src/shapes.hob +++ /dev/null @@ -1,3 +0,0 @@ -struct Square { - field width: Int32; -} diff --git a/examples/12-shared/hobgoblin.json5 b/examples/12-shared/hobgoblin.json5 new file mode 100644 index 0000000..19e0f93 --- /dev/null +++ b/examples/12-shared/hobgoblin.json5 @@ -0,0 +1,23 @@ +{ + outputs: { + langs: { + java: { + packageName: "org.zwobble.example.types", + }, + }, + generators: [ + { + generator: "java-types", + path: "output/java/src/gen/java", + }, + { + generator: "java-precisely-matchers", + path: "output/java/src/gen/java", + }, + { + generator: "rust-types", + path: "output/rust/src", + }, + ], + } +} diff --git a/examples/12-shared/output/java/.gitignore b/examples/12-shared/output/java/.gitignore new file mode 100644 index 0000000..ff511d4 --- /dev/null +++ b/examples/12-shared/output/java/.gitignore @@ -0,0 +1,2 @@ +/src/gen/ +/target/ diff --git a/examples/12-shared/output/java/pom.xml b/examples/12-shared/output/java/pom.xml new file mode 120000 index 0000000..445fa65 --- /dev/null +++ b/examples/12-shared/output/java/pom.xml @@ -0,0 +1 @@ +../../../templates/java-junit/pom.xml \ No newline at end of file diff --git a/examples/12-shared/output/java/src/test/java/org/zwobble/example/PointTests.java b/examples/12-shared/output/java/src/test/java/org/zwobble/example/PointTests.java new file mode 100644 index 0000000..b4c140d --- /dev/null +++ b/examples/12-shared/output/java/src/test/java/org/zwobble/example/PointTests.java @@ -0,0 +1,15 @@ +package org.zwobble.example; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; +import org.zwobble.example.types.point.Line; +import org.zwobble.example.types.point.Point; + +public class PointTests { + @Test + public void line() { + var point = new Point(10, 25); + var line = new Line(point, point); + assertEquals(line.start().x(), 10); + } +} diff --git a/examples/12-shared/output/rust/.gitignore b/examples/12-shared/output/rust/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/examples/12-shared/output/rust/.gitignore @@ -0,0 +1 @@ +/target diff --git a/examples/12-shared/output/rust/Cargo.lock b/examples/12-shared/output/rust/Cargo.lock new file mode 100644 index 0000000..9dc6e3e --- /dev/null +++ b/examples/12-shared/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/12-shared/output/rust/Cargo.toml b/examples/12-shared/output/rust/Cargo.toml new file mode 100644 index 0000000..5d4c622 --- /dev/null +++ b/examples/12-shared/output/rust/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "example" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/examples/12-shared/output/rust/rust-toolchain.toml b/examples/12-shared/output/rust/rust-toolchain.toml new file mode 100644 index 0000000..4fedb95 --- /dev/null +++ b/examples/12-shared/output/rust/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "1.96" diff --git a/examples/12-shared/output/rust/src/lib.rs b/examples/12-shared/output/rust/src/lib.rs new file mode 100644 index 0000000..af2c87f --- /dev/null +++ b/examples/12-shared/output/rust/src/lib.rs @@ -0,0 +1,14 @@ +mod point; + +#[cfg(test)] +mod test { + use std::sync::Arc; + use super::point::{Line, Point}; + + #[test] + fn line() { + let point = Arc::new(Point { x: 10, y: 25 }); + let line = Line { start: Arc::clone(&point), end: point }; + assert_eq!(line.start.x, 10); + } +} diff --git a/examples/12-shared/output/rust/src/point.rs b/examples/12-shared/output/rust/src/point.rs new file mode 100644 index 0000000..7bc470a --- /dev/null +++ b/examples/12-shared/output/rust/src/point.rs @@ -0,0 +1,15 @@ +// Generated by hobgoblin. + +/// A straight line from one point to another. +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct Line { + pub start: ::std::sync::Arc::, + pub end: ::std::sync::Arc::, +} + +/// A 2D point. +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct Point { + pub x: ::core::primitive::i32, + pub y: ::core::primitive::i32, +} diff --git a/examples/12-shared/src/point.hob b/examples/12-shared/src/point.hob new file mode 100644 index 0000000..7b9a8e6 --- /dev/null +++ b/examples/12-shared/src/point.hob @@ -0,0 +1,11 @@ +/// A straight line from one point to another. +struct Line { + field start: Shared[Point]; + field end: Shared[Point]; +} + +/// A 2D point. +struct Point { + field x: Int32; + field y: Int32; +} diff --git a/examples/13-custom-code/hobgoblin.json5 b/examples/13-custom-code/hobgoblin.json5 new file mode 100644 index 0000000..1d08a40 --- /dev/null +++ b/examples/13-custom-code/hobgoblin.json5 @@ -0,0 +1,19 @@ +{ + outputs: { + langs: { + java: { + packageName: "org.zwobble.example.types", + }, + }, + generators: [ + { + generator: "java-types", + path: "output/java/src/main/java", + }, + { + generator: "rust-types", + path: "output/rust/src/gen", + }, + ], + }, +} diff --git a/examples/13-custom-code/output/java/.gitignore b/examples/13-custom-code/output/java/.gitignore new file mode 100644 index 0000000..ff511d4 --- /dev/null +++ b/examples/13-custom-code/output/java/.gitignore @@ -0,0 +1,2 @@ +/src/gen/ +/target/ diff --git a/examples/13-custom-code/output/java/pom.xml b/examples/13-custom-code/output/java/pom.xml new file mode 120000 index 0000000..445fa65 --- /dev/null +++ b/examples/13-custom-code/output/java/pom.xml @@ -0,0 +1 @@ +../../../templates/java-junit/pom.xml \ No newline at end of file diff --git a/examples/13-custom-code/output/java/src/main/java/org/zwobble/example/types/shapes/Square.java b/examples/13-custom-code/output/java/src/main/java/org/zwobble/example/types/shapes/Square.java new file mode 100644 index 0000000..69a1ab1 --- /dev/null +++ b/examples/13-custom-code/output/java/src/main/java/org/zwobble/example/types/shapes/Square.java @@ -0,0 +1,37 @@ +// Generated by hobgoblin. + +package org.zwobble.example.types.shapes; + +// Custom area start: org.zwobble.example.types.shapes.Square imports +// Custom area end: org.zwobble.example.types.shapes.Square imports + +public record Square( + int width +) { + public static org.zwobble.example.types.shapes.Square.Builder arbitrary() { + return new org.zwobble.example.types.shapes.Square.Builder(0); + } + + public record Builder( + int width + ) { + public org.zwobble.example.types.shapes.Square build() { + return new org.zwobble.example.types.shapes.Square(width); + } + + public org.zwobble.example.types.shapes.Square.Builder withWidth(int width) { + return new org.zwobble.example.types.shapes.Square.Builder(width); + } + + // Custom area start: org.zwobble.example.types.shapes.Square.Builder body + // Custom area end: org.zwobble.example.types.shapes.Square.Builder body + } + + // Custom area start: org.zwobble.example.types.shapes.Square body + + public int area() { + return this.width * this.width; + } + + // Custom area end: org.zwobble.example.types.shapes.Square body +} diff --git a/examples/13-custom-code/output/java/src/test/java/org/zwobble/example/ShapesTests.java b/examples/13-custom-code/output/java/src/test/java/org/zwobble/example/ShapesTests.java new file mode 100644 index 0000000..fba8e6c --- /dev/null +++ b/examples/13-custom-code/output/java/src/test/java/org/zwobble/example/ShapesTests.java @@ -0,0 +1,13 @@ +package org.zwobble.example; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; +import org.zwobble.example.types.shapes.Square; + +public class ShapesTests { + @Test + public void canUseCustomCode() { + Square square = new Square(10); + assertEquals(square.area(), 100); + } +} diff --git a/examples/13-custom-code/output/rust/.gitignore b/examples/13-custom-code/output/rust/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/examples/13-custom-code/output/rust/.gitignore @@ -0,0 +1 @@ +/target diff --git a/examples/13-custom-code/output/rust/Cargo.lock b/examples/13-custom-code/output/rust/Cargo.lock new file mode 100644 index 0000000..9dc6e3e --- /dev/null +++ b/examples/13-custom-code/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/13-custom-code/output/rust/Cargo.toml b/examples/13-custom-code/output/rust/Cargo.toml new file mode 100644 index 0000000..5d4c622 --- /dev/null +++ b/examples/13-custom-code/output/rust/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "example" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/examples/13-custom-code/output/rust/rust-toolchain.toml b/examples/13-custom-code/output/rust/rust-toolchain.toml new file mode 100644 index 0000000..4fedb95 --- /dev/null +++ b/examples/13-custom-code/output/rust/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "1.96" diff --git a/examples/13-custom-code/output/rust/src/gen/shapes.rs b/examples/13-custom-code/output/rust/src/gen/shapes.rs new file mode 100644 index 0000000..d1582ad --- /dev/null +++ b/examples/13-custom-code/output/rust/src/gen/shapes.rs @@ -0,0 +1,6 @@ +// Generated by hobgoblin. + +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct Square { + pub width: ::core::primitive::i32, +} diff --git a/examples/13-custom-code/output/rust/src/lib.rs b/examples/13-custom-code/output/rust/src/lib.rs new file mode 100644 index 0000000..1825d72 --- /dev/null +++ b/examples/13-custom-code/output/rust/src/lib.rs @@ -0,0 +1,12 @@ +mod shapes; + +#[cfg(test)] +mod test { + use super::shapes::Square; + + #[test] + fn can_use_custom_code() { + let square = Square { width: 10 }; + assert_eq!(square.area(), 100); + } +} diff --git a/examples/13-custom-code/output/rust/src/shapes.rs b/examples/13-custom-code/output/rust/src/shapes.rs new file mode 100644 index 0000000..f806bbb --- /dev/null +++ b/examples/13-custom-code/output/rust/src/shapes.rs @@ -0,0 +1,7 @@ +include!("gen/shapes.rs"); + +impl Square { + pub fn area(&self) -> i32 { + self.width * self.width + } +} diff --git a/examples/13-custom-code/src/shapes.hob b/examples/13-custom-code/src/shapes.hob new file mode 100644 index 0000000..5dfe833 --- /dev/null +++ b/examples/13-custom-code/src/shapes.hob @@ -0,0 +1,3 @@ +struct Square { + field width: Int32; +} diff --git a/examples/13-imports/hobgoblin.json5 b/examples/13-imports/hobgoblin.json5 deleted file mode 100644 index 12713d4..0000000 --- a/examples/13-imports/hobgoblin.json5 +++ /dev/null @@ -1,19 +0,0 @@ -{ - outputs: { - langs: { - java: { - packageName: "org.zwobble.example.types", - }, - }, - generators: [ - { - generator: "java-types", - path: "output/java/src/gen/java", - }, - { - generator: "rust-types", - path: "output/rust/src", - }, - ], - }, -} diff --git a/examples/13-imports/output/java/.gitignore b/examples/13-imports/output/java/.gitignore deleted file mode 100644 index ff511d4..0000000 --- a/examples/13-imports/output/java/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -/src/gen/ -/target/ diff --git a/examples/13-imports/output/java/pom.xml b/examples/13-imports/output/java/pom.xml deleted file mode 120000 index 445fa65..0000000 --- a/examples/13-imports/output/java/pom.xml +++ /dev/null @@ -1 +0,0 @@ -../../../templates/java-junit/pom.xml \ No newline at end of file diff --git a/examples/13-imports/output/java/src/test/java/org/zwobble/example/LineTests.java b/examples/13-imports/output/java/src/test/java/org/zwobble/example/LineTests.java deleted file mode 100644 index c376b19..0000000 --- a/examples/13-imports/output/java/src/test/java/org/zwobble/example/LineTests.java +++ /dev/null @@ -1,15 +0,0 @@ -package org.zwobble.example; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import org.junit.jupiter.api.Test; -import org.zwobble.example.types.line.Line; -import org.zwobble.example.types.point.Point; - -public class LineTests { - @Test - public void canConstructLine() { - Point start = new Point(10, 25); - Point end = new Point(25, 10); - Line line = new Line(start, end); - } -} diff --git a/examples/13-imports/output/rust/.gitignore b/examples/13-imports/output/rust/.gitignore deleted file mode 100644 index ea8c4bf..0000000 --- a/examples/13-imports/output/rust/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target diff --git a/examples/13-imports/output/rust/Cargo.lock b/examples/13-imports/output/rust/Cargo.lock deleted file mode 100644 index 9dc6e3e..0000000 --- a/examples/13-imports/output/rust/Cargo.lock +++ /dev/null @@ -1,7 +0,0 @@ -# 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/13-imports/output/rust/Cargo.toml b/examples/13-imports/output/rust/Cargo.toml deleted file mode 100644 index 5d4c622..0000000 --- a/examples/13-imports/output/rust/Cargo.toml +++ /dev/null @@ -1,6 +0,0 @@ -[package] -name = "example" -version = "0.1.0" -edition = "2024" - -[dependencies] diff --git a/examples/13-imports/output/rust/rust-toolchain.toml b/examples/13-imports/output/rust/rust-toolchain.toml deleted file mode 100644 index 4fedb95..0000000 --- a/examples/13-imports/output/rust/rust-toolchain.toml +++ /dev/null @@ -1,2 +0,0 @@ -[toolchain] -channel = "1.96" diff --git a/examples/13-imports/output/rust/src/lib.rs b/examples/13-imports/output/rust/src/lib.rs deleted file mode 100644 index ffbcbf4..0000000 --- a/examples/13-imports/output/rust/src/lib.rs +++ /dev/null @@ -1,15 +0,0 @@ -mod line; -mod point; - -#[cfg(test)] -mod test { - use super::line::Line; - use super::point::Point; - - #[test] - fn can_construct_line() { - let start = Point { x: 10, y: 25 }; - let end = Point { x: 10, y: 25 }; - let line = Line { start, end }; - } -} diff --git a/examples/13-imports/output/rust/src/line.rs b/examples/13-imports/output/rust/src/line.rs deleted file mode 100644 index 8561605..0000000 --- a/examples/13-imports/output/rust/src/line.rs +++ /dev/null @@ -1,8 +0,0 @@ -// Generated by hobgoblin. - -/// A straight line from one point to another. -#[derive(Clone, Debug, Hash, PartialEq)] -pub struct Line { - pub start: crate::point::Point, - pub end: crate::point::Point, -} diff --git a/examples/13-imports/output/rust/src/point.rs b/examples/13-imports/output/rust/src/point.rs deleted file mode 100644 index 6d9b010..0000000 --- a/examples/13-imports/output/rust/src/point.rs +++ /dev/null @@ -1,8 +0,0 @@ -// Generated by hobgoblin. - -/// A 2D point. -#[derive(Clone, Debug, Hash, PartialEq)] -pub struct Point { - pub x: ::core::primitive::i32, - pub y: ::core::primitive::i32, -} diff --git a/examples/13-imports/src/line.hob b/examples/13-imports/src/line.hob deleted file mode 100644 index 562d77f..0000000 --- a/examples/13-imports/src/line.hob +++ /dev/null @@ -1,7 +0,0 @@ -import {Point} from ./point; - -/// A straight line from one point to another. -struct Line { - field start: Point; - field end: Point; -} diff --git a/examples/13-imports/src/point.hob b/examples/13-imports/src/point.hob deleted file mode 100644 index 87f9953..0000000 --- a/examples/13-imports/src/point.hob +++ /dev/null @@ -1,5 +0,0 @@ -/// A 2D point. -struct Point { - field x: Int32; - field y: Int32; -} diff --git a/examples/14-imports/hobgoblin.json5 b/examples/14-imports/hobgoblin.json5 new file mode 100644 index 0000000..12713d4 --- /dev/null +++ b/examples/14-imports/hobgoblin.json5 @@ -0,0 +1,19 @@ +{ + outputs: { + langs: { + java: { + packageName: "org.zwobble.example.types", + }, + }, + generators: [ + { + generator: "java-types", + path: "output/java/src/gen/java", + }, + { + generator: "rust-types", + path: "output/rust/src", + }, + ], + }, +} diff --git a/examples/14-imports/output/java/.gitignore b/examples/14-imports/output/java/.gitignore new file mode 100644 index 0000000..ff511d4 --- /dev/null +++ b/examples/14-imports/output/java/.gitignore @@ -0,0 +1,2 @@ +/src/gen/ +/target/ diff --git a/examples/14-imports/output/java/pom.xml b/examples/14-imports/output/java/pom.xml new file mode 120000 index 0000000..445fa65 --- /dev/null +++ b/examples/14-imports/output/java/pom.xml @@ -0,0 +1 @@ +../../../templates/java-junit/pom.xml \ No newline at end of file diff --git a/examples/14-imports/output/java/src/test/java/org/zwobble/example/LineTests.java b/examples/14-imports/output/java/src/test/java/org/zwobble/example/LineTests.java new file mode 100644 index 0000000..c376b19 --- /dev/null +++ b/examples/14-imports/output/java/src/test/java/org/zwobble/example/LineTests.java @@ -0,0 +1,15 @@ +package org.zwobble.example; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; +import org.zwobble.example.types.line.Line; +import org.zwobble.example.types.point.Point; + +public class LineTests { + @Test + public void canConstructLine() { + Point start = new Point(10, 25); + Point end = new Point(25, 10); + Line line = new Line(start, end); + } +} diff --git a/examples/14-imports/output/rust/.gitignore b/examples/14-imports/output/rust/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/examples/14-imports/output/rust/.gitignore @@ -0,0 +1 @@ +/target diff --git a/examples/14-imports/output/rust/Cargo.lock b/examples/14-imports/output/rust/Cargo.lock new file mode 100644 index 0000000..9dc6e3e --- /dev/null +++ b/examples/14-imports/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/14-imports/output/rust/Cargo.toml b/examples/14-imports/output/rust/Cargo.toml new file mode 100644 index 0000000..5d4c622 --- /dev/null +++ b/examples/14-imports/output/rust/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "example" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/examples/14-imports/output/rust/rust-toolchain.toml b/examples/14-imports/output/rust/rust-toolchain.toml new file mode 100644 index 0000000..4fedb95 --- /dev/null +++ b/examples/14-imports/output/rust/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "1.96" diff --git a/examples/14-imports/output/rust/src/lib.rs b/examples/14-imports/output/rust/src/lib.rs new file mode 100644 index 0000000..ffbcbf4 --- /dev/null +++ b/examples/14-imports/output/rust/src/lib.rs @@ -0,0 +1,15 @@ +mod line; +mod point; + +#[cfg(test)] +mod test { + use super::line::Line; + use super::point::Point; + + #[test] + fn can_construct_line() { + let start = Point { x: 10, y: 25 }; + let end = Point { x: 10, y: 25 }; + let line = Line { start, end }; + } +} diff --git a/examples/14-imports/output/rust/src/line.rs b/examples/14-imports/output/rust/src/line.rs new file mode 100644 index 0000000..8561605 --- /dev/null +++ b/examples/14-imports/output/rust/src/line.rs @@ -0,0 +1,8 @@ +// Generated by hobgoblin. + +/// A straight line from one point to another. +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct Line { + pub start: crate::point::Point, + pub end: crate::point::Point, +} diff --git a/examples/14-imports/output/rust/src/point.rs b/examples/14-imports/output/rust/src/point.rs new file mode 100644 index 0000000..6d9b010 --- /dev/null +++ b/examples/14-imports/output/rust/src/point.rs @@ -0,0 +1,8 @@ +// Generated by hobgoblin. + +/// A 2D point. +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct Point { + pub x: ::core::primitive::i32, + pub y: ::core::primitive::i32, +} diff --git a/examples/14-imports/src/line.hob b/examples/14-imports/src/line.hob new file mode 100644 index 0000000..562d77f --- /dev/null +++ b/examples/14-imports/src/line.hob @@ -0,0 +1,7 @@ +import {Point} from ./point; + +/// A straight line from one point to another. +struct Line { + field start: Point; + field end: Point; +} diff --git a/examples/14-imports/src/point.hob b/examples/14-imports/src/point.hob new file mode 100644 index 0000000..87f9953 --- /dev/null +++ b/examples/14-imports/src/point.hob @@ -0,0 +1,5 @@ +/// A 2D point. +struct Point { + field x: Int32; + field y: Int32; +} diff --git a/examples/14-transient-0/hobgoblin.json5 b/examples/14-transient-0/hobgoblin.json5 deleted file mode 100644 index 2160c60..0000000 --- a/examples/14-transient-0/hobgoblin.json5 +++ /dev/null @@ -1,27 +0,0 @@ -{ - outputs: { - langs: { - java: { - packageName: "org.zwobble.example.types", - }, - }, - generators: [ - { - generator: "java-types", - path: "output/java/src/gen/java", - }, - { - generator: "java-transient-0", - path: "output/java/src/gen/java", - }, - { - generator: "rust-types", - path: "output/rust/src/gen", - }, - { - generator: "rust-transient-0", - path: "output/rust/src/gen", - } - ], - } -} diff --git a/examples/14-transient-0/output/java/.gitignore b/examples/14-transient-0/output/java/.gitignore deleted file mode 100644 index 2fcf0b8..0000000 --- a/examples/14-transient-0/output/java/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -/src/gen/ -/target/ -/transient-0/ diff --git a/examples/14-transient-0/output/java/pom.xml b/examples/14-transient-0/output/java/pom.xml deleted file mode 120000 index 445fa65..0000000 --- a/examples/14-transient-0/output/java/pom.xml +++ /dev/null @@ -1 +0,0 @@ -../../../templates/java-junit/pom.xml \ No newline at end of file diff --git a/examples/14-transient-0/output/java/src/test/java/org/zwobble/example/Transient0Tests.java b/examples/14-transient-0/output/java/src/test/java/org/zwobble/example/Transient0Tests.java deleted file mode 100644 index 6732f72..0000000 --- a/examples/14-transient-0/output/java/src/test/java/org/zwobble/example/Transient0Tests.java +++ /dev/null @@ -1,353 +0,0 @@ -package org.zwobble.example; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.zwobble.example.types.data.EnumWithVariants; -import org.zwobble.example.types.data.StructSingleton; -import org.zwobble.example.types.data.StructWithBool; -import org.zwobble.example.types.data.StructWithBox; -import org.zwobble.example.types.data.StructWithDifferentSharedTypes; -import org.zwobble.example.types.data.StructWithEnum; -import org.zwobble.example.types.data.StructWithInt8; -import org.zwobble.example.types.data.StructWithInt32; -import org.zwobble.example.types.data.StructWithInt64; -import org.zwobble.example.types.data.StructWithList; -import org.zwobble.example.types.data.StructWithListOfShared; -import org.zwobble.example.types.data.StructWithOption; -import org.zwobble.example.types.data.StructWithSharedSumAndVariant; -import org.zwobble.example.types.data.StructWithString; -import org.zwobble.example.types.data.StructWithStruct; -import org.zwobble.example.types.data.StructWithSum; -import org.zwobble.example.types.data.VariantOne; -import org.zwobble.example.types.data.VariantTwo; -import org.zwobble.example.types.data.transient0.HobgoblinTransient0Data; -import org.zwobble.example.types.transient0.HobgoblinTransient0TaggedSharedValue; - -public class Transient0Tests { - @Test - public void structSingleton() throws IOException { - var value = StructSingleton.INSTANCE; - - assertRoundTripEncoding( - "StructSingleton", - value, - HobgoblinTransient0Data::encodeStructSingleton, - HobgoblinTransient0Data::decodeStructSingleton - ); - } - - @Test - public void structWithBool() throws IOException { - var value = new StructWithBool(true, false); - - assertRoundTripEncoding( - "StructWithBool", - value, - HobgoblinTransient0Data::encodeStructWithBool, - HobgoblinTransient0Data::decodeStructWithBool - ); - } - - @Test - public void structWithInt8() throws IOException { - var value = new StructWithInt8((byte)10, (byte)25); - - assertRoundTripEncoding( - "StructWithInt8", - value, - HobgoblinTransient0Data::encodeStructWithInt8, - HobgoblinTransient0Data::decodeStructWithInt8 - ); - } - - @Test - public void structWithInt32() throws IOException { - var value = new StructWithInt32(10, 25); - - assertRoundTripEncoding( - "StructWithInt32", - value, - HobgoblinTransient0Data::encodeStructWithInt32, - HobgoblinTransient0Data::decodeStructWithInt32 - ); - } - - @Test - public void structWithInt64() throws IOException { - var value = new StructWithInt64(10, 25); - - assertRoundTripEncoding( - "StructWithInt64", - value, - HobgoblinTransient0Data::encodeStructWithInt64, - HobgoblinTransient0Data::decodeStructWithInt64 - ); - } - - @Test - public void structWithString() throws IOException { - var value = new StructWithString("abc", "def"); - - assertRoundTripEncoding( - "StructWithString", - value, - HobgoblinTransient0Data::encodeStructWithString, - HobgoblinTransient0Data::decodeStructWithString - ); - } - - @Test - public void structWithStruct() throws IOException { - var value = new StructWithStruct(new StructWithInt32(10, 25), 42); - - assertRoundTripEncoding( - "StructWithStruct", - value, - HobgoblinTransient0Data::encodeStructWithStruct, - HobgoblinTransient0Data::decodeStructWithStruct - ); - } - - @Test - public void structWithList() throws IOException { - var value = new StructWithList( - List.of(10L, 25L), - List.of( - new StructWithInt32(42, 47), - new StructWithInt32(52, 57), - new StructWithInt32(62, 67) - ) - ); - - assertRoundTripEncoding( - "StructWithList", - value, - HobgoblinTransient0Data::encodeStructWithList, - HobgoblinTransient0Data::decodeStructWithList - ); - } - - @Test - public void structWithOptionNone() throws IOException { - var value = new StructWithOption( - Optional.empty(), - Optional.empty() - ); - - assertRoundTripEncoding( - "StructWithOption_None", - value, - HobgoblinTransient0Data::encodeStructWithOption, - HobgoblinTransient0Data::decodeStructWithOption - ); - } - - @Test - public void structWithOptionSome() throws IOException { - var value = new StructWithOption( - Optional.of(10L), - Optional.of(new StructWithInt32(42, 47)) - ); - - assertRoundTripEncoding( - "StructWithOption_Some", - value, - HobgoblinTransient0Data::encodeStructWithOption, - HobgoblinTransient0Data::decodeStructWithOption - ); - } - - @Test - public void enumWithVariants() throws IOException { - assertRoundTripEncoding( - "EnumWithVariants_zero", - EnumWithVariants.ZERO, - HobgoblinTransient0Data::encodeEnumWithVariants, - HobgoblinTransient0Data::decodeEnumWithVariants - ); - - assertRoundTripEncoding( - "EnumWithVariants_one", - EnumWithVariants.ONE, - HobgoblinTransient0Data::encodeEnumWithVariants, - HobgoblinTransient0Data::decodeEnumWithVariants - ); - - assertRoundTripEncoding( - "EnumWithVariants_two", - EnumWithVariants.TWO, - HobgoblinTransient0Data::encodeEnumWithVariants, - HobgoblinTransient0Data::decodeEnumWithVariants - ); - } - - @Test - public void structWithEnum() throws IOException { - var value = new StructWithEnum( - EnumWithVariants.ONE, - 42 - ); - - assertRoundTripEncoding( - "StructWithEnum", - value, - HobgoblinTransient0Data::encodeStructWithEnum, - HobgoblinTransient0Data::decodeStructWithEnum - ); - } - - @Test - public void sumWithVariants() throws IOException { - assertRoundTripEncoding( - "SumWithVariants_VariantOne", - new VariantOne(10), - HobgoblinTransient0Data::encodeSumWithVariants, - HobgoblinTransient0Data::decodeSumWithVariants - ); - - assertRoundTripEncoding( - "SumWithVariants_VariantTwo", - new VariantTwo(25), - HobgoblinTransient0Data::encodeSumWithVariants, - HobgoblinTransient0Data::decodeSumWithVariants - ); - } - - @Test - public void sumWithBoxedVariants() throws IOException { - assertRoundTripEncoding( - "SumWithBoxedVariants_VariantOne", - new VariantOne(10), - HobgoblinTransient0Data::encodeSumWithBoxedVariants, - HobgoblinTransient0Data::decodeSumWithBoxedVariants - ); - - assertRoundTripEncoding( - "SumWithBoxedVariants_VariantTwo", - new VariantTwo(25), - HobgoblinTransient0Data::encodeSumWithBoxedVariants, - HobgoblinTransient0Data::decodeSumWithBoxedVariants - ); - } - - @Test - public void structWithSum() throws IOException { - var value = new StructWithSum( - new VariantTwo(25), - 42 - ); - - assertRoundTripEncoding( - "StructWithSum", - value, - HobgoblinTransient0Data::encodeStructWithSum, - HobgoblinTransient0Data::decodeStructWithSum - ); - } - - @Test - public void structWithBox() throws IOException { - var value = new StructWithBox( - new StructWithInt32(10, 25) - ); - - assertRoundTripEncoding( - "StructWithBox", - value, - HobgoblinTransient0Data::encodeStructWithBox, - HobgoblinTransient0Data::decodeStructWithBox - ); - } - - @Test - public void structWithListOfShared() throws IOException { - var a = new StructWithInt32(10, 25); - var b = new StructWithInt32(42, 47); - - var value = new StructWithListOfShared(List.of(a, a, b, a, b)); - - var decodedValue = assertRoundTripEncoding( - "StructWithListOfShared", - value, - HobgoblinTransient0Data::encodeStructWithListOfShared, - HobgoblinTransient0Data::decodeStructWithListOfShared - ); - Assertions.assertSame(decodedValue.inner().get(0), decodedValue.inner().get(1)); - Assertions.assertSame(decodedValue.inner().get(0), decodedValue.inner().get(3)); - Assertions.assertSame(decodedValue.inner().get(2), decodedValue.inner().get(4)); - } - - @Test - public void structWithDifferentSharedTypes() throws IOException { - var a = new StructWithInt32(10, 25); - var b = new StructWithInt64(42, 47); - var c = new StructWithInt32(52, 57); - - var value = new StructWithDifferentSharedTypes(a, b, c); - - assertRoundTripEncoding( - "StructWithDifferentSharedTypes", - value, - HobgoblinTransient0Data::encodeStructWithDifferentSharedTypes, - HobgoblinTransient0Data::decodeStructWithDifferentSharedTypes - ); - } - - @Test - public void structWithSharedSumAndVariant() throws IOException { - var a = new VariantOne(10); - - var value = new StructWithSharedSumAndVariant(a, a); - - assertRoundTripEncoding( - "StructWithSharedSumAndVariant", - value, - HobgoblinTransient0Data::encodeStructWithSharedSumAndVariant, - HobgoblinTransient0Data::decodeStructWithSharedSumAndVariant - ); - } - - private T assertRoundTripEncoding( - String name, - T value, - Encoder encoder, - Decoder decoder - ) throws IOException { - var outputStream = new ByteArrayOutputStream(); - encoder.encode(value, outputStream, new HashMap<>()); - var bytes = outputStream.toByteArray(); - - var outputDir = System.getenv("HOBGOBLIN_OUTPUT_DIR"); - var transient0OutputDir = Path.of(outputDir).resolve("transient-0"); - Files.createDirectories(transient0OutputDir); - var transient0OutputPath = transient0OutputDir.resolve(name); - Files.write(transient0OutputPath, bytes); - - var inputStream = new ByteArrayInputStream(bytes); - var decodedValue = decoder.decode(inputStream, new ArrayList<>()); - - Assertions.assertEquals(value, decodedValue); - - return decodedValue; - } - - private interface Encoder { - void encode(T value, OutputStream outputStream, Map sharedValues) throws IOException; - } - - private interface Decoder { - T decode(InputStream inputStream, List sharedValues) throws IOException; - } -} diff --git a/examples/14-transient-0/output/rust/.gitignore b/examples/14-transient-0/output/rust/.gitignore deleted file mode 100644 index fc03479..0000000 --- a/examples/14-transient-0/output/rust/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -/target -/transient-0/ diff --git a/examples/14-transient-0/output/rust/Cargo.lock b/examples/14-transient-0/output/rust/Cargo.lock deleted file mode 100644 index 9dc6e3e..0000000 --- a/examples/14-transient-0/output/rust/Cargo.lock +++ /dev/null @@ -1,7 +0,0 @@ -# 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/14-transient-0/output/rust/Cargo.toml b/examples/14-transient-0/output/rust/Cargo.toml deleted file mode 100644 index 5d4c622..0000000 --- a/examples/14-transient-0/output/rust/Cargo.toml +++ /dev/null @@ -1,6 +0,0 @@ -[package] -name = "example" -version = "0.1.0" -edition = "2024" - -[dependencies] diff --git a/examples/14-transient-0/output/rust/rust-toolchain.toml b/examples/14-transient-0/output/rust/rust-toolchain.toml deleted file mode 100644 index 4fedb95..0000000 --- a/examples/14-transient-0/output/rust/rust-toolchain.toml +++ /dev/null @@ -1,2 +0,0 @@ -[toolchain] -channel = "1.96" diff --git a/examples/14-transient-0/output/rust/src/data.rs b/examples/14-transient-0/output/rust/src/data.rs deleted file mode 100644 index 7a0c4fa..0000000 --- a/examples/14-transient-0/output/rust/src/data.rs +++ /dev/null @@ -1,5 +0,0 @@ -include!("./gen/data.rs"); - -pub mod transient_0 { - include!("./gen/data/transient_0.rs"); -} diff --git a/examples/14-transient-0/output/rust/src/gen/data.rs b/examples/14-transient-0/output/rust/src/gen/data.rs deleted file mode 100644 index d4847ec..0000000 --- a/examples/14-transient-0/output/rust/src/gen/data.rs +++ /dev/null @@ -1,140 +0,0 @@ -// Generated by hobgoblin. - -#[derive(Clone, Debug, Hash, PartialEq)] -pub struct StructSingleton; - -#[derive(Clone, Debug, Hash, PartialEq)] -pub struct StructWithBool { - pub a: ::core::primitive::bool, - pub b: ::core::primitive::bool, -} - -#[derive(Clone, Debug, Hash, PartialEq)] -pub struct StructWithInt8 { - pub a: ::core::primitive::i8, - pub b: ::core::primitive::i8, -} - -#[derive(Clone, Debug, Hash, PartialEq)] -pub struct StructWithInt32 { - pub a: ::core::primitive::i32, - pub b: ::core::primitive::i32, -} - -#[derive(Clone, Debug, Hash, PartialEq)] -pub struct StructWithInt64 { - pub a: ::core::primitive::i64, - pub b: ::core::primitive::i64, -} - -#[derive(Clone, Debug, Hash, PartialEq)] -pub struct StructWithString { - pub a: ::std::string::String, - pub b: ::std::string::String, -} - -#[derive(Clone, Debug, Hash, PartialEq)] -pub struct StructWithStruct { - pub inner: crate::data::StructWithInt32, - pub c: ::core::primitive::i32, -} - -#[derive(Clone, Debug, Hash, PartialEq)] -pub struct StructWithList { - pub a: ::std::vec::Vec::<::core::primitive::i64>, - pub b: ::std::vec::Vec::, -} - -#[derive(Clone, Debug, Hash, PartialEq)] -pub struct StructWithOption { - pub a: ::std::option::Option::<::core::primitive::i64>, - pub b: ::std::option::Option::, -} - -#[derive(Clone, Copy, Debug, Hash, PartialEq)] -pub enum EnumWithVariants { - Zero, - One, - Two, -} - -#[derive(Clone, Debug, Hash, PartialEq)] -pub struct StructWithEnum { - pub inner: crate::data::EnumWithVariants, - pub c: ::core::primitive::i32, -} - -#[derive(Clone, Debug, Hash, PartialEq)] -pub enum SumWithVariants { - VariantOne(crate::data::VariantOne), - VariantTwo(crate::data::VariantTwo), -} - -impl ::std::convert::From:: for crate::data::SumWithVariants { - fn from(value: crate::data::VariantOne) -> Self { - Self::VariantOne(value) - } -} - -impl ::std::convert::From:: for crate::data::SumWithVariants { - fn from(value: crate::data::VariantTwo) -> Self { - Self::VariantTwo(value) - } -} - -#[derive(Clone, Debug, Hash, PartialEq)] -pub struct VariantOne { - pub a: ::core::primitive::i32, -} - -#[derive(Clone, Debug, Hash, PartialEq)] -pub struct VariantTwo { - pub a: ::core::primitive::i64, -} - -#[derive(Clone, Debug, Hash, PartialEq)] -pub enum SumWithBoxedVariants { - VariantOne(::std::boxed::Box::), - VariantTwo(::std::boxed::Box::), -} - -impl ::std::convert::From:: for crate::data::SumWithBoxedVariants { - fn from(value: crate::data::VariantOne) -> Self { - Self::VariantOne(::std::boxed::Box::new(value)) - } -} - -impl ::std::convert::From:: for crate::data::SumWithBoxedVariants { - fn from(value: crate::data::VariantTwo) -> Self { - Self::VariantTwo(::std::boxed::Box::new(value)) - } -} - -#[derive(Clone, Debug, Hash, PartialEq)] -pub struct StructWithSum { - pub inner: crate::data::SumWithVariants, - pub b: ::core::primitive::i32, -} - -#[derive(Clone, Debug, Hash, PartialEq)] -pub struct StructWithBox { - pub inner: ::std::boxed::Box::, -} - -#[derive(Clone, Debug, Hash, PartialEq)] -pub struct StructWithListOfShared { - pub inner: ::std::vec::Vec::<::std::sync::Arc::>, -} - -#[derive(Clone, Debug, Hash, PartialEq)] -pub struct StructWithDifferentSharedTypes { - pub a: ::std::sync::Arc::, - pub b: ::std::sync::Arc::, - pub c: ::std::sync::Arc::, -} - -#[derive(Clone, Debug, Hash, PartialEq)] -pub struct StructWithSharedSumAndVariant { - pub a: ::std::sync::Arc::, - pub b: ::std::sync::Arc::, -} diff --git a/examples/14-transient-0/output/rust/src/gen/data/transient_0.rs b/examples/14-transient-0/output/rust/src/gen/data/transient_0.rs deleted file mode 100644 index f802163..0000000 --- a/examples/14-transient-0/output/rust/src/gen/data/transient_0.rs +++ /dev/null @@ -1,455 +0,0 @@ -// Generated by hobgoblin. - -pub fn encode_struct_singleton(value: &crate::data::StructSingleton, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { - ::std::io::Result::Ok(()) -} - -pub fn decode_struct_singleton(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { - ::std::io::Result::Ok(crate::data::StructSingleton) -} - -pub fn encode_struct_with_bool(value: &crate::data::StructWithBool, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { - crate::transient_0::encode_bool(&value.a, writer, shared_values)?; - crate::transient_0::encode_bool(&value.b, writer, shared_values)?; - ::std::io::Result::Ok(()) -} - -pub fn decode_struct_with_bool(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { - let a = crate::transient_0::decode_bool(reader, shared_values)?; - let b = crate::transient_0::decode_bool(reader, shared_values)?; - ::std::io::Result::Ok(crate::data::StructWithBool { a: a, b: b }) -} - -pub fn encode_struct_with_int_8(value: &crate::data::StructWithInt8, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { - crate::transient_0::encode_int_8(&value.a, writer, shared_values)?; - crate::transient_0::encode_int_8(&value.b, writer, shared_values)?; - ::std::io::Result::Ok(()) -} - -pub fn decode_struct_with_int_8(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { - let a = crate::transient_0::decode_int_8(reader, shared_values)?; - let b = crate::transient_0::decode_int_8(reader, shared_values)?; - ::std::io::Result::Ok(crate::data::StructWithInt8 { a: a, b: b }) -} - -pub fn encode_struct_with_int_32(value: &crate::data::StructWithInt32, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { - crate::transient_0::encode_int_32(&value.a, writer, shared_values)?; - crate::transient_0::encode_int_32(&value.b, writer, shared_values)?; - ::std::io::Result::Ok(()) -} - -pub fn decode_struct_with_int_32(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { - let a = crate::transient_0::decode_int_32(reader, shared_values)?; - let b = crate::transient_0::decode_int_32(reader, shared_values)?; - ::std::io::Result::Ok(crate::data::StructWithInt32 { a: a, b: b }) -} - -pub fn encode_struct_with_int_64(value: &crate::data::StructWithInt64, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { - crate::transient_0::encode_int_64(&value.a, writer, shared_values)?; - crate::transient_0::encode_int_64(&value.b, writer, shared_values)?; - ::std::io::Result::Ok(()) -} - -pub fn decode_struct_with_int_64(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { - let a = crate::transient_0::decode_int_64(reader, shared_values)?; - let b = crate::transient_0::decode_int_64(reader, shared_values)?; - ::std::io::Result::Ok(crate::data::StructWithInt64 { a: a, b: b }) -} - -pub fn encode_struct_with_string(value: &crate::data::StructWithString, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { - crate::transient_0::encode_string(&value.a, writer, shared_values)?; - crate::transient_0::encode_string(&value.b, writer, shared_values)?; - ::std::io::Result::Ok(()) -} - -pub fn decode_struct_with_string(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { - let a = crate::transient_0::decode_string(reader, shared_values)?; - let b = crate::transient_0::decode_string(reader, shared_values)?; - ::std::io::Result::Ok(crate::data::StructWithString { a: a, b: b }) -} - -pub fn encode_struct_with_struct(value: &crate::data::StructWithStruct, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { - crate::data::transient_0::encode_struct_with_int_32(&value.inner, writer, shared_values)?; - crate::transient_0::encode_int_32(&value.c, writer, shared_values)?; - ::std::io::Result::Ok(()) -} - -pub fn decode_struct_with_struct(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { - let inner = crate::data::transient_0::decode_struct_with_int_32(reader, shared_values)?; - let c = crate::transient_0::decode_int_32(reader, shared_values)?; - ::std::io::Result::Ok(crate::data::StructWithStruct { inner: inner, c: c }) -} - -pub fn encode_struct_with_list(value: &crate::data::StructWithList, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { - { - crate::transient_0::encode_int_64(&(((&value.a).len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?, writer, shared_values)?; - for element in &value.a { - crate::transient_0::encode_int_64(element, writer, shared_values)?; - }; - }; - { - crate::transient_0::encode_int_64(&(((&value.b).len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?, writer, shared_values)?; - for element in &value.b { - crate::data::transient_0::encode_struct_with_int_32(element, writer, shared_values)?; - }; - }; - ::std::io::Result::Ok(()) -} - -pub fn decode_struct_with_list(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { - let a = { - let len = crate::transient_0::decode_int_64(reader, shared_values)?; - let mut elements = ::std::vec::Vec::with_capacity((len.try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?); - for _ in 0..len { - elements.push(crate::transient_0::decode_int_64(reader, shared_values)?); - }; - elements - }; - let b = { - let len = crate::transient_0::decode_int_64(reader, shared_values)?; - let mut elements = ::std::vec::Vec::with_capacity((len.try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?); - for _ in 0..len { - elements.push(crate::data::transient_0::decode_struct_with_int_32(reader, shared_values)?); - }; - elements - }; - ::std::io::Result::Ok(crate::data::StructWithList { a: a, b: b }) -} - -pub fn encode_struct_with_option(value: &crate::data::StructWithOption, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { - match &value.a { - ::std::option::Option::Some(value) => { - crate::transient_0::encode_bool(&true, writer, shared_values)?; - crate::transient_0::encode_int_64(&value, writer, shared_values)?; - }, - ::std::option::Option::None => { - crate::transient_0::encode_bool(&false, writer, shared_values)?; - }, - }; - match &value.b { - ::std::option::Option::Some(value) => { - crate::transient_0::encode_bool(&true, writer, shared_values)?; - crate::data::transient_0::encode_struct_with_int_32(&value, writer, shared_values)?; - }, - ::std::option::Option::None => { - crate::transient_0::encode_bool(&false, writer, shared_values)?; - }, - }; - ::std::io::Result::Ok(()) -} - -pub fn decode_struct_with_option(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { - let a = { - let is_some = crate::transient_0::decode_bool(reader, shared_values)?; - if is_some { - ::std::option::Option::Some(crate::transient_0::decode_int_64(reader, shared_values)?) - } else { - ::std::option::Option::None - } - }; - let b = { - let is_some = crate::transient_0::decode_bool(reader, shared_values)?; - if is_some { - ::std::option::Option::Some(crate::data::transient_0::decode_struct_with_int_32(reader, shared_values)?) - } else { - ::std::option::Option::None - } - }; - ::std::io::Result::Ok(crate::data::StructWithOption { a: a, b: b }) -} - -pub fn encode_enum_with_variants(value: &crate::data::EnumWithVariants, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { - crate::transient_0::encode_int_32(&match value { - crate::data::EnumWithVariants::Zero => 0, - crate::data::EnumWithVariants::One => 1, - crate::data::EnumWithVariants::Two => 2, - }, writer, shared_values)?; - ::std::io::Result::Ok(()) -} - -pub fn decode_enum_with_variants(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { - ::std::io::Result::Ok(match &crate::transient_0::decode_int_32(reader, shared_values)? { - 0 => crate::data::EnumWithVariants::Zero, - 1 => crate::data::EnumWithVariants::One, - 2 => crate::data::EnumWithVariants::Two, - _ => ::std::result::Result::Err(::std::io::Error::new(::std::io::ErrorKind::InvalidData, "unrecognised variant tag for data.EnumWithVariants"))?, - }) -} - -pub fn encode_struct_with_enum(value: &crate::data::StructWithEnum, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { - crate::data::transient_0::encode_enum_with_variants(&value.inner, writer, shared_values)?; - crate::transient_0::encode_int_32(&value.c, writer, shared_values)?; - ::std::io::Result::Ok(()) -} - -pub fn decode_struct_with_enum(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { - let inner = crate::data::transient_0::decode_enum_with_variants(reader, shared_values)?; - let c = crate::transient_0::decode_int_32(reader, shared_values)?; - ::std::io::Result::Ok(crate::data::StructWithEnum { inner: inner, c: c }) -} - -pub fn encode_sum_with_variants(value: &crate::data::SumWithVariants, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { - match value { - crate::data::SumWithVariants::VariantOne(value) => { - crate::transient_0::encode_int_32(&0, writer, shared_values)?; - crate::data::transient_0::encode_variant_one(value, writer, shared_values)?; - }, - crate::data::SumWithVariants::VariantTwo(value) => { - crate::transient_0::encode_int_32(&1, writer, shared_values)?; - crate::data::transient_0::encode_variant_two(value, writer, shared_values)?; - }, - }; - ::std::io::Result::Ok(()) -} - -pub fn decode_sum_with_variants(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { - ::std::io::Result::Ok(match &crate::transient_0::decode_int_32(reader, shared_values)? { - 0 => crate::data::SumWithVariants::VariantOne(crate::data::transient_0::decode_variant_one(reader, shared_values)?), - 1 => crate::data::SumWithVariants::VariantTwo(crate::data::transient_0::decode_variant_two(reader, shared_values)?), - _ => ::std::result::Result::Err(::std::io::Error::new(::std::io::ErrorKind::InvalidData, "unrecognised variant tag for data.SumWithVariants"))?, - }) -} - -pub fn encode_variant_one(value: &crate::data::VariantOne, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { - crate::transient_0::encode_int_32(&value.a, writer, shared_values)?; - ::std::io::Result::Ok(()) -} - -pub fn decode_variant_one(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { - let a = crate::transient_0::decode_int_32(reader, shared_values)?; - ::std::io::Result::Ok(crate::data::VariantOne { a: a }) -} - -pub fn encode_variant_two(value: &crate::data::VariantTwo, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { - crate::transient_0::encode_int_64(&value.a, writer, shared_values)?; - ::std::io::Result::Ok(()) -} - -pub fn decode_variant_two(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { - let a = crate::transient_0::decode_int_64(reader, shared_values)?; - ::std::io::Result::Ok(crate::data::VariantTwo { a: a }) -} - -pub fn encode_sum_with_boxed_variants(value: &crate::data::SumWithBoxedVariants, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { - match value { - crate::data::SumWithBoxedVariants::VariantOne(value) => { - crate::transient_0::encode_int_32(&0, writer, shared_values)?; - crate::data::transient_0::encode_variant_one(value.as_ref(), writer, shared_values)?; - }, - crate::data::SumWithBoxedVariants::VariantTwo(value) => { - crate::transient_0::encode_int_32(&1, writer, shared_values)?; - crate::data::transient_0::encode_variant_two(value.as_ref(), writer, shared_values)?; - }, - }; - ::std::io::Result::Ok(()) -} - -pub fn decode_sum_with_boxed_variants(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { - ::std::io::Result::Ok(match &crate::transient_0::decode_int_32(reader, shared_values)? { - 0 => crate::data::SumWithBoxedVariants::VariantOne(::std::boxed::Box::new(crate::data::transient_0::decode_variant_one(reader, shared_values)?)), - 1 => crate::data::SumWithBoxedVariants::VariantTwo(::std::boxed::Box::new(crate::data::transient_0::decode_variant_two(reader, shared_values)?)), - _ => ::std::result::Result::Err(::std::io::Error::new(::std::io::ErrorKind::InvalidData, "unrecognised variant tag for data.SumWithBoxedVariants"))?, - }) -} - -pub fn encode_struct_with_sum(value: &crate::data::StructWithSum, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { - crate::data::transient_0::encode_sum_with_variants(&value.inner, writer, shared_values)?; - crate::transient_0::encode_int_32(&value.b, writer, shared_values)?; - ::std::io::Result::Ok(()) -} - -pub fn decode_struct_with_sum(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { - let inner = crate::data::transient_0::decode_sum_with_variants(reader, shared_values)?; - let b = crate::transient_0::decode_int_32(reader, shared_values)?; - ::std::io::Result::Ok(crate::data::StructWithSum { inner: inner, b: b }) -} - -pub fn encode_struct_with_box(value: &crate::data::StructWithBox, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { - crate::data::transient_0::encode_struct_with_int_32((&value.inner).as_ref(), writer, shared_values)?; - ::std::io::Result::Ok(()) -} - -pub fn decode_struct_with_box(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { - let inner = ::std::boxed::Box::new(crate::data::transient_0::decode_struct_with_int_32(reader, shared_values)?); - ::std::io::Result::Ok(crate::data::StructWithBox { inner: inner }) -} - -pub fn encode_struct_with_list_of_shared(value: &crate::data::StructWithListOfShared, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { - { - crate::transient_0::encode_int_64(&(((&value.inner).len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?, writer, shared_values)?; - for element in &value.inner { - { - let ptr = ::std::sync::Arc::as_ptr(&element) as ::core::primitive::usize; - match (shared_values.get(&ptr)).copied() { - ::std::option::Option::Some(id) => { - crate::transient_0::encode_int_64(&id, writer, shared_values)?; - }, - ::std::option::Option::None => { - let id = ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?; - crate::transient_0::encode_int_64(&id, writer, shared_values)?; - shared_values.insert(ptr, id); - crate::data::transient_0::encode_struct_with_int_32(element.as_ref(), writer, shared_values)?; - }, - }; - }; - }; - }; - ::std::io::Result::Ok(()) -} - -pub fn decode_struct_with_list_of_shared(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { - let inner = { - let len = crate::transient_0::decode_int_64(reader, shared_values)?; - let mut elements = ::std::vec::Vec::with_capacity((len.try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?); - for _ in 0..len { - elements.push({ - let id = crate::transient_0::decode_int_64(reader, shared_values)?; - if id < ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))? { - ((::std::sync::Arc::clone(&shared_values[(::core::primitive::usize::try_from(id)).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?])).downcast()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, "referenced value has wrong type"))? - } else { - let decoded_value = ::std::sync::Arc::new(crate::data::transient_0::decode_struct_with_int_32(reader, shared_values)?); - shared_values.push(::std::sync::Arc::clone(&decoded_value) as ::std::sync::Arc::); - decoded_value - } - }); - }; - elements - }; - ::std::io::Result::Ok(crate::data::StructWithListOfShared { inner: inner }) -} - -pub fn encode_struct_with_different_shared_types(value: &crate::data::StructWithDifferentSharedTypes, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { - { - let ptr = ::std::sync::Arc::as_ptr(&&value.a) as ::core::primitive::usize; - match (shared_values.get(&ptr)).copied() { - ::std::option::Option::Some(id) => { - crate::transient_0::encode_int_64(&id, writer, shared_values)?; - }, - ::std::option::Option::None => { - let id = ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?; - crate::transient_0::encode_int_64(&id, writer, shared_values)?; - shared_values.insert(ptr, id); - crate::data::transient_0::encode_struct_with_int_32((&value.a).as_ref(), writer, shared_values)?; - }, - }; - }; - { - let ptr = ::std::sync::Arc::as_ptr(&&value.b) as ::core::primitive::usize; - match (shared_values.get(&ptr)).copied() { - ::std::option::Option::Some(id) => { - crate::transient_0::encode_int_64(&id, writer, shared_values)?; - }, - ::std::option::Option::None => { - let id = ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?; - crate::transient_0::encode_int_64(&id, writer, shared_values)?; - shared_values.insert(ptr, id); - crate::data::transient_0::encode_struct_with_int_64((&value.b).as_ref(), writer, shared_values)?; - }, - }; - }; - { - let ptr = ::std::sync::Arc::as_ptr(&&value.c) as ::core::primitive::usize; - match (shared_values.get(&ptr)).copied() { - ::std::option::Option::Some(id) => { - crate::transient_0::encode_int_64(&id, writer, shared_values)?; - }, - ::std::option::Option::None => { - let id = ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?; - crate::transient_0::encode_int_64(&id, writer, shared_values)?; - shared_values.insert(ptr, id); - crate::data::transient_0::encode_struct_with_int_32((&value.c).as_ref(), writer, shared_values)?; - }, - }; - }; - ::std::io::Result::Ok(()) -} - -pub fn decode_struct_with_different_shared_types(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { - let a = { - let id = crate::transient_0::decode_int_64(reader, shared_values)?; - if id < ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))? { - ((::std::sync::Arc::clone(&shared_values[(::core::primitive::usize::try_from(id)).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?])).downcast()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, "referenced value has wrong type"))? - } else { - let decoded_value = ::std::sync::Arc::new(crate::data::transient_0::decode_struct_with_int_32(reader, shared_values)?); - shared_values.push(::std::sync::Arc::clone(&decoded_value) as ::std::sync::Arc::); - decoded_value - } - }; - let b = { - let id = crate::transient_0::decode_int_64(reader, shared_values)?; - if id < ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))? { - ((::std::sync::Arc::clone(&shared_values[(::core::primitive::usize::try_from(id)).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?])).downcast()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, "referenced value has wrong type"))? - } else { - let decoded_value = ::std::sync::Arc::new(crate::data::transient_0::decode_struct_with_int_64(reader, shared_values)?); - shared_values.push(::std::sync::Arc::clone(&decoded_value) as ::std::sync::Arc::); - decoded_value - } - }; - let c = { - let id = crate::transient_0::decode_int_64(reader, shared_values)?; - if id < ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))? { - ((::std::sync::Arc::clone(&shared_values[(::core::primitive::usize::try_from(id)).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?])).downcast()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, "referenced value has wrong type"))? - } else { - let decoded_value = ::std::sync::Arc::new(crate::data::transient_0::decode_struct_with_int_32(reader, shared_values)?); - shared_values.push(::std::sync::Arc::clone(&decoded_value) as ::std::sync::Arc::); - decoded_value - } - }; - ::std::io::Result::Ok(crate::data::StructWithDifferentSharedTypes { a: a, b: b, c: c }) -} - -pub fn encode_struct_with_shared_sum_and_variant(value: &crate::data::StructWithSharedSumAndVariant, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { - { - let ptr = ::std::sync::Arc::as_ptr(&&value.a) as ::core::primitive::usize; - match (shared_values.get(&ptr)).copied() { - ::std::option::Option::Some(id) => { - crate::transient_0::encode_int_64(&id, writer, shared_values)?; - }, - ::std::option::Option::None => { - let id = ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?; - crate::transient_0::encode_int_64(&id, writer, shared_values)?; - shared_values.insert(ptr, id); - crate::data::transient_0::encode_sum_with_variants((&value.a).as_ref(), writer, shared_values)?; - }, - }; - }; - { - let ptr = ::std::sync::Arc::as_ptr(&&value.b) as ::core::primitive::usize; - match (shared_values.get(&ptr)).copied() { - ::std::option::Option::Some(id) => { - crate::transient_0::encode_int_64(&id, writer, shared_values)?; - }, - ::std::option::Option::None => { - let id = ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?; - crate::transient_0::encode_int_64(&id, writer, shared_values)?; - shared_values.insert(ptr, id); - crate::data::transient_0::encode_variant_one((&value.b).as_ref(), writer, shared_values)?; - }, - }; - }; - ::std::io::Result::Ok(()) -} - -pub fn decode_struct_with_shared_sum_and_variant(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { - let a = { - let id = crate::transient_0::decode_int_64(reader, shared_values)?; - if id < ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))? { - ((::std::sync::Arc::clone(&shared_values[(::core::primitive::usize::try_from(id)).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?])).downcast()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, "referenced value has wrong type"))? - } else { - let decoded_value = ::std::sync::Arc::new(crate::data::transient_0::decode_sum_with_variants(reader, shared_values)?); - shared_values.push(::std::sync::Arc::clone(&decoded_value) as ::std::sync::Arc::); - decoded_value - } - }; - let b = { - let id = crate::transient_0::decode_int_64(reader, shared_values)?; - if id < ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))? { - ((::std::sync::Arc::clone(&shared_values[(::core::primitive::usize::try_from(id)).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?])).downcast()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, "referenced value has wrong type"))? - } else { - let decoded_value = ::std::sync::Arc::new(crate::data::transient_0::decode_variant_one(reader, shared_values)?); - shared_values.push(::std::sync::Arc::clone(&decoded_value) as ::std::sync::Arc::); - decoded_value - } - }; - ::std::io::Result::Ok(crate::data::StructWithSharedSumAndVariant { a: a, b: b }) -} diff --git a/examples/14-transient-0/output/rust/src/gen/transient_0.rs b/examples/14-transient-0/output/rust/src/gen/transient_0.rs deleted file mode 100644 index 6505408..0000000 --- a/examples/14-transient-0/output/rust/src/gen/transient_0.rs +++ /dev/null @@ -1,84 +0,0 @@ -// Generated by hobgoblin. - -pub fn encode_bool(value: &::core::primitive::bool, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { - if *value { - writer.write_all(&1u8.to_le_bytes())?; - } else { - writer.write_all(&0u8.to_le_bytes())?; - }; - ::std::io::Result::Ok(()) -} - -pub fn decode_bool(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result::<::core::primitive::bool> { - let int_value = { - let bytes = { - let mut bytes = [0; 1]; - reader.read_exact(&mut bytes)?; - bytes - }; - u8::from_le_bytes(bytes) - }; - ::std::io::Result::Ok(if int_value == 0 { - false - } else { - true - }) -} - -pub fn encode_int_8(value: &::core::primitive::i8, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { - writer.write_all(&value.to_le_bytes())?; - ::std::io::Result::Ok(()) -} - -pub fn decode_int_8(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result::<::core::primitive::i8> { - let bytes = { - let mut bytes = [0; 1]; - reader.read_exact(&mut bytes)?; - bytes - }; - ::std::io::Result::Ok(::core::primitive::i8::from_le_bytes(bytes)) -} - -pub fn encode_int_32(value: &::core::primitive::i32, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { - writer.write_all(&value.to_le_bytes())?; - ::std::io::Result::Ok(()) -} - -pub fn decode_int_32(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result::<::core::primitive::i32> { - let bytes = { - let mut bytes = [0; 4]; - reader.read_exact(&mut bytes)?; - bytes - }; - ::std::io::Result::Ok(::core::primitive::i32::from_le_bytes(bytes)) -} - -pub fn encode_int_64(value: &::core::primitive::i64, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { - writer.write_all(&value.to_le_bytes())?; - ::std::io::Result::Ok(()) -} - -pub fn decode_int_64(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result::<::core::primitive::i64> { - let bytes = { - let mut bytes = [0; 8]; - reader.read_exact(&mut bytes)?; - bytes - }; - ::std::io::Result::Ok(::core::primitive::i64::from_le_bytes(bytes)) -} - -pub fn encode_string(value: &::std::string::String, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { - crate::transient_0::encode_int_64(&((value.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?, writer, shared_values)?; - writer.write_all(value.as_bytes())?; - ::std::io::Result::Ok(()) -} - -pub fn decode_string(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result::<::std::string::String> { - let len = crate::transient_0::decode_int_64(reader, shared_values)?; - let bytes = { - let mut bytes = vec![0u8; (len.try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?]; - reader.read_exact(&mut bytes)?; - bytes - }; - ::std::io::Result::Ok((::std::string::String::from_utf8(bytes)).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?) -} diff --git a/examples/14-transient-0/output/rust/src/lib.rs b/examples/14-transient-0/output/rust/src/lib.rs deleted file mode 100644 index f0f6e24..0000000 --- a/examples/14-transient-0/output/rust/src/lib.rs +++ /dev/null @@ -1,318 +0,0 @@ -pub mod data; -pub mod transient_0; - -#[cfg(test)] -mod test { - use std::any::Any; - use std::io::Cursor; - use std::path::PathBuf; - use std::sync::Arc; - use std::collections::HashMap; - use super::data::{EnumWithVariants, StructSingleton, StructWithBool, StructWithBox, StructWithDifferentSharedTypes, StructWithEnum, StructWithInt8, StructWithInt32, StructWithInt64, StructWithList, StructWithListOfShared, StructWithOption, StructWithSharedSumAndVariant, StructWithString, StructWithStruct, StructWithSum, SumWithBoxedVariants, SumWithVariants, VariantOne, VariantTwo }; - - #[test] - fn struct_singleton() { - let value = StructSingleton; - - assert_round_trip_encoding( - "StructSingleton", - value, - super::data::transient_0::encode_struct_singleton, - super::data::transient_0::decode_struct_singleton, - ); - } - - #[test] - fn struct_with_bool() { - let value = StructWithBool { a: true, b: false }; - - assert_round_trip_encoding( - "StructWithBool", - value, - super::data::transient_0::encode_struct_with_bool, - super::data::transient_0::decode_struct_with_bool, - ); - } - - #[test] - fn struct_with_int_8() { - let value = StructWithInt8 { a: 10, b: 25 }; - - assert_round_trip_encoding( - "StructWithInt8", - value, - super::data::transient_0::encode_struct_with_int_8, - super::data::transient_0::decode_struct_with_int_8, - ); - } - - #[test] - fn struct_with_int_32() { - let value = StructWithInt32 { a: 10, b: 25 }; - - assert_round_trip_encoding( - "StructWithInt32", - value, - super::data::transient_0::encode_struct_with_int_32, - super::data::transient_0::decode_struct_with_int_32, - ); - } - - #[test] - fn struct_with_int_64() { - let value = StructWithInt64 { a: 10, b: 25 }; - - assert_round_trip_encoding( - "StructWithInt64", - value, - super::data::transient_0::encode_struct_with_int_64, - super::data::transient_0::decode_struct_with_int_64, - ); - } - - #[test] - fn struct_with_string() { - let value = StructWithString { a: "abc".to_string(), b: "def".to_string() }; - - assert_round_trip_encoding( - "StructWithString", - value, - super::data::transient_0::encode_struct_with_string, - super::data::transient_0::decode_struct_with_string, - ); - } - - #[test] - fn struct_with_struct() { - let value = StructWithStruct { inner: StructWithInt32 { a: 10, b: 25 }, c: 42 }; - - assert_round_trip_encoding( - "StructWithStruct", - value, - super::data::transient_0::encode_struct_with_struct, - super::data::transient_0::decode_struct_with_struct, - ); - } - - #[test] - fn struct_with_list() { - let value = StructWithList { - a: vec![10, 25], - b: vec![ - StructWithInt32 { a: 42, b: 47 }, - StructWithInt32 { a: 52, b: 57 }, - StructWithInt32 { a: 62, b: 67 }, - ], - }; - - assert_round_trip_encoding( - "StructWithList", - value, - super::data::transient_0::encode_struct_with_list, - super::data::transient_0::decode_struct_with_list, - ); - } - - #[test] - fn struct_with_option_none() { - let value = StructWithOption { - a: None, - b: None, - }; - - assert_round_trip_encoding( - "StructWithOption_None", - value, - super::data::transient_0::encode_struct_with_option, - super::data::transient_0::decode_struct_with_option, - ); - } - - #[test] - fn struct_with_option_some() { - let value = StructWithOption { - a: Some(10), - b: Some(StructWithInt32 { a: 42, b: 47 }), - }; - - assert_round_trip_encoding( - "StructWithOption_Some", - value, - super::data::transient_0::encode_struct_with_option, - super::data::transient_0::decode_struct_with_option, - ); - } - - #[test] - fn enum_with_variants() { - assert_round_trip_encoding( - "EnumWithVariants_zero", - EnumWithVariants::Zero, - super::data::transient_0::encode_enum_with_variants, - super::data::transient_0::decode_enum_with_variants, - ); - - assert_round_trip_encoding( - "EnumWithVariants_one", - EnumWithVariants::One, - super::data::transient_0::encode_enum_with_variants, - super::data::transient_0::decode_enum_with_variants, - ); - - assert_round_trip_encoding( - "EnumWithVariants_two", - EnumWithVariants::Two, - super::data::transient_0::encode_enum_with_variants, - super::data::transient_0::decode_enum_with_variants, - ); - } - - #[test] - fn struct_with_enum() { - let value = StructWithEnum { inner: EnumWithVariants::One, c: 42 }; - - assert_round_trip_encoding( - "StructWithEnum", - value, - super::data::transient_0::encode_struct_with_enum, - super::data::transient_0::decode_struct_with_enum, - ); - } - - #[test] - fn sum_with_variants() { - assert_round_trip_encoding( - "SumWithVariants_VariantOne", - SumWithVariants::VariantOne(VariantOne { a: 10 }), - super::data::transient_0::encode_sum_with_variants, - super::data::transient_0::decode_sum_with_variants, - ); - - assert_round_trip_encoding( - "SumWithVariants_VariantTwo", - SumWithVariants::VariantTwo(VariantTwo { a: 25 }), - super::data::transient_0::encode_sum_with_variants, - super::data::transient_0::decode_sum_with_variants, - ); - } - - #[test] - fn sum_with_boxed_variants() { - assert_round_trip_encoding( - "SumWithBoxedVariants_VariantOne", - SumWithBoxedVariants::VariantOne(Box::new(VariantOne { a: 10 })), - super::data::transient_0::encode_sum_with_boxed_variants, - super::data::transient_0::decode_sum_with_boxed_variants, - ); - - assert_round_trip_encoding( - "SumWithBoxedVariants_VariantTwo", - SumWithBoxedVariants::VariantTwo(Box::new(VariantTwo { a: 25 })), - super::data::transient_0::encode_sum_with_boxed_variants, - super::data::transient_0::decode_sum_with_boxed_variants, - ); - } - - #[test] - fn struct_with_sum() { - let value = StructWithSum { - inner: SumWithVariants::VariantTwo(VariantTwo { a: 25 }), - b: 42, - }; - - assert_round_trip_encoding( - "StructWithSum", - value, - super::data::transient_0::encode_struct_with_sum, - super::data::transient_0::decode_struct_with_sum, - ); - } - - #[test] - fn struct_with_box() { - let value = StructWithBox { - inner: Box::new(StructWithInt32 { a: 10, b: 25 }), - }; - - assert_round_trip_encoding( - "StructWithBox", - value, - super::data::transient_0::encode_struct_with_box, - super::data::transient_0::decode_struct_with_box, - ); - } - - #[test] - fn struct_with_list_of_shared() { - let a = Arc::new(StructWithInt32 { a: 10, b: 25 }); - let b = Arc::new(StructWithInt32 { a: 42, b: 47 }); - - let value = StructWithListOfShared { - inner: vec![Arc::clone(&a), Arc::clone(&a), Arc::clone(&b), a, b], - }; - - let decoded_value = assert_round_trip_encoding( - "StructWithListOfShared", - value, - super::data::transient_0::encode_struct_with_list_of_shared, - super::data::transient_0::decode_struct_with_list_of_shared, - ); - assert!(Arc::ptr_eq(&decoded_value.inner[0], &decoded_value.inner[1])); - assert!(Arc::ptr_eq(&decoded_value.inner[0], &decoded_value.inner[3])); - assert!(Arc::ptr_eq(&decoded_value.inner[2], &decoded_value.inner[4])); - } - - #[test] - fn struct_with_different_shared_types() { - let a = Arc::new(StructWithInt32 { a: 10, b: 25 }); - let b = Arc::new(StructWithInt64 { a: 42, b: 47 }); - let c = Arc::new(StructWithInt32 { a: 52, b: 57 }); - - let value = StructWithDifferentSharedTypes { a, b, c }; - - assert_round_trip_encoding( - "StructWithDifferentSharedTypes", - value, - super::data::transient_0::encode_struct_with_different_shared_types, - super::data::transient_0::decode_struct_with_different_shared_types, - ); - } - - #[test] - fn struct_with_shared_sum_and_variant() { - let a = Arc::new(SumWithVariants::VariantOne(VariantOne { a: 10 })); - let b = Arc::new(VariantOne { a: 10 }); - - let value = StructWithSharedSumAndVariant { a, b }; - - assert_round_trip_encoding( - "StructWithSharedSumAndVariant", - value, - super::data::transient_0::encode_struct_with_shared_sum_and_variant, - super::data::transient_0::decode_struct_with_shared_sum_and_variant, - ); - } - - fn assert_round_trip_encoding( - name: &str, - value: T, - encode: impl Fn(&T, &mut Cursor>, &mut HashMap) -> std::io::Result<()>, - decode: impl Fn(&mut Cursor>, &mut Vec>) -> std::io::Result, - ) -> T { - let mut bytes = Cursor::new(Vec::new()); - encode(&value, &mut bytes, &mut HashMap::new()).unwrap(); - - let output_dir = std::env::var("HOBGOBLIN_OUTPUT_DIR").unwrap(); - let transient_0_output_dir = PathBuf::from(output_dir).join("transient-0"); - std::fs::create_dir_all(&transient_0_output_dir).unwrap(); - let transient_0_output_path = transient_0_output_dir.join(name); - std::fs::write(transient_0_output_path, bytes.get_ref()).unwrap(); - - bytes.set_position(0); - let decoded_value = decode(&mut bytes, &mut Vec::new()).unwrap(); - - assert_eq!(value, decoded_value); - - decoded_value - } -} diff --git a/examples/14-transient-0/output/rust/src/transient_0.rs b/examples/14-transient-0/output/rust/src/transient_0.rs deleted file mode 100644 index 8b2b800..0000000 --- a/examples/14-transient-0/output/rust/src/transient_0.rs +++ /dev/null @@ -1 +0,0 @@ -include!("./gen/transient_0.rs"); diff --git a/examples/14-transient-0/src/data.hob b/examples/14-transient-0/src/data.hob deleted file mode 100644 index fb53553..0000000 --- a/examples/14-transient-0/src/data.hob +++ /dev/null @@ -1,96 +0,0 @@ -struct StructSingleton { - singleton; -} - -struct StructWithBool { - field a: Bool; - field b: Bool; -} - -struct StructWithInt8 { - field a: Int8; - field b: Int8; -} - -struct StructWithInt32 { - field a: Int32; - field b: Int32; -} - -struct StructWithInt64 { - field a: Int64; - field b: Int64; -} - -struct StructWithString { - field a: String; - field b: String; -} - -struct StructWithStruct { - field inner: StructWithInt32; - field c: Int32; -} - -struct StructWithList { - field a: List[Int64]; - field b: List[StructWithInt32]; -} - -struct StructWithOption { - field a: Option[Int64]; - field b: Option[StructWithInt32]; -} - -enum EnumWithVariants { - variant zero; - variant one; - variant two; -} - -struct StructWithEnum { - field inner: EnumWithVariants; - field c: Int32; -} - -sum SumWithVariants { - variant VariantOne; - variant VariantTwo; -} - -struct VariantOne { - field a: Int32; -} - -struct VariantTwo { - field a: Int64; -} - -sum SumWithBoxedVariants { - variant Box[VariantOne]; - variant Box[VariantTwo]; -} - -struct StructWithSum { - field inner: SumWithVariants; - field b: Int32; -} - -struct StructWithBox { - field inner: Box[StructWithInt32]; -} - -struct StructWithListOfShared { - field inner: List[Shared[StructWithInt32]]; -} - -struct StructWithDifferentSharedTypes { - field a: Shared[StructWithInt32]; - field b: Shared[StructWithInt64]; - field c: Shared[StructWithInt32]; -} - -struct StructWithSharedSumAndVariant { - field a: Shared[SumWithVariants]; - field b: Shared[VariantOne]; -} diff --git a/examples/15-transient-0/hobgoblin.json5 b/examples/15-transient-0/hobgoblin.json5 new file mode 100644 index 0000000..2160c60 --- /dev/null +++ b/examples/15-transient-0/hobgoblin.json5 @@ -0,0 +1,27 @@ +{ + outputs: { + langs: { + java: { + packageName: "org.zwobble.example.types", + }, + }, + generators: [ + { + generator: "java-types", + path: "output/java/src/gen/java", + }, + { + generator: "java-transient-0", + path: "output/java/src/gen/java", + }, + { + generator: "rust-types", + path: "output/rust/src/gen", + }, + { + generator: "rust-transient-0", + path: "output/rust/src/gen", + } + ], + } +} diff --git a/examples/15-transient-0/output/java/.gitignore b/examples/15-transient-0/output/java/.gitignore new file mode 100644 index 0000000..2fcf0b8 --- /dev/null +++ b/examples/15-transient-0/output/java/.gitignore @@ -0,0 +1,3 @@ +/src/gen/ +/target/ +/transient-0/ diff --git a/examples/15-transient-0/output/java/pom.xml b/examples/15-transient-0/output/java/pom.xml new file mode 120000 index 0000000..445fa65 --- /dev/null +++ b/examples/15-transient-0/output/java/pom.xml @@ -0,0 +1 @@ +../../../templates/java-junit/pom.xml \ No newline at end of file diff --git a/examples/15-transient-0/output/java/src/test/java/org/zwobble/example/Transient0Tests.java b/examples/15-transient-0/output/java/src/test/java/org/zwobble/example/Transient0Tests.java new file mode 100644 index 0000000..6732f72 --- /dev/null +++ b/examples/15-transient-0/output/java/src/test/java/org/zwobble/example/Transient0Tests.java @@ -0,0 +1,353 @@ +package org.zwobble.example; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.zwobble.example.types.data.EnumWithVariants; +import org.zwobble.example.types.data.StructSingleton; +import org.zwobble.example.types.data.StructWithBool; +import org.zwobble.example.types.data.StructWithBox; +import org.zwobble.example.types.data.StructWithDifferentSharedTypes; +import org.zwobble.example.types.data.StructWithEnum; +import org.zwobble.example.types.data.StructWithInt8; +import org.zwobble.example.types.data.StructWithInt32; +import org.zwobble.example.types.data.StructWithInt64; +import org.zwobble.example.types.data.StructWithList; +import org.zwobble.example.types.data.StructWithListOfShared; +import org.zwobble.example.types.data.StructWithOption; +import org.zwobble.example.types.data.StructWithSharedSumAndVariant; +import org.zwobble.example.types.data.StructWithString; +import org.zwobble.example.types.data.StructWithStruct; +import org.zwobble.example.types.data.StructWithSum; +import org.zwobble.example.types.data.VariantOne; +import org.zwobble.example.types.data.VariantTwo; +import org.zwobble.example.types.data.transient0.HobgoblinTransient0Data; +import org.zwobble.example.types.transient0.HobgoblinTransient0TaggedSharedValue; + +public class Transient0Tests { + @Test + public void structSingleton() throws IOException { + var value = StructSingleton.INSTANCE; + + assertRoundTripEncoding( + "StructSingleton", + value, + HobgoblinTransient0Data::encodeStructSingleton, + HobgoblinTransient0Data::decodeStructSingleton + ); + } + + @Test + public void structWithBool() throws IOException { + var value = new StructWithBool(true, false); + + assertRoundTripEncoding( + "StructWithBool", + value, + HobgoblinTransient0Data::encodeStructWithBool, + HobgoblinTransient0Data::decodeStructWithBool + ); + } + + @Test + public void structWithInt8() throws IOException { + var value = new StructWithInt8((byte)10, (byte)25); + + assertRoundTripEncoding( + "StructWithInt8", + value, + HobgoblinTransient0Data::encodeStructWithInt8, + HobgoblinTransient0Data::decodeStructWithInt8 + ); + } + + @Test + public void structWithInt32() throws IOException { + var value = new StructWithInt32(10, 25); + + assertRoundTripEncoding( + "StructWithInt32", + value, + HobgoblinTransient0Data::encodeStructWithInt32, + HobgoblinTransient0Data::decodeStructWithInt32 + ); + } + + @Test + public void structWithInt64() throws IOException { + var value = new StructWithInt64(10, 25); + + assertRoundTripEncoding( + "StructWithInt64", + value, + HobgoblinTransient0Data::encodeStructWithInt64, + HobgoblinTransient0Data::decodeStructWithInt64 + ); + } + + @Test + public void structWithString() throws IOException { + var value = new StructWithString("abc", "def"); + + assertRoundTripEncoding( + "StructWithString", + value, + HobgoblinTransient0Data::encodeStructWithString, + HobgoblinTransient0Data::decodeStructWithString + ); + } + + @Test + public void structWithStruct() throws IOException { + var value = new StructWithStruct(new StructWithInt32(10, 25), 42); + + assertRoundTripEncoding( + "StructWithStruct", + value, + HobgoblinTransient0Data::encodeStructWithStruct, + HobgoblinTransient0Data::decodeStructWithStruct + ); + } + + @Test + public void structWithList() throws IOException { + var value = new StructWithList( + List.of(10L, 25L), + List.of( + new StructWithInt32(42, 47), + new StructWithInt32(52, 57), + new StructWithInt32(62, 67) + ) + ); + + assertRoundTripEncoding( + "StructWithList", + value, + HobgoblinTransient0Data::encodeStructWithList, + HobgoblinTransient0Data::decodeStructWithList + ); + } + + @Test + public void structWithOptionNone() throws IOException { + var value = new StructWithOption( + Optional.empty(), + Optional.empty() + ); + + assertRoundTripEncoding( + "StructWithOption_None", + value, + HobgoblinTransient0Data::encodeStructWithOption, + HobgoblinTransient0Data::decodeStructWithOption + ); + } + + @Test + public void structWithOptionSome() throws IOException { + var value = new StructWithOption( + Optional.of(10L), + Optional.of(new StructWithInt32(42, 47)) + ); + + assertRoundTripEncoding( + "StructWithOption_Some", + value, + HobgoblinTransient0Data::encodeStructWithOption, + HobgoblinTransient0Data::decodeStructWithOption + ); + } + + @Test + public void enumWithVariants() throws IOException { + assertRoundTripEncoding( + "EnumWithVariants_zero", + EnumWithVariants.ZERO, + HobgoblinTransient0Data::encodeEnumWithVariants, + HobgoblinTransient0Data::decodeEnumWithVariants + ); + + assertRoundTripEncoding( + "EnumWithVariants_one", + EnumWithVariants.ONE, + HobgoblinTransient0Data::encodeEnumWithVariants, + HobgoblinTransient0Data::decodeEnumWithVariants + ); + + assertRoundTripEncoding( + "EnumWithVariants_two", + EnumWithVariants.TWO, + HobgoblinTransient0Data::encodeEnumWithVariants, + HobgoblinTransient0Data::decodeEnumWithVariants + ); + } + + @Test + public void structWithEnum() throws IOException { + var value = new StructWithEnum( + EnumWithVariants.ONE, + 42 + ); + + assertRoundTripEncoding( + "StructWithEnum", + value, + HobgoblinTransient0Data::encodeStructWithEnum, + HobgoblinTransient0Data::decodeStructWithEnum + ); + } + + @Test + public void sumWithVariants() throws IOException { + assertRoundTripEncoding( + "SumWithVariants_VariantOne", + new VariantOne(10), + HobgoblinTransient0Data::encodeSumWithVariants, + HobgoblinTransient0Data::decodeSumWithVariants + ); + + assertRoundTripEncoding( + "SumWithVariants_VariantTwo", + new VariantTwo(25), + HobgoblinTransient0Data::encodeSumWithVariants, + HobgoblinTransient0Data::decodeSumWithVariants + ); + } + + @Test + public void sumWithBoxedVariants() throws IOException { + assertRoundTripEncoding( + "SumWithBoxedVariants_VariantOne", + new VariantOne(10), + HobgoblinTransient0Data::encodeSumWithBoxedVariants, + HobgoblinTransient0Data::decodeSumWithBoxedVariants + ); + + assertRoundTripEncoding( + "SumWithBoxedVariants_VariantTwo", + new VariantTwo(25), + HobgoblinTransient0Data::encodeSumWithBoxedVariants, + HobgoblinTransient0Data::decodeSumWithBoxedVariants + ); + } + + @Test + public void structWithSum() throws IOException { + var value = new StructWithSum( + new VariantTwo(25), + 42 + ); + + assertRoundTripEncoding( + "StructWithSum", + value, + HobgoblinTransient0Data::encodeStructWithSum, + HobgoblinTransient0Data::decodeStructWithSum + ); + } + + @Test + public void structWithBox() throws IOException { + var value = new StructWithBox( + new StructWithInt32(10, 25) + ); + + assertRoundTripEncoding( + "StructWithBox", + value, + HobgoblinTransient0Data::encodeStructWithBox, + HobgoblinTransient0Data::decodeStructWithBox + ); + } + + @Test + public void structWithListOfShared() throws IOException { + var a = new StructWithInt32(10, 25); + var b = new StructWithInt32(42, 47); + + var value = new StructWithListOfShared(List.of(a, a, b, a, b)); + + var decodedValue = assertRoundTripEncoding( + "StructWithListOfShared", + value, + HobgoblinTransient0Data::encodeStructWithListOfShared, + HobgoblinTransient0Data::decodeStructWithListOfShared + ); + Assertions.assertSame(decodedValue.inner().get(0), decodedValue.inner().get(1)); + Assertions.assertSame(decodedValue.inner().get(0), decodedValue.inner().get(3)); + Assertions.assertSame(decodedValue.inner().get(2), decodedValue.inner().get(4)); + } + + @Test + public void structWithDifferentSharedTypes() throws IOException { + var a = new StructWithInt32(10, 25); + var b = new StructWithInt64(42, 47); + var c = new StructWithInt32(52, 57); + + var value = new StructWithDifferentSharedTypes(a, b, c); + + assertRoundTripEncoding( + "StructWithDifferentSharedTypes", + value, + HobgoblinTransient0Data::encodeStructWithDifferentSharedTypes, + HobgoblinTransient0Data::decodeStructWithDifferentSharedTypes + ); + } + + @Test + public void structWithSharedSumAndVariant() throws IOException { + var a = new VariantOne(10); + + var value = new StructWithSharedSumAndVariant(a, a); + + assertRoundTripEncoding( + "StructWithSharedSumAndVariant", + value, + HobgoblinTransient0Data::encodeStructWithSharedSumAndVariant, + HobgoblinTransient0Data::decodeStructWithSharedSumAndVariant + ); + } + + private T assertRoundTripEncoding( + String name, + T value, + Encoder encoder, + Decoder decoder + ) throws IOException { + var outputStream = new ByteArrayOutputStream(); + encoder.encode(value, outputStream, new HashMap<>()); + var bytes = outputStream.toByteArray(); + + var outputDir = System.getenv("HOBGOBLIN_OUTPUT_DIR"); + var transient0OutputDir = Path.of(outputDir).resolve("transient-0"); + Files.createDirectories(transient0OutputDir); + var transient0OutputPath = transient0OutputDir.resolve(name); + Files.write(transient0OutputPath, bytes); + + var inputStream = new ByteArrayInputStream(bytes); + var decodedValue = decoder.decode(inputStream, new ArrayList<>()); + + Assertions.assertEquals(value, decodedValue); + + return decodedValue; + } + + private interface Encoder { + void encode(T value, OutputStream outputStream, Map sharedValues) throws IOException; + } + + private interface Decoder { + T decode(InputStream inputStream, List sharedValues) throws IOException; + } +} diff --git a/examples/15-transient-0/output/rust/.gitignore b/examples/15-transient-0/output/rust/.gitignore new file mode 100644 index 0000000..fc03479 --- /dev/null +++ b/examples/15-transient-0/output/rust/.gitignore @@ -0,0 +1,2 @@ +/target +/transient-0/ diff --git a/examples/15-transient-0/output/rust/Cargo.lock b/examples/15-transient-0/output/rust/Cargo.lock new file mode 100644 index 0000000..9dc6e3e --- /dev/null +++ b/examples/15-transient-0/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/15-transient-0/output/rust/Cargo.toml b/examples/15-transient-0/output/rust/Cargo.toml new file mode 100644 index 0000000..5d4c622 --- /dev/null +++ b/examples/15-transient-0/output/rust/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "example" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/examples/15-transient-0/output/rust/rust-toolchain.toml b/examples/15-transient-0/output/rust/rust-toolchain.toml new file mode 100644 index 0000000..4fedb95 --- /dev/null +++ b/examples/15-transient-0/output/rust/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "1.96" diff --git a/examples/15-transient-0/output/rust/src/data.rs b/examples/15-transient-0/output/rust/src/data.rs new file mode 100644 index 0000000..7a0c4fa --- /dev/null +++ b/examples/15-transient-0/output/rust/src/data.rs @@ -0,0 +1,5 @@ +include!("./gen/data.rs"); + +pub mod transient_0 { + include!("./gen/data/transient_0.rs"); +} diff --git a/examples/15-transient-0/output/rust/src/gen/data.rs b/examples/15-transient-0/output/rust/src/gen/data.rs new file mode 100644 index 0000000..d4847ec --- /dev/null +++ b/examples/15-transient-0/output/rust/src/gen/data.rs @@ -0,0 +1,140 @@ +// Generated by hobgoblin. + +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct StructSingleton; + +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct StructWithBool { + pub a: ::core::primitive::bool, + pub b: ::core::primitive::bool, +} + +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct StructWithInt8 { + pub a: ::core::primitive::i8, + pub b: ::core::primitive::i8, +} + +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct StructWithInt32 { + pub a: ::core::primitive::i32, + pub b: ::core::primitive::i32, +} + +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct StructWithInt64 { + pub a: ::core::primitive::i64, + pub b: ::core::primitive::i64, +} + +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct StructWithString { + pub a: ::std::string::String, + pub b: ::std::string::String, +} + +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct StructWithStruct { + pub inner: crate::data::StructWithInt32, + pub c: ::core::primitive::i32, +} + +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct StructWithList { + pub a: ::std::vec::Vec::<::core::primitive::i64>, + pub b: ::std::vec::Vec::, +} + +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct StructWithOption { + pub a: ::std::option::Option::<::core::primitive::i64>, + pub b: ::std::option::Option::, +} + +#[derive(Clone, Copy, Debug, Hash, PartialEq)] +pub enum EnumWithVariants { + Zero, + One, + Two, +} + +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct StructWithEnum { + pub inner: crate::data::EnumWithVariants, + pub c: ::core::primitive::i32, +} + +#[derive(Clone, Debug, Hash, PartialEq)] +pub enum SumWithVariants { + VariantOne(crate::data::VariantOne), + VariantTwo(crate::data::VariantTwo), +} + +impl ::std::convert::From:: for crate::data::SumWithVariants { + fn from(value: crate::data::VariantOne) -> Self { + Self::VariantOne(value) + } +} + +impl ::std::convert::From:: for crate::data::SumWithVariants { + fn from(value: crate::data::VariantTwo) -> Self { + Self::VariantTwo(value) + } +} + +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct VariantOne { + pub a: ::core::primitive::i32, +} + +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct VariantTwo { + pub a: ::core::primitive::i64, +} + +#[derive(Clone, Debug, Hash, PartialEq)] +pub enum SumWithBoxedVariants { + VariantOne(::std::boxed::Box::), + VariantTwo(::std::boxed::Box::), +} + +impl ::std::convert::From:: for crate::data::SumWithBoxedVariants { + fn from(value: crate::data::VariantOne) -> Self { + Self::VariantOne(::std::boxed::Box::new(value)) + } +} + +impl ::std::convert::From:: for crate::data::SumWithBoxedVariants { + fn from(value: crate::data::VariantTwo) -> Self { + Self::VariantTwo(::std::boxed::Box::new(value)) + } +} + +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct StructWithSum { + pub inner: crate::data::SumWithVariants, + pub b: ::core::primitive::i32, +} + +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct StructWithBox { + pub inner: ::std::boxed::Box::, +} + +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct StructWithListOfShared { + pub inner: ::std::vec::Vec::<::std::sync::Arc::>, +} + +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct StructWithDifferentSharedTypes { + pub a: ::std::sync::Arc::, + pub b: ::std::sync::Arc::, + pub c: ::std::sync::Arc::, +} + +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct StructWithSharedSumAndVariant { + pub a: ::std::sync::Arc::, + pub b: ::std::sync::Arc::, +} diff --git a/examples/15-transient-0/output/rust/src/gen/data/transient_0.rs b/examples/15-transient-0/output/rust/src/gen/data/transient_0.rs new file mode 100644 index 0000000..f802163 --- /dev/null +++ b/examples/15-transient-0/output/rust/src/gen/data/transient_0.rs @@ -0,0 +1,455 @@ +// Generated by hobgoblin. + +pub fn encode_struct_singleton(value: &crate::data::StructSingleton, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + ::std::io::Result::Ok(()) +} + +pub fn decode_struct_singleton(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { + ::std::io::Result::Ok(crate::data::StructSingleton) +} + +pub fn encode_struct_with_bool(value: &crate::data::StructWithBool, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + crate::transient_0::encode_bool(&value.a, writer, shared_values)?; + crate::transient_0::encode_bool(&value.b, writer, shared_values)?; + ::std::io::Result::Ok(()) +} + +pub fn decode_struct_with_bool(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { + let a = crate::transient_0::decode_bool(reader, shared_values)?; + let b = crate::transient_0::decode_bool(reader, shared_values)?; + ::std::io::Result::Ok(crate::data::StructWithBool { a: a, b: b }) +} + +pub fn encode_struct_with_int_8(value: &crate::data::StructWithInt8, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + crate::transient_0::encode_int_8(&value.a, writer, shared_values)?; + crate::transient_0::encode_int_8(&value.b, writer, shared_values)?; + ::std::io::Result::Ok(()) +} + +pub fn decode_struct_with_int_8(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { + let a = crate::transient_0::decode_int_8(reader, shared_values)?; + let b = crate::transient_0::decode_int_8(reader, shared_values)?; + ::std::io::Result::Ok(crate::data::StructWithInt8 { a: a, b: b }) +} + +pub fn encode_struct_with_int_32(value: &crate::data::StructWithInt32, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + crate::transient_0::encode_int_32(&value.a, writer, shared_values)?; + crate::transient_0::encode_int_32(&value.b, writer, shared_values)?; + ::std::io::Result::Ok(()) +} + +pub fn decode_struct_with_int_32(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { + let a = crate::transient_0::decode_int_32(reader, shared_values)?; + let b = crate::transient_0::decode_int_32(reader, shared_values)?; + ::std::io::Result::Ok(crate::data::StructWithInt32 { a: a, b: b }) +} + +pub fn encode_struct_with_int_64(value: &crate::data::StructWithInt64, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + crate::transient_0::encode_int_64(&value.a, writer, shared_values)?; + crate::transient_0::encode_int_64(&value.b, writer, shared_values)?; + ::std::io::Result::Ok(()) +} + +pub fn decode_struct_with_int_64(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { + let a = crate::transient_0::decode_int_64(reader, shared_values)?; + let b = crate::transient_0::decode_int_64(reader, shared_values)?; + ::std::io::Result::Ok(crate::data::StructWithInt64 { a: a, b: b }) +} + +pub fn encode_struct_with_string(value: &crate::data::StructWithString, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + crate::transient_0::encode_string(&value.a, writer, shared_values)?; + crate::transient_0::encode_string(&value.b, writer, shared_values)?; + ::std::io::Result::Ok(()) +} + +pub fn decode_struct_with_string(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { + let a = crate::transient_0::decode_string(reader, shared_values)?; + let b = crate::transient_0::decode_string(reader, shared_values)?; + ::std::io::Result::Ok(crate::data::StructWithString { a: a, b: b }) +} + +pub fn encode_struct_with_struct(value: &crate::data::StructWithStruct, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + crate::data::transient_0::encode_struct_with_int_32(&value.inner, writer, shared_values)?; + crate::transient_0::encode_int_32(&value.c, writer, shared_values)?; + ::std::io::Result::Ok(()) +} + +pub fn decode_struct_with_struct(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { + let inner = crate::data::transient_0::decode_struct_with_int_32(reader, shared_values)?; + let c = crate::transient_0::decode_int_32(reader, shared_values)?; + ::std::io::Result::Ok(crate::data::StructWithStruct { inner: inner, c: c }) +} + +pub fn encode_struct_with_list(value: &crate::data::StructWithList, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + { + crate::transient_0::encode_int_64(&(((&value.a).len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?, writer, shared_values)?; + for element in &value.a { + crate::transient_0::encode_int_64(element, writer, shared_values)?; + }; + }; + { + crate::transient_0::encode_int_64(&(((&value.b).len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?, writer, shared_values)?; + for element in &value.b { + crate::data::transient_0::encode_struct_with_int_32(element, writer, shared_values)?; + }; + }; + ::std::io::Result::Ok(()) +} + +pub fn decode_struct_with_list(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { + let a = { + let len = crate::transient_0::decode_int_64(reader, shared_values)?; + let mut elements = ::std::vec::Vec::with_capacity((len.try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?); + for _ in 0..len { + elements.push(crate::transient_0::decode_int_64(reader, shared_values)?); + }; + elements + }; + let b = { + let len = crate::transient_0::decode_int_64(reader, shared_values)?; + let mut elements = ::std::vec::Vec::with_capacity((len.try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?); + for _ in 0..len { + elements.push(crate::data::transient_0::decode_struct_with_int_32(reader, shared_values)?); + }; + elements + }; + ::std::io::Result::Ok(crate::data::StructWithList { a: a, b: b }) +} + +pub fn encode_struct_with_option(value: &crate::data::StructWithOption, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + match &value.a { + ::std::option::Option::Some(value) => { + crate::transient_0::encode_bool(&true, writer, shared_values)?; + crate::transient_0::encode_int_64(&value, writer, shared_values)?; + }, + ::std::option::Option::None => { + crate::transient_0::encode_bool(&false, writer, shared_values)?; + }, + }; + match &value.b { + ::std::option::Option::Some(value) => { + crate::transient_0::encode_bool(&true, writer, shared_values)?; + crate::data::transient_0::encode_struct_with_int_32(&value, writer, shared_values)?; + }, + ::std::option::Option::None => { + crate::transient_0::encode_bool(&false, writer, shared_values)?; + }, + }; + ::std::io::Result::Ok(()) +} + +pub fn decode_struct_with_option(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { + let a = { + let is_some = crate::transient_0::decode_bool(reader, shared_values)?; + if is_some { + ::std::option::Option::Some(crate::transient_0::decode_int_64(reader, shared_values)?) + } else { + ::std::option::Option::None + } + }; + let b = { + let is_some = crate::transient_0::decode_bool(reader, shared_values)?; + if is_some { + ::std::option::Option::Some(crate::data::transient_0::decode_struct_with_int_32(reader, shared_values)?) + } else { + ::std::option::Option::None + } + }; + ::std::io::Result::Ok(crate::data::StructWithOption { a: a, b: b }) +} + +pub fn encode_enum_with_variants(value: &crate::data::EnumWithVariants, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + crate::transient_0::encode_int_32(&match value { + crate::data::EnumWithVariants::Zero => 0, + crate::data::EnumWithVariants::One => 1, + crate::data::EnumWithVariants::Two => 2, + }, writer, shared_values)?; + ::std::io::Result::Ok(()) +} + +pub fn decode_enum_with_variants(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { + ::std::io::Result::Ok(match &crate::transient_0::decode_int_32(reader, shared_values)? { + 0 => crate::data::EnumWithVariants::Zero, + 1 => crate::data::EnumWithVariants::One, + 2 => crate::data::EnumWithVariants::Two, + _ => ::std::result::Result::Err(::std::io::Error::new(::std::io::ErrorKind::InvalidData, "unrecognised variant tag for data.EnumWithVariants"))?, + }) +} + +pub fn encode_struct_with_enum(value: &crate::data::StructWithEnum, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + crate::data::transient_0::encode_enum_with_variants(&value.inner, writer, shared_values)?; + crate::transient_0::encode_int_32(&value.c, writer, shared_values)?; + ::std::io::Result::Ok(()) +} + +pub fn decode_struct_with_enum(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { + let inner = crate::data::transient_0::decode_enum_with_variants(reader, shared_values)?; + let c = crate::transient_0::decode_int_32(reader, shared_values)?; + ::std::io::Result::Ok(crate::data::StructWithEnum { inner: inner, c: c }) +} + +pub fn encode_sum_with_variants(value: &crate::data::SumWithVariants, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + match value { + crate::data::SumWithVariants::VariantOne(value) => { + crate::transient_0::encode_int_32(&0, writer, shared_values)?; + crate::data::transient_0::encode_variant_one(value, writer, shared_values)?; + }, + crate::data::SumWithVariants::VariantTwo(value) => { + crate::transient_0::encode_int_32(&1, writer, shared_values)?; + crate::data::transient_0::encode_variant_two(value, writer, shared_values)?; + }, + }; + ::std::io::Result::Ok(()) +} + +pub fn decode_sum_with_variants(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { + ::std::io::Result::Ok(match &crate::transient_0::decode_int_32(reader, shared_values)? { + 0 => crate::data::SumWithVariants::VariantOne(crate::data::transient_0::decode_variant_one(reader, shared_values)?), + 1 => crate::data::SumWithVariants::VariantTwo(crate::data::transient_0::decode_variant_two(reader, shared_values)?), + _ => ::std::result::Result::Err(::std::io::Error::new(::std::io::ErrorKind::InvalidData, "unrecognised variant tag for data.SumWithVariants"))?, + }) +} + +pub fn encode_variant_one(value: &crate::data::VariantOne, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + crate::transient_0::encode_int_32(&value.a, writer, shared_values)?; + ::std::io::Result::Ok(()) +} + +pub fn decode_variant_one(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { + let a = crate::transient_0::decode_int_32(reader, shared_values)?; + ::std::io::Result::Ok(crate::data::VariantOne { a: a }) +} + +pub fn encode_variant_two(value: &crate::data::VariantTwo, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + crate::transient_0::encode_int_64(&value.a, writer, shared_values)?; + ::std::io::Result::Ok(()) +} + +pub fn decode_variant_two(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { + let a = crate::transient_0::decode_int_64(reader, shared_values)?; + ::std::io::Result::Ok(crate::data::VariantTwo { a: a }) +} + +pub fn encode_sum_with_boxed_variants(value: &crate::data::SumWithBoxedVariants, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + match value { + crate::data::SumWithBoxedVariants::VariantOne(value) => { + crate::transient_0::encode_int_32(&0, writer, shared_values)?; + crate::data::transient_0::encode_variant_one(value.as_ref(), writer, shared_values)?; + }, + crate::data::SumWithBoxedVariants::VariantTwo(value) => { + crate::transient_0::encode_int_32(&1, writer, shared_values)?; + crate::data::transient_0::encode_variant_two(value.as_ref(), writer, shared_values)?; + }, + }; + ::std::io::Result::Ok(()) +} + +pub fn decode_sum_with_boxed_variants(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { + ::std::io::Result::Ok(match &crate::transient_0::decode_int_32(reader, shared_values)? { + 0 => crate::data::SumWithBoxedVariants::VariantOne(::std::boxed::Box::new(crate::data::transient_0::decode_variant_one(reader, shared_values)?)), + 1 => crate::data::SumWithBoxedVariants::VariantTwo(::std::boxed::Box::new(crate::data::transient_0::decode_variant_two(reader, shared_values)?)), + _ => ::std::result::Result::Err(::std::io::Error::new(::std::io::ErrorKind::InvalidData, "unrecognised variant tag for data.SumWithBoxedVariants"))?, + }) +} + +pub fn encode_struct_with_sum(value: &crate::data::StructWithSum, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + crate::data::transient_0::encode_sum_with_variants(&value.inner, writer, shared_values)?; + crate::transient_0::encode_int_32(&value.b, writer, shared_values)?; + ::std::io::Result::Ok(()) +} + +pub fn decode_struct_with_sum(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { + let inner = crate::data::transient_0::decode_sum_with_variants(reader, shared_values)?; + let b = crate::transient_0::decode_int_32(reader, shared_values)?; + ::std::io::Result::Ok(crate::data::StructWithSum { inner: inner, b: b }) +} + +pub fn encode_struct_with_box(value: &crate::data::StructWithBox, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + crate::data::transient_0::encode_struct_with_int_32((&value.inner).as_ref(), writer, shared_values)?; + ::std::io::Result::Ok(()) +} + +pub fn decode_struct_with_box(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { + let inner = ::std::boxed::Box::new(crate::data::transient_0::decode_struct_with_int_32(reader, shared_values)?); + ::std::io::Result::Ok(crate::data::StructWithBox { inner: inner }) +} + +pub fn encode_struct_with_list_of_shared(value: &crate::data::StructWithListOfShared, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + { + crate::transient_0::encode_int_64(&(((&value.inner).len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?, writer, shared_values)?; + for element in &value.inner { + { + let ptr = ::std::sync::Arc::as_ptr(&element) as ::core::primitive::usize; + match (shared_values.get(&ptr)).copied() { + ::std::option::Option::Some(id) => { + crate::transient_0::encode_int_64(&id, writer, shared_values)?; + }, + ::std::option::Option::None => { + let id = ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?; + crate::transient_0::encode_int_64(&id, writer, shared_values)?; + shared_values.insert(ptr, id); + crate::data::transient_0::encode_struct_with_int_32(element.as_ref(), writer, shared_values)?; + }, + }; + }; + }; + }; + ::std::io::Result::Ok(()) +} + +pub fn decode_struct_with_list_of_shared(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { + let inner = { + let len = crate::transient_0::decode_int_64(reader, shared_values)?; + let mut elements = ::std::vec::Vec::with_capacity((len.try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?); + for _ in 0..len { + elements.push({ + let id = crate::transient_0::decode_int_64(reader, shared_values)?; + if id < ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))? { + ((::std::sync::Arc::clone(&shared_values[(::core::primitive::usize::try_from(id)).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?])).downcast()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, "referenced value has wrong type"))? + } else { + let decoded_value = ::std::sync::Arc::new(crate::data::transient_0::decode_struct_with_int_32(reader, shared_values)?); + shared_values.push(::std::sync::Arc::clone(&decoded_value) as ::std::sync::Arc::); + decoded_value + } + }); + }; + elements + }; + ::std::io::Result::Ok(crate::data::StructWithListOfShared { inner: inner }) +} + +pub fn encode_struct_with_different_shared_types(value: &crate::data::StructWithDifferentSharedTypes, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + { + let ptr = ::std::sync::Arc::as_ptr(&&value.a) as ::core::primitive::usize; + match (shared_values.get(&ptr)).copied() { + ::std::option::Option::Some(id) => { + crate::transient_0::encode_int_64(&id, writer, shared_values)?; + }, + ::std::option::Option::None => { + let id = ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?; + crate::transient_0::encode_int_64(&id, writer, shared_values)?; + shared_values.insert(ptr, id); + crate::data::transient_0::encode_struct_with_int_32((&value.a).as_ref(), writer, shared_values)?; + }, + }; + }; + { + let ptr = ::std::sync::Arc::as_ptr(&&value.b) as ::core::primitive::usize; + match (shared_values.get(&ptr)).copied() { + ::std::option::Option::Some(id) => { + crate::transient_0::encode_int_64(&id, writer, shared_values)?; + }, + ::std::option::Option::None => { + let id = ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?; + crate::transient_0::encode_int_64(&id, writer, shared_values)?; + shared_values.insert(ptr, id); + crate::data::transient_0::encode_struct_with_int_64((&value.b).as_ref(), writer, shared_values)?; + }, + }; + }; + { + let ptr = ::std::sync::Arc::as_ptr(&&value.c) as ::core::primitive::usize; + match (shared_values.get(&ptr)).copied() { + ::std::option::Option::Some(id) => { + crate::transient_0::encode_int_64(&id, writer, shared_values)?; + }, + ::std::option::Option::None => { + let id = ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?; + crate::transient_0::encode_int_64(&id, writer, shared_values)?; + shared_values.insert(ptr, id); + crate::data::transient_0::encode_struct_with_int_32((&value.c).as_ref(), writer, shared_values)?; + }, + }; + }; + ::std::io::Result::Ok(()) +} + +pub fn decode_struct_with_different_shared_types(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { + let a = { + let id = crate::transient_0::decode_int_64(reader, shared_values)?; + if id < ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))? { + ((::std::sync::Arc::clone(&shared_values[(::core::primitive::usize::try_from(id)).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?])).downcast()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, "referenced value has wrong type"))? + } else { + let decoded_value = ::std::sync::Arc::new(crate::data::transient_0::decode_struct_with_int_32(reader, shared_values)?); + shared_values.push(::std::sync::Arc::clone(&decoded_value) as ::std::sync::Arc::); + decoded_value + } + }; + let b = { + let id = crate::transient_0::decode_int_64(reader, shared_values)?; + if id < ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))? { + ((::std::sync::Arc::clone(&shared_values[(::core::primitive::usize::try_from(id)).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?])).downcast()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, "referenced value has wrong type"))? + } else { + let decoded_value = ::std::sync::Arc::new(crate::data::transient_0::decode_struct_with_int_64(reader, shared_values)?); + shared_values.push(::std::sync::Arc::clone(&decoded_value) as ::std::sync::Arc::); + decoded_value + } + }; + let c = { + let id = crate::transient_0::decode_int_64(reader, shared_values)?; + if id < ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))? { + ((::std::sync::Arc::clone(&shared_values[(::core::primitive::usize::try_from(id)).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?])).downcast()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, "referenced value has wrong type"))? + } else { + let decoded_value = ::std::sync::Arc::new(crate::data::transient_0::decode_struct_with_int_32(reader, shared_values)?); + shared_values.push(::std::sync::Arc::clone(&decoded_value) as ::std::sync::Arc::); + decoded_value + } + }; + ::std::io::Result::Ok(crate::data::StructWithDifferentSharedTypes { a: a, b: b, c: c }) +} + +pub fn encode_struct_with_shared_sum_and_variant(value: &crate::data::StructWithSharedSumAndVariant, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + { + let ptr = ::std::sync::Arc::as_ptr(&&value.a) as ::core::primitive::usize; + match (shared_values.get(&ptr)).copied() { + ::std::option::Option::Some(id) => { + crate::transient_0::encode_int_64(&id, writer, shared_values)?; + }, + ::std::option::Option::None => { + let id = ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?; + crate::transient_0::encode_int_64(&id, writer, shared_values)?; + shared_values.insert(ptr, id); + crate::data::transient_0::encode_sum_with_variants((&value.a).as_ref(), writer, shared_values)?; + }, + }; + }; + { + let ptr = ::std::sync::Arc::as_ptr(&&value.b) as ::core::primitive::usize; + match (shared_values.get(&ptr)).copied() { + ::std::option::Option::Some(id) => { + crate::transient_0::encode_int_64(&id, writer, shared_values)?; + }, + ::std::option::Option::None => { + let id = ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?; + crate::transient_0::encode_int_64(&id, writer, shared_values)?; + shared_values.insert(ptr, id); + crate::data::transient_0::encode_variant_one((&value.b).as_ref(), writer, shared_values)?; + }, + }; + }; + ::std::io::Result::Ok(()) +} + +pub fn decode_struct_with_shared_sum_and_variant(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result:: { + let a = { + let id = crate::transient_0::decode_int_64(reader, shared_values)?; + if id < ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))? { + ((::std::sync::Arc::clone(&shared_values[(::core::primitive::usize::try_from(id)).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?])).downcast()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, "referenced value has wrong type"))? + } else { + let decoded_value = ::std::sync::Arc::new(crate::data::transient_0::decode_sum_with_variants(reader, shared_values)?); + shared_values.push(::std::sync::Arc::clone(&decoded_value) as ::std::sync::Arc::); + decoded_value + } + }; + let b = { + let id = crate::transient_0::decode_int_64(reader, shared_values)?; + if id < ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))? { + ((::std::sync::Arc::clone(&shared_values[(::core::primitive::usize::try_from(id)).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?])).downcast()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, "referenced value has wrong type"))? + } else { + let decoded_value = ::std::sync::Arc::new(crate::data::transient_0::decode_variant_one(reader, shared_values)?); + shared_values.push(::std::sync::Arc::clone(&decoded_value) as ::std::sync::Arc::); + decoded_value + } + }; + ::std::io::Result::Ok(crate::data::StructWithSharedSumAndVariant { a: a, b: b }) +} diff --git a/examples/15-transient-0/output/rust/src/gen/transient_0.rs b/examples/15-transient-0/output/rust/src/gen/transient_0.rs new file mode 100644 index 0000000..6505408 --- /dev/null +++ b/examples/15-transient-0/output/rust/src/gen/transient_0.rs @@ -0,0 +1,84 @@ +// Generated by hobgoblin. + +pub fn encode_bool(value: &::core::primitive::bool, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + if *value { + writer.write_all(&1u8.to_le_bytes())?; + } else { + writer.write_all(&0u8.to_le_bytes())?; + }; + ::std::io::Result::Ok(()) +} + +pub fn decode_bool(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result::<::core::primitive::bool> { + let int_value = { + let bytes = { + let mut bytes = [0; 1]; + reader.read_exact(&mut bytes)?; + bytes + }; + u8::from_le_bytes(bytes) + }; + ::std::io::Result::Ok(if int_value == 0 { + false + } else { + true + }) +} + +pub fn encode_int_8(value: &::core::primitive::i8, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + writer.write_all(&value.to_le_bytes())?; + ::std::io::Result::Ok(()) +} + +pub fn decode_int_8(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result::<::core::primitive::i8> { + let bytes = { + let mut bytes = [0; 1]; + reader.read_exact(&mut bytes)?; + bytes + }; + ::std::io::Result::Ok(::core::primitive::i8::from_le_bytes(bytes)) +} + +pub fn encode_int_32(value: &::core::primitive::i32, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + writer.write_all(&value.to_le_bytes())?; + ::std::io::Result::Ok(()) +} + +pub fn decode_int_32(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result::<::core::primitive::i32> { + let bytes = { + let mut bytes = [0; 4]; + reader.read_exact(&mut bytes)?; + bytes + }; + ::std::io::Result::Ok(::core::primitive::i32::from_le_bytes(bytes)) +} + +pub fn encode_int_64(value: &::core::primitive::i64, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + writer.write_all(&value.to_le_bytes())?; + ::std::io::Result::Ok(()) +} + +pub fn decode_int_64(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result::<::core::primitive::i64> { + let bytes = { + let mut bytes = [0; 8]; + reader.read_exact(&mut bytes)?; + bytes + }; + ::std::io::Result::Ok(::core::primitive::i64::from_le_bytes(bytes)) +} + +pub fn encode_string(value: &::std::string::String, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + crate::transient_0::encode_int_64(&((value.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?, writer, shared_values)?; + writer.write_all(value.as_bytes())?; + ::std::io::Result::Ok(()) +} + +pub fn decode_string(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::>) -> ::std::io::Result::<::std::string::String> { + let len = crate::transient_0::decode_int_64(reader, shared_values)?; + let bytes = { + let mut bytes = vec![0u8; (len.try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?]; + reader.read_exact(&mut bytes)?; + bytes + }; + ::std::io::Result::Ok((::std::string::String::from_utf8(bytes)).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?) +} diff --git a/examples/15-transient-0/output/rust/src/lib.rs b/examples/15-transient-0/output/rust/src/lib.rs new file mode 100644 index 0000000..f0f6e24 --- /dev/null +++ b/examples/15-transient-0/output/rust/src/lib.rs @@ -0,0 +1,318 @@ +pub mod data; +pub mod transient_0; + +#[cfg(test)] +mod test { + use std::any::Any; + use std::io::Cursor; + use std::path::PathBuf; + use std::sync::Arc; + use std::collections::HashMap; + use super::data::{EnumWithVariants, StructSingleton, StructWithBool, StructWithBox, StructWithDifferentSharedTypes, StructWithEnum, StructWithInt8, StructWithInt32, StructWithInt64, StructWithList, StructWithListOfShared, StructWithOption, StructWithSharedSumAndVariant, StructWithString, StructWithStruct, StructWithSum, SumWithBoxedVariants, SumWithVariants, VariantOne, VariantTwo }; + + #[test] + fn struct_singleton() { + let value = StructSingleton; + + assert_round_trip_encoding( + "StructSingleton", + value, + super::data::transient_0::encode_struct_singleton, + super::data::transient_0::decode_struct_singleton, + ); + } + + #[test] + fn struct_with_bool() { + let value = StructWithBool { a: true, b: false }; + + assert_round_trip_encoding( + "StructWithBool", + value, + super::data::transient_0::encode_struct_with_bool, + super::data::transient_0::decode_struct_with_bool, + ); + } + + #[test] + fn struct_with_int_8() { + let value = StructWithInt8 { a: 10, b: 25 }; + + assert_round_trip_encoding( + "StructWithInt8", + value, + super::data::transient_0::encode_struct_with_int_8, + super::data::transient_0::decode_struct_with_int_8, + ); + } + + #[test] + fn struct_with_int_32() { + let value = StructWithInt32 { a: 10, b: 25 }; + + assert_round_trip_encoding( + "StructWithInt32", + value, + super::data::transient_0::encode_struct_with_int_32, + super::data::transient_0::decode_struct_with_int_32, + ); + } + + #[test] + fn struct_with_int_64() { + let value = StructWithInt64 { a: 10, b: 25 }; + + assert_round_trip_encoding( + "StructWithInt64", + value, + super::data::transient_0::encode_struct_with_int_64, + super::data::transient_0::decode_struct_with_int_64, + ); + } + + #[test] + fn struct_with_string() { + let value = StructWithString { a: "abc".to_string(), b: "def".to_string() }; + + assert_round_trip_encoding( + "StructWithString", + value, + super::data::transient_0::encode_struct_with_string, + super::data::transient_0::decode_struct_with_string, + ); + } + + #[test] + fn struct_with_struct() { + let value = StructWithStruct { inner: StructWithInt32 { a: 10, b: 25 }, c: 42 }; + + assert_round_trip_encoding( + "StructWithStruct", + value, + super::data::transient_0::encode_struct_with_struct, + super::data::transient_0::decode_struct_with_struct, + ); + } + + #[test] + fn struct_with_list() { + let value = StructWithList { + a: vec![10, 25], + b: vec![ + StructWithInt32 { a: 42, b: 47 }, + StructWithInt32 { a: 52, b: 57 }, + StructWithInt32 { a: 62, b: 67 }, + ], + }; + + assert_round_trip_encoding( + "StructWithList", + value, + super::data::transient_0::encode_struct_with_list, + super::data::transient_0::decode_struct_with_list, + ); + } + + #[test] + fn struct_with_option_none() { + let value = StructWithOption { + a: None, + b: None, + }; + + assert_round_trip_encoding( + "StructWithOption_None", + value, + super::data::transient_0::encode_struct_with_option, + super::data::transient_0::decode_struct_with_option, + ); + } + + #[test] + fn struct_with_option_some() { + let value = StructWithOption { + a: Some(10), + b: Some(StructWithInt32 { a: 42, b: 47 }), + }; + + assert_round_trip_encoding( + "StructWithOption_Some", + value, + super::data::transient_0::encode_struct_with_option, + super::data::transient_0::decode_struct_with_option, + ); + } + + #[test] + fn enum_with_variants() { + assert_round_trip_encoding( + "EnumWithVariants_zero", + EnumWithVariants::Zero, + super::data::transient_0::encode_enum_with_variants, + super::data::transient_0::decode_enum_with_variants, + ); + + assert_round_trip_encoding( + "EnumWithVariants_one", + EnumWithVariants::One, + super::data::transient_0::encode_enum_with_variants, + super::data::transient_0::decode_enum_with_variants, + ); + + assert_round_trip_encoding( + "EnumWithVariants_two", + EnumWithVariants::Two, + super::data::transient_0::encode_enum_with_variants, + super::data::transient_0::decode_enum_with_variants, + ); + } + + #[test] + fn struct_with_enum() { + let value = StructWithEnum { inner: EnumWithVariants::One, c: 42 }; + + assert_round_trip_encoding( + "StructWithEnum", + value, + super::data::transient_0::encode_struct_with_enum, + super::data::transient_0::decode_struct_with_enum, + ); + } + + #[test] + fn sum_with_variants() { + assert_round_trip_encoding( + "SumWithVariants_VariantOne", + SumWithVariants::VariantOne(VariantOne { a: 10 }), + super::data::transient_0::encode_sum_with_variants, + super::data::transient_0::decode_sum_with_variants, + ); + + assert_round_trip_encoding( + "SumWithVariants_VariantTwo", + SumWithVariants::VariantTwo(VariantTwo { a: 25 }), + super::data::transient_0::encode_sum_with_variants, + super::data::transient_0::decode_sum_with_variants, + ); + } + + #[test] + fn sum_with_boxed_variants() { + assert_round_trip_encoding( + "SumWithBoxedVariants_VariantOne", + SumWithBoxedVariants::VariantOne(Box::new(VariantOne { a: 10 })), + super::data::transient_0::encode_sum_with_boxed_variants, + super::data::transient_0::decode_sum_with_boxed_variants, + ); + + assert_round_trip_encoding( + "SumWithBoxedVariants_VariantTwo", + SumWithBoxedVariants::VariantTwo(Box::new(VariantTwo { a: 25 })), + super::data::transient_0::encode_sum_with_boxed_variants, + super::data::transient_0::decode_sum_with_boxed_variants, + ); + } + + #[test] + fn struct_with_sum() { + let value = StructWithSum { + inner: SumWithVariants::VariantTwo(VariantTwo { a: 25 }), + b: 42, + }; + + assert_round_trip_encoding( + "StructWithSum", + value, + super::data::transient_0::encode_struct_with_sum, + super::data::transient_0::decode_struct_with_sum, + ); + } + + #[test] + fn struct_with_box() { + let value = StructWithBox { + inner: Box::new(StructWithInt32 { a: 10, b: 25 }), + }; + + assert_round_trip_encoding( + "StructWithBox", + value, + super::data::transient_0::encode_struct_with_box, + super::data::transient_0::decode_struct_with_box, + ); + } + + #[test] + fn struct_with_list_of_shared() { + let a = Arc::new(StructWithInt32 { a: 10, b: 25 }); + let b = Arc::new(StructWithInt32 { a: 42, b: 47 }); + + let value = StructWithListOfShared { + inner: vec![Arc::clone(&a), Arc::clone(&a), Arc::clone(&b), a, b], + }; + + let decoded_value = assert_round_trip_encoding( + "StructWithListOfShared", + value, + super::data::transient_0::encode_struct_with_list_of_shared, + super::data::transient_0::decode_struct_with_list_of_shared, + ); + assert!(Arc::ptr_eq(&decoded_value.inner[0], &decoded_value.inner[1])); + assert!(Arc::ptr_eq(&decoded_value.inner[0], &decoded_value.inner[3])); + assert!(Arc::ptr_eq(&decoded_value.inner[2], &decoded_value.inner[4])); + } + + #[test] + fn struct_with_different_shared_types() { + let a = Arc::new(StructWithInt32 { a: 10, b: 25 }); + let b = Arc::new(StructWithInt64 { a: 42, b: 47 }); + let c = Arc::new(StructWithInt32 { a: 52, b: 57 }); + + let value = StructWithDifferentSharedTypes { a, b, c }; + + assert_round_trip_encoding( + "StructWithDifferentSharedTypes", + value, + super::data::transient_0::encode_struct_with_different_shared_types, + super::data::transient_0::decode_struct_with_different_shared_types, + ); + } + + #[test] + fn struct_with_shared_sum_and_variant() { + let a = Arc::new(SumWithVariants::VariantOne(VariantOne { a: 10 })); + let b = Arc::new(VariantOne { a: 10 }); + + let value = StructWithSharedSumAndVariant { a, b }; + + assert_round_trip_encoding( + "StructWithSharedSumAndVariant", + value, + super::data::transient_0::encode_struct_with_shared_sum_and_variant, + super::data::transient_0::decode_struct_with_shared_sum_and_variant, + ); + } + + fn assert_round_trip_encoding( + name: &str, + value: T, + encode: impl Fn(&T, &mut Cursor>, &mut HashMap) -> std::io::Result<()>, + decode: impl Fn(&mut Cursor>, &mut Vec>) -> std::io::Result, + ) -> T { + let mut bytes = Cursor::new(Vec::new()); + encode(&value, &mut bytes, &mut HashMap::new()).unwrap(); + + let output_dir = std::env::var("HOBGOBLIN_OUTPUT_DIR").unwrap(); + let transient_0_output_dir = PathBuf::from(output_dir).join("transient-0"); + std::fs::create_dir_all(&transient_0_output_dir).unwrap(); + let transient_0_output_path = transient_0_output_dir.join(name); + std::fs::write(transient_0_output_path, bytes.get_ref()).unwrap(); + + bytes.set_position(0); + let decoded_value = decode(&mut bytes, &mut Vec::new()).unwrap(); + + assert_eq!(value, decoded_value); + + decoded_value + } +} diff --git a/examples/15-transient-0/output/rust/src/transient_0.rs b/examples/15-transient-0/output/rust/src/transient_0.rs new file mode 100644 index 0000000..8b2b800 --- /dev/null +++ b/examples/15-transient-0/output/rust/src/transient_0.rs @@ -0,0 +1 @@ +include!("./gen/transient_0.rs"); diff --git a/examples/15-transient-0/src/data.hob b/examples/15-transient-0/src/data.hob new file mode 100644 index 0000000..fb53553 --- /dev/null +++ b/examples/15-transient-0/src/data.hob @@ -0,0 +1,96 @@ +struct StructSingleton { + singleton; +} + +struct StructWithBool { + field a: Bool; + field b: Bool; +} + +struct StructWithInt8 { + field a: Int8; + field b: Int8; +} + +struct StructWithInt32 { + field a: Int32; + field b: Int32; +} + +struct StructWithInt64 { + field a: Int64; + field b: Int64; +} + +struct StructWithString { + field a: String; + field b: String; +} + +struct StructWithStruct { + field inner: StructWithInt32; + field c: Int32; +} + +struct StructWithList { + field a: List[Int64]; + field b: List[StructWithInt32]; +} + +struct StructWithOption { + field a: Option[Int64]; + field b: Option[StructWithInt32]; +} + +enum EnumWithVariants { + variant zero; + variant one; + variant two; +} + +struct StructWithEnum { + field inner: EnumWithVariants; + field c: Int32; +} + +sum SumWithVariants { + variant VariantOne; + variant VariantTwo; +} + +struct VariantOne { + field a: Int32; +} + +struct VariantTwo { + field a: Int64; +} + +sum SumWithBoxedVariants { + variant Box[VariantOne]; + variant Box[VariantTwo]; +} + +struct StructWithSum { + field inner: SumWithVariants; + field b: Int32; +} + +struct StructWithBox { + field inner: Box[StructWithInt32]; +} + +struct StructWithListOfShared { + field inner: List[Shared[StructWithInt32]]; +} + +struct StructWithDifferentSharedTypes { + field a: Shared[StructWithInt32]; + field b: Shared[StructWithInt64]; + field c: Shared[StructWithInt32]; +} + +struct StructWithSharedSumAndVariant { + field a: Shared[SumWithVariants]; + field b: Shared[VariantOne]; +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedNativeTypeDefinitionNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedNativeTypeDefinitionNode.java index e81705b..487a80b 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedNativeTypeDefinitionNode.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedNativeTypeDefinitionNode.java @@ -3,13 +3,14 @@ package org.zwobble.hobgoblin.compiler.ast.typed; import org.zwobble.hobgoblin.compiler.ast.DocComment; import org.zwobble.hobgoblin.compiler.sources.Source; import org.zwobble.hobgoblin.compiler.types.SimpleNativeType; +import org.zwobble.hobgoblin.compiler.types.TypeOrConstructor; public record TypedNativeTypeDefinitionNode( - SimpleNativeType type, + TypeOrConstructor typeOrConstructor, DocComment docComment, Source source ) implements TypedNamespaceStatementNode { public String name() { - return this.type.name(); + return this.typeOrConstructor.name(); } } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java index eb47870..e403f5b 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java @@ -165,7 +165,17 @@ public class TypeChecker { TypeCheckerNamespaceContext context ) { var nativeType = new SimpleNativeType(context.namespaceName(), untyped.name()); - var metaType = new TypeLevelValueType(nativeType); + + var nativeTypeOrConstructor = untyped.typeParams().isEmpty() + ? new TypeOrConstructor.Type<>(nativeType) + : new TypeOrConstructor.Constructor<>(new NativeTypeConstructor( + untyped.typeParams().stream() + .map(typeParam -> new TypeParam(typeParam.name())) + .toList(), + nativeType + )); + + var metaType = new TypeLevelValueType(nativeTypeOrConstructor.value()); context.declare(untyped.name(), metaType, untyped.source()); return metaType; } @@ -174,10 +184,20 @@ public class TypeChecker { UntypedNativeTypeDefinitionNode untyped, TypeCheckerNamespaceContext context ) { - var nativeType = (SimpleNativeType) lookupMetaType(untyped.name(), untyped.source(), context); + var nativeMetaType = lookupTypeLevelValue(untyped.name(), untyped.source(), context); + var nativeTypeOrConstructor = switch (nativeMetaType) { + case SimpleNativeType type -> + new TypeOrConstructor.Type<>(type); + + case NativeTypeConstructor typeConstructor -> + new TypeOrConstructor.Constructor<>(typeConstructor); + + default -> + throw new UnsupportedOperationException("TODO"); + }; return new TypedNativeTypeDefinitionNode( - nativeType, + nativeTypeOrConstructor, untyped.docComment(), untyped.source() ); diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/types/TypeConstructor.java b/src/main/java/org/zwobble/hobgoblin/compiler/types/TypeConstructor.java index e2726fb..2c779d2 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/types/TypeConstructor.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/types/TypeConstructor.java @@ -9,4 +9,8 @@ public sealed interface TypeConstructor extends Typ default String describe() { return genericType().describe(); } + + default String name() { + return genericType().name(); + } } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/types/TypeOrConstructor.java b/src/main/java/org/zwobble/hobgoblin/compiler/types/TypeOrConstructor.java new file mode 100644 index 0000000..8844669 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/types/TypeOrConstructor.java @@ -0,0 +1,20 @@ +package org.zwobble.hobgoblin.compiler.types; + +public sealed interface TypeOrConstructor { + record Type(T value) implements TypeOrConstructor { + @Override + public String name() { + return this.value.name(); + } + } + + record Constructor(TypeConstructor value) implements TypeOrConstructor { + @Override + public String name() { + return this.value.name(); + } + } + + String name(); + TypeLevelValue value(); +} diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNativeTypeDefinitionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNativeTypeDefinitionTests.java index c0a329a..1777506 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNativeTypeDefinitionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNativeTypeDefinitionTests.java @@ -5,6 +5,7 @@ import org.zwobble.hobgoblin.compiler.ast.typed.TypedNativeTypeDefinitionNode; import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode; import org.zwobble.hobgoblin.compiler.types.NamespaceName; import org.zwobble.hobgoblin.compiler.types.SimpleNativeType; +import org.zwobble.hobgoblin.compiler.types.TypeOrConstructor; import static org.zwobble.hobgoblin.compiler.typechecker.TypeCheckerTesting.typeCheckNamespaceStatement; import static org.zwobble.precisely.AssertThat.assertThat; @@ -23,9 +24,9 @@ public class TypeCheckerNativeTypeDefinitionTests { assertThat(typed, instanceOf( TypedNativeTypeDefinitionNode.class, has( - "type", - TypedNativeTypeDefinitionNode::type, - equalTo(new SimpleNativeType(NamespaceName.of("a", "b"), "X")) + "typeOrConstructor", + TypedNativeTypeDefinitionNode::typeOrConstructor, + equalTo(new TypeOrConstructor.Type<>(new SimpleNativeType(NamespaceName.of("a", "b"), "X"))) ) )); } -- cgit v1.2.3