thedodd / wither

An ODM for MongoDB built on the official MongoDB Rust driver.
https://docs.rs/wither
Other
325 stars 40 forks source link

cannot find derive macro `Model` in this scope #45

Closed huangkaizh closed 4 years ago

huangkaizh commented 4 years ago

Compiling actix_demo v0.1.0 (D:\backCode\actix_demo) error: cannot find derive macro Model in this scope --> src\common\structs.rs:6:17 | 6 | #[derive(Debug, Model, Serialize, Deserialize, Clone)] | ^^^^^

error: aborting due to previous error

error: could not compile actix_demo.

To learn more, run the command again with --verbose.

The Code: structs.rs: use serde_derive::{Deserialize, Serialize}; use mongodb::{ oid::ObjectId, };

[derive(Debug, Model, Serialize, Deserialize, Clone)]

pub struct MyObj { /// The ID of the model.

[serde(rename = "_id", skip_serializing_if = "Option::is_none")]

pub id: Option<ObjectId>,
pub name: String,
pub number: i32,

} ...

main.rs:

[macro_use]

extern crate json;

[macro_use]

extern crate bson;

[macro_use]

extern crate mongodb; extern crate serde;

[macro_use(Serialize, Deserialize)]

extern crate serde_derive; extern crate wither;

[macro_use(Model)]

extern crate wither_derive;

[macro_use]

extern crate log; extern crate hex; extern crate actix_demo; extern crate chrono;

use actix_web::{ error, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer,guard }; use actix_web::http::{StatusCode}; use bytes::BytesMut; use json::JsonValue; use serde_derive::{Deserialize, Serialize};

use mongodb::{ ThreadedClient, db::{Database, ThreadedDatabase}, coll::options::IndexModel, oid::ObjectId, }; use wither::prelude::; use bson::Bson; use serde_json::{Value, Map}; use r2d2_mongodb::{MongodbConnectionManager, ConnectionOptions}; use r2d2::Pool; //use actix_demo::middlewareLocal::state::AppState; use actix_demo::middlewareLocal::auth::Auth; use actix_web::client::ClientRequest; use sha2::{Sha256, Digest}; use rand::prelude::random; use rand::Rng; use actix_service::ServiceExt; use reqwest::r#async::{Client, Response}; use futures::Future; use std::collections::HashMap; use reqwest::header::{USER_AGENT, CONTENT_TYPE, ACCESS_CONTROL_ALLOW_HEADERS}; //::header::{Headers, UserAgent, ContentType}; use actix_session::{CookieSession, Session}; use actix_identity::{Identity, CookieIdentityPolicy, IdentityService}; use qstring::QString; use actix_files as fs; use std::marker::PhantomData; use serde_json::value::Value::Object; use actix_service::Service; use actix_demo::common::structs::; ...

I am fixing it for hours, and failed. Please help.

thedodd commented 4 years ago

@huangkaizh sorry to hear about the difficulties. As far as I can tell, the code looks fine. Which Rust release channel are you using? There are times when running on the latest nightly channel where proc-macros from various libraries may break. I would recommend trying to compile on stable, preferably the most recent stable release.

Let me know how that goes. Also, I would recommend using Github's code formatting for that code sample above. Makes it quite a lot easier to read :) https://guides.github.com/features/mastering-markdown/

huangkaizh commented 4 years ago

@thedodd Thank you for your reply. The error occurs when I move the structs code from main.rs to another file(/src/common/structs.rs) Here is my repo. Please accept my invitation: https://github.com/huangkaizh/actix_mongo_demo/invitations My rust version info:

D:\backCode\actix_mongo_demo>rustc -V
rustc 1.39.0 (4560ea788 2019-11-04)

D:\backCode\actix_mongo_demo>rustup default
stable-x86_64-pc-windows-msvc (default)

Can you reproduce the error and help me to fix it?

thedodd commented 4 years ago

@huangkaizh so, given that you are using extern crate statements and exposing the Model derive proc-macro that way, you will need to ensure that the following statements are also available in the library code (lib.rs):

extern crate wither;
#[macro_use(Model)]
extern crate wither_derive;

Honestly, I would recommend just using rust 2018 semantics. I know I have not updated this crate's example code to show that in action, but it will still work with rust 2018 semantics.

thedodd commented 4 years ago

@huangkaizh any luck with that? It is certainly the issue. If that resolves your issue, mind closing?

huangkaizh commented 4 years ago

Yeah, I fixed it(The error occurs when I move the structs code from main.rs to another file) by modifying wither source, mainly adding derive feature like serde whose derive feature like Serialize and Deserialize works fine in my demo even if I move the structs code from main.rs to another file. Here is the push history: https://gitee.com/huangkaizh/wither/commit/7317d0e6448fd88716b9ad72e22806f7a3112f36 And then, here is my example demo.