-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbasic.rs
80 lines (62 loc) · 2.21 KB
/
basic.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
extern crate jsonbox;
use jsonbox::{Client, Error};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
struct Data {
name: String,
age: i32,
login: bool,
}
impl Data {
fn new(name: &str, age: i32, login: bool) -> Data {
Data {
name: name.to_string(),
age,
login,
}
}
}
fn main() -> Result<(), Error> {
let client = Client::new("box_8ed82aef3f93176996145");
let mut data = Data::new("kuy", 42, false);
let (record, meta1) = client.create(&data)?;
println!("CREATE: data={:?}, meta={:?}", record, meta1);
let (record, meta1) = client.read().id::<Data>(&meta1.id)?;
println!("READ single: data={:?}, meta={:?}", record, meta1);
let list = vec![Data::new("jsonbox", 21, false), Data::new("io", 16, true)];
let bulk = client.create_bulk(&list)?;
println!("CREATE bulk: len={}, bulk={:?}", bulk.len(), bulk);
let all = client.read().all::<Data>()?;
println!("READ: len={}, all={:?}", all.len(), all);
let asc = client.read().order_by("age").run::<Data>()?;
println!("READ: len={}, asc={:?}", asc.len(), asc);
let few = client.read().limit(1).run::<Data>()?;
println!("READ: len={}, few={:?}", few.len(), few);
data.name = format!("kuy {}", meta1.id);
client.update(&meta1.id, &data)?;
println!("UPDATE: OK");
let filtered = client
.read()
.filter_by("name:{}", &data.name)
.run::<Data>()?;
println!("READ: len={}, filtered={:?}", filtered.len(), filtered);
let filtered = client.read().filter_by("name:{}*", "kuy").run::<Data>()?;
println!("READ: len={}, filtered={:?}", filtered.len(), filtered);
let filtered = client
.read()
.filter_by("name:*{}", &meta1.id)
.run::<Data>()?;
println!("READ: len={}, filtered={:?}", filtered.len(), filtered);
data.age = 8;
client.update(&meta1.id, &data)?;
println!("UPDATE: OK");
let filtered = client
.read()
.filter_by("age:<{}", 10)
.and("login:{}", false)
.run::<Data>()?;
println!("READ: len={}, filtered={:?}", filtered.len(), filtered);
client.delete(&meta1.id)?;
println!("DELETE: OK");
Ok(())
}