Skip to content

Instantly share code, notes, and snippets.

@bjornharrtell
Created March 31, 2019 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjornharrtell/5ea10f0003378bf76d8f2abe04079e2a to your computer and use it in GitHub Desktop.
Save bjornharrtell/5ea10f0003378bf76d8f2abe04079e2a to your computer and use it in GitHub Desktop.
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket;
#[macro_use] extern crate serde_derive;
extern crate rocket_contrib;
use rocket_contrib::json::{Json};
#[derive(Serialize)]
pub struct User {
name: Option<String>
}
#[derive(Serialize)]
pub struct JsonError {
message: String
}
#[get("/")]
fn get() -> Result<Json<User>, Json<JsonError>> {
Err(Json(JsonError { message: "Unknown".to_string() }))
}
fn main() {
rocket::ignite()
.mount("/", routes![get])
.launch();
}
@bjornharrtell
Copy link
Author

Fails to compile with "the trait rocket::response::Responder<'_> is not implemented for std::result::Result<rocket_contrib::json::Json<User>, rocket_contrib::json::Json<JsonError>>". Switching out JsonError to a String and returning a Json(String) will compile without issues and I do not understand why.

@bjornharrtell
Copy link
Author

Problem is that the Err type needs to implement Debug, #[derive(Serialize)] -> #[derive(Serialize, Debug)] fixes the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment