You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use diesel::{AsExpression, FromSqlRow, Queryable, QueryableByName};
use diesel::backend::Backend;
use diesel::deserialize::FromSql;
use diesel::serialize::{Output, ToSql};
use diesel::sql_types::Text;
use regex::Regex;
#[derive(Clone, Debug, PartialEq, Eq, FromSqlRow, AsExpression, QueryableByName)]
#[sql_type = "Text"]
pub struct Password {
pub alg: Option<String>,
pub hash: String
}
impl<DB: Backend> FromSql<String, DB> for Password where String: FromSql<Text, DB> {
fn from_sql(bytes: DB::RawValue<'_>) -> diesel::deserialize::Result<Self> {
let re = Regex::new(r"(\{\w+\})?(.*)").expect("Could not compile Password regex");
let string_values = <String as FromSql<Text, DB>>::from_sql(bytes)?;
let captures = re.captures(&string_values).expect("Could not capture error");
let alg = captures.get(0).map(|m| m.as_str().to_string());
let hash = captures.get(1).map(|m| m.as_str().to_string()).expect("No hash part?");
Ok(Self { alg, hash })
}
}
impl<DB: Backend> ToSql<String, DB> for Password where String: ToSql<Text, DB> {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, DB>) -> diesel::serialize::Result {
let s = format!("{}{}", self.alg.unwrap_or(String::new()), self.hash);
s.to_sql(out)
}
}
Expect this to take a db row, and deserialize the password text column into a Password struct correctly, however:
error[E0277]: the trait bound `diesel::expression::select_by::SelectBy<db::model::User, Pg>: CompatibleType<db::model::User, Pg>` is not satisfied
--> backend/src/routes/api/login.rs:69:8
|
69 | .first::<db::User>(&mut conn)
| ^^^^^ the trait `CompatibleType<db::model::User, Pg>` is not implemented for `diesel::expression::select_by::SelectBy<db::model::User, Pg>`
|
= help: the trait `CompatibleType<U, DB>` is implemented for `diesel::expression::select_by::SelectBy<U, DB>`
= note: required for `SelectStatement<FromClause<table>, SelectClause<SelectBy<User, Pg>>, NoDistinctClause, ..., ..., ...>` to implement `diesel_async::methods::LoadQuery<'_, _, db::model::User>`
= note: the full type name has been written to '/home/akos/projects/rust/emailadmin/target/debug/deps/emailadmin-694eaea5ca79c3f9.long-type-3122529722960146015.txt'
This seems to be more a support question, which would belong to the discussion forum.
For the error messages: There is already #151 to track that, so even for that it's a duplicated issue. In addition there are flags for this specific case to improve the generated error messages. They are documented here and here
Closed as duplicate/misplaced support question in our issue tracker.
Setup
Versions
Feature Flags
Problem Description
Can't correctly map one database column to an entire struct and back
Steps to reproduce
Expect this to take a db row, and deserialize the password text column into a Password struct correctly, however:
Checklist
closed if this is not the case)
The text was updated successfully, but these errors were encountered: