Skip to content

Commit

Permalink
Avoid clippy warning in enum deserialization generated code (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
rooooooooob authored Mar 28, 2024
1 parent 656c507 commit fb5e489
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 36 deletions.
33 changes: 15 additions & 18 deletions src/generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2194,7 +2194,7 @@ impl GenerationScope {
deserializer_name,
func,
non_preserve_bounds_fn(x, &type_cfg.bounds),
p.to_string(),
p,
before_after.after_str(false)
));
deser_code.throws = true;
Expand All @@ -2208,7 +2208,7 @@ impl GenerationScope {
config.final_exprs,
"unsigned_integer",
"x",
&format!("x as {}", p.to_string()),
&format!("x as {}", p),
),
Primitive::U64 => {
deser_primitive(config.final_exprs, "unsigned_integer", "x", "x")
Expand Down Expand Up @@ -2247,7 +2247,7 @@ impl GenerationScope {
deserializer_name,
bounds_fn(&positive_bounds)
))
.line(format!("(x as {}, Some(enc))", p.to_string()))
.line(format!("(x as {}, Some(enc))", p))
.after(",");
type_check.push_block(pos);
// let this cover both the negative int case + error case
Expand All @@ -2257,7 +2257,7 @@ impl GenerationScope {
deserializer_name,
bounds_fn(&negative_bounds)
))
.line(format!("(x as {}, Some(enc))", p.to_string()))
.line(format!("(x as {}, Some(enc))", p))
.after(",");
type_check.push_block(neg);
} else {
Expand All @@ -2266,7 +2266,7 @@ impl GenerationScope {
"cbor_event::Type::UnsignedInteger => {}.unsigned_integer(){}? as {},",
deserializer_name,
non_preserve_bounds_fn("x", &positive_bounds),
p.to_string()));
p));
// https://github.com/primetype/cbor_event/issues/9
// cbor_event's negative_integer() doesn't support i64::MIN so we use the _sz function here instead as that one supports all nints
if *p == Primitive::I64 {
Expand All @@ -2284,16 +2284,14 @@ impl GenerationScope {
};
type_check.line(format!(
"_ => {}.negative_integer_sz(){}.map(|(x, _enc)| x)? as {},",
deserializer_name,
bounds_fn,
p.to_string()
deserializer_name, bounds_fn, p
));
} else {
type_check.line(format!(
"_ => {}.negative_integer(){}? as {},",
deserializer_name,
non_preserve_bounds_fn("x", &negative_bounds),
p.to_string()
p
));
}
}
Expand Down Expand Up @@ -6401,18 +6399,17 @@ fn make_enum_variant_return_if_deserialized(
deser_body.line(&format!(
"let deser_variant: Result<_, DeserializeError> = {single_line};"
));
Block::new("match deser_variant")
}
_ => {
let mut variant_deser =
Block::new("match (|raw: &mut Deserializer<_>| -> Result<_, DeserializeError>");
variant_deser.after(")(raw)");
let mut variant_deser = Block::new(
"let deser_variant = (|raw: &mut Deserializer<_>| -> Result<_, DeserializeError>",
);
variant_deser.after(")(raw);");
variant_deser.push_all(variant_deser_code.content);
deser_body.push_block(variant_deser);
// can't chain blocks so we just put them one after the other
Block::new("")
}
}
Block::new("match deser_variant")
}

fn surround_in_len_checks(
Expand Down Expand Up @@ -6996,13 +6993,13 @@ fn generate_enum(
cli,
);
let mut variant_deser = Block::new(
"match (|raw: &mut Deserializer<_>| -> Result<_, DeserializeError>",
"let variant_deser = (|raw: &mut Deserializer<_>| -> Result<_, DeserializeError>",
);
variant_deser.after(")(raw)");
variant_deser.after(")(raw);");
variant_deser.push_all(variant_deser_code.content);
deser_body.push_block(variant_deser);
// can't chain blocks so we just put them one after the other
let mut return_if_deserialized = Block::new("");
let mut return_if_deserialized = Block::new("match variant_deser");
return_if_deserialized.line("Ok(variant) => return Ok(variant),");
return_if_deserialized
}
Expand Down
40 changes: 22 additions & 18 deletions src/intermediate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -835,24 +835,28 @@ pub enum Primitive {
Bytes,
}

impl ToString for Primitive {
fn to_string(&self) -> String {
String::from(match self {
Primitive::Bool => "bool",
Primitive::F32 => "f32",
Primitive::F64 => "f64",
Primitive::U8 => "u8",
Primitive::I8 => "i8",
Primitive::U16 => "u16",
Primitive::I16 => "i16",
Primitive::U32 => "u32",
Primitive::I32 => "i32",
Primitive::U64 => "u64",
Primitive::I64 => "i64",
Primitive::N64 => "u64",
Primitive::Str => "String",
Primitive::Bytes => "Vec<u8>",
})
impl std::fmt::Display for Primitive {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
Primitive::Bool => "bool",
Primitive::F32 => "f32",
Primitive::F64 => "f64",
Primitive::U8 => "u8",
Primitive::I8 => "i8",
Primitive::U16 => "u16",
Primitive::I16 => "i16",
Primitive::U32 => "u32",
Primitive::I32 => "i32",
Primitive::U64 => "u64",
Primitive::I64 => "i64",
Primitive::N64 => "u64",
Primitive::Str => "String",
Primitive::Bytes => "Vec<u8>",
}
)
}
}
// TODO: impl display or fmt or whatever rust uses
Expand Down

0 comments on commit fb5e489

Please sign in to comment.