Skip to content

Commit fb5e489

Browse files
authored
Avoid clippy warning in enum deserialization generated code (#225)
1 parent 656c507 commit fb5e489

File tree

2 files changed

+37
-36
lines changed

2 files changed

+37
-36
lines changed

src/generation.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2194,7 +2194,7 @@ impl GenerationScope {
21942194
deserializer_name,
21952195
func,
21962196
non_preserve_bounds_fn(x, &type_cfg.bounds),
2197-
p.to_string(),
2197+
p,
21982198
before_after.after_str(false)
21992199
));
22002200
deser_code.throws = true;
@@ -2208,7 +2208,7 @@ impl GenerationScope {
22082208
config.final_exprs,
22092209
"unsigned_integer",
22102210
"x",
2211-
&format!("x as {}", p.to_string()),
2211+
&format!("x as {}", p),
22122212
),
22132213
Primitive::U64 => {
22142214
deser_primitive(config.final_exprs, "unsigned_integer", "x", "x")
@@ -2247,7 +2247,7 @@ impl GenerationScope {
22472247
deserializer_name,
22482248
bounds_fn(&positive_bounds)
22492249
))
2250-
.line(format!("(x as {}, Some(enc))", p.to_string()))
2250+
.line(format!("(x as {}, Some(enc))", p))
22512251
.after(",");
22522252
type_check.push_block(pos);
22532253
// let this cover both the negative int case + error case
@@ -2257,7 +2257,7 @@ impl GenerationScope {
22572257
deserializer_name,
22582258
bounds_fn(&negative_bounds)
22592259
))
2260-
.line(format!("(x as {}, Some(enc))", p.to_string()))
2260+
.line(format!("(x as {}, Some(enc))", p))
22612261
.after(",");
22622262
type_check.push_block(neg);
22632263
} else {
@@ -2266,7 +2266,7 @@ impl GenerationScope {
22662266
"cbor_event::Type::UnsignedInteger => {}.unsigned_integer(){}? as {},",
22672267
deserializer_name,
22682268
non_preserve_bounds_fn("x", &positive_bounds),
2269-
p.to_string()));
2269+
p));
22702270
// https://github.com/primetype/cbor_event/issues/9
22712271
// cbor_event's negative_integer() doesn't support i64::MIN so we use the _sz function here instead as that one supports all nints
22722272
if *p == Primitive::I64 {
@@ -2284,16 +2284,14 @@ impl GenerationScope {
22842284
};
22852285
type_check.line(format!(
22862286
"_ => {}.negative_integer_sz(){}.map(|(x, _enc)| x)? as {},",
2287-
deserializer_name,
2288-
bounds_fn,
2289-
p.to_string()
2287+
deserializer_name, bounds_fn, p
22902288
));
22912289
} else {
22922290
type_check.line(format!(
22932291
"_ => {}.negative_integer(){}? as {},",
22942292
deserializer_name,
22952293
non_preserve_bounds_fn("x", &negative_bounds),
2296-
p.to_string()
2294+
p
22972295
));
22982296
}
22992297
}
@@ -6401,18 +6399,17 @@ fn make_enum_variant_return_if_deserialized(
64016399
deser_body.line(&format!(
64026400
"let deser_variant: Result<_, DeserializeError> = {single_line};"
64036401
));
6404-
Block::new("match deser_variant")
64056402
}
64066403
_ => {
6407-
let mut variant_deser =
6408-
Block::new("match (|raw: &mut Deserializer<_>| -> Result<_, DeserializeError>");
6409-
variant_deser.after(")(raw)");
6404+
let mut variant_deser = Block::new(
6405+
"let deser_variant = (|raw: &mut Deserializer<_>| -> Result<_, DeserializeError>",
6406+
);
6407+
variant_deser.after(")(raw);");
64106408
variant_deser.push_all(variant_deser_code.content);
64116409
deser_body.push_block(variant_deser);
6412-
// can't chain blocks so we just put them one after the other
6413-
Block::new("")
64146410
}
64156411
}
6412+
Block::new("match deser_variant")
64166413
}
64176414

64186415
fn surround_in_len_checks(
@@ -6996,13 +6993,13 @@ fn generate_enum(
69966993
cli,
69976994
);
69986995
let mut variant_deser = Block::new(
6999-
"match (|raw: &mut Deserializer<_>| -> Result<_, DeserializeError>",
6996+
"let variant_deser = (|raw: &mut Deserializer<_>| -> Result<_, DeserializeError>",
70006997
);
7001-
variant_deser.after(")(raw)");
6998+
variant_deser.after(")(raw);");
70026999
variant_deser.push_all(variant_deser_code.content);
70037000
deser_body.push_block(variant_deser);
70047001
// can't chain blocks so we just put them one after the other
7005-
let mut return_if_deserialized = Block::new("");
7002+
let mut return_if_deserialized = Block::new("match variant_deser");
70067003
return_if_deserialized.line("Ok(variant) => return Ok(variant),");
70077004
return_if_deserialized
70087005
}

src/intermediate.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -835,24 +835,28 @@ pub enum Primitive {
835835
Bytes,
836836
}
837837

838-
impl ToString for Primitive {
839-
fn to_string(&self) -> String {
840-
String::from(match self {
841-
Primitive::Bool => "bool",
842-
Primitive::F32 => "f32",
843-
Primitive::F64 => "f64",
844-
Primitive::U8 => "u8",
845-
Primitive::I8 => "i8",
846-
Primitive::U16 => "u16",
847-
Primitive::I16 => "i16",
848-
Primitive::U32 => "u32",
849-
Primitive::I32 => "i32",
850-
Primitive::U64 => "u64",
851-
Primitive::I64 => "i64",
852-
Primitive::N64 => "u64",
853-
Primitive::Str => "String",
854-
Primitive::Bytes => "Vec<u8>",
855-
})
838+
impl std::fmt::Display for Primitive {
839+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
840+
write!(
841+
f,
842+
"{}",
843+
match self {
844+
Primitive::Bool => "bool",
845+
Primitive::F32 => "f32",
846+
Primitive::F64 => "f64",
847+
Primitive::U8 => "u8",
848+
Primitive::I8 => "i8",
849+
Primitive::U16 => "u16",
850+
Primitive::I16 => "i16",
851+
Primitive::U32 => "u32",
852+
Primitive::I32 => "i32",
853+
Primitive::U64 => "u64",
854+
Primitive::I64 => "i64",
855+
Primitive::N64 => "u64",
856+
Primitive::Str => "String",
857+
Primitive::Bytes => "Vec<u8>",
858+
}
859+
)
856860
}
857861
}
858862
// TODO: impl display or fmt or whatever rust uses

0 commit comments

Comments
 (0)