I have the file "user.rs" that has the struct of a postgres database table. Whenever I try to include it in my main.rs file (A Rocket web project), all of the Diesel "stuff" can't resolve. Here is my user.js file:
use super::schema::users;
pub mod handler;
pub mod repository;
pub mod router;
#[derive(Queryable, AsChangeset, Serialize, Deserialize)]
#[table_name = "users"]
pub struct User {
pub id: String,
pub username: String,
pub password: String,
}
#[derive(Insertable)]
#[table_name = "users"]
pub struct InsertableUser {
username: String,
password: String,
}
pub impl InsertableUser {
pub fn from_user(user: User) -> InsertableUser {
InsertableUser {
username: user.username,
password: user.password,
}
}
}
pub fn all(connection: &PgConnection) -> QueryResult<Vec<User>> {
users::table.load::<User>(&*connection)
}
pub fn get(id: i32, connection: &PgConnection) -> QueryResult<User> {
users::table.find(id).get_result::<User>(connection)
}
pub fn insert(user: User, connection: &PgConnection) -> QueryResult<User> {
diesel::insert_into(users::table)
.values(&InsertableUser::from_user(user))
.get_result(connection)
}
pub fn update(id: i32, user: User, connection: &PgConnection) -> QueryResult<User> {
diesel::update(users::table.find(id))
.set(&user)
.get_result(connection)
}
pub fn delete(id: i32, connection: &PgConnection) -> QueryResult<usize> {
diesel::delete(users::table.find(id)).execute(connection)
}
And here is my main.rs:
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate rocket;
#[macro_use]
extern crate rocket_contrib;
use rocket_contrib::databases::diesel;
#[database("camera-server-db")]
struct CameraServerDbConn(diesel::PgConnection);
mod user;
#[get("/")]
fn index() -> &'static str {
"Hello World!"
}
fn main() {
rocket::ignite()
.attach(CameraServerDbConn::fairing())
.mount("/", routes![index])
.launch();
}
If I remove mod user;
from main.rs, no error show up. When I run cargo check
, I get many "cannot find x in this scope". Here's an example:
error: cannot find derive macro `AsChangeset` in this scope
--> src/user.rs:7:21
|
7 | #[derive(Queryable, AsChangeset, Serialize, Deserialize)]
| ^^^^^^^^^^^
I'm trying to follow this guide (which is admittedly quite out of date, but it was one of the only actual guides I could find).
As mentioned in the linked guide in the section "The last step", you need to import diesel correctly otherwise the compiler cannot resolve those traits/derives/functions. That means you need to change your main.rs
file as following:
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate rocket;
#[macro_use]
extern crate rocket_contrib;
#[macro_use]
extern crate diesel;
use rocket_contrib::databases::diesel;
#[database("camera-server-db")]
struct CameraServerDbConn(diesel::PgConnection);
mod user;
#[get("/")]
fn index() -> &'static str {
"Hello World!"
}
fn main() {
rocket::ignite()
.attach(CameraServerDbConn::fairing())
.mount("/", routes![index])
.launch();
}
(note the additional #[macro_use] extern crate diesel;
in your extern crate section.)