I am a new learner of rust programming.
I am trying with the tutorial from youtube , and I got an error when I run the cargo run.
Here is my code:
src/schema.rs
table! {
books (id) {
id -> Int4,
title -> Varchar,
author -> Varchar,
published -> Bool,
}
}
src/model.rs
use diesel;
use diesel::prelude::*;
use diesel::pg::PgConnection;
use schema::books;
use schema::books::dsl::books as all_books;
#[derive(Queryable)]
pub struct Book {
pub id: i32,
pub title: String,
pub author: String,
pub published: bool,
}
#[derive(Insertable)]
#[table_name = "books"]
pub struct NewBook {
pub title: String,
pub author: String,
pub published: bool,
}
impl Book {
pub fn show(id: i32, conn: &PgConnection) -> Vec<Book> {
all_books
.find(id)
.load::<Book>(conn)
.expect("Error loading book")
}
pub fn all(conn: &PgConnection) -> Vec<Book> {
all_books
.order(books::id.desc())
.load::<Book>(conn)
.expect("error loading the books")
}
pub fn update_by_id(id: i32, conn: &PgConnection, book: NewBook) -> bool {
use schema::books::dsl::{author as a, published as p, title as t};
let NewBook {
title,
author,
published,
} = book;
diesel::update(all_books.find(id))
.set((a.eq(author), p.eq(published), t.eq(title)))
.get_result::<Book>(conn)
.is_ok()
}
pub fn insert(book: NewBook, conn: &PgConnection) -> bool {
diesel::insert_into(books::table)
.values(&book)
.execute(conn)
.is_ok()
}
pub fn delete_by_id(id: i32, conn: &PgConnection) -> bool {
if Book::show(id, conn).is_empty() {
return false;
};
diesel::delete(all_books.find(id)).execute(conn).is_ok()
}
pub fn all_by_author(author: String, conn: &PgConnection) -> Vec<Book> {
all_books
.filter(books::author.eq(author))
.load::<Book>(conn)
.expect("Error loading books by author")
}
}
src/main.rs
#[macro_use]
extern crate diesel;
extern crate dotenv;
use dotenv::dotenv;
use std::env;
use diesel::prelude::*;
use diesel::pg::PgConnection;
mod schema;
mod models;
fn main() {
dotenv().ok();
let database_url = env::var("DATABASE_URL").expect("set DATABASE_URL");
let conn = PgConnection::establish(&database_url).unwrap();
let book = models::NewBook {
title: String::from("Gravity's Rainbow"),
author: String::from("Thomas Pynchon"),
published: true,
};
if models::Book::insert(book, &conn) {
println!("success");
} else {
println!("failed");
}
}
Cargo.toml
[package]
name = "api"
name = "api"
version = "0.1.0"
authors = ["kanel soengkanel@gmail.com"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
diesel = {version="1.4.5", features=["postgres"] }
dotenv = "0.15.0"
Error Message
Compiling api v0.1.0 (/Users/kanel/Documents/Developments/rustcode/api)
error[E0433]: failed to resolve: use of undeclared type or module `schema`
--> src/models.rs:6:5
|
6 | use schema::books::dsl::books as all_books;
| ^^^^^^ use of undeclared type or module `schema`
error[E0433]: failed to resolve: use of undeclared type or module `schema`
--> src/models.rs:40:13
|
40 | use schema::books::dsl::{author as a, published as p, title as t};
| ^^^^^^ use of undeclared type or module `schema`
error[E0432]: unresolved import `schema`
--> src/models.rs:5:5
|
5 | use schema::books;
| ^^^^^^ help: a similar path exists: `crate::schema`
|
= note: `use` statements changed in Rust 2018; read more at <https://doc.rust-lang.org/edition-guide/rust-2018/module-system/path-clarity.html>
error[E0425]: cannot find value `all_books` in this scope
--> src/models.rs:26:9
|
26 | all_books
| ^^^^^^^^^ not found in this scope
error[E0425]: cannot find value `all_books` in this scope
--> src/models.rs:33:9
|
33 | all_books
| ^^^^^^^^^ not found in this scope
error[E0425]: cannot find value `all_books` in this scope
--> src/models.rs:47:24
|
47 | diesel::update(all_books.find(id))
| ^^^^^^^^^ not found in this scope
error[E0425]: cannot find value `a` in this scope
--> src/models.rs:48:19
|
48 | .set((a.eq(author), p.eq(published), t.eq(title)))
| ^ not found in this scope
error[E0425]: cannot find value `p` in this scope
--> src/models.rs:48:33
|
48 | .set((a.eq(author), p.eq(published), t.eq(title)))
| ^ not found in this scope
error[E0425]: cannot find value `t` in this scope
--> src/models.rs:48:50
|
48 | .set((a.eq(author), p.eq(published), t.eq(title)))
| ^ not found in this scope
error[E0425]: cannot find value `all_books` in this scope
--> src/models.rs:64:24
|
64 | diesel::delete(all_books.find(id)).execute(conn).is_ok()
| ^^^^^^^^^ not found in this scope
error[E0425]: cannot find value `all_books` in this scope
--> src/models.rs:68:9
|
68 | all_books
| ^^^^^^^^^ not found in this scope
error: aborting due to 11 previous errors
Some errors have detailed explanations: E0425, E0432, E0433.
For more information about an error, try `rustc --explain E0425`.
error: could not compile `api`.
To learn more, run the command again with --verbose.
kanel@kanelsoengs-MacBook-Pro api %
Please community help to check.