Skip to content

Commit

Permalink
Rust: Add serde json support (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
sisuresh authored Jul 13, 2022
1 parent ca59a92 commit 75d41fe
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/xdrgen/generators/rust.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/xdrgen/generators/rust/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ impl<T: WriteXdr, const N: usize> 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<T, const MAX: u32 = { u32::MAX }>(Vec<T>);

#[cfg(not(feature = "alloc"))]
Expand Down
2 changes: 2 additions & 0 deletions spec/output/generator_spec_rust/block_comments.x/MyXDR.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ impl<T: WriteXdr, const N: usize> 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<T, const MAX: u32 = { u32::MAX }>(Vec<T>);

#[cfg(not(feature = "alloc"))]
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions spec/output/generator_spec_rust/const.x/MyXDR.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ impl<T: WriteXdr, const N: usize> 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<T, const MAX: u32 = { u32::MAX }>(Vec<T>);

#[cfg(not(feature = "alloc"))]
Expand Down
4 changes: 4 additions & 0 deletions spec/output/generator_spec_rust/enum.x/MyXDR.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ impl<T: WriteXdr, const N: usize> 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<T, const MAX: u32 = { u32::MAX }>(Vec<T>);

#[cfg(not(feature = "alloc"))]
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions spec/output/generator_spec_rust/nesting.x/MyXDR.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ impl<T: WriteXdr, const N: usize> 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<T, const MAX: u32 = { u32::MAX }>(Vec<T>);

#[cfg(not(feature = "alloc"))]
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 2 additions & 0 deletions spec/output/generator_spec_rust/optional.x/MyXDR.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ impl<T: WriteXdr, const N: usize> 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<T, const MAX: u32 = { u32::MAX }>(Vec<T>);

#[cfg(not(feature = "alloc"))]
Expand Down Expand Up @@ -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<i32>,
pub second_option: Option<i32>,
Expand Down
2 changes: 2 additions & 0 deletions spec/output/generator_spec_rust/struct.x/MyXDR.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ impl<T: WriteXdr, const N: usize> 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<T, const MAX: u32 = { u32::MAX }>(Vec<T>);

#[cfg(not(feature = "alloc"))]
Expand Down Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions spec/output/generator_spec_rust/test.x/MyXDR.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ impl<T: WriteXdr, const N: usize> 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<T, const MAX: u32 = { u32::MAX }>(Vec<T>);

#[cfg(not(feature = "alloc"))]
Expand Down Expand Up @@ -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<Uint512> for [u8; 64] {
Expand Down Expand Up @@ -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::<u8, 64>);

impl From<Uint513> for VecM::<u8, 64> {
Expand Down Expand Up @@ -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::<u8>);

impl From<Uint514> for VecM::<u8> {
Expand Down Expand Up @@ -1212,6 +1216,7 @@ pub type Str2 = VecM::<u8>;
// 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<Hash> for [u8; 32] {
Expand Down Expand Up @@ -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<Hashes1> for [Hash; 12] {
Expand Down Expand Up @@ -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::<Hash, 12>);

impl From<Hashes2> for VecM::<Hash, 12> {
Expand Down Expand Up @@ -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::<Hash>);

impl From<Hashes3> for VecM::<Hash> {
Expand Down Expand Up @@ -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<Hash>);

impl From<OptHash1> for Option<Hash> {
Expand Down Expand Up @@ -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<Hash>);

impl From<OptHash2> for Option<Hash> {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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::<MyStruct>,
}
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions spec/output/generator_spec_rust/union.x/MyXDR.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ impl<T: WriteXdr, const N: usize> 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<T, const MAX: u32 = { u32::MAX }>(Vec<T>);

#[cfg(not(feature = "alloc"))]
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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<IntUnion2> for IntUnion {
Expand Down

0 comments on commit 75d41fe

Please sign in to comment.