Hey everyone, so I'm trying to use the UUID data type for my Announcement struct
use actix_web::{
get, post,
web::{Data, Json, Path},
Responder, HttpResponse
};
use serde::{Deserialize, Serialize} ;
use sqlx::{
self, postgres::PgRow, Error, FromRow, Postgres
};
use uuid::Uuid;
use crate::AppState;
use sqlx::Row;
#[derive(Serialize)]
struct Announcement {
announcement_uid: String,
info: String,
date: String,
club_uid: Uuid
}
The problem is that I want to bind my club_uid field to a sqlx query so I tried manually implementing the FromRow trait for the Announcement struct. Unfortunately, I then got the error: the trait bound Uuid: Type<Postgres> is not satisfied
impl<'r> FromRow<'r, PgRow> for Announcement
where
Uuid: ::sqlx::decode::Decode<'r, Postgres>,
Uuid: ::sqlx::types::Type<Postgres>,
{
fn from_row(row: &'r PgRow) -> Result<Self, Error> {
let announcement_uid: String = row.try_get("announcement_uid")?;
let info: String = row.try_get("info")?;
let date: String = row.try_get("date")?;
let club_uid: Uuid = row.try_get("club_uid")?;
Ok(Announcement { announcement_uid, info, date, club_uid })
}
}
Not only that but when I try binding my club_uid to a sqlx query, I got the error: the trait bound Result<Uuid, uuid::Error>: Encode<'_, _>
is not satisfied
#[post("/announcement")]
pub async fn create_announcement(state: Data<AppState>, body: Json<CreateAnnouncement>) -> impl Responder {
match sqlx::query_as::<_, Announcement>(
"INSERT INTO announcement (announcement_uid, info, date, club_uid) VALUES (uuid_generate_v4(), $1, $2, $3) RETURNING announcement_uid, info, date"
)
.bind(body.info.to_string())
.bind(body.date.to_string())
.bind(Uuid::parse_str(body.club_uid.as_str()))
.fetch_one(&state.db)
.await
{
Ok(announcement) => HttpResponse::Ok().json(announcement),
Err(e) => {
println!("{}", e);
HttpResponse::InternalServerError().json("Failed to create club announcement")
}
}
}
I'm really new to Rust so any assistance would be helpful. Thanks in advance!
Edit: Here's my cargo build
error[E0599]: no method named `as_str` found for struct `Uuid` in the current scope
--> src\services.rs:85:45
|
85 | .bind(Uuid::parse_str(body.club_uid.as_str()).expect("foo"))
| ^^^^^^ method not found in `Uuid`
For more information about this error, try `rustc --explain E0599`.