Search code examples
rustrust-rocket

I have a Cargo run error, on rocket_cors method


I'm trying to get my rust rocket app working through cargo run

use rocket::http::Method;

fn make_cors() -> Cors {
  let allowed_origins = AllowedOrigins::some_exact(&[ // 4.
    "http://localhost:3000",
    "http://127.0.0.1:3000",
    "http://localhost:8000",
    "http://0.0.0.0:8000",
  ]);
  CorsOptions { // 5.
    allowed_origins,
    allowed_methods: vec![Method::Get].into_iter().map(From::from).collect(), // 1.
    allowed_headers: AllowedHeaders::some(&[
      "Authorization",
      "Accept",
      "Access-Control-Allow-Origin", // 6.
    ]),
    allow_credentials: true,
    ..Default::default()
  }.to_cors().expect("error while building CORS")
}

The following error is bugging me:

the trait bound `rocket_cors::Method: std::convert::From<rocket::http::Method>` is not satisfied
the trait `std::convert::From<rocket::http::Method>` is not implemented for `rocket_cors::Method`

Edit:

fixed to rocket_cors::Method

which is now giving me the following error:

  no associated item named `Get` found for struct `rocket_cors::Method` in the current scope
  --> src/main.rs:24:35
   |
24 |     allowed_methods: vec![Method::Get].into_iter().map(From::from).collect(), // 1.
   |                                   ^^^ associated item not found in `rocket_cors::Method`

Solution

  • You should use the Method wrapped by the rocket_cors, not the one from rocket itself, since according to what rocket_cors has said

    A wrapper type around rocket::http::Method to support serialization and deserialization.

    so they're not the same.