extern crate git2;
use git2::Repository;
pub struct PromptCache {
git_cache: Option<Repository>,
}
impl PromptCache {
pub fn new() -> PromptCache {
PromptCache { git_cache: None }
}
pub fn invalidate(&mut self) {
self.git_cache = None
}
pub fn cache_git(&mut self, git_repo: Repository) {
self.git_cache = Some(git_repo);
}
pub fn get_cached_git(&self) -> Option<&Repository> {
match self.git_cache.as_ref() {
Some(g) => Some(&g),
None => None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_prompt_cache() {
let tmpdir: tempfile::TempDir = tempfile::TempDir::new().unwrap();
let git_repo: Repository = Repository::init(tmpdir.path()).unwrap();
let mut cache: PromptCache = PromptCache::new();
cache.cache_git(git_repo);
assert!(cache.get_cached_git().is_some());
cache.invalidate();
assert!(cache.get_cached_git().is_none());
}
}