Reputation: 149
I want to use the structure of model's rodo.rs in controller's todo.rs. How should I write the mod?
use actix_web::{get, post, web, HttpResponse,Responder};
mod model;
#[get("/todos/{id}")]
pub async fn get_todo(web::Path(id): web::Path<u32>) -> impl Responder {
println!("get_todo");
let id_option: Option<u32> = Some(id);
HttpResponse::Ok().json(model::Todo {
id: id_option,
content: String::from("やること"),
completed: false,
})
}
#[post("/todos")]
pub async fn post_todo(todo: web::Json<model::Todo>) -> impl Responder {
println!("post_todo");
println!("{:?}", todo);
HttpResponse::Ok().body("ok")
}
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct Todo {
id: Option<u32>,
content: String,
completed: bool,
}
I want to use the structure of model's rodo.rs in controller's todo.rs. How should I write the mod? How else can I use the file?
Upvotes: 0
Views: 75
Reputation: 715
I would propose a slightly different structure
//src/main.rs
mod app
//src/app.rs
pub mod controller;
pub mod model;
pub mod repository ... etc
//src/app/controller.rs
pub mod user;
... etc
//src/app/controller/user.rs //implementation controller
//src/app/repository.rs
pub mod user;
.. etc
//src/app/repository/user.rs //implementation repository
Upvotes: 1
Reputation: 27186
The way your files are layed out you'd have to add the following mod
declarations and use
statements to the files:
//src/main.rs
pub mod controller;
pub mod model;
// src/controller.rs
pub mod todo;
// src/model.rs
pub mod todo;
// src/controller/todo.rs
use crate::model::todo::*; // * to include everything from src/model/todo.rs which i guess you'll need
Upvotes: 1