-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsql.rs
40 lines (34 loc) · 1.11 KB
/
sql.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use rs_try::sql::{add_todo, complete_todo, list_todos, Args, Command};
use sqlx::sqlite::SqlitePool;
use std::env;
use structopt::StructOpt;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let args = Args::from_args_safe()?;
let pool = SqlitePool::connect(&env::var("DATABASE_URL")?).await?;
match args.cmd {
// todo x: 新增
Some(Command::Add { description }) => {
println!("Adding new todo with description '{}'", &description);
let todo_id = add_todo(&pool, description).await?;
println!("Added new todo with id {}", todo_id);
},
// todo x: 更新状态
Some(Command::Done { id }) => {
println!("Marking todo {} as done", id);
if complete_todo(&pool, id).await? {
println!("Todo {} is marked as done", id);
} else {
println!("Invalid id {}", id);
}
},
// todo x: 查询
None => {
println!("Printing list of all todos");
list_todos(&pool).await?;
},
}
// clean:
pool.close();
Ok(())
}