Initial commit
This commit is contained in:
74
src/lib.rs
Normal file
74
src/lib.rs
Normal file
@@ -0,0 +1,74 @@
|
||||
extern crate sqlite;
|
||||
use sqlite::Connection;
|
||||
|
||||
const DATA_SCHEMA: &'static str = "CREATE TABLE IF NOT EXISTS quotes (content TEXT);";
|
||||
const SELECT_QUERY: &'static str = "SELECT rowid, content FROM quotes ORDER BY RANDOM() LIMIT ?;";
|
||||
const INSERT_QUERY: &'static str = "INSERT INTO quotes (content) VALUES (?);";
|
||||
|
||||
pub struct Echobox {
|
||||
connection: Connection
|
||||
}
|
||||
|
||||
impl Echobox {
|
||||
pub fn with_file (path: &str) -> Result<Echobox, Error> {
|
||||
let connection = sqlite::open(path)?;
|
||||
Echobox::with_connection(connection)
|
||||
}
|
||||
|
||||
pub fn with_connection (connection: Connection) -> Result<Echobox, Error> {
|
||||
connection.execute(DATA_SCHEMA)?;
|
||||
Result::Ok(Echobox {
|
||||
connection: connection
|
||||
})
|
||||
}
|
||||
|
||||
pub fn echo (&self, quote: &str) -> Result<Quote, Error> {
|
||||
self.put(quote)?;
|
||||
Result::Ok(self.get(1)?[0].clone())
|
||||
}
|
||||
|
||||
pub fn put (&self, quote: &str) -> Result<(), Error> {
|
||||
let mut statement = self.connection.prepare(INSERT_QUERY)?;
|
||||
statement.bind(1, quote)?;
|
||||
statement.next()?;
|
||||
Result::Ok(())
|
||||
}
|
||||
|
||||
pub fn get (&self, number: i64) -> Result<Vec<Quote>, Error> {
|
||||
let mut statement = self.connection.prepare(SELECT_QUERY)?;
|
||||
statement.bind(1, number)?;
|
||||
let mut cursor = statement.cursor();
|
||||
|
||||
let mut quotes = vec![];
|
||||
while let Some(row) = cursor.next().unwrap() {
|
||||
quotes.push(Quote {
|
||||
id: row[0].as_integer().expect("Expected rowid in row"),
|
||||
text: String::from(row[1].as_string().expect("Expected text in row"))
|
||||
});
|
||||
}
|
||||
Result::Ok(quotes)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
Sql(sqlite::Error)
|
||||
}
|
||||
|
||||
impl From<sqlite::Error> for Error {
|
||||
fn from (error: sqlite::Error) -> Error {
|
||||
Error::Sql(error)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Quote {
|
||||
pub id: i64,
|
||||
pub text: String
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Quote {
|
||||
fn fmt (&self, formatter: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
|
||||
write!(formatter, "{}", self.text)
|
||||
}
|
||||
}
|
19
src/main.rs
Normal file
19
src/main.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
extern crate echobox;
|
||||
use echobox::Echobox;
|
||||
|
||||
use std::env;
|
||||
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
extern crate env_logger;
|
||||
|
||||
const ECHOBOX_FILE: &'static str = "echobox.db";
|
||||
|
||||
fn main () {
|
||||
env_logger::init().unwrap();
|
||||
|
||||
let in_quote = &env::args().nth(1).expect("Expected quote as first argument.")[..];
|
||||
let echobox = Echobox::with_file(ECHOBOX_FILE).unwrap();
|
||||
let out_quote = echobox.echo(in_quote).unwrap();
|
||||
println!("{}: {}", out_quote.id, out_quote.text);
|
||||
}
|
Reference in New Issue
Block a user