mirror of
https://github.com/perstarkse/minne.git
synced 2026-05-12 19:00:25 +02:00
refactoring: wip
This commit is contained in:
1
src/storage/mod.rs
Normal file
1
src/storage/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod types;
|
||||
42
src/storage/types/mod.rs
Normal file
42
src/storage/types/mod.rs
Normal 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
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
35
src/storage/types/text_content.rs
Normal file
35
src/storage/types/text_content.rs
Normal 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();
|
||||
}
|
||||
Reference in New Issue
Block a user