Commit b84dceb8 authored by Nugine's avatar Nugine
Browse files

refactor(codegen): use scoped-writer

parent 4bbcb88a
Loading
Loading
Loading
Loading
+7 −7
Original line number Diff line number Diff line
@@ -645,12 +645,6 @@ version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6"

[[package]]
name = "codegen-writer"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58651fe83afe19791cbdb8932117e770ad34766d649f4e332b1b6cb00aa3a597"

[[package]]
name = "colorchoice"
version = "1.0.3"
@@ -2119,12 +2113,12 @@ dependencies = [
name = "s3s-codegen"
version = "0.0.0"
dependencies = [
 "codegen-writer",
 "heck",
 "nugine-rust-utils",
 "numeric_cast",
 "regex",
 "s3s-model",
 "scoped-writer",
 "serde",
 "serde_json",
 "serde_urlencoded",
@@ -2244,6 +2238,12 @@ dependencies = [
 "windows-sys 0.59.0",
]

[[package]]
name = "scoped-writer"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95c04ad05b0a8087c941e1218d50414ae3a86d6dcd6eb9b1d4e42260c6f30b3f"

[[package]]
name = "scopeguard"
version = "1.2.0"
+1 −1
Original line number Diff line number Diff line
@@ -8,7 +8,7 @@ publish = false
workspace = true

[dependencies]
codegen-writer = "0.2.0"
scoped-writer = "0.2.0"
heck = "0.5.0"
nugine-rust-utils = "0.3.1"
std-next = "0.1.1"
+32 −33
Original line number Diff line number Diff line
@@ -2,44 +2,43 @@ use super::ops::Operations;

use crate::declare_codegen;

use codegen_writer::g;
use codegen_writer::glines;
use heck::ToSnakeCase;
use scoped_writer::g;

pub fn codegen(ops: &Operations) {
    declare_codegen!();

    glines![
        "use super::S3AccessContext;"
        ""
        "use crate::dto::*;"
        "use crate::error::S3Result;"
        "use crate::request::S3Request;"
        ""
        "#[async_trait::async_trait]"
        "pub trait S3Access: Send + Sync + 'static {"
        ""
    ];

    glines![
        "/// Checks whether the current request has accesses to the resources."
        "///"
        "/// This method is called before deserializing the operation input."
        "///"
        "/// By default, this method rejects all anonymous requests"
        "/// and returns [`AccessDenied`](crate::S3ErrorCode::AccessDenied) error."
        "///"
        "/// An access control provider can override this method to implement custom logic."
        "///"
        "/// Common fields in the context:"
        "/// + [`cx.credentials()`](S3AccessContext::credentials)"
        "/// + [`cx.s3_path()`](S3AccessContext::s3_path)"
        "/// + [`cx.s3_op().name()`](crate::S3Operation::name)"
        "/// + [`cx.extensions_mut()`](S3AccessContext::extensions_mut)"
        "async fn check(&self, cx: &mut S3AccessContext<'_>) -> S3Result<()> {"
        "    super::default_check(cx)"
        "}"
    ];
    g([
        "use super::S3AccessContext;",
        "",
        "use crate::dto::*;",
        "use crate::error::S3Result;",
        "use crate::request::S3Request;",
        "",
        "#[async_trait::async_trait]",
        "pub trait S3Access: Send + Sync + 'static {",
        "",
    ]);

    g([
        "/// Checks whether the current request has accesses to the resources.",
        "///",
        "/// This method is called before deserializing the operation input.",
        "///",
        "/// By default, this method rejects all anonymous requests",
        "/// and returns [`AccessDenied`](crate::S3ErrorCode::AccessDenied) error.",
        "///",
        "/// An access control provider can override this method to implement custom logic.",
        "///",
        "/// Common fields in the context:",
        "/// + [`cx.credentials()`](S3AccessContext::credentials)",
        "/// + [`cx.s3_path()`](S3AccessContext::s3_path)",
        "/// + [`cx.s3_op().name()`](crate::S3Operation::name)",
        "/// + [`cx.extensions_mut()`](S3AccessContext::extensions_mut)",
        "async fn check(&self, cx: &mut S3AccessContext<'_>) -> S3Result<()> {",
        "    super::default_check(cx)",
        "}",
    ]);

    for op in ops.values() {
        let method_name = op.name.to_snake_case();
+3 −6
Original line number Diff line number Diff line
@@ -8,19 +8,16 @@ use crate::declare_codegen;
use std::format as f;
use std::ops::Not;

use codegen_writer::g;
use codegen_writer::glines;
use heck::ToSnakeCase;
use heck::ToUpperCamelCase;
use scoped_writer::g;

#[allow(clippy::too_many_lines)]
pub fn codegen(ops: &Operations, rust_types: &RustTypes) {
    declare_codegen!();

    glines![
        "use super::*;"
        ""
    ];
    g!("use super::*;");
    g!();

    for (name, rust_type) in rust_types {
        match name.as_str() {
+24 −25
Original line number Diff line number Diff line
@@ -6,25 +6,24 @@ use crate::declare_codegen;

use std::format as f;

use codegen_writer::g;
use codegen_writer::glines;
use heck::ToSnakeCase;
use scoped_writer::g;

pub fn codegen(ops: &Operations, rust_types: &RustTypes) {
    declare_codegen!();

    glines![
        "use super::*;"
        ""
        "use crate::conv::{try_from_aws, try_into_aws};"
        ""
        "use s3s::S3;"
        "use s3s::{S3Request, S3Response};"
        "use s3s::S3Result;"
        ""
        "use tracing::debug;"
        ""
    ];
    g([
        "use super::*;",
        "",
        "use crate::conv::{try_from_aws, try_into_aws};",
        "",
        "use s3s::S3;",
        "use s3s::{S3Request, S3Response};",
        "use s3s::S3Result;",
        "",
        "use tracing::debug;",
        "",
    ]);

    g!("#[async_trait::async_trait]");
    g!("impl S3 for Proxy {{");
@@ -90,17 +89,17 @@ pub fn codegen(ops: &Operations, rust_types: &RustTypes) {
            g!("let result = b.send().await;");
        }

        glines![
            "match result {"
            "    Ok(output) => {"
            "        let headers = super::meta::build_headers(&output)?;"
            "        let output = try_from_aws(output)?;"
            "        debug!(?output);"
            "        Ok(S3Response::with_headers(output, headers))"
            "    },"
            "    Err(e) => Err(wrap_sdk_error!(e)),"
            "}"
        ];
        g([
            "match result {",
            "    Ok(output) => {",
            "        let headers = super::meta::build_headers(&output)?;",
            "        let output = try_from_aws(output)?;",
            "        debug!(?output);",
            "        Ok(S3Response::with_headers(output, headers))",
            "    },",
            "    Err(e) => Err(wrap_sdk_error!(e)),",
            "}",
        ]);

        g!("}}");
        g!();
Loading