Added docs for cache

This commit is contained in:
Thomas Forgione 2019-02-12 11:08:39 +01:00
parent 56e39a33f9
commit 949490b350
No known key found for this signature in database
GPG Key ID: 203DAEA747F48F41
1 changed files with 13 additions and 0 deletions

View File

@ -1,13 +1,21 @@
//! This module contains the cache manager.
use std::path::PathBuf;
use std::fs::File;
use std::io::{Write, BufRead, BufReader};
use walkdir::WalkDir;
use crate::{Result, GIT_DIR};
/// The cache of gclone.
///
/// When running the command `cdg`, if the computer just booted, finding all the directories can be
/// quite slow. The cache contains the list of entries in the git directory to avoid re-finding
/// them every time.
pub struct Cache(Vec<String>);
impl Cache {
/// Generates the cache by traversing the files in the git directory.
pub fn generate() -> Cache {
Cache(WalkDir::new(&*GIT_DIR)
.max_depth(3)
@ -17,6 +25,7 @@ impl Cache {
.collect())
}
/// Reads the cache file.
pub fn read() -> Result<Cache> {
let mut path = PathBuf::from(&*GIT_DIR);
@ -36,6 +45,7 @@ impl Cache {
Ok(Cache(values))
}
/// Reads the cache file, and if it failed, generate a new cache.
pub fn read_or_generate() -> Cache {
match Cache::read() {
Ok(c) => c,
@ -43,6 +53,7 @@ impl Cache {
}
}
/// Writes the current content of the cache to the cache file.
pub fn write(&self) -> Result<()> {
let mut path = PathBuf::from(&*GIT_DIR);
path.push(".cdgcache");
@ -55,6 +66,7 @@ impl Cache {
Ok(())
}
/// Search a directory in the cache, and returns all matching directories.
pub fn find(&self, dirname: &str) -> Vec<String> {
let dirname = &format!("/{}", dirname);
let mut matches = vec![];
@ -67,6 +79,7 @@ impl Cache {
matches
}
/// Appends a value to the cache.
pub fn append(&mut self, value: String) {
self.0.push(value);
}