refactoring: wip

This commit is contained in:
Per Stark
2024-11-20 16:09:35 +01:00
parent ec347d524c
commit 7222223c31
5 changed files with 100 additions and 14 deletions

1
src/storage/mod.rs Normal file
View File

@@ -0,0 +1 @@
pub mod types;

42
src/storage/types/mod.rs Normal file
View File

@@ -0,0 +1,42 @@
pub mod text_content;
#[macro_export]
macro_rules! stored_entity {
($name:ident, $table:expr, {$($field:ident: $ty:ty),*}) => {
use axum::async_trait;
use serde::{Deserialize, Deserializer, Serialize};
use surrealdb::sql::Thing;
fn thing_to_string<'de, D>(deserializer: D) -> Result<String, D::Error>
where
D: Deserializer<'de>,
{
let thing = Thing::deserialize(deserializer)?;
Ok(thing.id.to_raw())
}
#[async_trait]
pub trait StoredEntity: Serialize + for<'de> Deserialize<'de> {
fn table_name() -> &'static str;
fn get_id(&self) -> &str;
}
#[derive(Debug, Serialize, Deserialize)]
pub struct $name {
#[serde(deserialize_with = "thing_to_string")]
pub id: String,
$(pub $field: $ty),*
}
#[async_trait]
impl StoredEntity for $name {
fn table_name() -> &'static str {
$table
}
fn get_id(&self) -> &str {
&self.id
}
}
};
}

View File

@@ -0,0 +1,35 @@
use uuid::Uuid;
use crate::models::file_info::FileInfo;
use crate::stored_entity;
stored_entity!(TextContent, "text_content", {
text: String,
file_info: Option<FileInfo>,
instructions: String,
category: String
});
impl TextContent {
pub fn new(text: String, instructions: String, category: String) -> Self {
Self {
id: Uuid::new_v4().to_string(),
text,
file_info: None,
instructions,
category,
}
}
// Other methods...
}
fn test() {
let content = TextContent::new(
"hiho".to_string(),
"instructions".to_string(),
"cat".to_string(),
);
content.get_id();
}