use std::error::Error;
use std::fmt;
#[allow(warnings)]
use futures::future;
use futures::Future;
use rusoto_core::credential::ProvideAwsCredentials;
use rusoto_core::region;
use rusoto_core::request::{BufferedHttpResponse, DispatchSignedRequest};
use rusoto_core::{Client, RusotoError, RusotoFuture};
use rusoto_core::proto;
use rusoto_core::signature::SignedRequest;
use serde_json;
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[cfg_attr(test, derive(Serialize))]
pub struct AppliedTerminology {
#[serde(rename = "Name")]
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(rename = "Terms")]
#[serde(skip_serializing_if = "Option::is_none")]
pub terms: Option<Vec<Term>>,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize)]
pub struct DeleteTerminologyRequest {
#[serde(rename = "Name")]
pub name: String,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct EncryptionKey {
#[serde(rename = "Id")]
pub id: String,
#[serde(rename = "Type")]
pub type_: String,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize)]
pub struct GetTerminologyRequest {
#[serde(rename = "Name")]
pub name: String,
#[serde(rename = "TerminologyDataFormat")]
pub terminology_data_format: String,
}
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[cfg_attr(test, derive(Serialize))]
pub struct GetTerminologyResponse {
#[serde(rename = "TerminologyDataLocation")]
#[serde(skip_serializing_if = "Option::is_none")]
pub terminology_data_location: Option<TerminologyDataLocation>,
#[serde(rename = "TerminologyProperties")]
#[serde(skip_serializing_if = "Option::is_none")]
pub terminology_properties: Option<TerminologyProperties>,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize)]
pub struct ImportTerminologyRequest {
#[serde(rename = "Description")]
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(rename = "EncryptionKey")]
#[serde(skip_serializing_if = "Option::is_none")]
pub encryption_key: Option<EncryptionKey>,
#[serde(rename = "MergeStrategy")]
pub merge_strategy: String,
#[serde(rename = "Name")]
pub name: String,
#[serde(rename = "TerminologyData")]
pub terminology_data: TerminologyData,
}
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[cfg_attr(test, derive(Serialize))]
pub struct ImportTerminologyResponse {
#[serde(rename = "TerminologyProperties")]
#[serde(skip_serializing_if = "Option::is_none")]
pub terminology_properties: Option<TerminologyProperties>,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize)]
pub struct ListTerminologiesRequest {
#[serde(rename = "MaxResults")]
#[serde(skip_serializing_if = "Option::is_none")]
pub max_results: Option<i64>,
#[serde(rename = "NextToken")]
#[serde(skip_serializing_if = "Option::is_none")]
pub next_token: Option<String>,
}
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[cfg_attr(test, derive(Serialize))]
pub struct ListTerminologiesResponse {
#[serde(rename = "NextToken")]
#[serde(skip_serializing_if = "Option::is_none")]
pub next_token: Option<String>,
#[serde(rename = "TerminologyPropertiesList")]
#[serde(skip_serializing_if = "Option::is_none")]
pub terminology_properties_list: Option<Vec<TerminologyProperties>>,
}
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[cfg_attr(test, derive(Serialize))]
pub struct Term {
#[serde(rename = "SourceText")]
#[serde(skip_serializing_if = "Option::is_none")]
pub source_text: Option<String>,
#[serde(rename = "TargetText")]
#[serde(skip_serializing_if = "Option::is_none")]
pub target_text: Option<String>,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize)]
pub struct TerminologyData {
#[serde(rename = "File")]
#[serde(
deserialize_with = "::rusoto_core::serialization::SerdeBlob::deserialize_blob",
serialize_with = "::rusoto_core::serialization::SerdeBlob::serialize_blob",
default
)]
pub file: bytes::Bytes,
#[serde(rename = "Format")]
pub format: String,
}
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[cfg_attr(test, derive(Serialize))]
pub struct TerminologyDataLocation {
#[serde(rename = "Location")]
pub location: String,
#[serde(rename = "RepositoryType")]
pub repository_type: String,
}
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[cfg_attr(test, derive(Serialize))]
pub struct TerminologyProperties {
#[serde(rename = "Arn")]
#[serde(skip_serializing_if = "Option::is_none")]
pub arn: Option<String>,
#[serde(rename = "CreatedAt")]
#[serde(skip_serializing_if = "Option::is_none")]
pub created_at: Option<f64>,
#[serde(rename = "Description")]
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(rename = "EncryptionKey")]
#[serde(skip_serializing_if = "Option::is_none")]
pub encryption_key: Option<EncryptionKey>,
#[serde(rename = "LastUpdatedAt")]
#[serde(skip_serializing_if = "Option::is_none")]
pub last_updated_at: Option<f64>,
#[serde(rename = "Name")]
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(rename = "SizeBytes")]
#[serde(skip_serializing_if = "Option::is_none")]
pub size_bytes: Option<i64>,
#[serde(rename = "SourceLanguageCode")]
#[serde(skip_serializing_if = "Option::is_none")]
pub source_language_code: Option<String>,
#[serde(rename = "TargetLanguageCodes")]
#[serde(skip_serializing_if = "Option::is_none")]
pub target_language_codes: Option<Vec<String>>,
#[serde(rename = "TermCount")]
#[serde(skip_serializing_if = "Option::is_none")]
pub term_count: Option<i64>,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize)]
pub struct TranslateTextRequest {
#[serde(rename = "SourceLanguageCode")]
pub source_language_code: String,
#[serde(rename = "TargetLanguageCode")]
pub target_language_code: String,
#[serde(rename = "TerminologyNames")]
#[serde(skip_serializing_if = "Option::is_none")]
pub terminology_names: Option<Vec<String>>,
#[serde(rename = "Text")]
pub text: String,
}
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[cfg_attr(test, derive(Serialize))]
pub struct TranslateTextResponse {
#[serde(rename = "AppliedTerminologies")]
#[serde(skip_serializing_if = "Option::is_none")]
pub applied_terminologies: Option<Vec<AppliedTerminology>>,
#[serde(rename = "SourceLanguageCode")]
pub source_language_code: String,
#[serde(rename = "TargetLanguageCode")]
pub target_language_code: String,
#[serde(rename = "TranslatedText")]
pub translated_text: String,
}
#[derive(Debug, PartialEq)]
pub enum DeleteTerminologyError {
InternalServer(String),
ResourceNotFound(String),
TooManyRequests(String),
}
impl DeleteTerminologyError {
pub fn from_response(res: BufferedHttpResponse) -> RusotoError<DeleteTerminologyError> {
if let Some(err) = proto::json::Error::parse(&res) {
match err.typ.as_str() {
"InternalServerException" => {
return RusotoError::Service(DeleteTerminologyError::InternalServer(err.msg))
}
"ResourceNotFoundException" => {
return RusotoError::Service(DeleteTerminologyError::ResourceNotFound(err.msg))
}
"TooManyRequestsException" => {
return RusotoError::Service(DeleteTerminologyError::TooManyRequests(err.msg))
}
"ValidationException" => return RusotoError::Validation(err.msg),
_ => {}
}
}
return RusotoError::Unknown(res);
}
}
impl fmt::Display for DeleteTerminologyError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.description())
}
}
impl Error for DeleteTerminologyError {
fn description(&self) -> &str {
match *self {
DeleteTerminologyError::InternalServer(ref cause) => cause,
DeleteTerminologyError::ResourceNotFound(ref cause) => cause,
DeleteTerminologyError::TooManyRequests(ref cause) => cause,
}
}
}
#[derive(Debug, PartialEq)]
pub enum GetTerminologyError {
InternalServer(String),
InvalidParameterValue(String),
ResourceNotFound(String),
TooManyRequests(String),
}
impl GetTerminologyError {
pub fn from_response(res: BufferedHttpResponse) -> RusotoError<GetTerminologyError> {
if let Some(err) = proto::json::Error::parse(&res) {
match err.typ.as_str() {
"InternalServerException" => {
return RusotoError::Service(GetTerminologyError::InternalServer(err.msg))
}
"InvalidParameterValueException" => {
return RusotoError::Service(GetTerminologyError::InvalidParameterValue(
err.msg,
))
}
"ResourceNotFoundException" => {
return RusotoError::Service(GetTerminologyError::ResourceNotFound(err.msg))
}
"TooManyRequestsException" => {
return RusotoError::Service(GetTerminologyError::TooManyRequests(err.msg))
}
"ValidationException" => return RusotoError::Validation(err.msg),
_ => {}
}
}
return RusotoError::Unknown(res);
}
}
impl fmt::Display for GetTerminologyError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.description())
}
}
impl Error for GetTerminologyError {
fn description(&self) -> &str {
match *self {
GetTerminologyError::InternalServer(ref cause) => cause,
GetTerminologyError::InvalidParameterValue(ref cause) => cause,
GetTerminologyError::ResourceNotFound(ref cause) => cause,
GetTerminologyError::TooManyRequests(ref cause) => cause,
}
}
}
#[derive(Debug, PartialEq)]
pub enum ImportTerminologyError {
InternalServer(String),
InvalidParameterValue(String),
LimitExceeded(String),
TooManyRequests(String),
}
impl ImportTerminologyError {
pub fn from_response(res: BufferedHttpResponse) -> RusotoError<ImportTerminologyError> {
if let Some(err) = proto::json::Error::parse(&res) {
match err.typ.as_str() {
"InternalServerException" => {
return RusotoError::Service(ImportTerminologyError::InternalServer(err.msg))
}
"InvalidParameterValueException" => {
return RusotoError::Service(ImportTerminologyError::InvalidParameterValue(
err.msg,
))
}
"LimitExceededException" => {
return RusotoError::Service(ImportTerminologyError::LimitExceeded(err.msg))
}
"TooManyRequestsException" => {
return RusotoError::Service(ImportTerminologyError::TooManyRequests(err.msg))
}
"ValidationException" => return RusotoError::Validation(err.msg),
_ => {}
}
}
return RusotoError::Unknown(res);
}
}
impl fmt::Display for ImportTerminologyError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.description())
}
}
impl Error for ImportTerminologyError {
fn description(&self) -> &str {
match *self {
ImportTerminologyError::InternalServer(ref cause) => cause,
ImportTerminologyError::InvalidParameterValue(ref cause) => cause,
ImportTerminologyError::LimitExceeded(ref cause) => cause,
ImportTerminologyError::TooManyRequests(ref cause) => cause,
}
}
}
#[derive(Debug, PartialEq)]
pub enum ListTerminologiesError {
InternalServer(String),
InvalidParameterValue(String),
TooManyRequests(String),
}
impl ListTerminologiesError {
pub fn from_response(res: BufferedHttpResponse) -> RusotoError<ListTerminologiesError> {
if let Some(err) = proto::json::Error::parse(&res) {
match err.typ.as_str() {
"InternalServerException" => {
return RusotoError::Service(ListTerminologiesError::InternalServer(err.msg))
}
"InvalidParameterValueException" => {
return RusotoError::Service(ListTerminologiesError::InvalidParameterValue(
err.msg,
))
}
"TooManyRequestsException" => {
return RusotoError::Service(ListTerminologiesError::TooManyRequests(err.msg))
}
"ValidationException" => return RusotoError::Validation(err.msg),
_ => {}
}
}
return RusotoError::Unknown(res);
}
}
impl fmt::Display for ListTerminologiesError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.description())
}
}
impl Error for ListTerminologiesError {
fn description(&self) -> &str {
match *self {
ListTerminologiesError::InternalServer(ref cause) => cause,
ListTerminologiesError::InvalidParameterValue(ref cause) => cause,
ListTerminologiesError::TooManyRequests(ref cause) => cause,
}
}
}
#[derive(Debug, PartialEq)]
pub enum TranslateTextError {
DetectedLanguageLowConfidence(String),
InternalServer(String),
InvalidRequest(String),
ResourceNotFound(String),
ServiceUnavailable(String),
TextSizeLimitExceeded(String),
TooManyRequests(String),
UnsupportedLanguagePair(String),
}
impl TranslateTextError {
pub fn from_response(res: BufferedHttpResponse) -> RusotoError<TranslateTextError> {
if let Some(err) = proto::json::Error::parse(&res) {
match err.typ.as_str() {
"DetectedLanguageLowConfidenceException" => {
return RusotoError::Service(TranslateTextError::DetectedLanguageLowConfidence(
err.msg,
))
}
"InternalServerException" => {
return RusotoError::Service(TranslateTextError::InternalServer(err.msg))
}
"InvalidRequestException" => {
return RusotoError::Service(TranslateTextError::InvalidRequest(err.msg))
}
"ResourceNotFoundException" => {
return RusotoError::Service(TranslateTextError::ResourceNotFound(err.msg))
}
"ServiceUnavailableException" => {
return RusotoError::Service(TranslateTextError::ServiceUnavailable(err.msg))
}
"TextSizeLimitExceededException" => {
return RusotoError::Service(TranslateTextError::TextSizeLimitExceeded(err.msg))
}
"TooManyRequestsException" => {
return RusotoError::Service(TranslateTextError::TooManyRequests(err.msg))
}
"UnsupportedLanguagePairException" => {
return RusotoError::Service(TranslateTextError::UnsupportedLanguagePair(
err.msg,
))
}
"ValidationException" => return RusotoError::Validation(err.msg),
_ => {}
}
}
return RusotoError::Unknown(res);
}
}
impl fmt::Display for TranslateTextError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.description())
}
}
impl Error for TranslateTextError {
fn description(&self) -> &str {
match *self {
TranslateTextError::DetectedLanguageLowConfidence(ref cause) => cause,
TranslateTextError::InternalServer(ref cause) => cause,
TranslateTextError::InvalidRequest(ref cause) => cause,
TranslateTextError::ResourceNotFound(ref cause) => cause,
TranslateTextError::ServiceUnavailable(ref cause) => cause,
TranslateTextError::TextSizeLimitExceeded(ref cause) => cause,
TranslateTextError::TooManyRequests(ref cause) => cause,
TranslateTextError::UnsupportedLanguagePair(ref cause) => cause,
}
}
}
pub trait Translate {
fn delete_terminology(
&self,
input: DeleteTerminologyRequest,
) -> RusotoFuture<(), DeleteTerminologyError>;
fn get_terminology(
&self,
input: GetTerminologyRequest,
) -> RusotoFuture<GetTerminologyResponse, GetTerminologyError>;
fn import_terminology(
&self,
input: ImportTerminologyRequest,
) -> RusotoFuture<ImportTerminologyResponse, ImportTerminologyError>;
fn list_terminologies(
&self,
input: ListTerminologiesRequest,
) -> RusotoFuture<ListTerminologiesResponse, ListTerminologiesError>;
fn translate_text(
&self,
input: TranslateTextRequest,
) -> RusotoFuture<TranslateTextResponse, TranslateTextError>;
}
#[derive(Clone)]
pub struct TranslateClient {
client: Client,
region: region::Region,
}
impl TranslateClient {
pub fn new(region: region::Region) -> TranslateClient {
TranslateClient {
client: Client::shared(),
region,
}
}
pub fn new_with<P, D>(
request_dispatcher: D,
credentials_provider: P,
region: region::Region,
) -> TranslateClient
where
P: ProvideAwsCredentials + Send + Sync + 'static,
P::Future: Send,
D: DispatchSignedRequest + Send + Sync + 'static,
D::Future: Send,
{
TranslateClient {
client: Client::new_with(credentials_provider, request_dispatcher),
region,
}
}
}
impl Translate for TranslateClient {
fn delete_terminology(
&self,
input: DeleteTerminologyRequest,
) -> RusotoFuture<(), DeleteTerminologyError> {
let mut request = SignedRequest::new("POST", "translate", &self.region, "/");
request.set_content_type("application/x-amz-json-1.1".to_owned());
request.add_header(
"x-amz-target",
"AWSShineFrontendService_20170701.DeleteTerminology",
);
let encoded = serde_json::to_string(&input).unwrap();
request.set_payload(Some(encoded));
self.client.sign_and_dispatch(request, |response| {
if response.status.is_success() {
Box::new(future::ok(::std::mem::drop(response)))
} else {
Box::new(
response
.buffer()
.from_err()
.and_then(|response| Err(DeleteTerminologyError::from_response(response))),
)
}
})
}
fn get_terminology(
&self,
input: GetTerminologyRequest,
) -> RusotoFuture<GetTerminologyResponse, GetTerminologyError> {
let mut request = SignedRequest::new("POST", "translate", &self.region, "/");
request.set_content_type("application/x-amz-json-1.1".to_owned());
request.add_header(
"x-amz-target",
"AWSShineFrontendService_20170701.GetTerminology",
);
let encoded = serde_json::to_string(&input).unwrap();
request.set_payload(Some(encoded));
self.client.sign_and_dispatch(request, |response| {
if response.status.is_success() {
Box::new(response.buffer().from_err().and_then(|response| {
proto::json::ResponsePayload::new(&response)
.deserialize::<GetTerminologyResponse, _>()
}))
} else {
Box::new(
response
.buffer()
.from_err()
.and_then(|response| Err(GetTerminologyError::from_response(response))),
)
}
})
}
fn import_terminology(
&self,
input: ImportTerminologyRequest,
) -> RusotoFuture<ImportTerminologyResponse, ImportTerminologyError> {
let mut request = SignedRequest::new("POST", "translate", &self.region, "/");
request.set_content_type("application/x-amz-json-1.1".to_owned());
request.add_header(
"x-amz-target",
"AWSShineFrontendService_20170701.ImportTerminology",
);
let encoded = serde_json::to_string(&input).unwrap();
request.set_payload(Some(encoded));
self.client.sign_and_dispatch(request, |response| {
if response.status.is_success() {
Box::new(response.buffer().from_err().and_then(|response| {
proto::json::ResponsePayload::new(&response)
.deserialize::<ImportTerminologyResponse, _>()
}))
} else {
Box::new(
response
.buffer()
.from_err()
.and_then(|response| Err(ImportTerminologyError::from_response(response))),
)
}
})
}
fn list_terminologies(
&self,
input: ListTerminologiesRequest,
) -> RusotoFuture<ListTerminologiesResponse, ListTerminologiesError> {
let mut request = SignedRequest::new("POST", "translate", &self.region, "/");
request.set_content_type("application/x-amz-json-1.1".to_owned());
request.add_header(
"x-amz-target",
"AWSShineFrontendService_20170701.ListTerminologies",
);
let encoded = serde_json::to_string(&input).unwrap();
request.set_payload(Some(encoded));
self.client.sign_and_dispatch(request, |response| {
if response.status.is_success() {
Box::new(response.buffer().from_err().and_then(|response| {
proto::json::ResponsePayload::new(&response)
.deserialize::<ListTerminologiesResponse, _>()
}))
} else {
Box::new(
response
.buffer()
.from_err()
.and_then(|response| Err(ListTerminologiesError::from_response(response))),
)
}
})
}
fn translate_text(
&self,
input: TranslateTextRequest,
) -> RusotoFuture<TranslateTextResponse, TranslateTextError> {
let mut request = SignedRequest::new("POST", "translate", &self.region, "/");
request.set_content_type("application/x-amz-json-1.1".to_owned());
request.add_header(
"x-amz-target",
"AWSShineFrontendService_20170701.TranslateText",
);
let encoded = serde_json::to_string(&input).unwrap();
request.set_payload(Some(encoded));
self.client.sign_and_dispatch(request, |response| {
if response.status.is_success() {
Box::new(response.buffer().from_err().and_then(|response| {
proto::json::ResponsePayload::new(&response)
.deserialize::<TranslateTextResponse, _>()
}))
} else {
Box::new(
response
.buffer()
.from_err()
.and_then(|response| Err(TranslateTextError::from_response(response))),
)
}
})
}
}