Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot find derive macro FromForm in this scope #274

Closed
tekknolagi opened this issue Apr 23, 2017 · 13 comments
Closed

Cannot find derive macro FromForm in this scope #274

tekknolagi opened this issue Apr 23, 2017 · 13 comments
Labels
question A question (converts to discussion)

Comments

@tekknolagi
Copy link

I am using rocket, rocket_codegen, and rocket_contrib all version 0.2.6. Rust is 1.18.0-nightly (252d3da8a). I've poked around docs, general Rust documentation about custom derive macros, RFCs, and previous issues on Rocket. It feels like https://api.rocket.rs/rocket/request/trait.FromForm.html should answer this question, but it doesn't.

How can I have a struct deriving FromForm in one file, util.rs, and use it in the main webserver file web.rs? And if I can't, what are the best practices for receiving form input meant to create a resource?

My code currently looks something like:

// util.rs
extern crate serde;
extern crate serde_json;
extern crate chrono;

use std::error::Error;
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;

use self::chrono::prelude::*;

#[derive(Serialize, Deserialize, Debug, Clone, FromForm)]
pub struct Person {
    pub id: u64,
    pub name: String,
}
// web.rs
#![feature(plugin, custom_derive)]
#![plugin(rocket_codegen)]
extern crate rocket;
extern crate serde_json;
extern crate rocket_contrib;

use rocket::request::Form;
use rocket_contrib::Template;

#[post("/people/add", data="<user_input>")]
fn post_people_add(user_input: Form<util::Person>) -> String {
    let input: util::Person = user_input.into_inner();
    format!("{:#?}", input)
}

Currently util.rs throws an error about the derive macro FromForm not being in scope. When I make a struct in web.rs, however, that does not happen.

@SergioBenitez
Copy link
Member

Can you post the complete error output?

@tekknolagi
Copy link
Author

tekknolagi commented Apr 23, 2017

Sure!

willow% cargo build
   Compiling holdthedoor v0.1.0 (<long_path_here>)
error: cannot find derive macro `FromForm` in this scope
  --> src/util.rs:12:48
   |
12 | #[derive(Serialize, Deserialize, Debug, Clone, FromForm)]
   |                                                ^^^^^^^^

error: aborting due to previous error

error: Could not compile `holdthedoor`.
Build failed, waiting for other jobs to finish...
error: build failed
willow% 

@SergioBenitez
Copy link
Member

Which OS are you on? I can't replicate this on either OS X or Linux (Debian, Arch, or CoreOS).

This is unlikely to be an issue with Rocket, and instead, and issue with rustc. There was an issue with a previous nightly (rust-lang/rust#40663) that looks related; perhaps @jseyfried can chime in here. Maybe you can try reordering the derives or adding and removing a few of them.

@SergioBenitez SergioBenitez added bug Deviation from the specification or expected behavior upstream An unresolvable issue: an upstream dependency bug labels Apr 24, 2017
@tekknolagi
Copy link
Author

I am running Ubuntu 16.04. I'll try upgrading my nightly.

@tekknolagi
Copy link
Author

That didn't work, nor did re-ordering/removing some derives.

@rofrol
Copy link
Contributor

rofrol commented Apr 25, 2017

@tekknolagi

Try other version of nightly. This works for me:

rustup install nightly-2017-04-16
rustup override set nightly-2017-04-16

nightly-2017-04-16 contains minimum supported rustc version 2017-04-15

@tekknolagi
Copy link
Author

tekknolagi commented Apr 26, 2017

@rofrol negatory. Does not work.

@SergioBenitez SergioBenitez removed bug Deviation from the specification or expected behavior upstream An unresolvable issue: an upstream dependency bug labels Apr 26, 2017
@SergioBenitez
Copy link
Member

SergioBenitez commented Apr 26, 2017

I boot up a VM with Ubuntu 16.04 to verify this issue and was unable to. As such, I don't believe there is an issue with rustc here.

Looking at your example code, it's not clear how you're structuring your project. I had assumed that util was a module inside of web or some joint parent, but I imagine that's untrue given the error you're getting and the fact that you have extern crate in both files. In order to be able to derive FromForm, you need to have rocket_codegen as a plugin in the crate where you're deriving it. Where does util live? Does that crate root have a #![plugin(rocket_codegen)] crate attribute?

If these hints don't resolve your issue, please post a minimally reproducing example.

@tekknolagi
Copy link
Author

@SergioBenitez The folder structure is like this:

willow% tree .
.
├── Cargo.lock
├── Cargo.toml
├── db.json
├── src
│   ├── cli.rs
│   ├── util.rs
│   └── web.rs
├── static
│   ├── app.js
│   ├── index.html
│   ├── mustache.min.js
│   ├── person_add.html
│   └── zepto.min.js
└── templates
    ├── people
    │   └── list.html.hbs
    └── rules
        └── list.html.hbs

I'm rather new to Rust, so I might be structuring my project the wrong way. util is supposed to be shared between web and cli, two separate binaries.

@Sunspar
Copy link

Sunspar commented Apr 27, 2017

Im able to compile the current state of master on OS X, assuming we're talking about https://github.com/tekknolagi/holdthedoor. Are you doing something fancier than simply cargo build?

For reference, I have the following installed at the moment:

cargo 0.19.0-nightly (8326a3683 2017-04-19)
rustc 1.18.0-nightly (252d3da8a 2017-04-22)

@SergioBenitez
Copy link
Member

@tekknolagi Ah, alright. So this is indeed what I described earlier:

In order to be able to derive FromForm, you need to have rocket_codegen as a plugin in the crate where you're deriving it.

In this case, you have two crates. One is web and the other cli. Since both crates have util as a module, and that module requires FromForm, they must both have rocket and rocket_codegen as a dependency and plugin, respectively. Adding extern crate rocket and #![plugin(rocket_codegen)] to cli.rs should resolve your issue.

@tekknolagi
Copy link
Author

@Sunspar the current state of master contains my workaround for this problem, which includes a new struct FormPerson that has a .to_person() method. So it should compile.

@SergioBenitez Interesting. I'll give that a go!

@tekknolagi
Copy link
Author

It worked and I pushed the update to master of holdthedoor. Thank you so much @SergioBenitez!

@SergioBenitez SergioBenitez added the question A question (converts to discussion) label Jun 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question A question (converts to discussion)
Projects
None yet
Development

No branches or pull requests

4 participants