forked from loafle/openapi-generator-original
rust: Implement Display
instead of ToString
for enums (#19611)
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 <wiktor@metacode.biz>
This commit is contained in:
parent
7d8eacc197
commit
5732e2758f
@ -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}}
|
||||
|
@ -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 {
|
||||
|
@ -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 {
|
||||
|
@ -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 {
|
||||
|
@ -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 {
|
||||
|
@ -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 {
|
||||
|
@ -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 {
|
||||
|
@ -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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user