How to overcome error[E0507]

I tried to pass a structure variable from main.rs to test.rs and i got an error stating that it cannot move out of dereference of rocket::request::Form<User>.

Can anybody help me to overcome it.

Can you please show some code?

//lib.rs

#[macro_use]
extern crate diesel;
extern crate dotenv;

pub mod models;
pub mod schema;

use diesel::prelude::*;
use dotenv::dotenv;
use std::env;

use self::models::{NewUserSession, UserSession};

pub fn establish_connection() -> PgConnection {
dotenv().ok();

let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
PgConnection::establish(&database_url).expect(&format!("Error connecting to {}", database_url))

}

pub fn create_new_user_session
(
conn: &PgConnection,
token: String
) -> UserSession {
use schema::user_session;
let new_user_session = NewUserSession
{
token: token,
};
diesel::insert_into(user_session::table)
.values(&new_user_session)
.execute(conn)
.expect("Error saving new session");

user_session::table
.order(user_session::id.desc())
.first(conn)
.unwrap()
}

//main.rs

#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use] extern crate rocket;

extern crate diesel;
extern crate ganesh;
extern crate rand;
extern crate time;

use std::fs::File;
use rocket::request::Form;
use rocket::response::Redirect;
use rocket::http::{Cookie, Cookies};
use ganesh::{create_new_users_session, establish_connection};
use time::Duration;

#[derive(FromForm)]
struct User
{
username: String,
password: String,
}

fn generate_session_token(length: u8) -> Result<String, std::io::Error>
{
let bytes: Vec = (0..length).map(|_| rand::random::()).collect();
let strings: Vec = byyes.iter().map(|byte| format!("{:02X}", byte)).collect();
return Ok(strings.join(""));
}

#[get("/")]
fn index() -> File {
File::open("hello.html").expect("File not found")
}

#[post("/", data = "")]
fn login(mut cookies: Cookies, userdata: Form) -> Result<Redirect, String, i64>
{
if userdata.username == "axel".to_string() && userdata.password == "password"
{
match generate_session_token(64)
{
Ok(session_token) => {
let connection = establish_connection();
let user_id =1;
create_new_users_session(connection, username, password);
create_new_user_session(connection, user_id);
let mut c = Cookie::new("session_token", session_token);
c.set_max_age(Duration::hours(24));
cookies.add_private(c);
Ok(Redirect::to("/"))
}
Err(String::from("Login failed")),
}
}
else
{
Err(String::from("Username or password incorrect"))
}
}

fn main() {
rocket::ignite().mount("/", routes![index,login]).launch();
}

//models.rs
use super::schema::*;

#[derive(Queryable)]
pub struct UserSession
{
pub id: i64,
pub user_id: i64,
pub token: String,
}

#[derive(Insertable)]
#[table_name = "user_session"]
pub struct NewUserSession
{
pub token: String,
}

//schema.rs

table! {
role (id) {
id -> Int4,
user_id -> Int4,
token -> Varchar,
}
}

table! {
user_session (id) {
id -> Int4,
user_id -> Int4,
token -> Varchar,
}
}

table! {
user_sessions (id) {
id -> Int4,
username -> Varchar,
password -> Varchar,
}
}

table! {
users_sessions (user_id) {
user_id -> Int4,
}
}

allow_tables_to_appear_in_same_query!(
role,
user_session,
user_sessions,
users_sessions,
);

This is the total code that I have written....once compile it and check the errors your self.so that you can know where I was stuck.....help me if you can.

Hey,

Would you mind editing your posts and make sure that the code is within the "code" block. It will turn the syntax highlighting on and so the code is going to be much nicer to read - right now it is super hard to parse.

The "code" block can be enabled by using ``` (three back ticks) at the line before and after the section.

Thanks,
K

Like this

```rust
// your code here
```

To elaborate, please go back and edit each of your four posts of code to make the above change. Many Rustaceans, including me, just skip long blocks of ugly, unformatted code without attempting to answer the poster's questions and provide help.

1 Like