GONJY_MONJY
@GONJY_MONJY
В поисках новых горизонтов

Почему модуль rust не видит другой модуль?

Привет
Пишу сервер на rocket с использованием sea-orm.

Есть такая файловая структура:
src/
│ main.rs
│ mod.rs
│ setup.rs

├───entities/
│ mod.rs
│ prelude.rs
│ user.rs

├───routes/
│ mod.rs
│ user_route.rs

Содержимое файла src/mod.rs:
pub mod entities {
    pub mod User;
}

pub mod routes {
    pub mod user_route;
}

pub mod setup;


Содержимое файла src/entities/mod.rs:
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.15

pub mod prelude;

pub mod author;
pub mod book;
pub mod book_author;
pub mod book_genre;
pub mod book_rate;
pub mod chapter;
pub mod comment;
pub mod comment_rate;
pub mod genre;
pub mod user;


Содержимое файла src/entities/prelude.rs:
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.15

pub use super::author::Entity as Author;
pub use super::book::Entity as Book;
pub use super::book_author::Entity as BookAuthor;
pub use super::book_genre::Entity as BookGenre;
pub use super::book_rate::Entity as BookRate;
pub use super::chapter::Entity as Chapter;
pub use super::comment::Entity as Comment;
pub use super::comment_rate::Entity as CommentRate;
pub use super::genre::Entity as Genre;
pub use super::user::Entity as User;


Содержимое файла src/routes/mod.rs:
pub mod user_route;

Содержимое файла src/entities/user.rs:
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.15

use rocket::serde::{Deserialize, Serialize};
use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "user")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i32,
    pub display_name: String,
    pub email: String,
    pub login: String,
    pub password: String,
    #[sea_orm(column_type = "Binary(BlobSize::Blob(None))")]
    pub avatar: Vec<u8>,
    pub saved_books: Json,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    #[sea_orm(has_many = "super::book_rate::Entity")]
    BookRate,
    #[sea_orm(has_many = "super::comment::Entity")]
    Comment,
    #[sea_orm(has_many = "super::comment_rate::Entity")]
    CommentRate,
}

impl Related<super::book_rate::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::BookRate.def()
    }
}

impl Related<super::comment_rate::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::CommentRate.def()
    }
}

impl Related<super::book::Entity> for Entity {
    fn to() -> RelationDef {
        super::book_rate::Relation::Book.def()
    }
    fn via() -> Option<RelationDef> {
        Some(super::book_rate::Relation::User.def().rev())
    }
}

impl Related<super::comment::Entity> for Entity {
    fn to() -> RelationDef {
        super::comment_rate::Relation::Comment.def()
    }
    fn via() -> Option<RelationDef> {
        Some(super::comment_rate::Relation::User.def().rev())
    }
}

impl ActiveModelBehavior for ActiveModel {}


Проблема в том, что файл user_route.rs не видит модель user.rs:
use rocket::serde::json::Json;
use rocket::State;

use crate::entities::user;

use sea_orm::DatabaseConnection;

#[get("/user")]
pub async fn get_all_users(db: &State<DatabaseConnection>) -> Json<Vec<user::Model>> {
    let users = User::find().all(db).await.unwrap();
    Json(users)
}


Выдаёт ошибку "Unresolved import `crate::entities`. Could not find `entities` in the crate root"

Не понимаю в чём конкретно ошибка, ведь в mod файлах вроде всё указал
  • Вопрос задан
  • 86 просмотров
Решения вопроса 1
GONJY_MONJY
@GONJY_MONJY Автор вопроса
В поисках новых горизонтов
Ответ оказался до жути простым
Я забыл добавить модуль своих моделей в main.rs файл:
mod entities;
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы