From 691341a1958278b906a191044072e1f2ad29a8ce Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Tue, 30 Jul 2024 11:22:41 +1000 Subject: [PATCH 1/2] Rust: Enum names not only numbers --- lib/xdrgen/ast/definitions/enum_member.rb | 9 +- lib/xdrgen/ast/definitions/union_arm_case.rb | 7 +- spec/fixtures/generator/enum.x | 8 +- .../enum.x/MyXDR_generated.ex | 17 ++ .../enum.x/MyXDR_generated.go | 87 +++++++++- .../generator_spec_java/enum.x/Color3.java | 61 +++++++ .../generator_spec_java/enum.x/Red.java | 61 +++++++ .../enum.x/MyXDR_generated.js | 15 ++ .../generator_spec_python/enum.x/__init__.py | 1 + .../generator_spec_python/enum.x/color3.py | 50 ++++++ .../generator_spec_python/enum.x/red.py | 50 ++++++ .../generator_spec_ruby/enum.x/MyXDR.rb | 1 + .../generator_spec_ruby/enum.x/color3.rb | 21 +++ spec/output/generator_spec_ruby/enum.x/red.rb | 21 +++ .../generator_spec_rust/enum.x/MyXDR.rs | 149 ++++++++++++++++-- .../generator_spec_rust/test.x/MyXDR.rs | 20 +-- .../enum.x/MyXDR.rs | 149 ++++++++++++++++-- .../test.x/MyXDR.rs | 20 +-- .../enum.x/MyXDR.rs | 149 ++++++++++++++++-- .../test.x/MyXDR.rs | 20 +-- 20 files changed, 848 insertions(+), 68 deletions(-) create mode 100644 spec/output/generator_spec_java/enum.x/Color3.java create mode 100644 spec/output/generator_spec_java/enum.x/Red.java create mode 100644 spec/output/generator_spec_python/enum.x/color3.py create mode 100644 spec/output/generator_spec_python/enum.x/red.py create mode 100644 spec/output/generator_spec_ruby/enum.x/color3.rb create mode 100644 spec/output/generator_spec_ruby/enum.x/red.rb diff --git a/lib/xdrgen/ast/definitions/enum_member.rb b/lib/xdrgen/ast/definitions/enum_member.rb index 48bdfb470..c0d36f421 100644 --- a/lib/xdrgen/ast/definitions/enum_member.rb +++ b/lib/xdrgen/ast/definitions/enum_member.rb @@ -8,7 +8,12 @@ class EnumMember < Base def name_short prefix = find_common_prefix(enum.members.map(&:name)) - name.delete_prefix(prefix) + short = name.delete_prefix(prefix) + # Prefix the name with the first letter of the prefix if the name begins + # with a number, since in most languages identifiers cannot begin with + # numbers. + short = "#{prefix.first}#{short}" if /\A\d+/ === short + short end def value @@ -46,4 +51,4 @@ def defined_value end end end -end \ No newline at end of file +end diff --git a/lib/xdrgen/ast/definitions/union_arm_case.rb b/lib/xdrgen/ast/definitions/union_arm_case.rb index 498391d65..6a8ba1616 100644 --- a/lib/xdrgen/ast/definitions/union_arm_case.rb +++ b/lib/xdrgen/ast/definitions/union_arm_case.rb @@ -16,7 +16,12 @@ def value_s def name_short prefix = find_common_prefix(union.discriminant_type.members.map(&:name)) - value.name.delete_prefix(prefix) + short = value.name.delete_prefix(prefix) + # Prefix the name with the first letter of the prefix if the name begins + # with a number, since in most languages identifiers cannot begin with + # numbers. + short = "#{prefix.first}#{short}" if /\A\d+/ === short + short end end end diff --git a/spec/fixtures/generator/enum.x b/spec/fixtures/generator/enum.x index fda425cfb..1f7f59a7c 100644 --- a/spec/fixtures/generator/enum.x +++ b/spec/fixtures/generator/enum.x @@ -33,4 +33,10 @@ enum Color2 { RED2=RED, GREEN2=1, BLUE2=2 -}; \ No newline at end of file +}; + +enum Color3 { + RED_1=1, + RED_2_TWO=2, + RED_3=3 +}; diff --git a/spec/output/generator_spec_elixir/enum.x/MyXDR_generated.ex b/spec/output/generator_spec_elixir/enum.x/MyXDR_generated.ex index 85a9b236b..9390a0585 100644 --- a/spec/output/generator_spec_elixir/enum.x/MyXDR_generated.ex +++ b/spec/output/generator_spec_elixir/enum.x/MyXDR_generated.ex @@ -88,4 +88,21 @@ defmodule MyXDR do blue2: 2 ) + comment ~S""" + === xdr source ============================================================ + + enum Color3 { + RED_1=1, + RED_2_TWO=2, + RED_3=3 + }; + + =========================================================================== + """ + define_type("Color3", Enum, + red1: 1, + red2_two: 2, + red3: 3 + ) + end diff --git a/spec/output/generator_spec_go/enum.x/MyXDR_generated.go b/spec/output/generator_spec_go/enum.x/MyXDR_generated.go index 29f9683c1..3fb16f7fc 100644 --- a/spec/output/generator_spec_go/enum.x/MyXDR_generated.go +++ b/spec/output/generator_spec_go/enum.x/MyXDR_generated.go @@ -20,7 +20,7 @@ import ( // XdrFilesSHA256 is the SHA256 hashes of source files. var XdrFilesSHA256 = map[string]string{ - "spec/fixtures/generator/enum.x": "35cf5e97e2057039640ed260e8b38bb2733a3c3ca8529c93877bdec02a999d7f", + "spec/fixtures/generator/enum.x": "f764c2a2d349765e611f686e9d416b7f576ea881154d069355a2e75c898daf58", } var ErrMaxDecodingDepthReached = errors.New("maximum decoding depth reached") @@ -358,4 +358,89 @@ func (s Color2) xdrType() {} var _ xdrType = (*Color2)(nil) +// Color3 is an XDR Enum defines as: +// +// enum Color3 { +// RED_1=1, +// RED_2_TWO=2, +// RED_3=3 +// }; +// +type Color3 int32 +const ( + Color3Red1 Color3 = 1 + Color3Red2Two Color3 = 2 + Color3Red3 Color3 = 3 +) +var color3Map = map[int32]string{ + 1: "Color3Red1", + 2: "Color3Red2Two", + 3: "Color3Red3", +} + +// ValidEnum validates a proposed value for this enum. Implements +// the Enum interface for Color3 +func (e Color3) ValidEnum(v int32) bool { + _, ok := color3Map[v] + return ok +} +// String returns the name of `e` +func (e Color3) String() string { + name, _ := color3Map[int32(e)] + return name +} + +// EncodeTo encodes this value using the Encoder. +func (e Color3) EncodeTo(enc *xdr.Encoder) error { + if _, ok := color3Map[int32(e)]; !ok { + return fmt.Errorf("'%d' is not a valid Color3 enum value", e) + } + _, err := enc.EncodeInt(int32(e)) + return err +} +var _ decoderFrom = (*Color3)(nil) +// DecodeFrom decodes this value using the Decoder. +func (e *Color3) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, fmt.Errorf("decoding Color3: %w", ErrMaxDecodingDepthReached) + } + maxDepth -= 1 + v, n, err := d.DecodeInt() + if err != nil { + return n, fmt.Errorf("decoding Color3: %w", err) + } + if _, ok := color3Map[v]; !ok { + return n, fmt.Errorf("'%d' is not a valid Color3 enum value", v) + } + *e = Color3(v) + return n, nil +} +// MarshalBinary implements encoding.BinaryMarshaler. +func (s Color3) MarshalBinary() ([]byte, error) { + b := bytes.Buffer{} + e := xdr.NewEncoder(&b) + err := s.EncodeTo(e) + return b.Bytes(), err +} + +// UnmarshalBinary implements encoding.BinaryUnmarshaler. +func (s *Color3) UnmarshalBinary(inp []byte) error { + r := bytes.NewReader(inp) + o := xdr.DefaultDecodeOptions + o.MaxInputLen = len(inp) + d := xdr.NewDecoderWithOptions(r, o) + _, err := s.DecodeFrom(d, o.MaxDepth) + return err +} + +var ( + _ encoding.BinaryMarshaler = (*Color3)(nil) + _ encoding.BinaryUnmarshaler = (*Color3)(nil) +) + +// xdrType signals that this type represents XDR values defined by this package. +func (s Color3) xdrType() {} + +var _ xdrType = (*Color3)(nil) + var fmtTest = fmt.Sprint("this is a dummy usage of fmt") diff --git a/spec/output/generator_spec_java/enum.x/Color3.java b/spec/output/generator_spec_java/enum.x/Color3.java new file mode 100644 index 000000000..210d964db --- /dev/null +++ b/spec/output/generator_spec_java/enum.x/Color3.java @@ -0,0 +1,61 @@ +// Automatically generated by xdrgen +// DO NOT EDIT or your changes may be overwritten + +package MyXDR; + +import java.io.IOException; + +import org.stellar.sdk.Base64Factory; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; + +/** + * Color3's original definition in the XDR file is: + *
+ * enum Color3 {
+ *     RED_1=1,
+ *     RED_2_TWO=2,
+ *     RED_3=3
+ * };
+ * 
+ */ +public enum Color3 implements XdrElement { + RED_1(1), + RED_2_TWO(2), + RED_3(3); + + private final int value; + + Color3(int value) { + this.value = value; + } + + public int getValue() { + return value; + } + + public static Color3 decode(XdrDataInputStream stream) throws IOException { + int value = stream.readInt(); + switch (value) { + case 1: return RED_1; + case 2: return RED_2_TWO; + case 3: return RED_3; + default: + throw new IllegalArgumentException("Unknown enum value: " + value); + } + } + + public void encode(XdrDataOutputStream stream) throws IOException { + stream.writeInt(value); + } + public static Color3 fromXdrBase64(String xdr) throws IOException { + byte[] bytes = Base64Factory.getInstance().decode(xdr); + return fromXdrByteArray(bytes); + } + + public static Color3 fromXdrByteArray(byte[] xdr) throws IOException { + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); + XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); + return decode(xdrDataInputStream); + } +} diff --git a/spec/output/generator_spec_java/enum.x/Red.java b/spec/output/generator_spec_java/enum.x/Red.java new file mode 100644 index 000000000..cd6ac0f15 --- /dev/null +++ b/spec/output/generator_spec_java/enum.x/Red.java @@ -0,0 +1,61 @@ +// Automatically generated by xdrgen +// DO NOT EDIT or your changes may be overwritten + +package MyXDR; + +import java.io.IOException; + +import org.stellar.sdk.Base64Factory; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; + +/** + * Red's original definition in the XDR file is: + *
+ * enum Red {
+ *     RED1=1,
+ *     RED2=2,
+ *     RED3=3
+ * };
+ * 
+ */ +public enum Red implements XdrElement { + RED1(1), + RED2(2), + RED3(3); + + private final int value; + + Red(int value) { + this.value = value; + } + + public int getValue() { + return value; + } + + public static Red decode(XdrDataInputStream stream) throws IOException { + int value = stream.readInt(); + switch (value) { + case 1: return RED1; + case 2: return RED2; + case 3: return RED3; + default: + throw new IllegalArgumentException("Unknown enum value: " + value); + } + } + + public void encode(XdrDataOutputStream stream) throws IOException { + stream.writeInt(value); + } + public static Red fromXdrBase64(String xdr) throws IOException { + byte[] bytes = Base64Factory.getInstance().decode(xdr); + return fromXdrByteArray(bytes); + } + + public static Red fromXdrByteArray(byte[] xdr) throws IOException { + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); + XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); + return decode(xdrDataInputStream); + } +} diff --git a/spec/output/generator_spec_javascript/enum.x/MyXDR_generated.js b/spec/output/generator_spec_javascript/enum.x/MyXDR_generated.js index b4cb05a24..5e58aad8d 100644 --- a/spec/output/generator_spec_javascript/enum.x/MyXDR_generated.js +++ b/spec/output/generator_spec_javascript/enum.x/MyXDR_generated.js @@ -82,5 +82,20 @@ xdr.enum("Color2", { blue2: 2, }); +// === xdr source ============================================================ +// +// enum Color3 { +// RED_1=1, +// RED_2_TWO=2, +// RED_3=3 +// }; +// +// =========================================================================== +xdr.enum("Color3", { + red1: 1, + red2Two: 2, + red3: 3, +}); + }); export default types; diff --git a/spec/output/generator_spec_python/enum.x/__init__.py b/spec/output/generator_spec_python/enum.x/__init__.py index 0a9eac2af..3d4636082 100644 --- a/spec/output/generator_spec_python/enum.x/__init__.py +++ b/spec/output/generator_spec_python/enum.x/__init__.py @@ -5,3 +5,4 @@ from .message_type import MessageType from .color import Color from .color2 import Color2 +from .color3 import Color3 diff --git a/spec/output/generator_spec_python/enum.x/color3.py b/spec/output/generator_spec_python/enum.x/color3.py new file mode 100644 index 000000000..449df02ca --- /dev/null +++ b/spec/output/generator_spec_python/enum.x/color3.py @@ -0,0 +1,50 @@ +# This is an automatically generated file. +# DO NOT EDIT or your changes may be overwritten +from __future__ import annotations + +import base64 +from enum import IntEnum +from typing import List, Optional, TYPE_CHECKING +from xdrlib3 import Packer, Unpacker +from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque +from .constants import * + +__all__ = ['Color3'] +class Color3(IntEnum): + """ + XDR Source Code:: + + enum Color3 { + RED_1=1, + RED_2_TWO=2, + RED_3=3 + }; + """ + RED_1 = 1 + RED_2_TWO = 2 + RED_3 = 3 + def pack(self, packer: Packer) -> None: + packer.pack_int(self.value) + + @classmethod + def unpack(cls, unpacker: Unpacker) -> Color3: + value = unpacker.unpack_int() + return cls(value) + def to_xdr_bytes(self) -> bytes: + packer = Packer() + self.pack(packer) + return packer.get_buffer() + + @classmethod + def from_xdr_bytes(cls, xdr: bytes) -> Color3: + unpacker = Unpacker(xdr) + return cls.unpack(unpacker) + + def to_xdr(self) -> str: + xdr_bytes = self.to_xdr_bytes() + return base64.b64encode(xdr_bytes).decode() + + @classmethod + def from_xdr(cls, xdr: str) -> Color3: + xdr_bytes = base64.b64decode(xdr.encode()) + return cls.from_xdr_bytes(xdr_bytes) diff --git a/spec/output/generator_spec_python/enum.x/red.py b/spec/output/generator_spec_python/enum.x/red.py new file mode 100644 index 000000000..eb311195d --- /dev/null +++ b/spec/output/generator_spec_python/enum.x/red.py @@ -0,0 +1,50 @@ +# This is an automatically generated file. +# DO NOT EDIT or your changes may be overwritten +from __future__ import annotations + +import base64 +from enum import IntEnum +from typing import List, Optional, TYPE_CHECKING +from xdrlib3 import Packer, Unpacker +from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque +from .constants import * + +__all__ = ['Red'] +class Red(IntEnum): + """ + XDR Source Code:: + + enum Red { + RED1=1, + RED2=2, + RED3=3 + }; + """ + RED1 = 1 + RED2 = 2 + RED3 = 3 + def pack(self, packer: Packer) -> None: + packer.pack_int(self.value) + + @classmethod + def unpack(cls, unpacker: Unpacker) -> Red: + value = unpacker.unpack_int() + return cls(value) + def to_xdr_bytes(self) -> bytes: + packer = Packer() + self.pack(packer) + return packer.get_buffer() + + @classmethod + def from_xdr_bytes(cls, xdr: bytes) -> Red: + unpacker = Unpacker(xdr) + return cls.unpack(unpacker) + + def to_xdr(self) -> str: + xdr_bytes = self.to_xdr_bytes() + return base64.b64encode(xdr_bytes).decode() + + @classmethod + def from_xdr(cls, xdr: str) -> Red: + xdr_bytes = base64.b64decode(xdr.encode()) + return cls.from_xdr_bytes(xdr_bytes) diff --git a/spec/output/generator_spec_ruby/enum.x/MyXDR.rb b/spec/output/generator_spec_ruby/enum.x/MyXDR.rb index 5509a6f3e..ab042ef17 100644 --- a/spec/output/generator_spec_ruby/enum.x/MyXDR.rb +++ b/spec/output/generator_spec_ruby/enum.x/MyXDR.rb @@ -6,3 +6,4 @@ autoload :MessageType autoload :Color autoload :Color2 +autoload :Color3 diff --git a/spec/output/generator_spec_ruby/enum.x/color3.rb b/spec/output/generator_spec_ruby/enum.x/color3.rb new file mode 100644 index 000000000..0568da32b --- /dev/null +++ b/spec/output/generator_spec_ruby/enum.x/color3.rb @@ -0,0 +1,21 @@ +# This code was automatically generated using xdrgen +# DO NOT EDIT or your changes may be overwritten + +require 'xdr' + +# === xdr source ============================================================ +# +# enum Color3 { +# RED_1=1, +# RED_2_TWO=2, +# RED_3=3 +# }; +# +# =========================================================================== +class Color3 < XDR::Enum + member :red_1, 1 + member :red_2_two, 2 + member :red_3, 3 + + seal +end diff --git a/spec/output/generator_spec_ruby/enum.x/red.rb b/spec/output/generator_spec_ruby/enum.x/red.rb new file mode 100644 index 000000000..552a01768 --- /dev/null +++ b/spec/output/generator_spec_ruby/enum.x/red.rb @@ -0,0 +1,21 @@ +# This code was automatically generated using xdrgen +# DO NOT EDIT or your changes may be overwritten + +require 'xdr' + +# === xdr source ============================================================ +# +# enum Red { +# RED1=1, +# RED2=2, +# RED3=3 +# }; +# +# =========================================================================== +class Red < XDR::Enum + member :red1, 1 + member :red2, 2 + member :red3, 3 + + seal +end diff --git a/spec/output/generator_spec_rust/enum.x/MyXDR.rs b/spec/output/generator_spec_rust/enum.x/MyXDR.rs index 276d6dca4..093bb05a7 100644 --- a/spec/output/generator_spec_rust/enum.x/MyXDR.rs +++ b/spec/output/generator_spec_rust/enum.x/MyXDR.rs @@ -5,7 +5,7 @@ /// `XDR_FILES_SHA256` is a list of pairs of source files and their SHA256 hashes. pub const XDR_FILES_SHA256: [(&str, &str); 1] = [ - ("spec/fixtures/generator/enum.x", "35cf5e97e2057039640ed260e8b38bb2733a3c3ca8529c93877bdec02a999d7f") + ("spec/fixtures/generator/enum.x", "f764c2a2d349765e611f686e9d416b7f576ea881154d069355a2e75c898daf58") ]; use core::{array::TryFromSliceError, fmt, fmt::Debug, marker::Sized, ops::Deref, slice}; @@ -3178,6 +3178,115 @@ Self::Blue2 => "Blue2", } } +/// Color3 is an XDR Enum defines as: +/// +/// ```text +/// enum Color3 { +/// RED_1=1, +/// RED_2_TWO=2, +/// RED_3=3 +/// }; +/// ``` +/// +// enum +#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(feature = "arbitrary", derive(Arbitrary))] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "snake_case"))] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[repr(i32)] +pub enum Color3 { + R1 = 1, + R2Two = 2, + R3 = 3, +} + + impl Color3 { + pub const VARIANTS: [Color3; 3] = [ Color3::R1, +Color3::R2Two, +Color3::R3, ]; + pub const VARIANTS_STR: [&'static str; 3] = [ "R1", +"R2Two", +"R3", ]; + + #[must_use] + pub const fn name(&self) -> &'static str { + match self { + Self::R1 => "R1", +Self::R2Two => "R2Two", +Self::R3 => "R3", + } + } + + #[must_use] + pub const fn variants() -> [Color3; 3] { + Self::VARIANTS + } + } + + impl Name for Color3 { + #[must_use] + fn name(&self) -> &'static str { + Self::name(self) + } + } + + impl Variants for Color3 { + fn variants() -> slice::Iter<'static, Color3> { + Self::VARIANTS.iter() + } + } + + impl Enum for Color3 {} + + impl fmt::Display for Color3 { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(self.name()) + } + } + + impl TryFrom for Color3 { + type Error = Error; + + fn try_from(i: i32) -> Result { + let e = match i { + 1 => Color3::R1, +2 => Color3::R2Two, +3 => Color3::R3, + #[allow(unreachable_patterns)] + _ => return Err(Error::Invalid), + }; + Ok(e) + } + } + + impl From for i32 { + #[must_use] + fn from(e: Color3) -> Self { + e as Self + } + } + + impl ReadXdr for Color3 { + #[cfg(feature = "std")] + fn read_xdr(r: &mut Limited) -> Result { + r.with_limited_depth(|r| { + let e = i32::read_xdr(r)?; + let v: Self = e.try_into()?; + Ok(v) + }) + } + } + + impl WriteXdr for Color3 { + #[cfg(feature = "std")] + fn write_xdr(&self, w: &mut Limited) -> Result<()> { + w.with_limited_depth(|w| { + let i: i32 = (*self).into(); + i.write_xdr(w) + }) + } + } + #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] #[cfg_attr( all(feature = "serde", feature = "alloc"), @@ -3189,15 +3298,18 @@ Self::Blue2 => "Blue2", MessageType, Color, Color2, +Color3, } impl TypeVariant { - pub const VARIANTS: [TypeVariant; 3] = [ TypeVariant::MessageType, + pub const VARIANTS: [TypeVariant; 4] = [ TypeVariant::MessageType, TypeVariant::Color, -TypeVariant::Color2, ]; - pub const VARIANTS_STR: [&'static str; 3] = [ "MessageType", +TypeVariant::Color2, +TypeVariant::Color3, ]; + pub const VARIANTS_STR: [&'static str; 4] = [ "MessageType", "Color", -"Color2", ]; +"Color2", +"Color3", ]; #[must_use] #[allow(clippy::too_many_lines)] @@ -3206,12 +3318,13 @@ TypeVariant::Color2, ]; Self::MessageType => "MessageType", Self::Color => "Color", Self::Color2 => "Color2", +Self::Color3 => "Color3", } } #[must_use] #[allow(clippy::too_many_lines)] - pub const fn variants() -> [TypeVariant; 3] { + pub const fn variants() -> [TypeVariant; 4] { Self::VARIANTS } @@ -3223,6 +3336,7 @@ Self::Color2 => "Color2", Self::MessageType => gen.into_root_schema_for::(), Self::Color => gen.into_root_schema_for::(), Self::Color2 => gen.into_root_schema_for::(), +Self::Color3 => gen.into_root_schema_for::(), } } } @@ -3248,6 +3362,7 @@ Self::Color2 => gen.into_root_schema_for::(), "MessageType" => Ok(Self::MessageType), "Color" => Ok(Self::Color), "Color2" => Ok(Self::Color2), +"Color3" => Ok(Self::Color3), _ => Err(Error::Invalid), } } @@ -3265,15 +3380,18 @@ Self::Color2 => gen.into_root_schema_for::(), MessageType(Box), Color(Box), Color2(Box), +Color3(Box), } impl Type { - pub const VARIANTS: [TypeVariant; 3] = [ TypeVariant::MessageType, + pub const VARIANTS: [TypeVariant; 4] = [ TypeVariant::MessageType, TypeVariant::Color, -TypeVariant::Color2, ]; - pub const VARIANTS_STR: [&'static str; 3] = [ "MessageType", +TypeVariant::Color2, +TypeVariant::Color3, ]; + pub const VARIANTS_STR: [&'static str; 4] = [ "MessageType", "Color", -"Color2", ]; +"Color2", +"Color3", ]; #[cfg(feature = "std")] #[allow(clippy::too_many_lines)] @@ -3282,6 +3400,7 @@ TypeVariant::Color2, ]; TypeVariant::MessageType => r.with_limited_depth(|r| Ok(Self::MessageType(Box::new(MessageType::read_xdr(r)?)))), TypeVariant::Color => r.with_limited_depth(|r| Ok(Self::Color(Box::new(Color::read_xdr(r)?)))), TypeVariant::Color2 => r.with_limited_depth(|r| Ok(Self::Color2(Box::new(Color2::read_xdr(r)?)))), +TypeVariant::Color3 => r.with_limited_depth(|r| Ok(Self::Color3(Box::new(Color3::read_xdr(r)?)))), } } @@ -3318,6 +3437,7 @@ TypeVariant::Color2 => r.with_limited_depth(|r| Ok(Self::Color2(Box::new(Color2: TypeVariant::MessageType => Box::new(ReadXdrIter::<_, MessageType>::new(&mut r.inner, r.limits.clone()).map(|r| r.map(|t| Self::MessageType(Box::new(t))))), TypeVariant::Color => Box::new(ReadXdrIter::<_, Color>::new(&mut r.inner, r.limits.clone()).map(|r| r.map(|t| Self::Color(Box::new(t))))), TypeVariant::Color2 => Box::new(ReadXdrIter::<_, Color2>::new(&mut r.inner, r.limits.clone()).map(|r| r.map(|t| Self::Color2(Box::new(t))))), +TypeVariant::Color3 => Box::new(ReadXdrIter::<_, Color3>::new(&mut r.inner, r.limits.clone()).map(|r| r.map(|t| Self::Color3(Box::new(t))))), } } @@ -3328,6 +3448,7 @@ TypeVariant::Color2 => Box::new(ReadXdrIter::<_, Color2>::new(&mut r.inner, r.li TypeVariant::MessageType => Box::new(ReadXdrIter::<_, Frame>::new(&mut r.inner, r.limits.clone()).map(|r| r.map(|t| Self::MessageType(Box::new(t.0))))), TypeVariant::Color => Box::new(ReadXdrIter::<_, Frame>::new(&mut r.inner, r.limits.clone()).map(|r| r.map(|t| Self::Color(Box::new(t.0))))), TypeVariant::Color2 => Box::new(ReadXdrIter::<_, Frame>::new(&mut r.inner, r.limits.clone()).map(|r| r.map(|t| Self::Color2(Box::new(t.0))))), +TypeVariant::Color3 => Box::new(ReadXdrIter::<_, Frame>::new(&mut r.inner, r.limits.clone()).map(|r| r.map(|t| Self::Color3(Box::new(t.0))))), } } @@ -3339,6 +3460,7 @@ TypeVariant::Color2 => Box::new(ReadXdrIter::<_, Frame>::new(&mut r.inne TypeVariant::MessageType => Box::new(ReadXdrIter::<_, MessageType>::new(dec, r.limits.clone()).map(|r| r.map(|t| Self::MessageType(Box::new(t))))), TypeVariant::Color => Box::new(ReadXdrIter::<_, Color>::new(dec, r.limits.clone()).map(|r| r.map(|t| Self::Color(Box::new(t))))), TypeVariant::Color2 => Box::new(ReadXdrIter::<_, Color2>::new(dec, r.limits.clone()).map(|r| r.map(|t| Self::Color2(Box::new(t))))), +TypeVariant::Color3 => Box::new(ReadXdrIter::<_, Color3>::new(dec, r.limits.clone()).map(|r| r.map(|t| Self::Color3(Box::new(t))))), } } @@ -3364,6 +3486,7 @@ TypeVariant::Color2 => Box::new(ReadXdrIter::<_, Color2>::new(dec, r.limits.clon TypeVariant::MessageType => Ok(Self::MessageType(Box::new(serde_json::from_reader(r)?))), TypeVariant::Color => Ok(Self::Color(Box::new(serde_json::from_reader(r)?))), TypeVariant::Color2 => Ok(Self::Color2(Box::new(serde_json::from_reader(r)?))), +TypeVariant::Color3 => Ok(Self::Color3(Box::new(serde_json::from_reader(r)?))), } } @@ -3376,6 +3499,7 @@ TypeVariant::Color2 => Ok(Self::Color2(Box::new(serde_json::from_reader(r)?))), Self::MessageType(ref v) => v.as_ref(), Self::Color(ref v) => v.as_ref(), Self::Color2(ref v) => v.as_ref(), +Self::Color3(ref v) => v.as_ref(), } } @@ -3386,12 +3510,13 @@ Self::Color2(ref v) => v.as_ref(), Self::MessageType(_) => "MessageType", Self::Color(_) => "Color", Self::Color2(_) => "Color2", +Self::Color3(_) => "Color3", } } #[must_use] #[allow(clippy::too_many_lines)] - pub const fn variants() -> [TypeVariant; 3] { + pub const fn variants() -> [TypeVariant; 4] { Self::VARIANTS } @@ -3402,6 +3527,7 @@ Self::Color2(_) => "Color2", Self::MessageType(_) => TypeVariant::MessageType, Self::Color(_) => TypeVariant::Color, Self::Color2(_) => TypeVariant::Color2, +Self::Color3(_) => TypeVariant::Color3, } } } @@ -3427,6 +3553,7 @@ Self::Color2(_) => TypeVariant::Color2, Self::MessageType(v) => v.write_xdr(w), Self::Color(v) => v.write_xdr(w), Self::Color2(v) => v.write_xdr(w), +Self::Color3(v) => v.write_xdr(w), } } } diff --git a/spec/output/generator_spec_rust/test.x/MyXDR.rs b/spec/output/generator_spec_rust/test.x/MyXDR.rs index 4bfffad0c..6b43a7501 100644 --- a/spec/output/generator_spec_rust/test.x/MyXDR.rs +++ b/spec/output/generator_spec_rust/test.x/MyXDR.rs @@ -4190,21 +4190,21 @@ pub const BAR: u64 = FOO; #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] #[repr(i32)] pub enum NesterNestedEnum { - 1 = 0, - 2 = 1, + B1 = 0, + B2 = 1, } impl NesterNestedEnum { - pub const VARIANTS: [NesterNestedEnum; 2] = [ NesterNestedEnum::1, -NesterNestedEnum::2, ]; - pub const VARIANTS_STR: [&'static str; 2] = [ "1", -"2", ]; + pub const VARIANTS: [NesterNestedEnum; 2] = [ NesterNestedEnum::B1, +NesterNestedEnum::B2, ]; + pub const VARIANTS_STR: [&'static str; 2] = [ "B1", +"B2", ]; #[must_use] pub const fn name(&self) -> &'static str { match self { - Self::1 => "1", -Self::2 => "2", + Self::B1 => "B1", +Self::B2 => "B2", } } @@ -4240,8 +4240,8 @@ Self::2 => "2", fn try_from(i: i32) -> Result { let e = match i { - 0 => NesterNestedEnum::1, -1 => NesterNestedEnum::2, + 0 => NesterNestedEnum::B1, +1 => NesterNestedEnum::B2, #[allow(unreachable_patterns)] _ => return Err(Error::Invalid), }; diff --git a/spec/output/generator_spec_rust_custom_jsonschema_impls/enum.x/MyXDR.rs b/spec/output/generator_spec_rust_custom_jsonschema_impls/enum.x/MyXDR.rs index f85776cdd..7b37f5d9c 100644 --- a/spec/output/generator_spec_rust_custom_jsonschema_impls/enum.x/MyXDR.rs +++ b/spec/output/generator_spec_rust_custom_jsonschema_impls/enum.x/MyXDR.rs @@ -5,7 +5,7 @@ /// `XDR_FILES_SHA256` is a list of pairs of source files and their SHA256 hashes. pub const XDR_FILES_SHA256: [(&str, &str); 1] = [ - ("spec/fixtures/generator/enum.x", "35cf5e97e2057039640ed260e8b38bb2733a3c3ca8529c93877bdec02a999d7f") + ("spec/fixtures/generator/enum.x", "f764c2a2d349765e611f686e9d416b7f576ea881154d069355a2e75c898daf58") ]; use core::{array::TryFromSliceError, fmt, fmt::Debug, marker::Sized, ops::Deref, slice}; @@ -3177,6 +3177,115 @@ Self::Blue2 => "Blue2", } } +/// Color3 is an XDR Enum defines as: +/// +/// ```text +/// enum Color3 { +/// RED_1=1, +/// RED_2_TWO=2, +/// RED_3=3 +/// }; +/// ``` +/// +// enum +#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(feature = "arbitrary", derive(Arbitrary))] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "snake_case"))] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[repr(i32)] +pub enum Color3 { + R1 = 1, + R2Two = 2, + R3 = 3, +} + + impl Color3 { + pub const VARIANTS: [Color3; 3] = [ Color3::R1, +Color3::R2Two, +Color3::R3, ]; + pub const VARIANTS_STR: [&'static str; 3] = [ "R1", +"R2Two", +"R3", ]; + + #[must_use] + pub const fn name(&self) -> &'static str { + match self { + Self::R1 => "R1", +Self::R2Two => "R2Two", +Self::R3 => "R3", + } + } + + #[must_use] + pub const fn variants() -> [Color3; 3] { + Self::VARIANTS + } + } + + impl Name for Color3 { + #[must_use] + fn name(&self) -> &'static str { + Self::name(self) + } + } + + impl Variants for Color3 { + fn variants() -> slice::Iter<'static, Color3> { + Self::VARIANTS.iter() + } + } + + impl Enum for Color3 {} + + impl fmt::Display for Color3 { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(self.name()) + } + } + + impl TryFrom for Color3 { + type Error = Error; + + fn try_from(i: i32) -> Result { + let e = match i { + 1 => Color3::R1, +2 => Color3::R2Two, +3 => Color3::R3, + #[allow(unreachable_patterns)] + _ => return Err(Error::Invalid), + }; + Ok(e) + } + } + + impl From for i32 { + #[must_use] + fn from(e: Color3) -> Self { + e as Self + } + } + + impl ReadXdr for Color3 { + #[cfg(feature = "std")] + fn read_xdr(r: &mut Limited) -> Result { + r.with_limited_depth(|r| { + let e = i32::read_xdr(r)?; + let v: Self = e.try_into()?; + Ok(v) + }) + } + } + + impl WriteXdr for Color3 { + #[cfg(feature = "std")] + fn write_xdr(&self, w: &mut Limited) -> Result<()> { + w.with_limited_depth(|w| { + let i: i32 = (*self).into(); + i.write_xdr(w) + }) + } + } + #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] #[cfg_attr( all(feature = "serde", feature = "alloc"), @@ -3188,15 +3297,18 @@ Self::Blue2 => "Blue2", MessageType, Color, Color2, +Color3, } impl TypeVariant { - pub const VARIANTS: [TypeVariant; 3] = [ TypeVariant::MessageType, + pub const VARIANTS: [TypeVariant; 4] = [ TypeVariant::MessageType, TypeVariant::Color, -TypeVariant::Color2, ]; - pub const VARIANTS_STR: [&'static str; 3] = [ "MessageType", +TypeVariant::Color2, +TypeVariant::Color3, ]; + pub const VARIANTS_STR: [&'static str; 4] = [ "MessageType", "Color", -"Color2", ]; +"Color2", +"Color3", ]; #[must_use] #[allow(clippy::too_many_lines)] @@ -3205,12 +3317,13 @@ TypeVariant::Color2, ]; Self::MessageType => "MessageType", Self::Color => "Color", Self::Color2 => "Color2", +Self::Color3 => "Color3", } } #[must_use] #[allow(clippy::too_many_lines)] - pub const fn variants() -> [TypeVariant; 3] { + pub const fn variants() -> [TypeVariant; 4] { Self::VARIANTS } @@ -3222,6 +3335,7 @@ Self::Color2 => "Color2", Self::MessageType => gen.into_root_schema_for::(), Self::Color => gen.into_root_schema_for::(), Self::Color2 => gen.into_root_schema_for::(), +Self::Color3 => gen.into_root_schema_for::(), } } } @@ -3247,6 +3361,7 @@ Self::Color2 => gen.into_root_schema_for::(), "MessageType" => Ok(Self::MessageType), "Color" => Ok(Self::Color), "Color2" => Ok(Self::Color2), +"Color3" => Ok(Self::Color3), _ => Err(Error::Invalid), } } @@ -3264,15 +3379,18 @@ Self::Color2 => gen.into_root_schema_for::(), MessageType(Box), Color(Box), Color2(Box), +Color3(Box), } impl Type { - pub const VARIANTS: [TypeVariant; 3] = [ TypeVariant::MessageType, + pub const VARIANTS: [TypeVariant; 4] = [ TypeVariant::MessageType, TypeVariant::Color, -TypeVariant::Color2, ]; - pub const VARIANTS_STR: [&'static str; 3] = [ "MessageType", +TypeVariant::Color2, +TypeVariant::Color3, ]; + pub const VARIANTS_STR: [&'static str; 4] = [ "MessageType", "Color", -"Color2", ]; +"Color2", +"Color3", ]; #[cfg(feature = "std")] #[allow(clippy::too_many_lines)] @@ -3281,6 +3399,7 @@ TypeVariant::Color2, ]; TypeVariant::MessageType => r.with_limited_depth(|r| Ok(Self::MessageType(Box::new(MessageType::read_xdr(r)?)))), TypeVariant::Color => r.with_limited_depth(|r| Ok(Self::Color(Box::new(Color::read_xdr(r)?)))), TypeVariant::Color2 => r.with_limited_depth(|r| Ok(Self::Color2(Box::new(Color2::read_xdr(r)?)))), +TypeVariant::Color3 => r.with_limited_depth(|r| Ok(Self::Color3(Box::new(Color3::read_xdr(r)?)))), } } @@ -3317,6 +3436,7 @@ TypeVariant::Color2 => r.with_limited_depth(|r| Ok(Self::Color2(Box::new(Color2: TypeVariant::MessageType => Box::new(ReadXdrIter::<_, MessageType>::new(&mut r.inner, r.limits.clone()).map(|r| r.map(|t| Self::MessageType(Box::new(t))))), TypeVariant::Color => Box::new(ReadXdrIter::<_, Color>::new(&mut r.inner, r.limits.clone()).map(|r| r.map(|t| Self::Color(Box::new(t))))), TypeVariant::Color2 => Box::new(ReadXdrIter::<_, Color2>::new(&mut r.inner, r.limits.clone()).map(|r| r.map(|t| Self::Color2(Box::new(t))))), +TypeVariant::Color3 => Box::new(ReadXdrIter::<_, Color3>::new(&mut r.inner, r.limits.clone()).map(|r| r.map(|t| Self::Color3(Box::new(t))))), } } @@ -3327,6 +3447,7 @@ TypeVariant::Color2 => Box::new(ReadXdrIter::<_, Color2>::new(&mut r.inner, r.li TypeVariant::MessageType => Box::new(ReadXdrIter::<_, Frame>::new(&mut r.inner, r.limits.clone()).map(|r| r.map(|t| Self::MessageType(Box::new(t.0))))), TypeVariant::Color => Box::new(ReadXdrIter::<_, Frame>::new(&mut r.inner, r.limits.clone()).map(|r| r.map(|t| Self::Color(Box::new(t.0))))), TypeVariant::Color2 => Box::new(ReadXdrIter::<_, Frame>::new(&mut r.inner, r.limits.clone()).map(|r| r.map(|t| Self::Color2(Box::new(t.0))))), +TypeVariant::Color3 => Box::new(ReadXdrIter::<_, Frame>::new(&mut r.inner, r.limits.clone()).map(|r| r.map(|t| Self::Color3(Box::new(t.0))))), } } @@ -3338,6 +3459,7 @@ TypeVariant::Color2 => Box::new(ReadXdrIter::<_, Frame>::new(&mut r.inne TypeVariant::MessageType => Box::new(ReadXdrIter::<_, MessageType>::new(dec, r.limits.clone()).map(|r| r.map(|t| Self::MessageType(Box::new(t))))), TypeVariant::Color => Box::new(ReadXdrIter::<_, Color>::new(dec, r.limits.clone()).map(|r| r.map(|t| Self::Color(Box::new(t))))), TypeVariant::Color2 => Box::new(ReadXdrIter::<_, Color2>::new(dec, r.limits.clone()).map(|r| r.map(|t| Self::Color2(Box::new(t))))), +TypeVariant::Color3 => Box::new(ReadXdrIter::<_, Color3>::new(dec, r.limits.clone()).map(|r| r.map(|t| Self::Color3(Box::new(t))))), } } @@ -3363,6 +3485,7 @@ TypeVariant::Color2 => Box::new(ReadXdrIter::<_, Color2>::new(dec, r.limits.clon TypeVariant::MessageType => Ok(Self::MessageType(Box::new(serde_json::from_reader(r)?))), TypeVariant::Color => Ok(Self::Color(Box::new(serde_json::from_reader(r)?))), TypeVariant::Color2 => Ok(Self::Color2(Box::new(serde_json::from_reader(r)?))), +TypeVariant::Color3 => Ok(Self::Color3(Box::new(serde_json::from_reader(r)?))), } } @@ -3375,6 +3498,7 @@ TypeVariant::Color2 => Ok(Self::Color2(Box::new(serde_json::from_reader(r)?))), Self::MessageType(ref v) => v.as_ref(), Self::Color(ref v) => v.as_ref(), Self::Color2(ref v) => v.as_ref(), +Self::Color3(ref v) => v.as_ref(), } } @@ -3385,12 +3509,13 @@ Self::Color2(ref v) => v.as_ref(), Self::MessageType(_) => "MessageType", Self::Color(_) => "Color", Self::Color2(_) => "Color2", +Self::Color3(_) => "Color3", } } #[must_use] #[allow(clippy::too_many_lines)] - pub const fn variants() -> [TypeVariant; 3] { + pub const fn variants() -> [TypeVariant; 4] { Self::VARIANTS } @@ -3401,6 +3526,7 @@ Self::Color2(_) => "Color2", Self::MessageType(_) => TypeVariant::MessageType, Self::Color(_) => TypeVariant::Color, Self::Color2(_) => TypeVariant::Color2, +Self::Color3(_) => TypeVariant::Color3, } } } @@ -3426,6 +3552,7 @@ Self::Color2(_) => TypeVariant::Color2, Self::MessageType(v) => v.write_xdr(w), Self::Color(v) => v.write_xdr(w), Self::Color2(v) => v.write_xdr(w), +Self::Color3(v) => v.write_xdr(w), } } } diff --git a/spec/output/generator_spec_rust_custom_jsonschema_impls/test.x/MyXDR.rs b/spec/output/generator_spec_rust_custom_jsonschema_impls/test.x/MyXDR.rs index fdc5594db..cc984fe0f 100644 --- a/spec/output/generator_spec_rust_custom_jsonschema_impls/test.x/MyXDR.rs +++ b/spec/output/generator_spec_rust_custom_jsonschema_impls/test.x/MyXDR.rs @@ -4188,21 +4188,21 @@ pub const BAR: u64 = FOO; #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] #[repr(i32)] pub enum NesterNestedEnum { - 1 = 0, - 2 = 1, + B1 = 0, + B2 = 1, } impl NesterNestedEnum { - pub const VARIANTS: [NesterNestedEnum; 2] = [ NesterNestedEnum::1, -NesterNestedEnum::2, ]; - pub const VARIANTS_STR: [&'static str; 2] = [ "1", -"2", ]; + pub const VARIANTS: [NesterNestedEnum; 2] = [ NesterNestedEnum::B1, +NesterNestedEnum::B2, ]; + pub const VARIANTS_STR: [&'static str; 2] = [ "B1", +"B2", ]; #[must_use] pub const fn name(&self) -> &'static str { match self { - Self::1 => "1", -Self::2 => "2", + Self::B1 => "B1", +Self::B2 => "B2", } } @@ -4238,8 +4238,8 @@ Self::2 => "2", fn try_from(i: i32) -> Result { let e = match i { - 0 => NesterNestedEnum::1, -1 => NesterNestedEnum::2, + 0 => NesterNestedEnum::B1, +1 => NesterNestedEnum::B2, #[allow(unreachable_patterns)] _ => return Err(Error::Invalid), }; diff --git a/spec/output/generator_spec_rust_custom_str_impls/enum.x/MyXDR.rs b/spec/output/generator_spec_rust_custom_str_impls/enum.x/MyXDR.rs index 650ffec70..d900ec353 100644 --- a/spec/output/generator_spec_rust_custom_str_impls/enum.x/MyXDR.rs +++ b/spec/output/generator_spec_rust_custom_str_impls/enum.x/MyXDR.rs @@ -5,7 +5,7 @@ /// `XDR_FILES_SHA256` is a list of pairs of source files and their SHA256 hashes. pub const XDR_FILES_SHA256: [(&str, &str); 1] = [ - ("spec/fixtures/generator/enum.x", "35cf5e97e2057039640ed260e8b38bb2733a3c3ca8529c93877bdec02a999d7f") + ("spec/fixtures/generator/enum.x", "f764c2a2d349765e611f686e9d416b7f576ea881154d069355a2e75c898daf58") ]; use core::{array::TryFromSliceError, fmt, fmt::Debug, marker::Sized, ops::Deref, slice}; @@ -3178,6 +3178,115 @@ Self::Blue2 => "Blue2", } } +/// Color3 is an XDR Enum defines as: +/// +/// ```text +/// enum Color3 { +/// RED_1=1, +/// RED_2_TWO=2, +/// RED_3=3 +/// }; +/// ``` +/// +// enum +#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(feature = "arbitrary", derive(Arbitrary))] +#[cfg_attr(all(feature = "serde", feature = "alloc"), derive(serde::Serialize, serde::Deserialize), serde(rename_all = "snake_case"))] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[repr(i32)] +pub enum Color3 { + R1 = 1, + R2Two = 2, + R3 = 3, +} + + impl Color3 { + pub const VARIANTS: [Color3; 3] = [ Color3::R1, +Color3::R2Two, +Color3::R3, ]; + pub const VARIANTS_STR: [&'static str; 3] = [ "R1", +"R2Two", +"R3", ]; + + #[must_use] + pub const fn name(&self) -> &'static str { + match self { + Self::R1 => "R1", +Self::R2Two => "R2Two", +Self::R3 => "R3", + } + } + + #[must_use] + pub const fn variants() -> [Color3; 3] { + Self::VARIANTS + } + } + + impl Name for Color3 { + #[must_use] + fn name(&self) -> &'static str { + Self::name(self) + } + } + + impl Variants for Color3 { + fn variants() -> slice::Iter<'static, Color3> { + Self::VARIANTS.iter() + } + } + + impl Enum for Color3 {} + + impl fmt::Display for Color3 { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(self.name()) + } + } + + impl TryFrom for Color3 { + type Error = Error; + + fn try_from(i: i32) -> Result { + let e = match i { + 1 => Color3::R1, +2 => Color3::R2Two, +3 => Color3::R3, + #[allow(unreachable_patterns)] + _ => return Err(Error::Invalid), + }; + Ok(e) + } + } + + impl From for i32 { + #[must_use] + fn from(e: Color3) -> Self { + e as Self + } + } + + impl ReadXdr for Color3 { + #[cfg(feature = "std")] + fn read_xdr(r: &mut Limited) -> Result { + r.with_limited_depth(|r| { + let e = i32::read_xdr(r)?; + let v: Self = e.try_into()?; + Ok(v) + }) + } + } + + impl WriteXdr for Color3 { + #[cfg(feature = "std")] + fn write_xdr(&self, w: &mut Limited) -> Result<()> { + w.with_limited_depth(|w| { + let i: i32 = (*self).into(); + i.write_xdr(w) + }) + } + } + #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] #[cfg_attr( all(feature = "serde", feature = "alloc"), @@ -3189,15 +3298,18 @@ Self::Blue2 => "Blue2", MessageType, Color, Color2, +Color3, } impl TypeVariant { - pub const VARIANTS: [TypeVariant; 3] = [ TypeVariant::MessageType, + pub const VARIANTS: [TypeVariant; 4] = [ TypeVariant::MessageType, TypeVariant::Color, -TypeVariant::Color2, ]; - pub const VARIANTS_STR: [&'static str; 3] = [ "MessageType", +TypeVariant::Color2, +TypeVariant::Color3, ]; + pub const VARIANTS_STR: [&'static str; 4] = [ "MessageType", "Color", -"Color2", ]; +"Color2", +"Color3", ]; #[must_use] #[allow(clippy::too_many_lines)] @@ -3206,12 +3318,13 @@ TypeVariant::Color2, ]; Self::MessageType => "MessageType", Self::Color => "Color", Self::Color2 => "Color2", +Self::Color3 => "Color3", } } #[must_use] #[allow(clippy::too_many_lines)] - pub const fn variants() -> [TypeVariant; 3] { + pub const fn variants() -> [TypeVariant; 4] { Self::VARIANTS } @@ -3223,6 +3336,7 @@ Self::Color2 => "Color2", Self::MessageType => gen.into_root_schema_for::(), Self::Color => gen.into_root_schema_for::(), Self::Color2 => gen.into_root_schema_for::(), +Self::Color3 => gen.into_root_schema_for::(), } } } @@ -3248,6 +3362,7 @@ Self::Color2 => gen.into_root_schema_for::(), "MessageType" => Ok(Self::MessageType), "Color" => Ok(Self::Color), "Color2" => Ok(Self::Color2), +"Color3" => Ok(Self::Color3), _ => Err(Error::Invalid), } } @@ -3265,15 +3380,18 @@ Self::Color2 => gen.into_root_schema_for::(), MessageType(Box), Color(Box), Color2(Box), +Color3(Box), } impl Type { - pub const VARIANTS: [TypeVariant; 3] = [ TypeVariant::MessageType, + pub const VARIANTS: [TypeVariant; 4] = [ TypeVariant::MessageType, TypeVariant::Color, -TypeVariant::Color2, ]; - pub const VARIANTS_STR: [&'static str; 3] = [ "MessageType", +TypeVariant::Color2, +TypeVariant::Color3, ]; + pub const VARIANTS_STR: [&'static str; 4] = [ "MessageType", "Color", -"Color2", ]; +"Color2", +"Color3", ]; #[cfg(feature = "std")] #[allow(clippy::too_many_lines)] @@ -3282,6 +3400,7 @@ TypeVariant::Color2, ]; TypeVariant::MessageType => r.with_limited_depth(|r| Ok(Self::MessageType(Box::new(MessageType::read_xdr(r)?)))), TypeVariant::Color => r.with_limited_depth(|r| Ok(Self::Color(Box::new(Color::read_xdr(r)?)))), TypeVariant::Color2 => r.with_limited_depth(|r| Ok(Self::Color2(Box::new(Color2::read_xdr(r)?)))), +TypeVariant::Color3 => r.with_limited_depth(|r| Ok(Self::Color3(Box::new(Color3::read_xdr(r)?)))), } } @@ -3318,6 +3437,7 @@ TypeVariant::Color2 => r.with_limited_depth(|r| Ok(Self::Color2(Box::new(Color2: TypeVariant::MessageType => Box::new(ReadXdrIter::<_, MessageType>::new(&mut r.inner, r.limits.clone()).map(|r| r.map(|t| Self::MessageType(Box::new(t))))), TypeVariant::Color => Box::new(ReadXdrIter::<_, Color>::new(&mut r.inner, r.limits.clone()).map(|r| r.map(|t| Self::Color(Box::new(t))))), TypeVariant::Color2 => Box::new(ReadXdrIter::<_, Color2>::new(&mut r.inner, r.limits.clone()).map(|r| r.map(|t| Self::Color2(Box::new(t))))), +TypeVariant::Color3 => Box::new(ReadXdrIter::<_, Color3>::new(&mut r.inner, r.limits.clone()).map(|r| r.map(|t| Self::Color3(Box::new(t))))), } } @@ -3328,6 +3448,7 @@ TypeVariant::Color2 => Box::new(ReadXdrIter::<_, Color2>::new(&mut r.inner, r.li TypeVariant::MessageType => Box::new(ReadXdrIter::<_, Frame>::new(&mut r.inner, r.limits.clone()).map(|r| r.map(|t| Self::MessageType(Box::new(t.0))))), TypeVariant::Color => Box::new(ReadXdrIter::<_, Frame>::new(&mut r.inner, r.limits.clone()).map(|r| r.map(|t| Self::Color(Box::new(t.0))))), TypeVariant::Color2 => Box::new(ReadXdrIter::<_, Frame>::new(&mut r.inner, r.limits.clone()).map(|r| r.map(|t| Self::Color2(Box::new(t.0))))), +TypeVariant::Color3 => Box::new(ReadXdrIter::<_, Frame>::new(&mut r.inner, r.limits.clone()).map(|r| r.map(|t| Self::Color3(Box::new(t.0))))), } } @@ -3339,6 +3460,7 @@ TypeVariant::Color2 => Box::new(ReadXdrIter::<_, Frame>::new(&mut r.inne TypeVariant::MessageType => Box::new(ReadXdrIter::<_, MessageType>::new(dec, r.limits.clone()).map(|r| r.map(|t| Self::MessageType(Box::new(t))))), TypeVariant::Color => Box::new(ReadXdrIter::<_, Color>::new(dec, r.limits.clone()).map(|r| r.map(|t| Self::Color(Box::new(t))))), TypeVariant::Color2 => Box::new(ReadXdrIter::<_, Color2>::new(dec, r.limits.clone()).map(|r| r.map(|t| Self::Color2(Box::new(t))))), +TypeVariant::Color3 => Box::new(ReadXdrIter::<_, Color3>::new(dec, r.limits.clone()).map(|r| r.map(|t| Self::Color3(Box::new(t))))), } } @@ -3364,6 +3486,7 @@ TypeVariant::Color2 => Box::new(ReadXdrIter::<_, Color2>::new(dec, r.limits.clon TypeVariant::MessageType => Ok(Self::MessageType(Box::new(serde_json::from_reader(r)?))), TypeVariant::Color => Ok(Self::Color(Box::new(serde_json::from_reader(r)?))), TypeVariant::Color2 => Ok(Self::Color2(Box::new(serde_json::from_reader(r)?))), +TypeVariant::Color3 => Ok(Self::Color3(Box::new(serde_json::from_reader(r)?))), } } @@ -3376,6 +3499,7 @@ TypeVariant::Color2 => Ok(Self::Color2(Box::new(serde_json::from_reader(r)?))), Self::MessageType(ref v) => v.as_ref(), Self::Color(ref v) => v.as_ref(), Self::Color2(ref v) => v.as_ref(), +Self::Color3(ref v) => v.as_ref(), } } @@ -3386,12 +3510,13 @@ Self::Color2(ref v) => v.as_ref(), Self::MessageType(_) => "MessageType", Self::Color(_) => "Color", Self::Color2(_) => "Color2", +Self::Color3(_) => "Color3", } } #[must_use] #[allow(clippy::too_many_lines)] - pub const fn variants() -> [TypeVariant; 3] { + pub const fn variants() -> [TypeVariant; 4] { Self::VARIANTS } @@ -3402,6 +3527,7 @@ Self::Color2(_) => "Color2", Self::MessageType(_) => TypeVariant::MessageType, Self::Color(_) => TypeVariant::Color, Self::Color2(_) => TypeVariant::Color2, +Self::Color3(_) => TypeVariant::Color3, } } } @@ -3427,6 +3553,7 @@ Self::Color2(_) => TypeVariant::Color2, Self::MessageType(v) => v.write_xdr(w), Self::Color(v) => v.write_xdr(w), Self::Color2(v) => v.write_xdr(w), +Self::Color3(v) => v.write_xdr(w), } } } diff --git a/spec/output/generator_spec_rust_custom_str_impls/test.x/MyXDR.rs b/spec/output/generator_spec_rust_custom_str_impls/test.x/MyXDR.rs index 1f8261303..bacfd42ed 100644 --- a/spec/output/generator_spec_rust_custom_str_impls/test.x/MyXDR.rs +++ b/spec/output/generator_spec_rust_custom_str_impls/test.x/MyXDR.rs @@ -4190,21 +4190,21 @@ pub const BAR: u64 = FOO; #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] #[repr(i32)] pub enum NesterNestedEnum { - 1 = 0, - 2 = 1, + B1 = 0, + B2 = 1, } impl NesterNestedEnum { - pub const VARIANTS: [NesterNestedEnum; 2] = [ NesterNestedEnum::1, -NesterNestedEnum::2, ]; - pub const VARIANTS_STR: [&'static str; 2] = [ "1", -"2", ]; + pub const VARIANTS: [NesterNestedEnum; 2] = [ NesterNestedEnum::B1, +NesterNestedEnum::B2, ]; + pub const VARIANTS_STR: [&'static str; 2] = [ "B1", +"B2", ]; #[must_use] pub const fn name(&self) -> &'static str { match self { - Self::1 => "1", -Self::2 => "2", + Self::B1 => "B1", +Self::B2 => "B2", } } @@ -4240,8 +4240,8 @@ Self::2 => "2", fn try_from(i: i32) -> Result { let e = match i { - 0 => NesterNestedEnum::1, -1 => NesterNestedEnum::2, + 0 => NesterNestedEnum::B1, +1 => NesterNestedEnum::B2, #[allow(unreachable_patterns)] _ => return Err(Error::Invalid), }; From 4f697615ac3fef7de2831fcaa52ced7ac4a1488d Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Tue, 30 Jul 2024 11:51:41 +1000 Subject: [PATCH 2/2] fix --- .../generator_spec_java/enum.x/Red.java | 61 ------------------- .../generator_spec_python/enum.x/red.py | 50 --------------- spec/output/generator_spec_ruby/enum.x/red.rb | 21 ------- 3 files changed, 132 deletions(-) delete mode 100644 spec/output/generator_spec_java/enum.x/Red.java delete mode 100644 spec/output/generator_spec_python/enum.x/red.py delete mode 100644 spec/output/generator_spec_ruby/enum.x/red.rb diff --git a/spec/output/generator_spec_java/enum.x/Red.java b/spec/output/generator_spec_java/enum.x/Red.java deleted file mode 100644 index cd6ac0f15..000000000 --- a/spec/output/generator_spec_java/enum.x/Red.java +++ /dev/null @@ -1,61 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; - -/** - * Red's original definition in the XDR file is: - *
- * enum Red {
- *     RED1=1,
- *     RED2=2,
- *     RED3=3
- * };
- * 
- */ -public enum Red implements XdrElement { - RED1(1), - RED2(2), - RED3(3); - - private final int value; - - Red(int value) { - this.value = value; - } - - public int getValue() { - return value; - } - - public static Red decode(XdrDataInputStream stream) throws IOException { - int value = stream.readInt(); - switch (value) { - case 1: return RED1; - case 2: return RED2; - case 3: return RED3; - default: - throw new IllegalArgumentException("Unknown enum value: " + value); - } - } - - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(value); - } - public static Red fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static Red fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_python/enum.x/red.py b/spec/output/generator_spec_python/enum.x/red.py deleted file mode 100644 index eb311195d..000000000 --- a/spec/output/generator_spec_python/enum.x/red.py +++ /dev/null @@ -1,50 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -__all__ = ['Red'] -class Red(IntEnum): - """ - XDR Source Code:: - - enum Red { - RED1=1, - RED2=2, - RED3=3 - }; - """ - RED1 = 1 - RED2 = 2 - RED3 = 3 - def pack(self, packer: Packer) -> None: - packer.pack_int(self.value) - - @classmethod - def unpack(cls, unpacker: Unpacker) -> Red: - value = unpacker.unpack_int() - return cls(value) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> Red: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> Red: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) diff --git a/spec/output/generator_spec_ruby/enum.x/red.rb b/spec/output/generator_spec_ruby/enum.x/red.rb deleted file mode 100644 index 552a01768..000000000 --- a/spec/output/generator_spec_ruby/enum.x/red.rb +++ /dev/null @@ -1,21 +0,0 @@ -# This code was automatically generated using xdrgen -# DO NOT EDIT or your changes may be overwritten - -require 'xdr' - -# === xdr source ============================================================ -# -# enum Red { -# RED1=1, -# RED2=2, -# RED3=3 -# }; -# -# =========================================================================== -class Red < XDR::Enum - member :red1, 1 - member :red2, 2 - member :red3, 3 - - seal -end