use crate::vector::models::ai::ai_interactions::AIInteractions;
use conundrum_lang::ecosystem::db::db_traits::db_entity::DBEntity;
use conundrum_lang::ecosystem::db::db_traits::db_entity::DBSchema;
use conundrum_lang::ecosystem::db::db_traits::db_field::DatabaseField;
use conundrum_lang::ecosystem::db::db_traits::into_partial::IntoPartial;
use conundrum_lang::ecosystem::db::db_traits::validate::ValidateSelf;
use conundrum_lang::ecosystem::db::tables::DatabaseTable;
use conundrum_lang::ecosystem::error_handling::db_error::DatabaseError;
use conundrum_lang::ecosystem::error_handling::db_error::DatabaseResult;
use conundrum_lang::impl_default_crud;
use conundrum_lang::lang::constants::file_types::ParsableFileType;
use conundrum_lang::lifted_models::primitives::date_time::DateTime;
use conundrum_fs::workspace_management::file_walk_config::FileWalkConfig;
use conundrum_fs::workspace_management::get_filetype_recursively::ParsableFileTypePathMap;
use conundrum_fs::workspace_management::get_filetype_recursively::get_filetype_in_workspace_recursively;
use conundrum_macros::DatabaseEntity;
use fake::Dummy;
use lancedb::arrow::arrow_schema::DataType;
use lancedb::arrow::arrow_schema::Field;
use serde::{Deserialize, Serialize};
use specta::Type;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
static USER_WORKSPACE_PRIMARY_KEY: &str = "root";
static USER_WORKSPACE_MERGE_KEYS: &[&str] = &[USER_WORKSPACE_PRIMARY_KEY];
pub fn default_partial_ctime() -> Option<DateTime> {
Some(DateTime::new_now())
}
#[derive(Serialize, Deserialize, Clone, Debug, Type, Dummy, DatabaseEntity)]
#[db(table = DatabaseTable::UserWorkspace)]
pub struct UserWorkspace {
#[db(primary)]
pub root: String,
pub label: Option<String>,
pub respect_gitignore: bool,
pub ignore_hidden: bool,
#[serde(default = "Default::default")]
#[db(partial(default = "Default::default"))]
pub resource_dir: String,
pub ai: AIInteractions,
#[serde(default = "DateTime::new_now")]
#[db(partial(default = "default_partial_ctime"))]
pub ctime: DateTime,
}
impl UserWorkspace {
pub async fn exists(&self) -> bool {
tokio::fs::try_exists(self.root.clone()).await.is_ok_and(|x| x)
}
pub fn join_path<P: AsRef<Path>>(&self, fp: P) -> PathBuf {
Path::new(&self.root).join(fp)
}
pub async fn parsable_files_of_type(&self) -> DatabaseResult<HashMap<ParsableFileType, Vec<String>>> {
let c = self.into_file_walk_config();
let mutex = get_filetype_in_workspace_recursively(c).await.map_err(DatabaseError::FileSystemError)?;
if let Ok(items) = Arc::try_unwrap(mutex) {
let x = items.into_inner();
Ok(x)
} else {
Err(DatabaseError::ThreadError)
}
}
pub fn from_root_and_label(root: String, label: Option<String>) -> Self {
Self { root,
label,
ignore_hidden: true,
respect_gitignore: true,
resource_dir: "/resources".to_string(),
ai: AIInteractions::default(),
ctime: DateTime::new_now() }
}
pub fn item_field_def() -> Field {
Field::new("item", DataType::Utf8, true)
}
fn into_file_walk_config(&self) -> FileWalkConfig {
FileWalkConfig { ignore_hidden: self.ignore_hidden,
respect_git_ignore: self.respect_gitignore,
root: self.root.clone() }
}
}
impl ValidateSelf for UserWorkspace {
async fn validate(&self) -> DatabaseResult<()> {
if self.exists().await {
Ok(())
} else {
Err(DatabaseError::InvalidWorkspacePath(self.root.clone()))
}
}
}
impl From<String> for UserWorkspace {
fn from(value: String) -> Self {
Self { root: value,
label: None,
respect_gitignore: true,
ignore_hidden: true,
resource_dir: "/resources".to_string(),
ai: AIInteractions::default(),
ctime: DateTime::new_now() }
}
}
impl IntoPartial<UserWorkspacePartial> for UserWorkspace {
fn into_partial(&self) -> UserWorkspacePartial {
UserWorkspacePartial { root: self.root.clone(),
label: Some(self.label.clone()),
ignore_hidden: Some(self.ignore_hidden),
respect_gitignore: Some(self.respect_gitignore),
ai: Some(self.ai.clone()),
resource_dir: Some(self.resource_dir.clone()),
ctime: Some(self.ctime.clone()) }
}
}
#[cfg(test)]
mod tests {
use conundrum_lang::ecosystem::db::db_traits::entity_crud::EntityCRUD;
use fake::Fake;
use crate::test_crud_functionality;
use super::*;
#[tokio::test]
async fn user_workspace_crud_functionality() {
test_crud_functionality!(UserWorkspace, <UserWorkspace as DBSchema>::PartialUpdateType, "UserWorkspace");
}
}