Skip to content

Commit

Permalink
examples/value_list: return Result
Browse files Browse the repository at this point in the history
There were a lot of unwraps in this example, which are
not encouraged and introduce some noise.

Let's return a Result from main and propagate the errors.
  • Loading branch information
muzarski committed Aug 28, 2024
1 parent e365bb7 commit 017d025
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions examples/value_list.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
use anyhow::Result;
use scylla::{Session, SessionBuilder};
use std::env;

#[tokio::main]
async fn main() {
async fn main() -> Result<()> {
let uri = env::var("SCYLLA_URI").unwrap_or_else(|_| "127.0.0.1:9042".to_string());

println!("Connecting to {} ...", uri);

let session: Session = SessionBuilder::new().known_node(uri).build().await.unwrap();
let session: Session = SessionBuilder::new().known_node(uri).build().await?;

session.query_unpaged("CREATE KEYSPACE IF NOT EXISTS examples_ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await.unwrap();
session.query_unpaged("CREATE KEYSPACE IF NOT EXISTS examples_ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await?;

session
.query_unpaged(
"CREATE TABLE IF NOT EXISTS examples_ks.my_type (k int, my text, primary key (k))",
&[],
)
.await
.unwrap();
.await?;

#[derive(scylla::SerializeRow)]
struct MyType<'a> {
Expand All @@ -35,8 +35,7 @@ async fn main() {
"INSERT INTO examples_ks.my_type (k, my) VALUES (?, ?)",
to_insert,
)
.await
.unwrap();
.await?;

// You can also use type generics:
#[derive(scylla::SerializeRow)]
Expand All @@ -55,13 +54,13 @@ async fn main() {
"INSERT INTO examples_ks.my_type (k, my) VALUES (?, ?)",
to_insert_2,
)
.await
.unwrap();
.await?;

let q = session
.query_unpaged("SELECT * FROM examples_ks.my_type", &[])
.await
.unwrap();
.await?;

println!("Q: {:?}", q.rows);

Ok(())
}

0 comments on commit 017d025

Please sign in to comment.