I've the below struct:
use chrono::prelude::*;
#[derive(Serialize, Deserialize, Debug)]
pub struct Person {
pub id: usize,
pub fname: String,
pub lname: String,
pub uname: String,
pub pswd: String,
pub role: String,
pub emil: String,
pub created_on: NaiveDateTime,
pub last_login: NaiveDateTime
}
And with rocket. and postgres and recieving username
and password
as json
, reading the database to confirm user existence with correct password using the below code:
#[derive(Serialize, Deserialize, Debug)]
pub struct Login_user{
pub name: String,
pub password: String
}
#[post("/login", format = "application/json", data = "<user>")]
pub fn user_login(mut cookies: Cookies, user: Json<Login_user>) -> &'static str {
println!("user: {:#?}", user);
let mut uid: i32 = 0;
let mut uname = String::new();
let mut pswd = String::new();
let mut person = Person {
id: 0,
fname: "".to_string(),
lname: "".to_string(),
uname: "".to_string(),
pswd: "".to_string(),
role: "".to_string(),
emil: "".to_string(),
created_on: Local::now().naive_local(),
last_login: Local::now().naive_local()
};
for row in &conn().query("
SELECT * FROM public.users
where username = $1
", &[&user.name]).unwrap() {
uid = row.get(0);
uname = row.get(1);
pswd = row.get(2);
person = Person {
id: uid as usize,
fname: row.get(4),
lname: row.get(5),
uname: uname,
pswd: pswd,
role: row.get(3),
emil: row.get(6),
created_on: row.get(7),
last_login: row.get(8)
};
}
println!("Found person {:#?}", &person);
match (user.name, user.password) {
(person.uname, person.pswd) => {
cookies.add_private(Cookie::new("logged", "YES"));
"user found"
}
(person.uname, _) => "wrong password",
_ => "user not found"
}
}
But I'm getting the below error:
> error: expected one of `)`, `,`, or `@`, found `.`
> --> src\routes\login.rs:62:20
> |
> 62 | (person.uname, person.pswd) => {
> | ^ expected one of `)`, `,`, or `@` here
Any idea how to fix it, this is the main point of this thread.
I've 2 other points appreciate if got resolved:
- How can i ensure the
match
is done after theSQL
statement check, I tried adding it to the.unwrap(){...}
block, but failed. - How can I define default
struct
values forPerson
, so I do not need to repeat the code as above.