use std::collections::HashMap;
use std::path::{Path, PathBuf};
use tempo_cli::models::Project;
#[derive(Debug)]
#[allow(dead_code)]
pub struct ProjectCache {
by_path: HashMap<PathBuf, ProjectEntry>,
by_id: HashMap<i64, PathBuf>,
}
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct ProjectEntry {
pub id: i64,
pub name: String,
pub is_archived: bool,
pub git_hash: Option<String>,
}
impl From<&Project> for ProjectEntry {
fn from(project: &Project) -> Self {
Self {
id: project.id.unwrap_or(0),
name: project.name.clone(),
is_archived: project.is_archived,
git_hash: project.git_hash.clone(),
}
}
}
#[allow(dead_code)]
impl ProjectCache {
pub fn new() -> Self {
Self {
by_path: HashMap::new(),
by_id: HashMap::new(),
}
}
pub fn insert(&mut self, project: Project) {
if let Some(id) = project.id {
let path = project.path.clone();
let entry = ProjectEntry::from(&project);
self.by_path.insert(path.clone(), entry);
self.by_id.insert(id, path);
}
}
pub fn get_by_path(&self, path: &Path) -> Option<&ProjectEntry> {
self.by_path.get(path)
}
pub fn get_by_id(&self, id: i64) -> Option<&ProjectEntry> {
self.by_id.get(&id).and_then(|path| self.by_path.get(path))
}
pub fn contains_path(&self, path: &Path) -> bool {
self.by_path.contains_key(path)
}
pub fn contains_id(&self, id: i64) -> bool {
self.by_id.contains_key(&id)
}
pub fn remove_by_path(&mut self, path: &Path) -> Option<ProjectEntry> {
if let Some(entry) = self.by_path.remove(path) {
self.by_id.remove(&entry.id);
Some(entry)
} else {
None
}
}
pub fn remove_by_id(&mut self, id: i64) -> Option<ProjectEntry> {
if let Some(path) = self.by_id.remove(&id) {
self.by_path.remove(&path)
} else {
None
}
}
pub fn clear(&mut self) {
self.by_path.clear();
self.by_id.clear();
}
pub fn len(&self) -> usize {
self.by_path.len()
}
pub fn is_empty(&self) -> bool {
self.by_path.is_empty()
}
pub fn iter(&self) -> impl Iterator<Item = (&PathBuf, &ProjectEntry)> {
self.by_path.iter()
}
pub fn paths(&self) -> impl Iterator<Item = &PathBuf> {
self.by_path.keys()
}
pub fn update_entry<F>(&mut self, path: &Path, updater: F) -> bool
where
F: FnOnce(&mut ProjectEntry),
{
if let Some(entry) = self.by_path.get_mut(path) {
updater(entry);
true
} else {
false
}
}
pub fn insert_all(&mut self, projects: Vec<Project>) {
for project in projects {
self.insert(project);
}
}
}
impl Default for ProjectCache {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
fn create_test_project(id: i64, name: &str, path: &str) -> Project {
Project {
id: Some(id),
name: name.to_string(),
path: PathBuf::from(path),
description: None,
git_hash: None,
is_archived: false,
created_at: chrono::Utc::now(),
updated_at: chrono::Utc::now(),
}
}
#[test]
fn test_cache_insert_and_lookup() {
let mut cache = ProjectCache::new();
let project = create_test_project(1, "Test Project", "/test/path");
cache.insert(project);
assert!(cache.contains_id(1));
assert!(cache.contains_path(&PathBuf::from("/test/path")));
assert_eq!(cache.len(), 1);
}
#[test]
fn test_cache_lookup_by_path() {
let mut cache = ProjectCache::new();
let project = create_test_project(1, "Test Project", "/test/path");
cache.insert(project);
let entry = cache.get_by_path(&PathBuf::from("/test/path")).unwrap();
assert_eq!(entry.id, 1);
assert_eq!(entry.name, "Test Project");
}
#[test]
fn test_cache_lookup_by_id() {
let mut cache = ProjectCache::new();
let project = create_test_project(1, "Test Project", "/test/path");
cache.insert(project);
let entry = cache.get_by_id(1).unwrap();
assert_eq!(entry.id, 1);
assert_eq!(entry.name, "Test Project");
}
#[test]
fn test_cache_remove() {
let mut cache = ProjectCache::new();
let project = create_test_project(1, "Test Project", "/test/path");
cache.insert(project);
assert_eq!(cache.len(), 1);
let removed = cache.remove_by_path(&PathBuf::from("/test/path"));
assert!(removed.is_some());
assert_eq!(cache.len(), 0);
assert!(!cache.contains_id(1));
}
#[test]
fn test_cache_update() {
let mut cache = ProjectCache::new();
let project = create_test_project(1, "Test Project", "/test/path");
cache.insert(project);
let updated = cache.update_entry(&PathBuf::from("/test/path"), |entry| {
entry.name = "Updated Project".to_string();
});
assert!(updated);
let entry = cache.get_by_path(&PathBuf::from("/test/path")).unwrap();
assert_eq!(entry.name, "Updated Project");
}
}