diff options
| author | Arne Dußin | 2021-07-31 11:51:25 +0200 |
|---|---|---|
| committer | Arne Dußin | 2021-07-31 11:51:25 +0200 |
| commit | 2f14f7edf29bd7010e5f8be8691a907c8ede1278 (patch) | |
| tree | 2708ee050e3dba8ca75be2160428e1c4bd487367 /src/zizek_db.rs | |
| parent | af7514d42b3ec6743fd8037c6e909ae8134dfa4f (diff) | |
| download | matrix-zizek-bot-2f14f7edf29bd7010e5f8be8691a907c8ede1278.tar.gz matrix-zizek-bot-2f14f7edf29bd7010e5f8be8691a907c8ede1278.zip | |
First basic testing zizek bot
Diffstat (limited to 'src/zizek_db.rs')
| -rw-r--r-- | src/zizek_db.rs | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/src/zizek_db.rs b/src/zizek_db.rs new file mode 100644 index 0000000..1195822 --- /dev/null +++ b/src/zizek_db.rs @@ -0,0 +1,110 @@ +//! 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<String>, +} + +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<P>(filename: P) -> Result<Self, io::Error> + where + P: AsRef<Path>, + { + let file = File::open(filename)?; + let lines = BufReader::new(file).lines(); + + let mut quotes: Vec<String> = 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<P>(&self, filename: P) -> Result<(), io::Error> + where + P: AsRef<Path>, + { + 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 + } +} |
