Commit 5c460a8c authored by Nugine's avatar Nugine
Browse files

feat(codegen): minio feature

parent ef0bd703
Loading
Loading
Loading
Loading
+5 −2
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ use super::smithy::SmithyTraitsExt;
use super::{rust, smithy};

use crate::declare_codegen;
use crate::v1::Patch;

use std::borrow::Cow;
use std::collections::BTreeMap;
@@ -391,7 +392,7 @@ fn unify_operation_types(ops: &Operations, space: &mut RustTypes) {
    }
}

pub fn codegen(rust_types: &RustTypes, ops: &Operations) {
pub fn codegen(rust_types: &RustTypes, ops: &Operations, patch: Option<Patch>) {
    declare_codegen!();

    g([
@@ -447,8 +448,10 @@ pub fn codegen(rust_types: &RustTypes, ops: &Operations) {

    codegen_dto_ext(rust_types);

    if matches!(patch, Some(Patch::Minio)) {
        super::minio::codegen_in_dto();
    }
}

fn codegen_struct(ty: &rust::Struct, rust_types: &RustTypes, ops: &Operations) {
    codegen_doc(ty.doc.as_deref());
+0 −22
Original line number Diff line number Diff line
@@ -2,26 +2,8 @@ use scoped_writer::g;

use super::smithy;

fn git_branch() -> String {
    let output = std::process::Command::new("git")
        .args(["rev-parse", "--abbrev-ref", "HEAD"])
        .output()
        .unwrap();
    let stdout = core::str::from_utf8(&output.stdout).unwrap();
    stdout.trim().to_owned()
}

fn is_minio_branch() -> bool {
    let branch_name = git_branch();
    matches!(branch_name.as_str(), "minio" | "feat/minio")
}

/// <https://github.com/Nugine/s3s/issues/192>
pub fn patch(model: &mut smithy::Model) {
    if !is_minio_branch() {
        return;
    }

    let patches = smithy::Model::load_json("data/minio-patches.json").unwrap();

    for (shape_name, patch) in patches.shapes {
@@ -44,10 +26,6 @@ pub fn patch(model: &mut smithy::Model) {

#[allow(clippy::too_many_lines)]
pub fn codegen_in_dto() {
    if !is_minio_branch() {
        return;
    }

    let code = r#"

#[derive(Debug, Default)]
+37 −17
Original line number Diff line number Diff line
@@ -25,37 +25,57 @@ fn write_file(path: &str, f: impl FnOnce()) {
    scoped_writer::scoped(&mut writer, f);
}

#[derive(Debug, Clone, Copy)]
enum Patch {
    Minio,
}

pub fn run() {
    inner_run(None);
    inner_run(Some(Patch::Minio));
}

fn inner_run(code_patch: Option<Patch>) {
    let model = {
        let mut s3_model = smithy::Model::load_json("data/s3.json").unwrap();

        let mut sts_model = smithy::Model::load_json("data/sts.json").unwrap();
        sts::reduce(&mut sts_model);
        s3_model.shapes.append(&mut sts_model.shapes);

        if matches!(code_patch, Some(Patch::Minio)) {
            minio::patch(&mut s3_model);
        }

        s3_model
    };

    let ops = ops::collect_operations(&model);
    let rust_types = dto::collect_rust_types(&model, &ops);

    let suffix = match code_patch {
        Some(Patch::Minio) => "_minio",
        None => "",
    };

    {
        let path = "crates/s3s/src/dto/generated.rs";
        write_file(path, || dto::codegen(&rust_types, &ops));
        let path = format!("crates/s3s/src/dto/generated{suffix}.rs");
        write_file(&path, || dto::codegen(&rust_types, &ops, code_patch));
    }

    {
        let path = "crates/s3s/src/header/generated.rs";
        write_file(path, || headers::codegen(&model));
        let path = format!("crates/s3s/src/header/generated{suffix}.rs");
        write_file(&path, || headers::codegen(&model));
    }

    {
        let path = "crates/s3s/src/error/generated.rs";
        write_file(path, || error::codegen(&model));
        let path = format!("crates/s3s/src/error/generated{suffix}.rs");
        write_file(&path, || error::codegen(&model));
    }

    {
        let path = "crates/s3s/src/xml/generated.rs";
        write_file(path, || xml::codegen(&ops, &rust_types));
        let path = format!("crates/s3s/src/xml/generated{suffix}.rs");
        write_file(&path, || xml::codegen(&ops, &rust_types));
    }

    {
@@ -64,22 +84,22 @@ pub fn run() {
    }

    {
        let path = "crates/s3s/src/ops/generated.rs";
        write_file(path, || ops::codegen(&ops, &rust_types));
        let path = format!("crates/s3s/src/ops/generated{suffix}.rs");
        write_file(&path, || ops::codegen(&ops, &rust_types));
    }

    {
        let path = "crates/s3s/src/access/generated.rs";
        write_file(path, || access::codegen(&ops));
        let path = format!("crates/s3s/src/access/generated{suffix}.rs");
        write_file(&path, || access::codegen(&ops));
    }

    {
        let path = "crates/s3s-aws/src/conv/generated.rs";
        write_file(path, || aws_conv::codegen(&ops, &rust_types));
        let path = format!("crates/s3s-aws/src/conv/generated{suffix}.rs");
        write_file(&path, || aws_conv::codegen(&ops, &rust_types));
    }

    {
        let path = "crates/s3s-aws/src/proxy/generated.rs";
        write_file(path, || aws_proxy::codegen(&ops, &rust_types));
        let path = format!("crates/s3s-aws/src/proxy/generated{suffix}.rs");
        write_file(&path, || aws_proxy::codegen(&ops, &rust_types));
    }
}
+4 −1
Original line number Diff line number Diff line
@@ -13,6 +13,9 @@ rust-version.workspace = true
[lints]
workspace = true

[features]
minio = ["s3s/minio"]

[dependencies]
async-trait = "0.1.89"
aws-sdk-s3 = "1.105.0"
@@ -20,7 +23,7 @@ aws-smithy-runtime-api = { version = "1.9.0", features = ["client", "http-1x"] }
aws-smithy-types = { version = "1.3.2", features = ["http-body-1-x"] }
aws-smithy-types-convert = { version = "0.60.9", features = ["convert-time"] }
hyper = "1.7.0"
s3s = { version = "0.12.0-dev", path = "../s3s" }
s3s = { version = "0.12.0-dev", path = "../s3s", default-features = false }
std-next = "0.1.9"
sync_wrapper = "1.0.2"
tracing = "0.1.41"
+9192 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading