-
- fn get_quote(&self) -> Result<String> {
- let now: DateTime<Local> = Local::now();
- let today = now.date_naive();
-
- // check cache
- {
- let cache_guard = self.cached_quote.lock().unwrap();
- if let Some(ref cached) = *cache_guard {
- if cached.date == today {
- return Ok(cached.quote.clone());
- }
- }
- }
-
- // cache miss: get a new quote
- let quote = get_random_line(&self.quotes_file)?;
- let new_cached_quote = CachedQuote {
- date: today,
- quote: quote.clone(),
- };
-
- // update cache
- {
- let mut cache_guard = self.cached_quote.lock().unwrap();
- *cache_guard = Some(new_cached_quote);
- }
-
- Ok(quote)
- }
-}
-
-fn get_random_line(file_path: &Path) -> Result<String> {
- let file = File::open(file_path)?;