HOLY FLUFFING GOOBER IT COMPILES

This commit is contained in:
Gvidas Juknevičius 2025-09-14 14:18:39 +03:00
parent 66704b4e2e
commit 61ae1b1b53
Signed by: MCorange
GPG Key ID: 5BE6B533CB76FE86
13 changed files with 59 additions and 53 deletions

View File

@ -12,7 +12,7 @@ pub struct Award {
pub modified_at: i64, pub modified_at: i64,
} }
impl TableMeta<'_> for Award { impl TableMeta for Award {
type PrimaryKey = i64; type PrimaryKey = i64;
const TABLE: &'static str = "awards"; const TABLE: &'static str = "awards";
} }

View File

@ -14,7 +14,7 @@ pub struct Mission {
pub modified_at: i64, pub modified_at: i64,
} }
impl TableMeta<'_> for Mission { impl TableMeta for Mission {
type PrimaryKey = i64; type PrimaryKey = i64;
const TABLE: &'static str = "missions"; const TABLE: &'static str = "missions";
} }

View File

@ -12,7 +12,7 @@ pub struct Qualification {
pub modified_at: i64, pub modified_at: i64,
} }
impl TableMeta<'_> for Qualification { impl TableMeta for Qualification {
type PrimaryKey = i64; type PrimaryKey = i64;
const TABLE: &'static str = "qualifications"; const TABLE: &'static str = "qualifications";
} }

View File

@ -12,7 +12,7 @@ pub struct Rank {
pub modified_at: i64, pub modified_at: i64,
} }
impl TableMeta<'_> for Rank { impl TableMeta for Rank {
type PrimaryKey = i64; type PrimaryKey = i64;
const TABLE: &'static str = "ranks"; const TABLE: &'static str = "ranks";
} }

View File

@ -12,7 +12,7 @@ pub struct Training {
pub modified_at: i64, pub modified_at: i64,
} }
impl TableMeta<'_> for Training { impl TableMeta for Training {
type PrimaryKey = i64; type PrimaryKey = i64;
const TABLE: &'static str = "trainings"; const TABLE: &'static str = "trainings";
} }

View File

@ -12,21 +12,22 @@ pub mod records;
pub mod sessions; pub mod sessions;
pub mod user; pub mod user;
pub trait TableMeta<'a>: for<'r> sqlx::FromRow<'r, PgRow> { pub trait TableMeta: for<'r> sqlx::FromRow<'r, PgRow> {
type PrimaryKey: Encode<'a, super::CurrDb> + Type<super::CurrDb> + Clone; type PrimaryKey: Type<super::CurrDb> + Clone;
const TABLE: &'static str; const TABLE: &'static str;
} }
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
pub struct ForeignKey<'a, FK: TableMeta<'a>> { pub struct ForeignKey<FK: TableMeta> {
id: FK::PrimaryKey, id: FK::PrimaryKey,
_ft: PhantomData<FK>, _ft: PhantomData<FK>,
} }
impl<'a, FK: TableMeta<'a> + Send + for<'r> sqlx::FromRow<'r, PgRow> + Unpin + 'a> impl<'a, FK: TableMeta + Send + for<'r> sqlx::FromRow<'r, PgRow> + Unpin + 'a> ForeignKey<FK> {
ForeignKey<'a, FK> pub async fn extract(&self, db: &super::CurrPool) -> anyhow::Result<FK>
{ where
pub async fn extract(&self, db: &super::CurrPool) -> anyhow::Result<FK> { <FK as TableMeta>::PrimaryKey: for<'b> sqlx::Encode<'b, super::CurrDb>,
{
let id = self.id.clone(); let id = self.id.clone();
let res = QueryBuilder::new("") let res = QueryBuilder::new("")
@ -40,39 +41,44 @@ impl<'a, FK: TableMeta<'a> + Send + for<'r> sqlx::FromRow<'r, PgRow> + Unpin + '
} }
} }
impl<'a, TM: TableMeta<'a>> Type<super::CurrDb> for ForeignKey<'a, TM> { impl<'a, TM: TableMeta> Type<super::CurrDb> for ForeignKey<TM> {
fn type_info() -> <super::CurrDb as sqlx::Database>::TypeInfo { fn type_info() -> <super::CurrDb as sqlx::Database>::TypeInfo {
TM::PrimaryKey::type_info() TM::PrimaryKey::type_info()
} }
} }
impl<'a, TM: TableMeta<'a>> Decode<'a, super::CurrDb> for ForeignKey<'a, TM> impl<'a, TM: TableMeta> Decode<'a, super::CurrDb> for ForeignKey<TM>
where where
&'a str: Decode<'a, super::CurrDb>, &'a str: Decode<'a, super::CurrDb>,
<TM as TableMeta<'a>>::PrimaryKey: sqlx::Decode<'a, super::CurrDb>, <TM as TableMeta>::PrimaryKey: sqlx::Decode<'a, super::CurrDb>,
{ {
fn decode( fn decode(
value: <super::CurrDb as sqlx::Database>::ValueRef<'a>, value: <super::CurrDb as sqlx::Database>::ValueRef<'a>,
) -> Result<Self, Box<dyn Error + 'static + Send + Sync>> { ) -> Result<Self, Box<dyn Error + 'static + Send + Sync>> {
<TM::PrimaryKey as Decode<super::CurrDb>>::decode(value); match <TM::PrimaryKey as Decode<super::CurrDb>>::decode(value) {
todo!() Ok(v) => Ok(Self {
id: v,
_ft: PhantomData,
}),
Err(e) => Err(e),
}
} }
} }
impl<'a, TM: TableMeta<'a>> Deref for ForeignKey<'a, TM> { impl<TM: TableMeta> Deref for ForeignKey<TM> {
type Target = TM::PrimaryKey; type Target = TM::PrimaryKey;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.id &self.id
} }
} }
impl<'a, TM: TableMeta<'a>> DerefMut for ForeignKey<'a, TM> { impl<TM: TableMeta> DerefMut for ForeignKey<TM> {
fn deref_mut(&mut self) -> &mut Self::Target { fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.id &mut self.id
} }
} }
impl<'a, FK: TableMeta<'a, PrimaryKey = i64>> From<i64> for ForeignKey<'a, FK> { impl<FK: TableMeta<PrimaryKey = i64>> From<i64> for ForeignKey<FK> {
fn from(value: FK::PrimaryKey) -> Self { fn from(value: FK::PrimaryKey) -> Self {
Self { Self {
id: value, id: value,

View File

@ -7,20 +7,20 @@ use crate::db::{
}; };
#[derive(Debug, Default, Clone, FromRow)] #[derive(Debug, Default, Clone, FromRow)]
pub struct AwardRecord<'a> { pub struct AwardRecord {
pub id: i64, pub id: i64,
pub user_id: ForeignKey<'a, User>, pub user_id: ForeignKey<User>,
pub award_id: ForeignKey<'a, Award>, pub award_id: ForeignKey<Award>,
pub author_id: ForeignKey<'a, User>, pub author_id: ForeignKey<User>,
pub created_at: i64, pub created_at: i64,
} }
impl<'a> TableMeta<'a> for AwardRecord<'a> { impl TableMeta for AwardRecord {
type PrimaryKey = i64; type PrimaryKey = i64;
const TABLE: &'static str = "records_awards"; const TABLE: &'static str = "records_awards";
} }
impl AwardRecord<'_> { impl AwardRecord {
pub async fn insert_new(&self, pool: &CurrPool) -> Result<Self> { pub async fn insert_new(&self, pool: &CurrPool) -> Result<Self> {
let session = sqlx::query_as!( let session = sqlx::query_as!(
AwardRecord, AwardRecord,
@ -37,7 +37,7 @@ impl AwardRecord<'_> {
.fetch_one(pool) .fetch_one(pool)
.await?; .await?;
self.author_id.extract(pool); let user = self.author_id.extract(pool).await?;
Ok(session) Ok(session)
} }

View File

@ -7,20 +7,20 @@ use crate::db::{
}; };
#[derive(Debug, Default, Clone, FromRow)] #[derive(Debug, Default, Clone, FromRow)]
pub struct MissionRecord<'a> { pub struct MissionRecord {
pub id: i64, pub id: i64,
pub user_id: ForeignKey<'a, User>, pub user_id: ForeignKey<User>,
pub mission_id: ForeignKey<'a, Mission>, pub mission_id: ForeignKey<Mission>,
pub author_id: ForeignKey<'a, User>, pub author_id: ForeignKey<User>,
pub created_at: i64, pub created_at: i64,
} }
impl TableMeta<'_> for MissionRecord<'_> { impl TableMeta for MissionRecord {
type PrimaryKey = i64; type PrimaryKey = i64;
const TABLE: &'static str = "records_missions"; const TABLE: &'static str = "records_missions";
} }
impl MissionRecord<'_> { impl MissionRecord {
pub async fn insert_new(&self, pool: &CurrPool) -> Result<Self> { pub async fn insert_new(&self, pool: &CurrPool) -> Result<Self> {
let session = sqlx::query_as!( let session = sqlx::query_as!(
MissionRecord, MissionRecord,

View File

@ -7,20 +7,20 @@ use crate::db::{
}; };
#[derive(Debug, Default, Clone, FromRow)] #[derive(Debug, Default, Clone, FromRow)]
pub struct QualificationRecord<'a> { pub struct QualificationRecord {
pub id: i64, pub id: i64,
pub user_id: ForeignKey<'a, User>, pub user_id: ForeignKey<User>,
pub author_id: ForeignKey<'a, User>, pub author_id: ForeignKey<User>,
pub qualification_id: ForeignKey<'a, Qualification>, pub qualification_id: ForeignKey<Qualification>,
pub created_at: i64, pub created_at: i64,
} }
impl<'a> TableMeta<'a> for QualificationRecord<'a> { impl TableMeta for QualificationRecord {
type PrimaryKey = i64; type PrimaryKey = i64;
const TABLE: &'static str = "records_qualifications"; const TABLE: &'static str = "records_qualifications";
} }
impl<'a> QualificationRecord<'a> { impl QualificationRecord {
pub async fn insert_new(&self, pool: &CurrPool) -> Result<Self> { pub async fn insert_new(&self, pool: &CurrPool) -> Result<Self> {
let session = sqlx::query_as!( let session = sqlx::query_as!(
QualificationRecord, QualificationRecord,

View File

@ -7,20 +7,20 @@ use crate::db::{
}; };
#[derive(Debug, Default, Clone, FromRow)] #[derive(Debug, Default, Clone, FromRow)]
pub struct RankRecord<'a> { pub struct RankRecord {
pub id: i64, pub id: i64,
pub user_id: ForeignKey<'a, User>, pub user_id: ForeignKey<User>,
pub rank_id: ForeignKey<'a, Rank>, pub rank_id: ForeignKey<Rank>,
pub author_id: ForeignKey<'a, User>, pub author_id: ForeignKey<User>,
pub created_at: i64, pub created_at: i64,
} }
impl<'a> TableMeta<'a> for RankRecord<'a> { impl TableMeta for RankRecord {
type PrimaryKey = i64; type PrimaryKey = i64;
const TABLE: &'static str = "records_ranks"; const TABLE: &'static str = "records_ranks";
} }
impl<'a> RankRecord<'a> { impl RankRecord {
pub async fn insert_new(&self, pool: &CurrPool) -> Result<Self> { pub async fn insert_new(&self, pool: &CurrPool) -> Result<Self> {
let session = sqlx::query_as!( let session = sqlx::query_as!(
RankRecord, RankRecord,

View File

@ -7,20 +7,20 @@ use crate::db::{
}; };
#[derive(Debug, Default, Clone, FromRow)] #[derive(Debug, Default, Clone, FromRow)]
pub struct TrainingRecord<'a> { pub struct TrainingRecord {
pub id: i64, pub id: i64,
pub user_id: ForeignKey<'a, User>, pub user_id: ForeignKey<User>,
pub training_id: ForeignKey<'a, Training>, pub training_id: ForeignKey<Training>,
pub author_id: ForeignKey<'a, User>, pub author_id: ForeignKey<User>,
pub created_at: i64, pub created_at: i64,
} }
impl<'a> TableMeta<'a> for TrainingRecord<'a> { impl TableMeta for TrainingRecord {
type PrimaryKey = i64; type PrimaryKey = i64;
const TABLE: &'static str = "records_trainings"; const TABLE: &'static str = "records_trainings";
} }
impl<'a> TrainingRecord<'a> { impl TrainingRecord {
pub async fn insert_new(&self, pool: &CurrPool) -> Result<Self> { pub async fn insert_new(&self, pool: &CurrPool) -> Result<Self> {
let session = sqlx::query_as!( let session = sqlx::query_as!(
TrainingRecord, TrainingRecord,

View File

@ -10,7 +10,7 @@ pub struct Session {
pub expires: i64, pub expires: i64,
} }
impl TableMeta<'_> for Session { impl TableMeta for Session {
type PrimaryKey = i64; type PrimaryKey = i64;
const TABLE: &'static str = "sessions"; const TABLE: &'static str = "sessions";
} }

View File

@ -14,7 +14,7 @@ pub struct User {
pub rank_id: i64, pub rank_id: i64,
} }
impl TableMeta<'_> for User { impl TableMeta for User {
type PrimaryKey = i64; type PrimaryKey = i64;
const TABLE: &'static str = "users"; const TABLE: &'static str = "users";
} }