carllerche / tower-web

A fast, boilerplate free, web framework for Rust
MIT License
980 stars 51 forks source link

Can't Extract over Enum #189

Open 0xpr03 opened 5 years ago

0xpr03 commented 5 years ago

When trying to do an Extract for a enum I'm getting "not yet implemented" errors:

#[derive(Debug, Clone, Extract)]
pub enum InstanceType {
    A(String),
    B(i32),
}

Throws

error: proc-macro derive panicked
  --> src/main.rs:25:24
   |
25 | #[derive(Debug, Clone, Extract)]
   |                        ^^^^^^^
   |
   = help: message: not yet implemented

It works as long as I don't try to mark the enum as Extract. I've tried to find similar issues.

carllerche commented 5 years ago

How would tower-web pick the variant to extract to?

0xpr03 commented 5 years ago

Not sure whether I get this right but I was expecting to receive the value back like when using serde and then match over that.

0xpr03 commented 5 years ago

So I can do something like this:

#[post("/post-type")]
fn post_type(&self, body: InstanceType) -> Result<MyResponse, ()> {
    println!("Body: {:?}", body);
    match body {
        A(v) => ...
        B(v) => ...
    }
}
carllerche commented 5 years ago

Ah! I see, yeah i think that makes sense.

carllerche commented 5 years ago

I think you can work around this by doing:

#[derive(Extract)]
struct ExtractInstance(InstantType);

#[derive(Deserialize)]
enum InstanceType {
/// ....
}
0xpr03 commented 5 years ago

Yes that works and is fine for my case as I start from a struct and have an enum as child value. Wasn't aware that you can just use "Deserialize" at the child types instead of Extract.

carllerche commented 5 years ago

I'm going to leave the issue open as the original snippet should be made to work.

Thanks for the report.