Search code examples
rustrust-rocket

How do I use rocket_contrib::Uuid in a form?


I am using the 0.5.0-dev version of rocket and rocket_contrib. I want to use a Uuid in my form, but I get the following compilation error:

the trait `FromFormField<'_>` is not implemented for `rocket_contrib::uuid::Uuid`

I do not understand this as according to the docs Uuid implements FromFormField. What am I doing wrong?

#[macro_use]
extern crate rocket;

use rocket::data::TempFile;
use rocket::form::{DataField, Form};
use rocket_contrib::uuid::Uuid;

#[derive(FromForm)]
struct FileUploadForm<'v> {
    id: Uuid,
    file: TempFile<'v>,
}

#[post("/upload", data = "<data>")]
fn upload(data: Form<FileUploadForm>) -> Result<String, std::io::Error> {
    let id = "uuid".to_string();
    Ok(id)
}

fn rocket() -> rocket::Rocket {
    rocket::ignite().mount("/", routes![upload])
}

#[rocket::main]
async fn main() {
    rocket().launch().await;
}

Solution

  • My Cargo.toml was incorrect, I did not properly included rocket_contrib. The correct version would be the following

    [package]
    name = "stackoverflow"
    version = "0.1.0"
    edition = "2018"
    
    [dependencies]
    rocket = { git = "https://github.com/SergioBenitez/Rocket", version = "0.5.0-dev" }
    
    [dependencies.rocket_contrib]
    git = "https://github.com/SergioBenitez/Rocket"
    version = "0.5.0-dev"
    default-features = false
    features = ["uuid"]