diff --git a/lib/xdrgen/generators/rust.rb b/lib/xdrgen/generators/rust.rb index 5dc551b8e..33092f0dc 100644 --- a/lib/xdrgen/generators/rust.rb +++ b/lib/xdrgen/generators/rust.rb @@ -134,6 +134,7 @@ def render_source_comment(out, defn) def render_struct(out, struct) out.puts "#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]" + out.puts %{#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))]} out.puts "pub struct #{name struct} {" out.indent do struct.members.each do |m| @@ -170,6 +171,7 @@ def render_struct(out, struct) def render_enum(out, enum) out.puts "// enum" out.puts "#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]" + out.puts %{#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))]} out.puts "#[repr(i32)]" out.puts "pub enum #{name enum} {" out.indent do @@ -275,6 +277,7 @@ def render_union(out, union) discriminant_type_builtin = is_builtin_type(union.discriminant.type) || (is_builtin_type(union.discriminant.type.resolved_type.type) if union.discriminant.type.respond_to?(:resolved_type) && AST::Definitions::Typedef === union.discriminant.type.resolved_type) out.puts "// union with discriminant #{discriminant_type}" out.puts "#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]" + out.puts %{#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))]} out.puts "#[allow(clippy::large_enum_variant)]" out.puts "pub enum #{name union} {" out.indent do @@ -373,6 +376,7 @@ def render_typedef(out, typedef) else out.puts "#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]" out.puts "#[derive(Default)]" if is_var_array_type(typedef.type) + out.puts %{#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))]} out.puts "pub struct #{name typedef}(pub #{reference(typedef, typedef.type)});" out.puts "" out.puts <<-EOS.strip_heredoc diff --git a/lib/xdrgen/generators/rust/src/types.rs b/lib/xdrgen/generators/rust/src/types.rs index f2f230d48..b603a1847 100644 --- a/lib/xdrgen/generators/rust/src/types.rs +++ b/lib/xdrgen/generators/rust/src/types.rs @@ -473,6 +473,7 @@ impl WriteXdr for [T; N] { #[cfg(feature = "alloc")] #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct VecM(Vec); #[cfg(not(feature = "alloc"))] diff --git a/spec/output/generator_spec_rust/block_comments.x/MyXDR.rs b/spec/output/generator_spec_rust/block_comments.x/MyXDR.rs index 13773b79a..1f37082b5 100644 --- a/spec/output/generator_spec_rust/block_comments.x/MyXDR.rs +++ b/spec/output/generator_spec_rust/block_comments.x/MyXDR.rs @@ -483,6 +483,7 @@ impl WriteXdr for [T; N] { #[cfg(feature = "alloc")] #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct VecM(Vec); #[cfg(not(feature = "alloc"))] @@ -935,6 +936,7 @@ mod tests { // // enum #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))] #[repr(i32)] pub enum AccountFlags { AuthRequiredFlag = 1, diff --git a/spec/output/generator_spec_rust/const.x/MyXDR.rs b/spec/output/generator_spec_rust/const.x/MyXDR.rs index 67becab18..8861b66f0 100644 --- a/spec/output/generator_spec_rust/const.x/MyXDR.rs +++ b/spec/output/generator_spec_rust/const.x/MyXDR.rs @@ -483,6 +483,7 @@ impl WriteXdr for [T; N] { #[cfg(feature = "alloc")] #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct VecM(Vec); #[cfg(not(feature = "alloc"))] diff --git a/spec/output/generator_spec_rust/enum.x/MyXDR.rs b/spec/output/generator_spec_rust/enum.x/MyXDR.rs index 214191ca9..1ddbd4786 100644 --- a/spec/output/generator_spec_rust/enum.x/MyXDR.rs +++ b/spec/output/generator_spec_rust/enum.x/MyXDR.rs @@ -483,6 +483,7 @@ impl WriteXdr for [T; N] { #[cfg(feature = "alloc")] #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct VecM(Vec); #[cfg(not(feature = "alloc"))] @@ -954,6 +955,7 @@ mod tests { // // enum #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))] #[repr(i32)] pub enum MessageType { ErrorMsg = 0, @@ -1069,6 +1071,7 @@ Self::FbaMessage => "FbaMessage", // // enum #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))] #[repr(i32)] pub enum Color { Red = 0, @@ -1151,6 +1154,7 @@ Self::Blue => "Blue", // // enum #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))] #[repr(i32)] pub enum Color2 { Red2 = 0, diff --git a/spec/output/generator_spec_rust/nesting.x/MyXDR.rs b/spec/output/generator_spec_rust/nesting.x/MyXDR.rs index 9c0330ba3..5f3e02ae6 100644 --- a/spec/output/generator_spec_rust/nesting.x/MyXDR.rs +++ b/spec/output/generator_spec_rust/nesting.x/MyXDR.rs @@ -483,6 +483,7 @@ impl WriteXdr for [T; N] { #[cfg(feature = "alloc")] #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct VecM(Vec); #[cfg(not(feature = "alloc"))] @@ -936,6 +937,7 @@ mod tests { // // enum #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))] #[repr(i32)] pub enum UnionKey { One = 1, @@ -1021,6 +1023,7 @@ pub type Foo = i32; // } // #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))] pub struct MyUnionOne { pub some_int: i32, } @@ -1050,6 +1053,7 @@ impl WriteXdr for MyUnionOne { // } // #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))] pub struct MyUnionTwo { pub some_int: i32, pub foo: i32, @@ -1095,6 +1099,7 @@ self.foo.write_xdr(w)?; // // union with discriminant UnionKey #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))] #[allow(clippy::large_enum_variant)] pub enum MyUnion { One(MyUnionOne), diff --git a/spec/output/generator_spec_rust/optional.x/MyXDR.rs b/spec/output/generator_spec_rust/optional.x/MyXDR.rs index f173b4f6d..7c25cda36 100644 --- a/spec/output/generator_spec_rust/optional.x/MyXDR.rs +++ b/spec/output/generator_spec_rust/optional.x/MyXDR.rs @@ -483,6 +483,7 @@ impl WriteXdr for [T; N] { #[cfg(feature = "alloc")] #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct VecM(Vec); #[cfg(not(feature = "alloc"))] @@ -942,6 +943,7 @@ pub type Arr = [i32; 2]; // }; // #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))] pub struct HasOptions { pub first_option: Option, pub second_option: Option, diff --git a/spec/output/generator_spec_rust/struct.x/MyXDR.rs b/spec/output/generator_spec_rust/struct.x/MyXDR.rs index f9ff0c6f1..23e910590 100644 --- a/spec/output/generator_spec_rust/struct.x/MyXDR.rs +++ b/spec/output/generator_spec_rust/struct.x/MyXDR.rs @@ -483,6 +483,7 @@ impl WriteXdr for [T; N] { #[cfg(feature = "alloc")] #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct VecM(Vec); #[cfg(not(feature = "alloc"))] @@ -944,6 +945,7 @@ pub type Int64 = i64; // }; // #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))] pub struct MyStruct { pub some_int: i32, pub a_big_int: i64, diff --git a/spec/output/generator_spec_rust/test.x/MyXDR.rs b/spec/output/generator_spec_rust/test.x/MyXDR.rs index de2ea80e6..d0ce5b0d9 100644 --- a/spec/output/generator_spec_rust/test.x/MyXDR.rs +++ b/spec/output/generator_spec_rust/test.x/MyXDR.rs @@ -483,6 +483,7 @@ impl WriteXdr for [T; N] { #[cfg(feature = "alloc")] #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct VecM(Vec); #[cfg(not(feature = "alloc"))] @@ -931,6 +932,7 @@ mod tests { // typedef opaque uint512[64]; // #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))] pub struct Uint512(pub [u8; 64]); impl From for [u8; 64] { @@ -1013,6 +1015,7 @@ impl AsRef<[u8]> for Uint512 { // #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] #[derive(Default)] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))] pub struct Uint513(pub VecM::); impl From for VecM:: { @@ -1107,6 +1110,7 @@ impl AsRef<[u8]> for Uint513 { // #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] #[derive(Default)] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))] pub struct Uint514(pub VecM::); impl From for VecM:: { @@ -1212,6 +1216,7 @@ pub type Str2 = VecM::; // typedef opaque Hash[32]; // #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))] pub struct Hash(pub [u8; 32]); impl From for [u8; 32] { @@ -1293,6 +1298,7 @@ impl AsRef<[u8]> for Hash { // typedef Hash Hashes1[12]; // #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))] pub struct Hashes1(pub [Hash; 12]); impl From for [Hash; 12] { @@ -1375,6 +1381,7 @@ impl AsRef<[Hash]> for Hashes1 { // #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] #[derive(Default)] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))] pub struct Hashes2(pub VecM::); impl From for VecM:: { @@ -1469,6 +1476,7 @@ impl AsRef<[Hash]> for Hashes2 { // #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] #[derive(Default)] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))] pub struct Hashes3(pub VecM::); impl From for VecM:: { @@ -1562,6 +1570,7 @@ impl AsRef<[Hash]> for Hashes3 { // typedef Hash *optHash1; // #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))] pub struct OptHash1(pub Option); impl From for Option { @@ -1606,6 +1615,7 @@ impl WriteXdr for OptHash1 { // typedef Hash* optHash2; // #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))] pub struct OptHash2(pub Option); impl From for Option { @@ -1683,6 +1693,7 @@ pub type Int4 = u64; // }; // #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))] pub struct MyStruct { pub field1: Uint512, pub field2: OptHash1, @@ -1730,6 +1741,7 @@ self.field7.write_xdr(w)?; // }; // #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))] pub struct LotsOfMyStructs { pub members: VecM::, } @@ -1759,6 +1771,7 @@ impl WriteXdr for LotsOfMyStructs { // }; // #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))] pub struct HasStuff { pub data: LotsOfMyStructs, } @@ -1790,6 +1803,7 @@ impl WriteXdr for HasStuff { // // enum #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))] #[repr(i32)] pub enum Color { Red = 0, @@ -1883,6 +1897,7 @@ pub const BAR: u64 = FOO; // // enum #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))] #[repr(i32)] pub enum NesterNestedEnum { 1 = 0, @@ -1959,6 +1974,7 @@ Self::2 => "2", // } // #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))] pub struct NesterNestedStruct { pub blah: i32, } @@ -1991,6 +2007,7 @@ impl WriteXdr for NesterNestedStruct { // // union with discriminant Color #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))] #[allow(clippy::large_enum_variant)] pub enum NesterNestedUnion { Red, @@ -2079,6 +2096,7 @@ impl WriteXdr for NesterNestedUnion { // }; // #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))] pub struct Nester { pub nested_enum: NesterNestedEnum, pub nested_struct: NesterNestedStruct, diff --git a/spec/output/generator_spec_rust/union.x/MyXDR.rs b/spec/output/generator_spec_rust/union.x/MyXDR.rs index 9056d3c8f..aafa05015 100644 --- a/spec/output/generator_spec_rust/union.x/MyXDR.rs +++ b/spec/output/generator_spec_rust/union.x/MyXDR.rs @@ -483,6 +483,7 @@ impl WriteXdr for [T; N] { #[cfg(feature = "alloc")] #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct VecM(Vec); #[cfg(not(feature = "alloc"))] @@ -947,6 +948,7 @@ pub type Multi = i32; // // enum #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))] #[repr(i32)] pub enum UnionKey { Error = 0, @@ -1030,6 +1032,7 @@ Self::Multi => "Multi", // // union with discriminant UnionKey #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))] #[allow(clippy::large_enum_variant)] pub enum MyUnion { Error(i32), @@ -1112,6 +1115,7 @@ Self::Multi(v) => v.write_xdr(w)?, // // union with discriminant i32 #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))] #[allow(clippy::large_enum_variant)] pub enum IntUnion { V0(i32), @@ -1186,6 +1190,7 @@ Self::V1(v) => v.write_xdr(w)?, // typedef IntUnion IntUnion2; // #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase"))] pub struct IntUnion2(pub IntUnion); impl From for IntUnion {