diff options
author | Ben Morrison <ben@gbmor.dev> | 2019-08-27 00:50:51 -0400 |
---|---|---|
committer | Ben Morrison <ben@gbmor.dev> | 2019-08-27 00:50:51 -0400 |
commit | 9ee17f15bb380d31d55d83586ebdf3644cf326af (patch) | |
tree | 35ef8c7289f2fbd52564a1fec4fd3f0dfc892e92 /src | |
parent | d64124e97bdf26169f20e98fbdf1af934b265b74 (diff) | |
download | clinte-9ee17f15bb380d31d55d83586ebdf3644cf326af.tar.gz |
fleshing out types
Diffstat (limited to 'src')
-rw-r--r-- | src/db.rs | 81 | ||||
-rw-r--r-- | src/main.rs | 2 |
2 files changed, 83 insertions, 0 deletions
diff --git a/src/db.rs b/src/db.rs new file mode 100644 index 0000000..d3ecd6a --- /dev/null +++ b/src/db.rs @@ -0,0 +1,81 @@ +use rusqlite; +use std::sync::mpsc; + +#[derive(Debug)] +pub struct Post { + id: u32, + title: String, + author: String, + body: String, +} + +pub struct Conn { + db: rusqlite::Connection, + tx: mpsc::Sender<Cmd>, +} + +#[derive(Debug)] +pub enum Cmd { + Create, + Update, + Disconnect, + NOOP, +} + +impl Conn { + fn init() -> rusqlite::Connection { + let conn = rusqlite::Connection::open_with_flags( + "/tmp/db.sql", + rusqlite::OpenFlags::SQLITE_OPEN_FULL_MUTEX + | rusqlite::OpenFlags::SQLITE_OPEN_CREATE + | rusqlite::OpenFlags::SQLITE_OPEN_READ_WRITE, + ) + .expect("Could not connect to DB"); + + conn.execute( + "CREATE TABLE IF NOT EXISTS posts ( + id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + title TEXT NOT NULL, + author TEXT NOT NULL, + body TEXT NOT NULL + )", + rusqlite::NO_PARAMS, + ) + .expect("Could not initialize DB"); + + conn + } + + pub fn new(tx: mpsc::Sender<Cmd>) -> Self { + Conn { + db: Conn::init(), + tx, + } + } +} + +impl Cmd { + pub fn new(txt: &str) -> Self { + match txt { + "create" => Cmd::Create, + "update" => Cmd::Update, + "disconnect" => Cmd::Disconnect, + _ => Cmd::NOOP, + } + } +} + +impl Post { + pub fn id(&self) -> String { + format!("{}", self.id) + } + pub fn title(&self) -> String { + self.title.clone() + } + pub fn author(&self) -> String { + self.author.clone() + } + pub fn body(&self) -> String { + self.body.clone() + } +} diff --git a/src/main.rs b/src/main.rs index 848bafd..9bc08ad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +mod db; + fn main() { println!("clinte-0.1-dev"); println!("a community notices system"); |