Good morning,
I'm working on a Rust project using Diesel to interact with a PostgreSQL database, and I'm trying to implement the ToSql
interface for a custom UserRole
enum that represents different user roles in my application. My goal is to convert these roles to text when inserting into the database.
Here is my current code:
/**
* UserRole enum
* enum pour les rôles des utilisateurs
* trainer: formateur
* student: étudiant
* autoriser la conversion de UserRole en Text pour la base de données
*/
impl<DB: diesel::backend::Backend> ToSql<Text, DB> for UserRole {
fn to_sql<W: std::io::Write>(&self, out: &mut diesel::serialize::Output<W, DB>) -> diesel::serialize::Result {
match *self {
UserRole::Trainer => out.write_all(b"trainer".as_ref())?,
UserRole::Student => out.write_all(b"student".as_ref())?,
}
Ok(diesel::serialize::IsNull::No)
}
}
However, when I compile my code, I receive the following error:
[E0599] no method named `write_all` found for mutable reference `&mut diesel::serialize::Output<'_, '_, W>` in the current scope.
[Note] method not found in `&mut Output<'_, '_, W>`
It seems that I cannot use write_all on the Output object provided by Diesel. I searched the Diesel documentation but couldn't find any clear examples on how to correctly write data into this Output object for an enum.
Can anyone tell me what I'm doing wrong or suggest a correct way to implement ToSql for my UserRole enum? Any help or pointers to relevant resources would be greatly appreciated.
Thank you in advance for your help.