Skip to content

Commit 09270c2

Browse files
DOC-5785 added Rust examples in updated format
1 parent 36e5487 commit 09270c2

File tree

3 files changed

+229
-171
lines changed

3 files changed

+229
-171
lines changed

build/components/example.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
'java-reactive': '@Test',
1717
'c#': r'\[Fact]|\[SkipIfRedis\(.*\)]',
1818
'c#-sync': r'\[Fact]|\[SkipIfRedis\(.*\)]',
19-
'c#-async': r'\[Fact]|\[SkipIfRedis\(.*\)]'
19+
'c#-async': r'\[Fact]|\[SkipIfRedis\(.*\)]',
20+
'rust': r'#\[test]|#\[cfg\(test\)]|#\[tokio::test]'
2021
}
2122
PREFIXES = {
2223
'python': '#',
Lines changed: 111 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,130 @@
11
// EXAMPLE: landing
2-
// STEP_START import
3-
use redis::AsyncCommands;
4-
// STEP_END
2+
#[cfg(test)]
3+
mod tests {
4+
// STEP_START import
5+
use redis::AsyncCommands;
6+
// STEP_END
57

6-
#[tokio::main]
7-
async fn main() {
8-
// STEP_START connect
9-
let mut r = match redis::Client::open("redis://127.0.0.1") {
10-
Ok(client) => {
11-
match client.get_multiplexed_async_connection().await {
12-
Ok(conn) => conn,
13-
Err(e) => {
14-
println!("Failed to connect to Redis: {e}");
15-
return;
8+
#[tokio::test]
9+
async fn run() {
10+
// STEP_START connect
11+
let mut r = match redis::Client::open("redis://127.0.0.1") {
12+
Ok(client) => {
13+
match client.get_multiplexed_async_connection().await {
14+
Ok(conn) => conn,
15+
Err(e) => {
16+
println!("Failed to connect to Redis: {e}");
17+
return;
18+
}
1619
}
20+
},
21+
Err(e) => {
22+
println!("Failed to create Redis client: {e}");
23+
return;
1724
}
18-
},
19-
Err(e) => {
20-
println!("Failed to create Redis client: {e}");
21-
return;
22-
}
23-
};
24-
// STEP_END
25-
26-
// STEP_START set_get_string
27-
if let Ok(res) = r.set("foo", "bar").await {
28-
let res: String = res;
29-
println!("{res}"); // >>> OK
30-
} else {
31-
println!("Error setting foo");
32-
}
25+
};
26+
// STEP_END
3327

34-
match r.get("foo").await {
35-
Ok(res) => {
28+
// STEP_START set_get_string
29+
if let Ok(res) = r.set("foo", "bar").await {
3630
let res: String = res;
37-
println!("{res}"); // >>> bar
38-
},
39-
Err(e) => {
40-
println!("Error getting foo: {e}");
41-
return;
31+
println!("{res}"); // >>> OK
32+
// REMOVE_START
33+
assert_eq!(res, "OK");
34+
// REMOVE_END
35+
} else {
36+
println!("Error setting foo");
4237
}
43-
};
44-
// STEP_END
4538

46-
// STEP_START set_get_hash
47-
let hash_fields = [
48-
("model", "Deimos"),
49-
("brand", "Ergonom"),
50-
("type", "Enduro bikes"),
51-
("price", "4972"),
52-
];
39+
match r.get("foo").await {
40+
Ok(res) => {
41+
let res: String = res;
42+
println!("{res}"); // >>> bar
43+
// REMOVE_START
44+
assert_eq!(res, "bar");
45+
// REMOVE_END
46+
},
47+
Err(e) => {
48+
println!("Error getting foo: {e}");
49+
return;
50+
}
51+
};
52+
// STEP_END
5353

54-
if let Ok(res) = r.hset_multiple("bike:1", &hash_fields).await {
55-
let res: String = res;
56-
println!("{res}"); // >>> OK
57-
} else {
58-
println!("Error setting bike:1");
59-
}
54+
// STEP_START set_get_hash
55+
let hash_fields = [
56+
("model", "Deimos"),
57+
("brand", "Ergonom"),
58+
("type", "Enduro bikes"),
59+
("price", "4972"),
60+
];
6061

61-
match r.hget("bike:1", "model").await {
62-
Ok(res) => {
62+
if let Ok(res) = r.hset_multiple("bike:1", &hash_fields).await {
6363
let res: String = res;
64-
println!("{res}"); // >>> Deimos
65-
},
66-
Err(e) => {
67-
println!("Error getting bike:1 model: {e}");
68-
return;
64+
println!("{res}"); // >>> OK
65+
// REMOVE_START
66+
assert_eq!(res, "OK");
67+
// REMOVE_END
68+
} else {
69+
println!("Error setting bike:1");
6970
}
70-
}
7171

72-
match r.hget("bike:1", "price").await {
73-
Ok(res) => {
74-
let res: String = res;
75-
println!("{res}"); // >>> 4972
76-
},
77-
Err(e) => {
78-
println!("Error getting bike:1 price: {e}");
79-
return;
72+
match r.hget("bike:1", "model").await {
73+
Ok(res) => {
74+
let res: String = res;
75+
println!("{res}"); // >>> Deimos
76+
// REMOVE_START
77+
assert_eq!(res, "Deimos");
78+
// REMOVE_END
79+
},
80+
Err(e) => {
81+
println!("Error getting bike:1 model: {e}");
82+
return;
83+
}
8084
}
81-
}
8285

83-
match r.hgetall("bike:1").await {
84-
Ok(res) => {
85-
let res: Vec<(String, String)> = res;
86-
for (key, value) in res {
87-
println!("{key}: {value}");
86+
match r.hget("bike:1", "price").await {
87+
Ok(res) => {
88+
let res: String = res;
89+
println!("{res}"); // >>> 4972
90+
// REMOVE_START
91+
assert_eq!(res, "4972");
92+
// REMOVE_END
93+
},
94+
Err(e) => {
95+
println!("Error getting bike:1 price: {e}");
96+
return;
8897
}
89-
// >>> model: Deimos
90-
// >>> brand: Ergonom
91-
// >>> type: Enduro bikes
92-
// >>> price: 4972
93-
},
94-
Err(e) => {
95-
println!("Error getting bike:1: {e}");
96-
return;
9798
}
98-
// STEP_END
99+
100+
match r.hgetall("bike:1").await {
101+
Ok(res) => {
102+
let res: Vec<(String, String)> = res;
103+
// REMOVE_START
104+
assert_eq!(res.len(), 4);
105+
assert_eq!(res[0].0, "model");
106+
assert_eq!(res[0].1, "Deimos");
107+
assert_eq!(res[1].0, "brand");
108+
assert_eq!(res[1].1, "Ergonom");
109+
assert_eq!(res[2].0, "type");
110+
assert_eq!(res[2].1, "Enduro bikes");
111+
assert_eq!(res[3].0, "price");
112+
assert_eq!(res[3].1, "4972");
113+
// REMOVE_END
114+
115+
for (key, value) in res {
116+
println!("{key}: {value}");
117+
}
118+
// >>> model: Deimos
119+
// >>> brand: Ergonom
120+
// >>> type: Enduro bikes
121+
// >>> price: 4972
122+
},
123+
Err(e) => {
124+
println!("Error getting bike:1: {e}");
125+
return;
126+
}
127+
// STEP_END
128+
}
99129
}
100130
}

0 commit comments

Comments
 (0)