Skip to content

Commit

Permalink
Rust: Add fn for deserializing multiple types in json from a stream (s…
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmcculloch authored Aug 28, 2024
1 parent dda3c5e commit b7bc57e
Show file tree
Hide file tree
Showing 25 changed files with 486 additions and 25 deletions.
16 changes: 15 additions & 1 deletion lib/xdrgen/generators/rust.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,27 @@ def render_enum_of_all_types(out, types)
}
#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
#[deprecated(note = "use from_json")]
pub fn read_json(v: TypeVariant, r: impl Read) -> Result<Self> {
Self::from_json(v, r)
}
#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
pub fn from_json(v: TypeVariant, r: impl Read) -> Result<Self> {
match v {
#{types.map { |t| "TypeVariant::#{t} => Ok(Self::#{t}(Box::new(serde_json::from_reader(r)?)))," }.join("\n")}
}
}
#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
pub fn deserialize_json<'r, R: serde_json::de::Read<'r>>(v: TypeVariant, r: &mut serde_json::de::Deserializer<R>) -> Result<Self> {
match v {
#{types.map { |t| "TypeVariant::#{t} => Ok(Self::#{t}(Box::new(serde::de::Deserialize::deserialize(r)?)))," }.join("\n")}
}
}
#[cfg(feature = "alloc")]
#[must_use]
#[allow(clippy::too_many_lines)]
Expand Down
16 changes: 15 additions & 1 deletion spec/output/generator_spec_rust/block_comments.x/MyXDR.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3030,13 +3030,27 @@ impl Type {
}

#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
#[deprecated(note = "use from_json")]
pub fn read_json(v: TypeVariant, r: impl Read) -> Result<Self> {
Self::from_json(v, r)
}

#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
pub fn from_json(v: TypeVariant, r: impl Read) -> Result<Self> {
match v {
TypeVariant::AccountFlags => Ok(Self::AccountFlags(Box::new(serde_json::from_reader(r)?))),
}
}

#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
pub fn deserialize_json<'r, R: serde_json::de::Read<'r>>(v: TypeVariant, r: &mut serde_json::de::Deserializer<R>) -> Result<Self> {
match v {
TypeVariant::AccountFlags => Ok(Self::AccountFlags(Box::new(serde::de::Deserialize::deserialize(r)?))),
}
}

#[cfg(feature = "alloc")]
#[must_use]
#[allow(clippy::too_many_lines)]
Expand Down
17 changes: 16 additions & 1 deletion spec/output/generator_spec_rust/const.x/MyXDR.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2969,14 +2969,29 @@ TypeVariant::TestArray2 => Box::new(ReadXdrIter::<_, TestArray2>::new(dec, r.lim
}

#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
#[deprecated(note = "use from_json")]
pub fn read_json(v: TypeVariant, r: impl Read) -> Result<Self> {
Self::from_json(v, r)
}

#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
pub fn from_json(v: TypeVariant, r: impl Read) -> Result<Self> {
match v {
TypeVariant::TestArray => Ok(Self::TestArray(Box::new(serde_json::from_reader(r)?))),
TypeVariant::TestArray2 => Ok(Self::TestArray2(Box::new(serde_json::from_reader(r)?))),
}
}

#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
pub fn deserialize_json<'r, R: serde_json::de::Read<'r>>(v: TypeVariant, r: &mut serde_json::de::Deserializer<R>) -> Result<Self> {
match v {
TypeVariant::TestArray => Ok(Self::TestArray(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::TestArray2 => Ok(Self::TestArray2(Box::new(serde::de::Deserialize::deserialize(r)?))),
}
}

#[cfg(feature = "alloc")]
#[must_use]
#[allow(clippy::too_many_lines)]
Expand Down
19 changes: 18 additions & 1 deletion spec/output/generator_spec_rust/enum.x/MyXDR.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3480,8 +3480,14 @@ TypeVariant::Color3 => Box::new(ReadXdrIter::<_, Color3>::new(dec, r.limits.clon
}

#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
#[deprecated(note = "use from_json")]
pub fn read_json(v: TypeVariant, r: impl Read) -> Result<Self> {
Self::from_json(v, r)
}

#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
pub fn from_json(v: TypeVariant, r: impl Read) -> Result<Self> {
match v {
TypeVariant::MessageType => Ok(Self::MessageType(Box::new(serde_json::from_reader(r)?))),
TypeVariant::Color => Ok(Self::Color(Box::new(serde_json::from_reader(r)?))),
Expand All @@ -3490,6 +3496,17 @@ TypeVariant::Color3 => Ok(Self::Color3(Box::new(serde_json::from_reader(r)?))),
}
}

#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
pub fn deserialize_json<'r, R: serde_json::de::Read<'r>>(v: TypeVariant, r: &mut serde_json::de::Deserializer<R>) -> Result<Self> {
match v {
TypeVariant::MessageType => Ok(Self::MessageType(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::Color => Ok(Self::Color(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::Color2 => Ok(Self::Color2(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::Color3 => Ok(Self::Color3(Box::new(serde::de::Deserialize::deserialize(r)?))),
}
}

#[cfg(feature = "alloc")]
#[must_use]
#[allow(clippy::too_many_lines)]
Expand Down
20 changes: 19 additions & 1 deletion spec/output/generator_spec_rust/nesting.x/MyXDR.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3305,8 +3305,14 @@ TypeVariant::MyUnionTwo => Box::new(ReadXdrIter::<_, MyUnionTwo>::new(dec, r.lim
}

#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
#[deprecated(note = "use from_json")]
pub fn read_json(v: TypeVariant, r: impl Read) -> Result<Self> {
Self::from_json(v, r)
}

#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
pub fn from_json(v: TypeVariant, r: impl Read) -> Result<Self> {
match v {
TypeVariant::UnionKey => Ok(Self::UnionKey(Box::new(serde_json::from_reader(r)?))),
TypeVariant::Foo => Ok(Self::Foo(Box::new(serde_json::from_reader(r)?))),
Expand All @@ -3316,6 +3322,18 @@ TypeVariant::MyUnionTwo => Ok(Self::MyUnionTwo(Box::new(serde_json::from_reader(
}
}

#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
pub fn deserialize_json<'r, R: serde_json::de::Read<'r>>(v: TypeVariant, r: &mut serde_json::de::Deserializer<R>) -> Result<Self> {
match v {
TypeVariant::UnionKey => Ok(Self::UnionKey(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::Foo => Ok(Self::Foo(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::MyUnion => Ok(Self::MyUnion(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::MyUnionOne => Ok(Self::MyUnionOne(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::MyUnionTwo => Ok(Self::MyUnionTwo(Box::new(serde::de::Deserialize::deserialize(r)?))),
}
}

#[cfg(feature = "alloc")]
#[must_use]
#[allow(clippy::too_many_lines)]
Expand Down
17 changes: 16 additions & 1 deletion spec/output/generator_spec_rust/optional.x/MyXDR.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2999,14 +2999,29 @@ TypeVariant::HasOptions => Box::new(ReadXdrIter::<_, HasOptions>::new(dec, r.lim
}

#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
#[deprecated(note = "use from_json")]
pub fn read_json(v: TypeVariant, r: impl Read) -> Result<Self> {
Self::from_json(v, r)
}

#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
pub fn from_json(v: TypeVariant, r: impl Read) -> Result<Self> {
match v {
TypeVariant::Arr => Ok(Self::Arr(Box::new(serde_json::from_reader(r)?))),
TypeVariant::HasOptions => Ok(Self::HasOptions(Box::new(serde_json::from_reader(r)?))),
}
}

#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
pub fn deserialize_json<'r, R: serde_json::de::Read<'r>>(v: TypeVariant, r: &mut serde_json::de::Deserializer<R>) -> Result<Self> {
match v {
TypeVariant::Arr => Ok(Self::Arr(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::HasOptions => Ok(Self::HasOptions(Box::new(serde::de::Deserialize::deserialize(r)?))),
}
}

#[cfg(feature = "alloc")]
#[must_use]
#[allow(clippy::too_many_lines)]
Expand Down
17 changes: 16 additions & 1 deletion spec/output/generator_spec_rust/struct.x/MyXDR.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3007,14 +3007,29 @@ TypeVariant::MyStruct => Box::new(ReadXdrIter::<_, MyStruct>::new(dec, r.limits.
}

#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
#[deprecated(note = "use from_json")]
pub fn read_json(v: TypeVariant, r: impl Read) -> Result<Self> {
Self::from_json(v, r)
}

#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
pub fn from_json(v: TypeVariant, r: impl Read) -> Result<Self> {
match v {
TypeVariant::Int64 => Ok(Self::Int64(Box::new(serde_json::from_reader(r)?))),
TypeVariant::MyStruct => Ok(Self::MyStruct(Box::new(serde_json::from_reader(r)?))),
}
}

#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
pub fn deserialize_json<'r, R: serde_json::de::Read<'r>>(v: TypeVariant, r: &mut serde_json::de::Deserializer<R>) -> Result<Self> {
match v {
TypeVariant::Int64 => Ok(Self::Int64(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::MyStruct => Ok(Self::MyStruct(Box::new(serde::de::Deserialize::deserialize(r)?))),
}
}

#[cfg(feature = "alloc")]
#[must_use]
#[allow(clippy::too_many_lines)]
Expand Down
38 changes: 37 additions & 1 deletion spec/output/generator_spec_rust/test.x/MyXDR.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4916,8 +4916,14 @@ TypeVariant::NesterNestedUnion => Box::new(ReadXdrIter::<_, NesterNestedUnion>::
}

#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
#[deprecated(note = "use from_json")]
pub fn read_json(v: TypeVariant, r: impl Read) -> Result<Self> {
Self::from_json(v, r)
}

#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
pub fn from_json(v: TypeVariant, r: impl Read) -> Result<Self> {
match v {
TypeVariant::Uint512 => Ok(Self::Uint512(Box::new(serde_json::from_reader(r)?))),
TypeVariant::Uint513 => Ok(Self::Uint513(Box::new(serde_json::from_reader(r)?))),
Expand Down Expand Up @@ -4945,6 +4951,36 @@ TypeVariant::NesterNestedUnion => Ok(Self::NesterNestedUnion(Box::new(serde_json
}
}

#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
pub fn deserialize_json<'r, R: serde_json::de::Read<'r>>(v: TypeVariant, r: &mut serde_json::de::Deserializer<R>) -> Result<Self> {
match v {
TypeVariant::Uint512 => Ok(Self::Uint512(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::Uint513 => Ok(Self::Uint513(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::Uint514 => Ok(Self::Uint514(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::Str => Ok(Self::Str(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::Str2 => Ok(Self::Str2(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::Hash => Ok(Self::Hash(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::Hashes1 => Ok(Self::Hashes1(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::Hashes2 => Ok(Self::Hashes2(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::Hashes3 => Ok(Self::Hashes3(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::OptHash1 => Ok(Self::OptHash1(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::OptHash2 => Ok(Self::OptHash2(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::Int1 => Ok(Self::Int1(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::Int2 => Ok(Self::Int2(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::Int3 => Ok(Self::Int3(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::Int4 => Ok(Self::Int4(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::MyStruct => Ok(Self::MyStruct(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::LotsOfMyStructs => Ok(Self::LotsOfMyStructs(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::HasStuff => Ok(Self::HasStuff(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::Color => Ok(Self::Color(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::Nester => Ok(Self::Nester(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::NesterNestedEnum => Ok(Self::NesterNestedEnum(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::NesterNestedStruct => Ok(Self::NesterNestedStruct(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::NesterNestedUnion => Ok(Self::NesterNestedUnion(Box::new(serde::de::Deserialize::deserialize(r)?))),
}
}

#[cfg(feature = "alloc")]
#[must_use]
#[allow(clippy::too_many_lines)]
Expand Down
21 changes: 20 additions & 1 deletion spec/output/generator_spec_rust/union.x/MyXDR.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3391,8 +3391,14 @@ TypeVariant::IntUnion2 => Box::new(ReadXdrIter::<_, IntUnion2>::new(dec, r.limit
}

#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
#[deprecated(note = "use from_json")]
pub fn read_json(v: TypeVariant, r: impl Read) -> Result<Self> {
Self::from_json(v, r)
}

#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
pub fn from_json(v: TypeVariant, r: impl Read) -> Result<Self> {
match v {
TypeVariant::SError => Ok(Self::SError(Box::new(serde_json::from_reader(r)?))),
TypeVariant::Multi => Ok(Self::Multi(Box::new(serde_json::from_reader(r)?))),
Expand All @@ -3403,6 +3409,19 @@ TypeVariant::IntUnion2 => Ok(Self::IntUnion2(Box::new(serde_json::from_reader(r)
}
}

#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
pub fn deserialize_json<'r, R: serde_json::de::Read<'r>>(v: TypeVariant, r: &mut serde_json::de::Deserializer<R>) -> Result<Self> {
match v {
TypeVariant::SError => Ok(Self::SError(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::Multi => Ok(Self::Multi(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::UnionKey => Ok(Self::UnionKey(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::MyUnion => Ok(Self::MyUnion(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::IntUnion => Ok(Self::IntUnion(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::IntUnion2 => Ok(Self::IntUnion2(Box::new(serde::de::Deserialize::deserialize(r)?))),
}
}

#[cfg(feature = "alloc")]
#[must_use]
#[allow(clippy::too_many_lines)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3030,13 +3030,27 @@ impl Type {
}

#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
#[deprecated(note = "use from_json")]
pub fn read_json(v: TypeVariant, r: impl Read) -> Result<Self> {
Self::from_json(v, r)
}

#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
pub fn from_json(v: TypeVariant, r: impl Read) -> Result<Self> {
match v {
TypeVariant::AccountFlags => Ok(Self::AccountFlags(Box::new(serde_json::from_reader(r)?))),
}
}

#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
pub fn deserialize_json<'r, R: serde_json::de::Read<'r>>(v: TypeVariant, r: &mut serde_json::de::Deserializer<R>) -> Result<Self> {
match v {
TypeVariant::AccountFlags => Ok(Self::AccountFlags(Box::new(serde::de::Deserialize::deserialize(r)?))),
}
}

#[cfg(feature = "alloc")]
#[must_use]
#[allow(clippy::too_many_lines)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2969,14 +2969,29 @@ TypeVariant::TestArray2 => Box::new(ReadXdrIter::<_, TestArray2>::new(dec, r.lim
}

#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
#[deprecated(note = "use from_json")]
pub fn read_json(v: TypeVariant, r: impl Read) -> Result<Self> {
Self::from_json(v, r)
}

#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
pub fn from_json(v: TypeVariant, r: impl Read) -> Result<Self> {
match v {
TypeVariant::TestArray => Ok(Self::TestArray(Box::new(serde_json::from_reader(r)?))),
TypeVariant::TestArray2 => Ok(Self::TestArray2(Box::new(serde_json::from_reader(r)?))),
}
}

#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
pub fn deserialize_json<'r, R: serde_json::de::Read<'r>>(v: TypeVariant, r: &mut serde_json::de::Deserializer<R>) -> Result<Self> {
match v {
TypeVariant::TestArray => Ok(Self::TestArray(Box::new(serde::de::Deserialize::deserialize(r)?))),
TypeVariant::TestArray2 => Ok(Self::TestArray2(Box::new(serde::de::Deserialize::deserialize(r)?))),
}
}

#[cfg(feature = "alloc")]
#[must_use]
#[allow(clippy::too_many_lines)]
Expand Down
Loading

0 comments on commit b7bc57e

Please sign in to comment.