Skip to content

Commit

Permalink
Handle updates to Rust nullability from smithy-lang/smithy-rs#2916
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidSouther committed Sep 28, 2023
1 parent 7c4ed39 commit ff0628f
Show file tree
Hide file tree
Showing 44 changed files with 260 additions and 259 deletions.
1 change: 1 addition & 0 deletions rust_dev_preview/cross_service/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Note: this workspace exists solely for the convenience of
# compiling/auto-formatting/testing all the AWS Rust SDK examples together.
[workspace]
resolver = "2"
members = [
"detect_faces",
"detect_labels",
Expand Down
7 changes: 6 additions & 1 deletion rust_dev_preview/cross_service/rest_ses/src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ pub async fn send_report(

let email = ses
.send_raw_email()
.raw_message(RawMessage::builder().data(data).build())
.raw_message(
RawMessage::builder()
.data(data)
.build()
.expect("building RawMessage"),
)
.send()
.await
.map_err(|err| ReportError::SesError(err.into()))?;
Expand Down
3 changes: 3 additions & 0 deletions rust_dev_preview/examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Note: this workspace exists solely for the convenience of
# compiling/auto-formatting/testing all the AWS Rust SDK examples together.
[workspace]

resolver = "2"

members = [
"apigateway",
"apigatewaymanagement",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,15 @@ async fn list_groups(client: &Client) -> Result<(), Error> {

println!("Groups:");

let groups = resp.auto_scaling_groups().unwrap_or_default();
let groups = resp.auto_scaling_groups();

for group in groups {
println!(
"Name: {}",
group.auto_scaling_group_name().unwrap_or("Unknown"),
);
println!("Name: {}", group.auto_scaling_group_name(),);
println!(
"Arn: {}",
group.auto_scaling_group_arn().unwrap_or("unknown"),
);
println!(
"Zones: {:?}",
group.availability_zones().unwrap_or_default(),
);
println!("Zones: {:?}", group.availability_zones(),);
println!();
}

Expand Down
4 changes: 2 additions & 2 deletions rust_dev_preview/examples/batch/src/bin/batch-helloworld.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ async fn show_envs(client: &Client) -> Result<(), Error> {
let compute_envs = rsp.compute_environments().unwrap_or_default();
println!("Found {} compute environments:", compute_envs.len());
for env in compute_envs {
let arn = env.compute_environment_arn().unwrap_or_default();
let name = env.compute_environment_name().unwrap_or_default();
let arn = env.compute_environment_arn();
let name = env.compute_environment_name();

println!(" Name : {}", name);
println!(" ARN: {}", arn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async fn describe_stack(client: &Client, name: &str) -> Result<(), Error> {
.unwrap()
.stack_status();

println!("Stack status: {}", status.unwrap().as_ref());
println!("Stack status: {}", status.as_str());

println!();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ async fn list_stacks(client: &Client) -> Result<(), Error> {
let stacks = client.list_stacks().send().await?;

for stack in stacks.stack_summaries().unwrap_or_default() {
println!("{}", stack.stack_name().unwrap_or_default());
println!(" Status: {:?}", stack.stack_status().unwrap());
println!("{}", stack.stack_name());
println!(" Status: {:?}", stack.stack_status());
println!();
}

Expand Down
4 changes: 2 additions & 2 deletions rust_dev_preview/examples/cloudwatch/src/bin/list-metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ async fn show_metrics(

if let Some(dimension) = metric.dimensions.as_ref() {
for d in dimension {
println!(" Name: {}", d.name().unwrap_or_default());
println!(" Value: {}", d.value().unwrap_or_default());
println!(" Name: {}", d.name());
println!(" Value: {}", d.value());
println!();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ async fn describe_pool(client: &Client, id: &str) -> Result<(), Error> {
}

let developer_provider = response.developer_provider_name().unwrap_or_default();
let id = response.identity_pool_id().unwrap_or_default();
let name = response.identity_pool_name().unwrap_or_default();
let id = response.identity_pool_id();
let name = response.identity_pool_name();

println!(" Developer provider: {}", developer_provider);
println!(" Identity pool ID: {}", id);
Expand Down
9 changes: 6 additions & 3 deletions rust_dev_preview/examples/dynamodb/src/bin/crud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,20 @@ async fn make_table(
let ad = AttributeDefinition::builder()
.attribute_name(key)
.attribute_type(ScalarAttributeType::S)
.build();
.build()
.expect("creating AttributeDefinition");

let ks = KeySchemaElement::builder()
.attribute_name(key)
.key_type(KeyType::Hash)
.build();
.build()
.expect("creating KeySchemaElement");

let pt = ProvisionedThroughput::builder()
.read_capacity_units(10)
.write_capacity_units(5)
.build();
.build()
.expect("creating ProvisionedThroughput");

match client
.create_table()
Expand Down
39 changes: 21 additions & 18 deletions rust_dev_preview/examples/dynamodb/src/bin/dynamodb-helloworld.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,30 @@ async fn list_tables(client: &Client) -> Result<(), Error> {
// Creates the test-table table.
// snippet-start:[dynamodb.rust.dynamodb-helloworld-create_table]
async fn create_table(client: &Client) -> Result<(), Error> {
let ks = KeySchemaElement::builder()
.attribute_name("k")
.key_type(KeyType::Hash)
.build()
.expect("creating KeySchemaElement");

let ad = AttributeDefinition::builder()
.attribute_name("k")
.attribute_type(ScalarAttributeType::S)
.build()
.expect("creating AttributeDefinition");

let pt = ProvisionedThroughput::builder()
.write_capacity_units(10)
.read_capacity_units(10)
.build()
.expect("creating ProvisionedThroughput");

let new_table = client
.create_table()
.table_name("test-table")
.key_schema(
KeySchemaElement::builder()
.attribute_name("k")
.key_type(KeyType::Hash)
.build(),
)
.attribute_definitions(
AttributeDefinition::builder()
.attribute_name("k")
.attribute_type(ScalarAttributeType::S)
.build(),
)
.provisioned_throughput(
ProvisionedThroughput::builder()
.write_capacity_units(10)
.read_capacity_units(10)
.build(),
)
.key_schema(ks)
.attribute_definitions(ad)
.provisioned_throughput(pt)
.send()
.await?;
println!(
Expand Down
9 changes: 6 additions & 3 deletions rust_dev_preview/examples/dynamodb/src/bin/partiql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,20 @@ async fn make_table(
let ad = AttributeDefinition::builder()
.attribute_name(key)
.attribute_type(ScalarAttributeType::S)
.build();
.build()
.expect("creating AttributeDefinition");

let ks = KeySchemaElement::builder()
.attribute_name(key)
.key_type(KeyType::Hash)
.build();
.build()
.expect("creating KeySchemaElement");

let pt = ProvisionedThroughput::builder()
.read_capacity_units(10)
.write_capacity_units(5)
.build();
.build()
.expect("creating ProvisionedThroughput");

match client
.create_table()
Expand Down
9 changes: 6 additions & 3 deletions rust_dev_preview/examples/dynamodb/src/scenario/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,20 @@ pub async fn create_table(
let ad = AttributeDefinition::builder()
.attribute_name(&a_name)
.attribute_type(ScalarAttributeType::S)
.build();
.build()
.map_err(Error::BuildError)?;

let ks = KeySchemaElement::builder()
.attribute_name(&a_name)
.key_type(KeyType::Hash)
.build();
.build()
.map_err(Error::BuildError)?;

let pt = ProvisionedThroughput::builder()
.read_capacity_units(10)
.write_capacity_units(5)
.build();
.build()
.map_err(Error::BuildError)?;

let create_table_response = client
.create_table()
Expand Down
4 changes: 4 additions & 0 deletions rust_dev_preview/examples/dynamodb/src/scenario/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@

use std::error::Error as StdError;

use aws_smithy_http::operation::error::BuildError;

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("table was not ready after several attempts: {0}")]
TableNotReady(String),
#[error("problem building schema key or element: {0}")]
BuildError(BuildError),
#[error("unhandled error")]
Unhandled(#[source] Box<dyn StdError + Send + Sync + 'static>),
}
Expand Down
6 changes: 4 additions & 2 deletions rust_dev_preview/examples/dynamodb/src/scenario/movies/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use aws_sdk_dynamodb::error::SdkError;
use aws_sdk_dynamodb::types::{AttributeValue, PutRequest};
use aws_smithy_http::operation::error::BuildError;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
Expand Down Expand Up @@ -140,8 +141,9 @@ impl From<&HashMap<String, AttributeValue>> for Movie {
}
}

impl From<&Movie> for PutRequest {
fn from(movie: &Movie) -> Self {
impl TryFrom<&Movie> for PutRequest {
type Error = BuildError;
fn try_from(movie: &Movie) -> Result<Self, Self::Error> {
PutRequest::builder()
.item("year", AttributeValue::N(movie.year.to_string()))
.item("title", AttributeValue::S(movie.title.clone()))
Expand Down
27 changes: 17 additions & 10 deletions rust_dev_preview/examples/dynamodb/src/scenario/movies/startup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub async fn initialize(client: &Client, table_name: &str) -> Result<(), Error>
info!("Found existing table {table_name}");
} else {
info!("Table does not exist, creating {table_name}");
create_table(client, table_name, "year", "title", CAPACITY)
create_table(client, table_name, "year", "title", CAPACITY)?
.send()
.await?;
await_table(client, table_name).await?;
Expand Down Expand Up @@ -54,41 +54,46 @@ pub fn create_table(
primary_key: &str,
sort_key: &str,
capacity: i64,
) -> CreateTableFluentBuilder {
) -> Result<CreateTableFluentBuilder, Error> {
info!("Creating table: {table_name} with capacity {capacity} and key structure {primary_key}:{sort_key}");
client
Ok(client
.create_table()
.table_name(table_name)
.key_schema(
KeySchemaElement::builder()
.attribute_name(primary_key)
.key_type(KeyType::Hash)
.build(),
.build()
.expect("Failed to build KeySchema"),
)
.attribute_definitions(
AttributeDefinition::builder()
.attribute_name(primary_key)
.attribute_type(ScalarAttributeType::N)
.build(),
.build()
.expect("Failed to build attribute definition"),
)
.key_schema(
KeySchemaElement::builder()
.attribute_name(sort_key)
.key_type(KeyType::Range)
.build(),
.build()
.expect("Failed to build KeySchema"),
)
.attribute_definitions(
AttributeDefinition::builder()
.attribute_name(sort_key)
.attribute_type(ScalarAttributeType::S)
.build(),
.build()
.expect("Failed to build attribute definition"),
)
.provisioned_throughput(
ProvisionedThroughput::builder()
.read_capacity_units(capacity)
.write_capacity_units(capacity)
.build(),
)
.build()
.expect("Failed to specify ProvisionedThroughput"),
))
}
// snippet-end:[dynamodb.rust.movies-create_table_request]

Expand Down Expand Up @@ -133,7 +138,9 @@ pub async fn load_data(client: &Client, table_name: &str) -> Result<(), Error> {
.iter()
.map(|v| {
WriteRequest::builder()
.set_put_request(Some(v.into()))
.set_put_request(Some(
v.try_into().expect("Failed to convert movie to PutRequest"),
))
.build()
})
.collect::<Vec<WriteRequest>>();
Expand Down
13 changes: 9 additions & 4 deletions rust_dev_preview/examples/firehose/src/bin/put-records-batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ struct Opt {
async fn put_record_batch(
client: &Client,
stream: &str,
data: Option<Vec<Record>>,
data: Vec<Record>,
) -> Result<PutRecordBatchOutput, SdkError<PutRecordBatchError>> {
client
.put_record_batch()
.delivery_stream_name(stream)
.set_records(data)
.set_records(Some(data))
.send()
.await
}
Expand Down Expand Up @@ -83,9 +83,14 @@ async fn main() -> Result<(), Error> {
let mut data = Vec::with_capacity(500);
let payload = String::from("Some random payload");
let tmp = Some(Blob::new(payload.to_string()));
data.push(Record::builder().set_data(tmp).build());
data.push(
Record::builder()
.set_data(tmp)
.build()
.expect("Failed to create a new record"),
);

let resp = put_record_batch(&client, &firehose_stream, Some(data)).await;
let resp = put_record_batch(&client, &firehose_stream, data).await;
match resp {
Ok(_) => Ok(()),
Err(e) => Err(Error::from(e)),
Expand Down
9 changes: 2 additions & 7 deletions rust_dev_preview/examples/glue/src/cleanup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ impl GlueScenario {
// snippet-start:[rust.glue.delete_table]
for t in &self.tables {
glue.delete_table()
.name(
t.name()
.ok_or_else(|| GlueMvpError::Unknown("Couldn't find table".to_string()))?,
)
.name(t.name())
.database_name(self.database())
.send()
.await
Expand Down Expand Up @@ -106,9 +103,7 @@ impl GlueScenario {
.to_owned();

if let Some(database) = database.database() {
return Err(GlueMvpError::Cleanup(
database.name().unwrap_or_default().into(),
));
return Err(GlueMvpError::Cleanup(database.name().into()));
}

let crawler = glue
Expand Down
Loading

0 comments on commit ff0628f

Please sign in to comment.