diff --git a/src/updater.rs b/src/cache.rs similarity index 89% rename from src/updater.rs rename to src/cache.rs index 28c5476..cb57a4b 100644 --- a/src/updater.rs +++ b/src/cache.rs @@ -10,25 +10,25 @@ use tar::Archive; use curl::http; use time; -use error::TldrError::{self, UpdateError}; +use error::TldrError::{self, CacheError, UpdateError}; #[derive(Debug)] -pub struct Updater { +pub struct Cache { url: String, } -impl Updater { +impl Cache { - pub fn new(url: S) -> Updater where S: Into { - Updater { + pub fn new(url: S) -> Cache where S: Into { + Cache { url: url.into(), } } /// Return the path to the cache directory. fn get_cache_dir(&self) -> Result { - let home_dir = try!(env::home_dir().ok_or(UpdateError("Could not determine home directory".into()))); + let home_dir = try!(env::home_dir().ok_or(CacheError("Could not determine home directory".into()))); Ok(home_dir.join(".cache").join("tldr-rs")) } @@ -74,8 +74,8 @@ impl Updater { Ok(()) } - /// Return the number of seconds since the cache directory was last modified. #[cfg(unix)] + /// Return the number of seconds since the cache directory was last modified. pub fn last_update(&self) -> Option { if let Ok(cache_dir) = self.get_cache_dir() { if let Ok(metadata) = fs::metadata(cache_dir) { diff --git a/src/error.rs b/src/error.rs index 6af0965..8643a7a 100644 --- a/src/error.rs +++ b/src/error.rs @@ -3,7 +3,8 @@ use curl::ErrCode; #[derive(Debug)] pub enum TldrError { - UpdateError(String) + CacheError(String), + UpdateError(String), } diff --git a/src/main.rs b/src/main.rs index c914fdb..5f6becb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,12 +19,12 @@ use docopt::Docopt; mod types; mod tokenizer; mod formatter; -mod updater; +mod cache; mod error; use tokenizer::Tokenizer; -use updater::Updater; -use error::TldrError; +use cache::Cache; +use error::TldrError::{UpdateError, CacheError}; use formatter::print_lines; @@ -112,8 +112,8 @@ fn main() { process::exit(0); } - // Initialize updater - let dl = Updater::new(ARCHIVE_URL); + // Initialize cache + let cache = Cache::new(ARCHIVE_URL); // Clear cache, pass through if args.flag_clear_cache { @@ -123,9 +123,9 @@ fn main() { // Update cache, pass through if args.flag_update { - dl.update().unwrap_or_else(|e| { + cache.update().unwrap_or_else(|e| { match e { - TldrError::UpdateError(msg) => println!("Could not update cache: {}", msg), + UpdateError(msg) | CacheError(msg) => println!("Could not update cache: {}", msg), }; process::exit(1); }); @@ -160,8 +160,10 @@ fn main() { // Show command from cache if let Some(command) = args.arg_command { + + // Check cache if !args.flag_update { - match dl.last_update() { + match cache.last_update() { Some(ago) if ago > MAX_CACHE_AGE => { println!("Cache wasn't updated in {} days.", MAX_CACHE_AGE / 24 / 3600); println!("You should probably run `tldr --update` soon."); @@ -173,6 +175,8 @@ fn main() { _ => {}, } } + + // Search for command in cache println!("Page rendering from cache not yet implemented."); process::exit(1); }