From d14b7d028aec3d491d9f162fc84aa8f4e51ec814 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 14 Jul 2026 22:36:32 +0100 Subject: Support struct with Int32 in rust-transient-0 --- examples/10-transient-0/hobgoblin.json5 | 4 +- examples/10-transient-0/output/rust/src/data.rs | 85 +----------- .../output/rust/src/data/transient_0.rs | 3 - .../10-transient-0/output/rust/src/gen/data.rs | 84 ++++++++++++ .../output/rust/src/gen/data/transient_0.rs | 149 +++++++++++++++++++++ examples/10-transient-0/output/rust/src/lib.rs | 33 +++++ examples/10-transient-0/output/rust/src/main.rs | 5 - 7 files changed, 271 insertions(+), 92 deletions(-) delete mode 100644 examples/10-transient-0/output/rust/src/data/transient_0.rs create mode 100644 examples/10-transient-0/output/rust/src/gen/data.rs create mode 100644 examples/10-transient-0/output/rust/src/gen/data/transient_0.rs create mode 100644 examples/10-transient-0/output/rust/src/lib.rs delete mode 100644 examples/10-transient-0/output/rust/src/main.rs (limited to 'examples') diff --git a/examples/10-transient-0/hobgoblin.json5 b/examples/10-transient-0/hobgoblin.json5 index 37617ed..2160c60 100644 --- a/examples/10-transient-0/hobgoblin.json5 +++ b/examples/10-transient-0/hobgoblin.json5 @@ -16,11 +16,11 @@ }, { generator: "rust-types", - path: "output/rust/src", + path: "output/rust/src/gen", }, { generator: "rust-transient-0", - path: "output/rust/src", + path: "output/rust/src/gen", } ], } diff --git a/examples/10-transient-0/output/rust/src/data.rs b/examples/10-transient-0/output/rust/src/data.rs index 814e4ca..7a0c4fa 100644 --- a/examples/10-transient-0/output/rust/src/data.rs +++ b/examples/10-transient-0/output/rust/src/data.rs @@ -1,84 +1,5 @@ -// Generated by hobgoblin. +include!("./gen/data.rs"); -#[derive(Debug, PartialEq)] -pub struct StructWithBool { - pub a: ::core::primitive::bool, - pub b: ::core::primitive::bool, -} - -#[derive(Debug, PartialEq)] -pub struct StructWithInt32 { - pub a: ::core::primitive::i32, - pub b: ::core::primitive::i32, -} - -#[derive(Debug, PartialEq)] -pub struct StructWithInt64 { - pub a: ::core::primitive::i64, - pub b: ::core::primitive::i64, -} - -#[derive(Debug, PartialEq)] -pub struct StructWithString { - pub a: ::std::string::String, - pub b: ::std::string::String, -} - -#[derive(Debug, PartialEq)] -pub struct OuterStruct { - pub inner: crate::data::InnerStruct, - pub c: ::core::primitive::i32, -} - -#[derive(Debug, PartialEq)] -pub struct InnerStruct { - pub a: ::core::primitive::i32, - pub b: ::core::primitive::i32, -} - -#[derive(Debug, PartialEq)] -pub struct StructWithList { - pub a: ::std::vec::Vec::<::core::primitive::i64>, - pub b: ::std::vec::Vec::, -} - -#[derive(Debug, PartialEq)] -pub struct StructWithOption { - pub a: ::std::option::Option::<::core::primitive::i64>, - pub b: ::std::option::Option::, -} - -#[derive(Debug, PartialEq)] -pub enum EnumWithVariants { - Zero, - One, - Two, -} - -#[derive(Debug, 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(Debug, PartialEq)] -pub struct VariantOne { - pub a: ::core::primitive::i32, -} - -#[derive(Debug, PartialEq)] -pub struct VariantTwo { - pub a: ::core::primitive::i64, +pub mod transient_0 { + include!("./gen/data/transient_0.rs"); } diff --git a/examples/10-transient-0/output/rust/src/data/transient_0.rs b/examples/10-transient-0/output/rust/src/data/transient_0.rs deleted file mode 100644 index 1e240de..0000000 --- a/examples/10-transient-0/output/rust/src/data/transient_0.rs +++ /dev/null @@ -1,3 +0,0 @@ -// Generated by hobgoblin. - - diff --git a/examples/10-transient-0/output/rust/src/gen/data.rs b/examples/10-transient-0/output/rust/src/gen/data.rs new file mode 100644 index 0000000..814e4ca --- /dev/null +++ b/examples/10-transient-0/output/rust/src/gen/data.rs @@ -0,0 +1,84 @@ +// Generated by hobgoblin. + +#[derive(Debug, PartialEq)] +pub struct StructWithBool { + pub a: ::core::primitive::bool, + pub b: ::core::primitive::bool, +} + +#[derive(Debug, PartialEq)] +pub struct StructWithInt32 { + pub a: ::core::primitive::i32, + pub b: ::core::primitive::i32, +} + +#[derive(Debug, PartialEq)] +pub struct StructWithInt64 { + pub a: ::core::primitive::i64, + pub b: ::core::primitive::i64, +} + +#[derive(Debug, PartialEq)] +pub struct StructWithString { + pub a: ::std::string::String, + pub b: ::std::string::String, +} + +#[derive(Debug, PartialEq)] +pub struct OuterStruct { + pub inner: crate::data::InnerStruct, + pub c: ::core::primitive::i32, +} + +#[derive(Debug, PartialEq)] +pub struct InnerStruct { + pub a: ::core::primitive::i32, + pub b: ::core::primitive::i32, +} + +#[derive(Debug, PartialEq)] +pub struct StructWithList { + pub a: ::std::vec::Vec::<::core::primitive::i64>, + pub b: ::std::vec::Vec::, +} + +#[derive(Debug, PartialEq)] +pub struct StructWithOption { + pub a: ::std::option::Option::<::core::primitive::i64>, + pub b: ::std::option::Option::, +} + +#[derive(Debug, PartialEq)] +pub enum EnumWithVariants { + Zero, + One, + Two, +} + +#[derive(Debug, 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(Debug, PartialEq)] +pub struct VariantOne { + pub a: ::core::primitive::i32, +} + +#[derive(Debug, PartialEq)] +pub struct VariantTwo { + pub a: ::core::primitive::i64, +} diff --git a/examples/10-transient-0/output/rust/src/gen/data/transient_0.rs b/examples/10-transient-0/output/rust/src/gen/data/transient_0.rs new file mode 100644 index 0000000..e899c4c --- /dev/null +++ b/examples/10-transient-0/output/rust/src/gen/data/transient_0.rs @@ -0,0 +1,149 @@ +// Generated by hobgoblin. + +pub fn encode_struct_with_bool(value: &crate::data::StructWithBool, writer: &mut impl std::io::Write) { + todo!(); + todo!(); +} + +pub fn decode_struct_with_bool(reader: &mut impl std::io::Read) -> crate::data::StructWithBool { + let a = todo!(); + let b = todo!(); + crate::data::StructWithBool { a: a, b: b } +} + +pub fn encode_struct_with_int_32(value: &crate::data::StructWithInt32, writer: &mut impl std::io::Write) { + writer.write_all(&value.a.to_le_bytes()); + writer.write_all(&value.b.to_le_bytes()); +} + +pub fn decode_struct_with_int_32(reader: &mut impl std::io::Read) -> crate::data::StructWithInt32 { + let a = { + let bytes = { + let mut bytes = [0; 4]; + reader.read_exact(&mut bytes); + bytes + }; + i32::from_le_bytes(bytes) + }; + let b = { + let bytes = { + let mut bytes = [0; 4]; + reader.read_exact(&mut bytes); + bytes + }; + i32::from_le_bytes(bytes) + }; + crate::data::StructWithInt32 { a: a, b: b } +} + +pub fn encode_struct_with_int_64(value: &crate::data::StructWithInt64, writer: &mut impl std::io::Write) { + todo!(); + todo!(); +} + +pub fn decode_struct_with_int_64(reader: &mut impl std::io::Read) -> crate::data::StructWithInt64 { + let a = todo!(); + let b = todo!(); + crate::data::StructWithInt64 { a: a, b: b } +} + +pub fn encode_struct_with_string(value: &crate::data::StructWithString, writer: &mut impl std::io::Write) { + todo!(); + todo!(); +} + +pub fn decode_struct_with_string(reader: &mut impl std::io::Read) -> crate::data::StructWithString { + let a = todo!(); + let b = todo!(); + crate::data::StructWithString { a: a, b: b } +} + +pub fn encode_outer_struct(value: &crate::data::OuterStruct, writer: &mut impl std::io::Write) { + todo!(); + writer.write_all(&value.c.to_le_bytes()); +} + +pub fn decode_outer_struct(reader: &mut impl std::io::Read) -> crate::data::OuterStruct { + let inner = todo!(); + let c = { + let bytes = { + let mut bytes = [0; 4]; + reader.read_exact(&mut bytes); + bytes + }; + i32::from_le_bytes(bytes) + }; + crate::data::OuterStruct { inner: inner, c: c } +} + +pub fn encode_inner_struct(value: &crate::data::InnerStruct, writer: &mut impl std::io::Write) { + writer.write_all(&value.a.to_le_bytes()); + writer.write_all(&value.b.to_le_bytes()); +} + +pub fn decode_inner_struct(reader: &mut impl std::io::Read) -> crate::data::InnerStruct { + let a = { + let bytes = { + let mut bytes = [0; 4]; + reader.read_exact(&mut bytes); + bytes + }; + i32::from_le_bytes(bytes) + }; + let b = { + let bytes = { + let mut bytes = [0; 4]; + reader.read_exact(&mut bytes); + bytes + }; + i32::from_le_bytes(bytes) + }; + crate::data::InnerStruct { a: a, b: b } +} + +pub fn encode_struct_with_list(value: &crate::data::StructWithList, writer: &mut impl std::io::Write) { + todo!(); + todo!(); +} + +pub fn decode_struct_with_list(reader: &mut impl std::io::Read) -> crate::data::StructWithList { + let a = todo!(); + let b = todo!(); + crate::data::StructWithList { a: a, b: b } +} + +pub fn encode_struct_with_option(value: &crate::data::StructWithOption, writer: &mut impl std::io::Write) { + todo!(); + todo!(); +} + +pub fn decode_struct_with_option(reader: &mut impl std::io::Read) -> crate::data::StructWithOption { + let a = todo!(); + let b = todo!(); + crate::data::StructWithOption { a: a, b: b } +} + +pub fn encode_variant_one(value: &crate::data::VariantOne, writer: &mut impl std::io::Write) { + writer.write_all(&value.a.to_le_bytes()); +} + +pub fn decode_variant_one(reader: &mut impl std::io::Read) -> crate::data::VariantOne { + let a = { + let bytes = { + let mut bytes = [0; 4]; + reader.read_exact(&mut bytes); + bytes + }; + i32::from_le_bytes(bytes) + }; + crate::data::VariantOne { a: a } +} + +pub fn encode_variant_two(value: &crate::data::VariantTwo, writer: &mut impl std::io::Write) { + todo!(); +} + +pub fn decode_variant_two(reader: &mut impl std::io::Read) -> crate::data::VariantTwo { + let a = todo!(); + crate::data::VariantTwo { a: a } +} diff --git a/examples/10-transient-0/output/rust/src/lib.rs b/examples/10-transient-0/output/rust/src/lib.rs new file mode 100644 index 0000000..865555e --- /dev/null +++ b/examples/10-transient-0/output/rust/src/lib.rs @@ -0,0 +1,33 @@ +pub mod data; + +#[cfg(test)] +mod test { + use std::io::Cursor; + use super::data::StructWithInt32; + use super::data::StructWithInt64; + + #[test] + fn struct_with_int_32() { + let value = StructWithInt32 { a: 10, b: 25 }; + + assert_round_trip_encoding( + value, + super::data::transient_0::encode_struct_with_int_32, + super::data::transient_0::decode_struct_with_int_32, + ); + } + + fn assert_round_trip_encoding( + value: T, + encode: impl Fn(&T, &mut Cursor>), + decode: impl Fn(&mut Cursor>) -> T, + ) { + let mut bytes = Cursor::new(Vec::new()); + encode(&value, &mut bytes); + + bytes.set_position(0); + let decoded_value = decode(&mut bytes); + + assert_eq!(value, decoded_value); + } +} diff --git a/examples/10-transient-0/output/rust/src/main.rs b/examples/10-transient-0/output/rust/src/main.rs deleted file mode 100644 index 32b5edf..0000000 --- a/examples/10-transient-0/output/rust/src/main.rs +++ /dev/null @@ -1,5 +0,0 @@ -mod data; - -#[cfg(test)] -mod test { -} -- cgit v1.2.3