Skip to content

Commit 4ddd70c

Browse files
authored
Merge pull request #16 from golemcloud/replace_to_string_with_display
replace ToString with Display
2 parents 7a591a1 + 8866a89 commit 4ddd70c

File tree

3 files changed

+28
-29
lines changed

3 files changed

+28
-29
lines changed

src/rust/model_gen.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ pub fn model_gen(reference: &str, open_api: &OpenAPI, ref_cache: &mut RefCache)
407407
fn make_match_case(enum_name: &str, name: &str) -> RustPrinter {
408408
let rust_name = name.to_case(Case::UpperCamel);
409409

410-
line(unit() + enum_name + "::" + rust_name + r#" => ""# + name + r#"".to_string(),"#)
410+
line(unit() + enum_name + "::" + rust_name + r#" => write!(f, ""# + name + r#""),"#)
411411
}
412412

413413
let match_cases = string_type
@@ -418,27 +418,25 @@ pub fn model_gen(reference: &str, open_api: &OpenAPI, ref_cache: &mut RefCache)
418418
.unwrap_or_else(unit);
419419

420420
#[rustfmt::skip]
421-
let code = unit() +
421+
let code = unit() +
422422
derive_line() +
423423
line(unit() + "pub enum " + &name + " {") +
424424
indented(
425425
cases
426426
) +
427427
line(unit() + "}") +
428428
NewLine +
429-
line(unit() + "#[allow(clippy::to_string_trait_impl)]") +
430-
NewLine +
431-
line(unit() + "impl ToString for " + &name + "{") +
429+
line(unit() + "impl " + rust_name("std::fmt", "Display") + " for " + &name + "{") +
432430
indented(
433-
line("fn to_string(&self) -> String {") +
431+
line(unit() + "fn fmt(&self, f: &mut " + rust_name("std::fmt", "Formatter") + "<'_>) -> " + rust_name("std::fmt", "Result") + " {") +
432+
indented(
433+
line("match self {") +
434434
indented(
435-
line("match self {") +
436-
indented(
437-
match_cases
438-
) +
439-
line("}")
435+
match_cases
440436
) +
441437
line("}")
438+
) +
439+
line("}")
442440
) +
443441
line("}");
444442

src/rust/printer.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use crate::printer::{IndentContext, NewLine, PrintContext, Printer, TreePrinter};
1616
use itertools::Itertools;
1717
use std::collections::HashSet;
18+
use std::fmt::Display;
1819
use std::ops::Add;
1920

2021
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -42,18 +43,17 @@ impl RustContext {
4243
}
4344
}
4445

45-
#[allow(clippy::to_string_trait_impl)]
46-
impl ToString for RustContext {
46+
impl Display for RustContext {
4747
#[allow(unstable_name_collisions)]
48-
fn to_string(&self) -> String {
48+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4949
let RustContext {
5050
imports,
5151
code,
5252
depth: _,
5353
} = self;
5454

5555
if imports.is_empty() {
56-
code.to_string()
56+
write!(f, "{}", code)
5757
} else {
5858
let imports: String = imports
5959
.iter()
@@ -62,7 +62,7 @@ impl ToString for RustContext {
6262
.intersperse("\n".to_string())
6363
.collect();
6464

65-
format!("{imports}\n\n{code}")
65+
write!(f, "{imports}\n\n{code}")
6666
}
6767
}
6868
}

src/rust/types.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use openapiv3::{
2121
AdditionalProperties, IntegerFormat, ReferenceOr, Schema, SchemaKind, StringFormat, Type,
2222
VariantOrUnknownOrEmpty,
2323
};
24+
use std::fmt::Display;
2425

2526
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2627
pub struct ModelType {
@@ -42,19 +43,19 @@ pub enum IntFormat {
4243
I64,
4344
}
4445

45-
#[allow(clippy::to_string_trait_impl)]
46-
impl ToString for IntFormat {
47-
fn to_string(&self) -> String {
48-
match self {
49-
IntFormat::U8 => "u8".to_string(),
50-
IntFormat::U16 => "u16".to_string(),
51-
IntFormat::U32 => "u32".to_string(),
52-
IntFormat::U64 => "u64".to_string(),
53-
IntFormat::I8 => "i8".to_string(),
54-
IntFormat::I16 => "i16".to_string(),
55-
IntFormat::I32 => "i32".to_string(),
56-
IntFormat::I64 => "i64".to_string(),
57-
}
46+
impl Display for IntFormat {
47+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48+
let str = match self {
49+
IntFormat::U8 => "u8",
50+
IntFormat::U16 => "u16",
51+
IntFormat::U32 => "u32",
52+
IntFormat::U64 => "u64",
53+
IntFormat::I8 => "i8",
54+
IntFormat::I16 => "i16",
55+
IntFormat::I32 => "i32",
56+
IntFormat::I64 => "i64",
57+
};
58+
write!(f, "{}", str)
5859
}
5960
}
6061

0 commit comments

Comments
 (0)