do a query from postgresql using rocket_db_pools with rocket and rust

127 Views Asked by At

this is literally the first time that i try to work with rust, so.. my apologizes if i confuse stuff.

I'm creating a rust project that will serve as a JSON web server using the rocket framework while connecting to a postgresql with a Pool using rocket_db_pools.

so I created a get request to /id/<id> that will just select that id that id (i64) and will return it in a a struct.

this is my code:

use rocket_db_pools::{Connection, Database};


#[macro_use] extern crate rocket;

#[derive(Database)]
#[database("dreamhomeinisrael")]
struct MyDb(deadpool_postgres::Pool);

#[get("/")]
fn index() -> &'static str {
    "Hello, world!"
}

struct MyStruct {
    id: i64,
    name: String,
}


#[get("/id/<id>")]
async fn read(mut db: Connection<MyDb>, id: i64) -> Option<MyStruct> {
    let stmt = db.prepare_cached("select $1").await.unwrap();
    let rows = db.query(&stmt, &[&id]).await.unwrap();
    let value: i64 = rows[0].get(0);
    //assert_eq!(value, id);
    let my_struct = MyStruct {
        id: value,
        name: String::new()
    };
    return Some(my_struct);
}

#[launch]
async fn rocket() -> _ {
    rocket::build().mount("/", routes![index]).attach(MyDb::init())
}

and this is the error that I'm getting:

error[E0277]: the trait bound `MyStruct: Responder<'_, '_>` is not satisfied
   --> src/main.rs:22:53
    |
22  | async fn read(mut db: Connection<MyDb>, id: i64) -> Option<MyStruct> {
    |                                                     ^^^^^^ the trait `Responder<'_, '_>` is not implemented for `MyStruct`
    |
    = help: the following other types implement trait `Responder<'r, 'o>`:
              <&'o [u8] as Responder<'r, 'o>>
              <&'o str as Responder<'r, 'o>>
              <() as Responder<'r, 'static>>
              <(ContentType, R) as Responder<'r, 'o>>
              <(rocket::http::Status, R) as Responder<'r, 'o>>
              <Accepted<R> as Responder<'r, 'o>>
              <Arc<[u8]> as Responder<'r, 'static>>
              <Arc<str> as Responder<'r, 'static>>
            and 37 others
    = note: required for `std::option::Option<MyStruct>` to implement `Responder<'_, '_>`
note: required by a bound in `route::handler::<impl Outcome<rocket::Response<'o>, rocket::http::Status, rocket::Data<'o>>>::from`
   --> /home/ufk/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rocket-0.5.0-rc.3/src/route/handler.rs:188:20
    |
188 |     pub fn from<R: Responder<'r, 'o>>(req: &'r Request<'_>, responder: R) -> Outcome<'r> {
    |                    ^^^^^^^^^^^^^^^^^ required by this bound in `route::handler::<impl Outcome<Response<'o>, Status, Data<'o>>>::from`

this error is quite confusing and i'm really new so i'm really lost here, so any information regarding this issue would be greatly appreciated.

versions of packages:

[dependencies]
deadpool-postgres = "0.10.5"
rocket = "=0.5.0-rc.3"

[dependencies.rocket_db_pools]
version = "=0.1.0-rc.3"
features = ["deadpool_postgres"]

any ideas what I'm missing?

thanks

#update

this what i've got so far:

use rocket::response::Responder;
use rocket_db_pools::{Connection, Database};

#[macro_use] extern crate rocket;

#[derive(Database)]
#[database("dreamhomeinisrael")]
struct MyDb(deadpool_postgres::Pool);

#[get("/")]
fn index() -> &'static str {
    "Hello, world!"
}

#[derive(Responder)]
pub struct MyStruct {
    name: String,
    #[response(ignore)]
    id: i32,

}


#[get("/<id>")]
async fn read(db: Connection<MyDb>, id: &str) -> Option<MyStruct> {
    let stmt = db.prepare_cached("select $1").await.unwrap();
    let rows = db.query(&stmt, &[&id]).await.unwrap();
    let value: String = rows[0].get(0);
    //assert_eq!(value, id);
    let my_struct = MyStruct {
        id: 3,
        name: value
    };
    return Some(my_struct);
}

#[launch]
async fn rocket() -> _ {
    rocket::build().mount("/", routes![index])
        .mount("/id", routes![read])
        .attach(MyDb::init())
}

so I'm deriving responder instead of implementing it,

as you can see i'm ignoring the id in the response #[response(ignore)] or else it complains that the i64 variable should implement Into<Header<'_>>

how can i resolve it? am I in the right track ?

0

There are 0 best solutions below