From bc511ec77f0231d95b10a876877b4f45723cc65b Mon Sep 17 00:00:00 2001 From: Russell Cohen Date: Thu, 1 Apr 2021 21:49:57 -0400 Subject: [PATCH] Add two Amazon Polly examples (#286) * Add to Amazon Polly examples * Fix copy-paste error * Update aws/sdk/examples/polly-helloworld/src/main.rs Co-authored-by: David Barsky * Update aws/sdk/examples/polly-helloworld/src/main.rs Co-authored-by: David Barsky * Some cleanups * Rerun precommit * CR Feedback * Fix clippy lint * Small cleanup Co-authored-by: David Barsky --- .../examples/polly-generate-speech/.gitignore | 1 + .../examples/polly-generate-speech/Cargo.toml | 11 +++++ .../polly-generate-speech/src/main.rs | 25 +++++++++++ aws/sdk/examples/polly-helloworld/Cargo.toml | 11 +++++ aws/sdk/examples/polly-helloworld/src/main.rs | 43 +++++++++++++++++++ codegen-test/model/rest-json-extras.smithy | 2 +- .../generators/CombinedErrorGenerator.kt | 4 +- design/src/SUMMARY.md | 1 - 8 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 aws/sdk/examples/polly-generate-speech/.gitignore create mode 100644 aws/sdk/examples/polly-generate-speech/Cargo.toml create mode 100644 aws/sdk/examples/polly-generate-speech/src/main.rs create mode 100644 aws/sdk/examples/polly-helloworld/Cargo.toml create mode 100644 aws/sdk/examples/polly-helloworld/src/main.rs diff --git a/aws/sdk/examples/polly-generate-speech/.gitignore b/aws/sdk/examples/polly-generate-speech/.gitignore new file mode 100644 index 0000000000..cbf363136a --- /dev/null +++ b/aws/sdk/examples/polly-generate-speech/.gitignore @@ -0,0 +1 @@ +audio.mp3 diff --git a/aws/sdk/examples/polly-generate-speech/Cargo.toml b/aws/sdk/examples/polly-generate-speech/Cargo.toml new file mode 100644 index 0000000000..632da40aab --- /dev/null +++ b/aws/sdk/examples/polly-generate-speech/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "polly-generate-speech" +version = "0.1.0" +authors = ["Russell Cohen "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +polly = { path = "../../build/aws-sdk/polly"} +tokio = { version = "1", features = ["full"] } diff --git a/aws/sdk/examples/polly-generate-speech/src/main.rs b/aws/sdk/examples/polly-generate-speech/src/main.rs new file mode 100644 index 0000000000..5aa9240119 --- /dev/null +++ b/aws/sdk/examples/polly-generate-speech/src/main.rs @@ -0,0 +1,25 @@ +use polly::model::{Engine, OutputFormat, VoiceId}; +use std::error::Error; +use tokio::fs::File; +use tokio::io::AsyncWriteExt; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let client = polly::fluent::Client::from_env(); + let resp = client + .synthesize_speech() + .voice_id(VoiceId::Emma) + .engine(Engine::Neural) + .output_format(OutputFormat::Mp3) + .text("Hello, I am polly!") + .send() + .await?; + let audio = resp.audio_stream.expect("data should be included"); + let mut file = File::create("audio.mp3").await?; + file.write_all(audio.as_ref()).await?; + println!( + "Audio written to audio.mp3 ({} bytes)", + audio.as_ref().len() + ); + Ok(()) +} diff --git a/aws/sdk/examples/polly-helloworld/Cargo.toml b/aws/sdk/examples/polly-helloworld/Cargo.toml new file mode 100644 index 0000000000..810e3c10b8 --- /dev/null +++ b/aws/sdk/examples/polly-helloworld/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "polly-helloworld" +version = "0.1.0" +authors = ["Russell Cohen "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +polly = { path = "../../build/aws-sdk/polly"} +tokio = { version = "1", features = ["full"] } diff --git a/aws/sdk/examples/polly-helloworld/src/main.rs b/aws/sdk/examples/polly-helloworld/src/main.rs new file mode 100644 index 0000000000..a3a13c7b2c --- /dev/null +++ b/aws/sdk/examples/polly-helloworld/src/main.rs @@ -0,0 +1,43 @@ +use polly::model::{Engine, Voice}; +use std::error::Error; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let client = polly::fluent::Client::from_env(); + let mut tok = None; + let mut voices: Vec = vec![]; + // Below is an an example of how pagination can be implemented manually. + loop { + let mut req = client.describe_voices(); + if let Some(tok) = tok { + req = req.next_token(tok); + } + let resp = req.send().await?; + for voice in resp.voices.unwrap_or_default() { + println!( + "I can speak as: {} in {:?}", + voice.name.as_ref().unwrap(), + voice.language_name.as_ref().unwrap() + ); + voices.push(voice); + } + tok = match resp.next_token { + Some(next) => Some(next), + None => break, + }; + } + let neural_voices = voices + .iter() + .filter(|voice| { + voice + .supported_engines + .as_deref() + .unwrap_or_default() + .contains(&Engine::Neural) + }) + .map(|voice| voice.id.as_ref().unwrap()) + .collect::>(); + + println!("Voices supporting a neural engine: {:?}", neural_voices); + Ok(()) +} diff --git a/codegen-test/model/rest-json-extras.smithy b/codegen-test/model/rest-json-extras.smithy index d9ba1fdf18..565f557589 100644 --- a/codegen-test/model/rest-json-extras.smithy +++ b/codegen-test/model/rest-json-extras.smithy @@ -101,4 +101,4 @@ structure EnumQueryInput { @httpLabel @required enum: StringEnum -} \ No newline at end of file +} diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/CombinedErrorGenerator.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/CombinedErrorGenerator.kt index 82f74c8c72..d2314bdc55 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/CombinedErrorGenerator.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/CombinedErrorGenerator.kt @@ -80,7 +80,7 @@ class CombinedErrorGenerator( rust( """ /// An unexpected error, eg. invalid JSON returned by the service or an unknown error code - Unhandled(Box), + Unhandled(Box), """, RuntimeType.StdError ) @@ -125,7 +125,7 @@ class CombinedErrorGenerator( Self { kind, meta } } - pub fn unhandled(err: impl Into>) -> Self { + pub fn unhandled(err: impl Into>) -> Self { Self { kind: ${symbol.name}Kind::Unhandled(err.into()), meta: Default::default() diff --git a/design/src/SUMMARY.md b/design/src/SUMMARY.md index f6a01f3d09..1db8af1da2 100644 --- a/design/src/SUMMARY.md +++ b/design/src/SUMMARY.md @@ -3,4 +3,3 @@ - [Http Operations](./operation.md) - [Endpoint Resolution](./endpoint.md) - [HTTP middleware](./middleware.md) -