miniweb/rear/src/depot/registry.rs

165 lines
5.4 KiB
Rust

use std::{collections::HashMap, sync::Arc};
use tokio::sync::Mutex;
use super::{
context::{DepotModel, DepotSection},
repository::{DepotModelConfig, DepotRepository, DepotRepositoryWrapper, DynDepotRepository},
};
pub struct DepotRegistry {
base_path: String,
sections: HashMap<String, internal::DepotSectionInfo>,
models: HashMap<String, internal::DepotModelInfo>,
repositories: HashMap<String, Arc<Mutex<dyn DynDepotRepository>>>,
}
impl DepotRegistry {
pub fn new(base_path: &str) -> Self {
DepotRegistry {
base_path: base_path.to_owned(),
sections: HashMap::new(),
models: HashMap::new(),
repositories: HashMap::new(),
}
}
pub fn get_sections(&self) -> Vec<DepotSection> {
self.sections
.iter()
.map(|(key, section_info)| self.get_section(key, section_info))
.collect()
}
fn get_section(&self, key: &str, section_info: &internal::DepotSectionInfo) -> DepotSection {
let my_models = self.get_models(key);
DepotSection {
key: key.to_owned(),
name: section_info.name.to_owned(),
section_url: format!("/{}/{}", self.base_path, key.to_owned()),
models: my_models,
}
}
pub fn register_section(&mut self, name: &str) -> String {
let key = self.get_key(name);
self.sections.insert(
key.to_owned(),
internal::DepotSectionInfo {
key: key.to_owned(),
name: name.to_owned(),
},
);
key
}
fn get_key(&self, name: &str) -> String {
slug::slugify(name)
}
fn model_from_model_info(&self, model_info: &internal::DepotModelInfo) -> DepotModel {
let model_url = format!(
"/{}/{}/model/{}",
self.base_path, model_info.section_key, model_info.model_key
);
DepotModel {
key: model_info.model_key.clone(),
name: model_info.name.clone(),
view_only: false,
add_url: Some(format!("{}/add", model_url)),
model_url: model_url,
}
}
pub fn get_models(&self, section_key: &str) -> Vec<DepotModel> {
self.models
.iter()
.filter(|(key, _)| key.starts_with(&format!("{}.", section_key)))
.map(|(_, model)| self.model_from_model_info(model))
.collect()
}
pub fn get_model(&self, section_key: &str, model_key: &str) -> Option<DepotModel> {
let full_model_key = format!("{}.{}", section_key, model_key);
let internal_model = self.models.get(&full_model_key)?;
Some(self.model_from_model_info(internal_model))
}
fn register_model_config(&mut self, model: DepotModelConfig) -> Result<String, String> {
let local_config = internal::DepotModelInfo::from(model);
if local_config.model_key.is_empty() {
return Err("No model name".to_owned());
}
let local_config_name = format!("{}.{}", local_config.section_key, local_config.model_key);
if self.models.contains_key(&local_config_name) {
return Err(format!("Model {} already exists", local_config_name));
}
let full_model_key = local_config_name.clone();
self.models.insert(local_config_name, local_config);
Ok(full_model_key)
}
pub fn register_model<R: DepotRepository + 'static>(
&mut self,
model: DepotModelConfig,
repository: R,
) -> Result<(), String> {
let model_key = self.register_model_config(model)?;
let repository = DepotRepositoryWrapper::new(repository);
self.repositories
.insert(model_key, Arc::new(Mutex::new(repository)));
Ok(())
}
pub(crate) fn get_repository(
&self,
section_key: &str,
model_key: &str,
) -> Result<Arc<Mutex<dyn DynDepotRepository>>, String> {
let full_model_key = format!("{}.{}", section_key, model_key);
if let Some(repo) = self.repositories.get(&full_model_key) {
// Clone the Arc to return a reference to the repository
return Ok(Arc::clone(repo));
} else {
return Err("Couldn't find repository".to_owned());
}
}
}
mod internal {
// how the registry saves data internally.
use super::super::repository::DepotModelConfig;
#[derive(Clone)]
pub(super) struct DepotSectionInfo {
pub key: String,
pub name: String,
}
#[derive(Clone)]
pub(super) struct DepotModelInfo {
pub section_key: String,
pub model_key: String,
pub name: String,
}
impl From<DepotModelConfig> for DepotModelInfo {
fn from(value: DepotModelConfig) -> Self {
DepotModelInfo {
section_key: value.section_key,
model_key: slug::slugify(value.name.clone()),
name: value.name,
}
}
}
impl From<(&str, &str)> for DepotModelInfo {
fn from(value: (&str, &str)) -> Self {
DepotModelInfo {
section_key: value.0.to_owned(),
model_key: slug::slugify(value.1.to_owned()),
name: value.1.to_owned(),
}
}
}
}