mirror of
https://github.com/perstarkse/minne.git
synced 2026-05-04 22:54:28 +02:00
refactoring: working macro and generics
This commit is contained in:
27
src/storage/db.rs
Normal file
27
src/storage/db.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
use surrealdb::{engine::remote::ws::Client, Surreal};
|
||||
|
||||
use crate::error::ProcessingError;
|
||||
|
||||
use super::types::StoredObject;
|
||||
|
||||
/// Operation to store a object in SurrealDB, requires the struct to implement StoredObject
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `db_client` - A initialized database client
|
||||
/// * `item` - The item to be stored
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result` - Item or Error
|
||||
pub async fn store_item<T>(
|
||||
db_client: &Surreal<Client>,
|
||||
item: T,
|
||||
) -> Result<Option<T>, ProcessingError>
|
||||
where
|
||||
T: StoredObject + Send + Sync + 'static,
|
||||
{
|
||||
db_client
|
||||
.create((T::table_name(), item.get_id()))
|
||||
.content(item)
|
||||
.await
|
||||
.map_err(ProcessingError::from)
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
pub mod db;
|
||||
pub mod types;
|
||||
|
||||
@@ -1,11 +1,21 @@
|
||||
use axum::async_trait;
|
||||
use serde::{Deserialize, Serialize};
|
||||
pub mod text_chunk;
|
||||
pub mod text_content;
|
||||
|
||||
#[async_trait]
|
||||
pub trait StoredObject: Serialize + for<'de> Deserialize<'de> {
|
||||
fn table_name() -> &'static str;
|
||||
fn get_id(&self) -> &str;
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! stored_entity {
|
||||
macro_rules! stored_object {
|
||||
($name:ident, $table:expr, {$($field:ident: $ty:ty),*}) => {
|
||||
use axum::async_trait;
|
||||
use serde::{Deserialize, Deserializer, Serialize};
|
||||
use surrealdb::sql::Thing;
|
||||
use $crate::storage::types::StoredObject;
|
||||
|
||||
fn thing_to_string<'de, D>(deserializer: D) -> Result<String, D::Error>
|
||||
where
|
||||
@@ -15,13 +25,8 @@ macro_rules! stored_entity {
|
||||
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)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct $name {
|
||||
#[serde(deserialize_with = "thing_to_string")]
|
||||
pub id: String,
|
||||
@@ -29,7 +34,7 @@ macro_rules! stored_entity {
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl StoredEntity for $name {
|
||||
impl StoredObject for $name {
|
||||
fn table_name() -> &'static str {
|
||||
$table
|
||||
}
|
||||
|
||||
19
src/storage/types/text_chunk.rs
Normal file
19
src/storage/types/text_chunk.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
use crate::stored_object;
|
||||
use uuid::Uuid;
|
||||
|
||||
stored_object!(TextChunk, "text_chunk", {
|
||||
source_id: String,
|
||||
chunk: String,
|
||||
embedding: Vec<f32>
|
||||
});
|
||||
|
||||
impl TextChunk {
|
||||
pub fn new(source_id: String, chunk: String, embedding: Vec<f32>) -> Self {
|
||||
Self {
|
||||
id: Uuid::new_v4().to_string(),
|
||||
source_id,
|
||||
chunk,
|
||||
embedding,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::models::file_info::FileInfo;
|
||||
use crate::stored_entity;
|
||||
use crate::stored_object;
|
||||
|
||||
stored_entity!(TextContent, "text_content", {
|
||||
stored_object!(TextContent, "text_content", {
|
||||
text: String,
|
||||
file_info: Option<FileInfo>,
|
||||
instructions: String,
|
||||
@@ -23,13 +23,3 @@ impl TextContent {
|
||||
|
||||
// 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