Unverified Commit 91bd1187 authored by Nugine's avatar Nugine
Browse files

codegen: aws_conv: error

parent d7afd1c9
Loading
Loading
Loading
Loading
+8 −22
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ pub fn codegen(ops: &Operations, rust_types: &RustTypes, g: &mut Codegen) {

        g.ln(f!("impl AwsConversion for {s3s_path} {{"));
        g.ln(f!("    type Target = {aws_name};"));
        g.ln("type Error = S3Error;");
        g.lf();

        if contains_deprecated_field(name) {
@@ -52,30 +53,15 @@ pub fn codegen(ops: &Operations, rust_types: &RustTypes, g: &mut Codegen) {
                        "type_" => "r#type",
                        s => s,
                    };
                    let field_type = &rust_types[field.type_.as_str()];

                    'gen: {
                        if let rust::Type::Provided(ty) = field_type {
                            match ty.name.as_str() {
                                "StreamingBlob" => {
                                    g.ln(f!("{s3s_field_name}: stream_from_aws(x.{aws_field_name}),"));
                                    break 'gen;
                                }
                                "Body" => {}
                                "ContentType" | "CopySource" | "Range" => {
                                    // typed header value
                                }
                                _ => unimplemented!("{ty:#?}"),
                            }
                        }

                        if field.option_type || field.default_value.is_some() {
                    if field.type_ == "StreamingBlob" {
                        g.ln(f!("{s3s_field_name}: Some(try_from_aws(x.{aws_field_name})?),"));
                    } else if field.option_type || field.default_value.is_some() {
                        g.ln(f!("{s3s_field_name}: try_from_aws(x.{aws_field_name})?,"));
                    } else {
                        g.ln(f!("{s3s_field_name}: unwrap_from_aws(x.{aws_field_name}, \"{s3s_field_name}\")?,"));
                    }
                }
                }
                g.ln("})");
            }
            rust::Type::StrEnum(ty) => {
+385 −5

File changed.

Preview size limit exceeded, changes collapsed.

+33 −22
Original line number Diff line number Diff line
@@ -4,26 +4,31 @@ use s3s::s3_error;
use s3s::{S3Error, S3Result};

use std::collections::HashMap;
use std::convert::Infallible;

pub trait AwsConversion: Sized {
    type Target;
    type Error;

    fn try_from_aws(x: Self::Target) -> S3Result<Self>;
    fn try_from_aws(x: Self::Target) -> Result<Self, Self::Error>;

    fn try_into_aws(x: Self) -> S3Result<Self::Target>;
    fn try_into_aws(x: Self) -> Result<Self::Target, Self::Error>;
}

pub fn try_from_aws<T: AwsConversion>(x: T::Target) -> S3Result<T> {
pub fn try_from_aws<T: AwsConversion>(x: T::Target) -> Result<T, T::Error> {
    T::try_from_aws(x)
}

pub fn try_into_aws<T: AwsConversion>(x: T) -> S3Result<T::Target> {
pub fn try_into_aws<T: AwsConversion>(x: T) -> S3Result<T::Target, T::Error> {
    T::try_into_aws(x)
}

fn unwrap_from_aws<T: AwsConversion>(opt: Option<T::Target>, field_name: &str) -> S3Result<T> {
fn unwrap_from_aws<T: AwsConversion>(opt: Option<T::Target>, field_name: &str) -> S3Result<T>
where
    S3Error: From<T::Error>,
{
    match opt {
        Some(x) => T::try_from_aws(x),
        Some(x) => T::try_from_aws(x).map_err(Into::into),
        None => Err(s3_error!(InternalError, "missing field: {}", field_name)),
    }
}
@@ -33,14 +38,15 @@ macro_rules! identity_impl {
        $(
            impl AwsConversion for $ty {
                type Target = $ty;
                type Error = Infallible;

                #[inline(always)]
                fn try_from_aws(x: Self::Target) -> S3Result<Self> {
                fn try_from_aws(x: Self::Target) -> Result<Self, Self::Error> {
                    Ok(x)
                }

                #[inline(always)]
                fn try_into_aws(x: Self) -> S3Result<Self::Target> {
                fn try_into_aws(x: Self) -> Result<Self::Target, Self::Error> {
                    Ok(x)
                }
            }
@@ -52,30 +58,33 @@ identity_impl!(bool, i32, i64, String, HashMap<String, String>);

impl<T: AwsConversion> AwsConversion for Option<T> {
    type Target = Option<T::Target>;
    type Error = T::Error;

    fn try_from_aws(x: Self::Target) -> S3Result<Self> {
    fn try_from_aws(x: Self::Target) -> Result<Self, Self::Error> {
        x.map(try_from_aws).transpose()
    }

    fn try_into_aws(x: Self) -> S3Result<Self::Target> {
    fn try_into_aws(x: Self) -> Result<Self::Target, Self::Error> {
        x.map(try_into_aws).transpose()
    }
}

impl<T: AwsConversion> AwsConversion for Vec<T> {
    type Target = Vec<T::Target>;
    type Error = T::Error;

    fn try_from_aws(x: Self::Target) -> S3Result<Self> {
    fn try_from_aws(x: Self::Target) -> Result<Self, Self::Error> {
        x.into_iter().map(try_from_aws).collect()
    }

    fn try_into_aws(x: Self) -> S3Result<Self::Target> {
    fn try_into_aws(x: Self) -> Result<Self::Target, Self::Error> {
        x.into_iter().map(try_into_aws).collect()
    }
}

impl AwsConversion for s3s::dto::Timestamp {
    type Target = aws_sdk_s3::types::DateTime;
    type Error = S3Error;

    fn try_from_aws(x: Self::Target) -> S3Result<Self> {
        use aws_smithy_types_convert::date_time::DateTimeExt;
@@ -90,6 +99,7 @@ impl AwsConversion for s3s::dto::Timestamp {

impl AwsConversion for s3s::dto::ContentType {
    type Target = String;
    type Error = S3Error;

    fn try_from_aws(x: Self::Target) -> S3Result<Self> {
        x.parse::<Self>().map_err(S3Error::internal_error)
@@ -102,6 +112,7 @@ impl AwsConversion for s3s::dto::ContentType {

impl AwsConversion for s3s::dto::CopySource {
    type Target = String;
    type Error = S3Error;

    fn try_from_aws(x: Self::Target) -> S3Result<Self> {
        Self::parse(x.as_str()).map_err(S3Error::internal_error)
@@ -114,6 +125,7 @@ impl AwsConversion for s3s::dto::CopySource {

impl AwsConversion for s3s::dto::Range {
    type Target = String;
    type Error = S3Error;

    fn try_from_aws(x: Self::Target) -> S3Result<Self> {
        Self::parse(x.as_str()).map_err(S3Error::internal_error)
@@ -126,40 +138,39 @@ impl AwsConversion for s3s::dto::Range {

impl AwsConversion for s3s::dto::Event {
    type Target = aws_sdk_s3::model::Event;
    type Error = Infallible;

    fn try_from_aws(x: Self::Target) -> S3Result<Self> {
    fn try_from_aws(x: Self::Target) -> Result<Self, Self::Error> {
        Ok(Self::from(x.as_str().to_owned()))
    }

    fn try_into_aws(x: Self) -> S3Result<Self::Target> {
    fn try_into_aws(x: Self) -> Result<Self::Target, Self::Error> {
        Ok(Self::Target::from(x))
    }
}

fn stream_from_aws(x: aws_sdk_s3::types::ByteStream) -> Option<s3s::dto::StreamingBlob> {
    Some(s3s::dto::StreamingBlob::wrap(x))
}

impl AwsConversion for s3s::dto::StreamingBlob {
    type Target = aws_sdk_s3::types::ByteStream;
    type Error = Infallible;

    fn try_from_aws(x: Self::Target) -> S3Result<Self> {
    fn try_from_aws(x: Self::Target) -> Result<Self, Self::Error> {
        Ok(Self::wrap(x))
    }

    fn try_into_aws(x: Self) -> S3Result<Self::Target> {
    fn try_into_aws(x: Self) -> Result<Self::Target, Self::Error> {
        Ok(hyper::Body::wrap_stream(x).into())
    }
}

impl AwsConversion for s3s::dto::Body {
    type Target = aws_sdk_s3::types::Blob;
    type Error = Infallible;

    fn try_from_aws(x: Self::Target) -> S3Result<Self> {
    fn try_from_aws(x: Self::Target) -> Result<Self, Self::Error> {
        Ok(x.into_inner().into())
    }

    fn try_into_aws(x: Self) -> S3Result<Self::Target> {
    fn try_into_aws(x: Self) -> Result<Self::Target, Self::Error> {
        Ok(Self::Target::new(x))
    }
}