//! Handling of a database of Slavoj Žižek quotes use std::fs::File; use std::io::{self, BufRead, BufReader, BufWriter, Write}; use std::path::Path; use rand::seq::SliceRandom; /// Struct containing all currently loaded Žižek quotes and the means to /// manipulate them. pub struct ZizekDb { quotes: Vec, } impl ZizekDb { /// Read quotes from the file with the given name. /// /// # Return /// Database containing the quotes in case the load was successful or an /// `io::Error` in case it could not be loaded. pub fn from_file

(filename: P) -> Result where P: AsRef, { let file = File::open(filename)?; let lines = BufReader::new(file).lines(); let mut quotes: Vec = Vec::new(); for line in lines { let line = line?.trim().to_owned(); if !line.is_empty() { quotes.push(line); } } Ok(Self { quotes }) } /// Write contents of the database to the file. /// The file is overwritten by the contents of the database, so be careful /// if you want to concatenate the quotes of this database to the ones /// of a different one. pub fn write_to_file

(&self, filename: P) -> Result<(), io::Error> where P: AsRef, { let file = File::create(filename)?; let mut writer = BufWriter::new(file); for q in &self.quotes { write!(writer, "{}\n", q)?; } writer.flush() } /// Check if a quote exists in the database /// /// # Return /// `true` if the quote is registered, `false` if not pub fn has_quote(&self, quote: &str) -> bool { self.quotes.iter().any(|q| q == quote) } /// Get a random piece of philosophic goodness pub fn random_quote(&self) -> Option<&String> { let mut rng = rand::thread_rng(); self.quotes.choose(&mut rng) } /// Add a quote to the index /// /// # Return /// `true` in case the quote was successfully added or `false` if it could /// not, in case the quote already exists or an empty string was given. pub fn add_quote(&mut self, quote: &str) -> bool { let quote = quote.trim(); if quote.is_empty() { return false; } if !self.has_quote(quote) { self.quotes.push(quote.to_owned()); true } else { false } } /// Remove a quote from the database /// /// # Return /// `true` if the quote was found and removed, `false` if there was no such /// quote pub fn remove_quote(&mut self, quote: &str) -> bool { for (i, q) in &mut self.quotes.iter_mut().enumerate() { if q == quote { self.quotes.remove(i); return true; } } false } }