· 6 years ago · Mar 25, 2019, 05:50 AM
1use dirs;
2use rusqlite::{Connection, NO_PARAMS};
3use std::boxed::Box;
4use std::error::Error;
5use std::io;
6use std::io::Read;
7use std::path::PathBuf;
8use structopt::*;
9
10macro_rules! home {
11 ( $($x:expr),* ) => {
12 {
13
14 let mut temp_vec = PathBuf::new();
15
16 temp_vec.push(dirs::home_dir().unwrap());
17
18 $(
19 temp_vec.push($x);
20 )*
21
22
23 temp_vec
24 }
25 }
26}
27
28#[derive(StructOpt, Debug)]
29#[structopt(name = "cap")]
30struct Options {
31 #[structopt(short = "l", long = "database-location", parse(from_str))]
32 db_location: Option<PathBuf>,
33}
34
35/*
36#[derive(Debug)]
37struct Entry {
38 id: i32,
39 body: String,
40 created_at: time::Timespec,
41}
42*/
43
44fn main() -> Result<(), Box<dyn Error>> {
45 let options = Options::from_args();
46
47 println!("{:?}", options);
48
49 let db_location = if let Some(db_location) = options.db_location {
50 db_location
51 } else {
52 home!("cap.db")
53 };
54
55 println!("{:?}", db_location);
56
57 let conn = Connection::open(db_location)?;
58
59 conn.execute(
60 "CREATE TABLE IF NOT EXISTS entries (
61 id INTEGER PRIMARY KEY AUTOINCREMENT,
62 body TEXT,
63 t TIMESTAMP DEFAULT CURRENT_TIMESTAMP
64 );",
65 NO_PARAMS,
66 )?;
67
68 let mut buffer = String::new();
69 io::stdin().read_to_string(&mut buffer)?;
70
71 println!("{}", buffer);
72
73 conn.execute("INSERT INTO entries (body) VALUES (?1)", &[buffer])?;
74
75 /*
76 let mut select_star = conn.prepare("SELECT * FROM entries;")?;
77
78 let entries = select_star.query_map(NO_PARAMS, |row| {
79 Ok(Entry {
80 id: row.get(0)?,
81 body: row.get(1)?,
82 created_at: row.get(2)?,
83 })
84 })?;
85
86 for entry in entries {
87 let e = &(entry?);
88 println!("{:?}, {:?}, {:?}", e.id, e.body, time::at_utc(e.created_at));
89 }
90 */
91
92 Ok(())
93}