rust - 一旦我包含一个文件,柴油和火箭的进口就会中断
问题描述
我有一个具有 postgres 数据库表结构的文件“user.rs”。每当我尝试将它包含在我的 main.rs 文件(A Rocket web 项目)中时,所有 Diesel “东西”都无法解决。这是我的 user.js 文件:
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)
}
这是我的 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();
}
如果我mod user;
从 main.rs 中删除,则不会出现错误。当我运行时cargo check
,我得到很多“在这个范围内找不到 x”。这是一个例子:
error: cannot find derive macro `AsChangeset` in this scope
--> src/user.rs:7:21
|
7 | #[derive(Queryable, AsChangeset, Serialize, Deserialize)]
| ^^^^^^^^^^^
我正在尝试遵循本指南(诚然,它已经过时了,但它是我能找到的唯一实际指南之一)。
解决方案
如“最后一步”部分的链接指南中所述,您需要正确导入柴油,否则编译器无法解析这些特征/派生/函数。这意味着您需要按以下方式更改main.rs
文件:
#![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();
}
(请注意#[macro_use] extern crate diesel;
您的 extern crate 部分中的附加内容。)
推荐阅读
- javascript - 如何以可扩展的方式分配 javascript 变量
- javafx - 场景构建器中 javafx 中可调整大小的图像视图
- php - zabbix ldap登录失败
- amazon-web-services - 带有负载均衡器公共端点的 kubeadm
- javascript - 基于多个选项的过滤器数组(Vue/JavaScript)
- asp.net-mvc - 从 mvc 视图重定向到角度路由 - Angular 6
- python - 如何在 zip 中包含 .whl 文件以用于 lambda 函数
- c# - 我的 Unity 第一人称角色相机很奇怪而且摇摇晃晃
- c# - 在 ASP.net MVC 视图中显示“解决冲突”窗口和合并工具
- c - 安全地直接写入内存地址