lerouxrgd / rsgen-avro

Command line and library for generating Rust types from Avro schemas
MIT License
34 stars 26 forks source link

Union of Bytes and String generates code that doesn't build #68

Closed mati865 closed 1 month ago

mati865 commented 1 month ago

Input (input.avsc):

{
  "name": "foo",
  "type": "record",
  "fields": [
    {
        "name": "bar",
        "type": [
            "string",
            "bytes"
        ]
    }
  ]
}

Generated code:

/// Auto-generated type for unnamed Avro union variants.
#[derive(Debug, PartialEq, Eq, Clone, serde::Deserialize, serde::Serialize)]
pub enum UnionStringBytes {
    String(String),
    Bytes(#[serde(with = "apache_avro::serde_avro_bytes")] Vec<u8>),
}

impl From<String> for UnionStringBytes {
    fn from(v: String) -> Self {
        Self::String(v)
    }
}

impl From<UnionStringBytes> for String {
    fn from(v: UnionStringBytes) -> Self {
        let UnionStringBytes::String(v) = v;
        v
    }
}

#[derive(Debug, PartialEq, Eq, Clone, serde::Deserialize, serde::Serialize)]
pub struct Foo {
    pub bar: UnionStringBytes,
}

Build error:

error[E0005]: refutable pattern in local binding
  --> <project>/target/debug/build/common-17245974b0920ca8/out/foo.rs:17:13
   |
17 |         let UnionStringBytes::String(v) = v;
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^ pattern `UnionStringBytes::Bytes(_)` not covered
   |
   = note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
   = note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
note: `UnionStringBytes` defined here
  --> <project>/target/debug/build/common-17245974b0920ca8/out/foo.rs:4:10
   |
4  | pub enum UnionStringBytes {
   |          ^^^^^^^^^^^^^^^^
5  |     String(String),
6  |     Bytes(#[serde(with = "apache_avro::serde_avro_bytes")] Vec<u8>),
   |     ----- not covered
   = note: the matched value is of type `UnionStringBytes`
help: you might want to use `let else` to handle the variant that isn't matched
   |
17 |         let UnionStringBytes::String(v) = v else { todo!() };
   |                                             ++++++++++++++++

Using rsgen-avro 0.14.1.