Unverified Commit 72391473 authored by Nugine's avatar Nugine
Browse files

codegen: aws_proxy

parent 0677bfc5
Loading
Loading
Loading
Loading
+63 −0
Original line number Diff line number Diff line
use crate::dto::RustTypes;
use crate::f;
use crate::gen::Codegen;
use crate::ops::Operations;
use crate::rust;

use heck::ToSnakeCase;

pub fn codegen(ops: &Operations, rust_types: &RustTypes, g: &mut Codegen) {
    g.ln("use super::*;");
    g.lf();
    g.ln("use crate::conv::{try_from_aws, try_into_aws};");
    g.lf();
    g.ln("use s3s::S3;");
    g.ln("use s3s::S3Result;");
    g.lf();

    g.ln("#[async_trait::async_trait]");
    g.ln("impl S3 for Proxy {");

    for op in ops.values() {
        let method_name = op.name.to_snake_case();
        let s3s_input = f!("s3s::dto::{}", op.input);
        let s3s_output = f!("s3s::dto::{}", op.output);

        let arg = if op.smithy_input == "Unit" { "_" } else { "input" };
        g.ln(f!(
            "async fn {method_name}(&self, {arg}: {s3s_input}) -> S3Result<{s3s_output}> {{"
        ));

        if op.smithy_input == "Unit" {
            g.ln(f!("let result = self.0.{method_name}().send().await;"));
        } else {
            g.ln(f!("let mut b = self.0.{method_name}();"));
            let rust::Type::Struct(ty) = &rust_types[op.input.as_str()] else { panic!() };
            for field in &ty.fields {
                let s3s_field_name = field.name.as_str();
                let aws_field_name = match s3s_field_name {
                    "checksum_crc32c" => "checksum_crc32_c",
                    "type_" => "type",
                    s => s,
                };

                if field.option_type {
                    g.ln(f!("b = b.set_{aws_field_name}(try_into_aws(input.{s3s_field_name})?);"));
                } else {
                    g.ln(f!("b = b.set_{aws_field_name}(Some(try_into_aws(input.{s3s_field_name})?));"));
                }
            }
            g.ln("let result = b.send().await;");
        }

        g.ln("match result {");
        g.ln("Ok(output) => try_from_aws(output),");
        g.ln("Err(e) => Err(wrap_sdk_error!(e)),");
        g.ln("}");

        g.ln("}");
        g.lf();
    }

    g.ln("}");
}
+7 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@ mod headers;
mod ops;

mod aws_conv;
mod aws_proxy;

use crate::gen::Codegen;

@@ -69,4 +70,10 @@ fn main() {
        let mut gen = Codegen::create_file(path).unwrap();
        aws_conv::codegen(&ops, &rust_types, &mut gen);
    }

    {
        let path = "crates/s3s-aws/src/proxy/generated.rs";
        let mut gen = Codegen::create_file(path).unwrap();
        aws_proxy::codegen(&ops, &rust_types, &mut gen);
    }
}
+1 −0
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ keywords = ["s3"]
categories = ["web-programming", "web-programming::http-server"]

[dependencies]
async-trait = "0.1.62"
aws-sdk-s3 = "0.23.0"
aws-smithy-http = "0.53.1"
aws-smithy-types = "0.53.1"

crates/s3s-aws/src/proxy.rs

deleted100644 → 0
+0 −9
Original line number Diff line number Diff line
use aws_sdk_s3::Client;

pub struct Proxy(Client);

impl From<Client> for Proxy {
    fn from(val: Client) -> Self {
        Self(val)
    }
}
+1591 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading