Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion build/components/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
'java-reactive': '@Test',
'c#': r'\[Fact]|\[SkipIfRedis\(.*\)]',
'c#-sync': r'\[Fact]|\[SkipIfRedis\(.*\)]',
'c#-async': r'\[Fact]|\[SkipIfRedis\(.*\)]'
'c#-async': r'\[Fact]|\[SkipIfRedis\(.*\)]',
'rust': r'#\[test]|#\[cfg\(test\)]|#\[tokio::test]'
}
PREFIXES = {
'python': '#',
Expand Down
192 changes: 111 additions & 81 deletions local_examples/client-specific/rust-async/landing.rs
Original file line number Diff line number Diff line change
@@ -1,100 +1,130 @@
// EXAMPLE: landing
// STEP_START import
use redis::AsyncCommands;
// STEP_END
#[cfg(test)]
mod tests {
// STEP_START import
use redis::AsyncCommands;
// STEP_END

#[tokio::main]
async fn main() {
// STEP_START connect
let mut r = match redis::Client::open("redis://127.0.0.1") {
Ok(client) => {
match client.get_multiplexed_async_connection().await {
Ok(conn) => conn,
Err(e) => {
println!("Failed to connect to Redis: {e}");
return;
#[tokio::test]
async fn run() {
// STEP_START connect
let mut r = match redis::Client::open("redis://127.0.0.1") {
Ok(client) => {
match client.get_multiplexed_async_connection().await {
Ok(conn) => conn,
Err(e) => {
println!("Failed to connect to Redis: {e}");
return;
}
}
},
Err(e) => {
println!("Failed to create Redis client: {e}");
return;
}
},
Err(e) => {
println!("Failed to create Redis client: {e}");
return;
}
};
// STEP_END

// STEP_START set_get_string
if let Ok(res) = r.set("foo", "bar").await {
let res: String = res;
println!("{res}"); // >>> OK
} else {
println!("Error setting foo");
}
};
// STEP_END

match r.get("foo").await {
Ok(res) => {
// STEP_START set_get_string
if let Ok(res) = r.set("foo", "bar").await {
let res: String = res;
println!("{res}"); // >>> bar
},
Err(e) => {
println!("Error getting foo: {e}");
return;
println!("{res}"); // >>> OK
// REMOVE_START
assert_eq!(res, "OK");
// REMOVE_END
} else {
println!("Error setting foo");
}
};
// STEP_END

// STEP_START set_get_hash
let hash_fields = [
("model", "Deimos"),
("brand", "Ergonom"),
("type", "Enduro bikes"),
("price", "4972"),
];
match r.get("foo").await {
Ok(res) => {
let res: String = res;
println!("{res}"); // >>> bar
// REMOVE_START
assert_eq!(res, "bar");
// REMOVE_END
},
Err(e) => {
println!("Error getting foo: {e}");
return;
}
};
// STEP_END

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

match r.hget("bike:1", "model").await {
Ok(res) => {
if let Ok(res) = r.hset_multiple("bike:1", &hash_fields).await {
let res: String = res;
println!("{res}"); // >>> Deimos
},
Err(e) => {
println!("Error getting bike:1 model: {e}");
return;
println!("{res}"); // >>> OK
// REMOVE_START
assert_eq!(res, "OK");
// REMOVE_END
} else {
println!("Error setting bike:1");
}
}

match r.hget("bike:1", "price").await {
Ok(res) => {
let res: String = res;
println!("{res}"); // >>> 4972
},
Err(e) => {
println!("Error getting bike:1 price: {e}");
return;
match r.hget("bike:1", "model").await {
Ok(res) => {
let res: String = res;
println!("{res}"); // >>> Deimos
// REMOVE_START
assert_eq!(res, "Deimos");
// REMOVE_END
},
Err(e) => {
println!("Error getting bike:1 model: {e}");
return;
}
}
}

match r.hgetall("bike:1").await {
Ok(res) => {
let res: Vec<(String, String)> = res;
for (key, value) in res {
println!("{key}: {value}");
match r.hget("bike:1", "price").await {
Ok(res) => {
let res: String = res;
println!("{res}"); // >>> 4972
// REMOVE_START
assert_eq!(res, "4972");
// REMOVE_END
},
Err(e) => {
println!("Error getting bike:1 price: {e}");
return;
}
// >>> model: Deimos
// >>> brand: Ergonom
// >>> type: Enduro bikes
// >>> price: 4972
},
Err(e) => {
println!("Error getting bike:1: {e}");
return;
}
// STEP_END

match r.hgetall("bike:1").await {
Ok(res) => {
let res: Vec<(String, String)> = res;
// REMOVE_START
assert_eq!(res.len(), 4);
assert_eq!(res[0].0, "model");
assert_eq!(res[0].1, "Deimos");
assert_eq!(res[1].0, "brand");
assert_eq!(res[1].1, "Ergonom");
assert_eq!(res[2].0, "type");
assert_eq!(res[2].1, "Enduro bikes");
assert_eq!(res[3].0, "price");
assert_eq!(res[3].1, "4972");
// REMOVE_END

for (key, value) in res {
println!("{key}: {value}");
}
// >>> model: Deimos
// >>> brand: Ergonom
// >>> type: Enduro bikes
// >>> price: 4972
},
Err(e) => {
println!("Error getting bike:1: {e}");
return;
}
// STEP_END
}
}
}
Loading