From 5732e2758fa3a2bd854fbd12016b49bd5f6ed6ba Mon Sep 17 00:00:00 2001 From: Wiktor Kwapisiewicz Date: Tue, 24 Sep 2024 15:07:12 +0200 Subject: [PATCH] rust: Implement `Display` instead of `ToString` for enums (#19611) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Rust docs for `ToString` explicitly recommend implementing `Display`: > This trait is automatically implemented for any type which implements the Display trait. As such, ToString shouldn’t be implemented directly: Display should be implemented instead, and you get the ToString implementation for free. See: https://doc.rust-lang.org/std/string/trait.ToString.html See: https://github.com/Nitrokey/nethsm-sdk-rs/pull/33 Signed-off-by: Wiktor Kwapisiewicz --- .../src/main/resources/rust/model.mustache | 10 +++++----- .../petstore/src/models/numeric_enum_testing.rs | 16 ++++++++-------- .../petstore/src/models/numeric_enum_testing.rs | 16 ++++++++-------- .../src/models/numeric_enum_testing.rs | 16 ++++++++-------- .../src/models/numeric_enum_testing.rs | 16 ++++++++-------- .../src/models/numeric_enum_testing.rs | 16 ++++++++-------- .../src/models/numeric_enum_testing.rs | 16 ++++++++-------- .../petstore/src/models/numeric_enum_testing.rs | 16 ++++++++-------- 8 files changed, 61 insertions(+), 61 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/rust/model.mustache b/modules/openapi-generator/src/main/resources/rust/model.mustache index 79d3cda1c2b..7367124d7c9 100644 --- a/modules/openapi-generator/src/main/resources/rust/model.mustache +++ b/modules/openapi-generator/src/main/resources/rust/model.mustache @@ -27,15 +27,15 @@ pub enum {{{classname}}} { {{/enumVars}}{{/allowableValues}} } -impl ToString for {{{classname}}} { - fn to_string(&self) -> String { - match self { +impl std::fmt::Display for {{{classname}}} { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", match self { {{#allowableValues}} {{#enumVars}} - Self::{{{name}}} => String::from("{{{value}}}"), + Self::{{{name}}} => "{{{value}}}", {{/enumVars}} {{/allowableValues}} - } + }) } } {{/isInteger}} diff --git a/samples/client/petstore/rust/hyper/petstore/src/models/numeric_enum_testing.rs b/samples/client/petstore/rust/hyper/petstore/src/models/numeric_enum_testing.rs index 9b8f0cb0465..e3681aa6177 100644 --- a/samples/client/petstore/rust/hyper/petstore/src/models/numeric_enum_testing.rs +++ b/samples/client/petstore/rust/hyper/petstore/src/models/numeric_enum_testing.rs @@ -24,14 +24,14 @@ pub enum NumericEnumTesting { } -impl ToString for NumericEnumTesting { - fn to_string(&self) -> String { - match self { - Self::Variant0 => String::from("0"), - Self::Variant1 => String::from("1"), - Self::Variant2 => String::from("2"), - Self::Variant3 => String::from("3"), - } +impl std::fmt::Display for NumericEnumTesting { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", match self { + Self::Variant0 => "0", + Self::Variant1 => "1", + Self::Variant2 => "2", + Self::Variant3 => "3", + }) } } impl Default for NumericEnumTesting { diff --git a/samples/client/petstore/rust/hyper0x/petstore/src/models/numeric_enum_testing.rs b/samples/client/petstore/rust/hyper0x/petstore/src/models/numeric_enum_testing.rs index 9b8f0cb0465..e3681aa6177 100644 --- a/samples/client/petstore/rust/hyper0x/petstore/src/models/numeric_enum_testing.rs +++ b/samples/client/petstore/rust/hyper0x/petstore/src/models/numeric_enum_testing.rs @@ -24,14 +24,14 @@ pub enum NumericEnumTesting { } -impl ToString for NumericEnumTesting { - fn to_string(&self) -> String { - match self { - Self::Variant0 => String::from("0"), - Self::Variant1 => String::from("1"), - Self::Variant2 => String::from("2"), - Self::Variant3 => String::from("3"), - } +impl std::fmt::Display for NumericEnumTesting { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", match self { + Self::Variant0 => "0", + Self::Variant1 => "1", + Self::Variant2 => "2", + Self::Variant3 => "3", + }) } } impl Default for NumericEnumTesting { diff --git a/samples/client/petstore/rust/reqwest/petstore-async-middleware/src/models/numeric_enum_testing.rs b/samples/client/petstore/rust/reqwest/petstore-async-middleware/src/models/numeric_enum_testing.rs index 9b8f0cb0465..e3681aa6177 100644 --- a/samples/client/petstore/rust/reqwest/petstore-async-middleware/src/models/numeric_enum_testing.rs +++ b/samples/client/petstore/rust/reqwest/petstore-async-middleware/src/models/numeric_enum_testing.rs @@ -24,14 +24,14 @@ pub enum NumericEnumTesting { } -impl ToString for NumericEnumTesting { - fn to_string(&self) -> String { - match self { - Self::Variant0 => String::from("0"), - Self::Variant1 => String::from("1"), - Self::Variant2 => String::from("2"), - Self::Variant3 => String::from("3"), - } +impl std::fmt::Display for NumericEnumTesting { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", match self { + Self::Variant0 => "0", + Self::Variant1 => "1", + Self::Variant2 => "2", + Self::Variant3 => "3", + }) } } impl Default for NumericEnumTesting { diff --git a/samples/client/petstore/rust/reqwest/petstore-async/src/models/numeric_enum_testing.rs b/samples/client/petstore/rust/reqwest/petstore-async/src/models/numeric_enum_testing.rs index 9b8f0cb0465..e3681aa6177 100644 --- a/samples/client/petstore/rust/reqwest/petstore-async/src/models/numeric_enum_testing.rs +++ b/samples/client/petstore/rust/reqwest/petstore-async/src/models/numeric_enum_testing.rs @@ -24,14 +24,14 @@ pub enum NumericEnumTesting { } -impl ToString for NumericEnumTesting { - fn to_string(&self) -> String { - match self { - Self::Variant0 => String::from("0"), - Self::Variant1 => String::from("1"), - Self::Variant2 => String::from("2"), - Self::Variant3 => String::from("3"), - } +impl std::fmt::Display for NumericEnumTesting { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", match self { + Self::Variant0 => "0", + Self::Variant1 => "1", + Self::Variant2 => "2", + Self::Variant3 => "3", + }) } } impl Default for NumericEnumTesting { diff --git a/samples/client/petstore/rust/reqwest/petstore-avoid-box/src/models/numeric_enum_testing.rs b/samples/client/petstore/rust/reqwest/petstore-avoid-box/src/models/numeric_enum_testing.rs index 9b8f0cb0465..e3681aa6177 100644 --- a/samples/client/petstore/rust/reqwest/petstore-avoid-box/src/models/numeric_enum_testing.rs +++ b/samples/client/petstore/rust/reqwest/petstore-avoid-box/src/models/numeric_enum_testing.rs @@ -24,14 +24,14 @@ pub enum NumericEnumTesting { } -impl ToString for NumericEnumTesting { - fn to_string(&self) -> String { - match self { - Self::Variant0 => String::from("0"), - Self::Variant1 => String::from("1"), - Self::Variant2 => String::from("2"), - Self::Variant3 => String::from("3"), - } +impl std::fmt::Display for NumericEnumTesting { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", match self { + Self::Variant0 => "0", + Self::Variant1 => "1", + Self::Variant2 => "2", + Self::Variant3 => "3", + }) } } impl Default for NumericEnumTesting { diff --git a/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/models/numeric_enum_testing.rs b/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/models/numeric_enum_testing.rs index 9b8f0cb0465..e3681aa6177 100644 --- a/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/models/numeric_enum_testing.rs +++ b/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/models/numeric_enum_testing.rs @@ -24,14 +24,14 @@ pub enum NumericEnumTesting { } -impl ToString for NumericEnumTesting { - fn to_string(&self) -> String { - match self { - Self::Variant0 => String::from("0"), - Self::Variant1 => String::from("1"), - Self::Variant2 => String::from("2"), - Self::Variant3 => String::from("3"), - } +impl std::fmt::Display for NumericEnumTesting { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", match self { + Self::Variant0 => "0", + Self::Variant1 => "1", + Self::Variant2 => "2", + Self::Variant3 => "3", + }) } } impl Default for NumericEnumTesting { diff --git a/samples/client/petstore/rust/reqwest/petstore/src/models/numeric_enum_testing.rs b/samples/client/petstore/rust/reqwest/petstore/src/models/numeric_enum_testing.rs index 9b8f0cb0465..e3681aa6177 100644 --- a/samples/client/petstore/rust/reqwest/petstore/src/models/numeric_enum_testing.rs +++ b/samples/client/petstore/rust/reqwest/petstore/src/models/numeric_enum_testing.rs @@ -24,14 +24,14 @@ pub enum NumericEnumTesting { } -impl ToString for NumericEnumTesting { - fn to_string(&self) -> String { - match self { - Self::Variant0 => String::from("0"), - Self::Variant1 => String::from("1"), - Self::Variant2 => String::from("2"), - Self::Variant3 => String::from("3"), - } +impl std::fmt::Display for NumericEnumTesting { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", match self { + Self::Variant0 => "0", + Self::Variant1 => "1", + Self::Variant2 => "2", + Self::Variant3 => "3", + }) } } impl Default for NumericEnumTesting {