use std::path::Path;
use anyhow::Result;
pub use handler::{Operation, RepositoryHandler};
use crate::IgnoreStore;
use crate::{Project, ProjectId, ProjectPath, RelativePath};
pub struct ProjectInfo {
id: ProjectId,
path: String,
}
pub mod handler;
impl ProjectInfo {
pub fn new(id: ProjectId, path: &str) -> Self {
Self {
id,
path: path.to_string(),
}
}
pub fn id(&self) -> ProjectId {
self.id
}
pub fn path(&self) -> &str {
&self.path
}
}
pub trait Repository: IgnoreStore {
fn insert_ignored_words(&mut self, words: &[&str]) -> Result<()>;
fn ignore(&mut self, word: &str) -> Result<()>;
fn new_project(&mut self, project_path: &ProjectPath) -> Result<ProjectId>;
fn project_exists(&self, project_path: &ProjectPath) -> Result<bool>;
fn ensure_project(&mut self, project_path: &ProjectPath) -> Result<Project> {
if !self.project_exists(project_path)? {
self.new_project(project_path)?;
}
let id = self.get_project_id(project_path)?;
Ok(Project::new(id, project_path.clone()))
}
fn remove_project(&mut self, project_id: ProjectId) -> Result<()>;
fn get_project_id(&self, project_path: &ProjectPath) -> Result<ProjectId>;
fn projects(&self) -> Result<Vec<ProjectInfo>>;
fn clean(&mut self) -> Result<()> {
for project in self.projects()? {
let path = project.path();
let path = Path::new(&path);
let id = project.id();
if !path.exists() {
self.remove_project(id)?;
println!("Removed non longer existing project: {}", path.display());
}
}
Ok(())
}
fn skip_file_name(&mut self, file_name: &str) -> Result<()>;
fn ignore_for_extension(&mut self, word: &str, extension: &str) -> Result<()>;
fn ignore_for_project(&mut self, word: &str, project_id: ProjectId) -> Result<()>;
fn ignore_for_path(
&mut self,
word: &str,
project_id: ProjectId,
relative_path: &RelativePath,
) -> Result<()>;
fn remove_ignored(&mut self, word: &str) -> Result<()>;
fn remove_ignored_for_extension(&mut self, word: &str, extension: &str) -> Result<()>;
fn remove_ignored_for_path(
&mut self,
word: &str,
project_id: ProjectId,
relative_path: &RelativePath,
) -> Result<()>;
fn remove_ignored_for_project(&mut self, word: &str, project_id: ProjectId) -> Result<()>;
fn skip_path(&mut self, project_id: ProjectId, relative_path: &RelativePath) -> Result<()>;
fn unskip_file_name(&mut self, file_name: &str) -> Result<()>;
fn unskip_path(&mut self, project_id: ProjectId, relative_path: &RelativePath) -> Result<()>;
fn insert_operation(&mut self, operation: &Operation) -> Result<()>;
fn pop_last_operation(&mut self) -> Result<Option<Operation>>;
}