Unverified Commit 9555e11a authored by Liam Perlaki's avatar Liam Perlaki Committed by GitHub
Browse files

feat(s3s): handle fmt message with implicit arguments in s3_error macro (#228)

* handle fmt message with implicit arguments in s3_error macro

* remove use super::* from unit test

* use std::fmt::format instead of to_string
parent 787319c5
Loading
Loading
Loading
Loading
+27 −5
Original line number Diff line number Diff line
@@ -52,6 +52,17 @@ impl S3Error {
        this
    }

    #[doc(hidden)]
    pub fn with_message_fmt(code: S3ErrorCode, args: fmt::Arguments<'_>) -> Self {
        let msg = if let Some(msg) = args.as_str() {
            Cow::Borrowed(msg)
        } else {
            Cow::Owned(fmt::format(args))
        };

        Self::with_message(code, msg)
    }

    #[must_use]
    pub fn with_source(code: S3ErrorCode, source: StdError) -> Self {
        let mut this = Self::new(code);
@@ -207,11 +218,8 @@ macro_rules! s3_error {
    ($code:ident) => {
        $crate::S3Error::new($crate::S3ErrorCode::$code)
    };
    ($code:ident, $msg:literal) => {
        $crate::S3Error::with_message($crate::S3ErrorCode::$code, $msg)
    };
    ($code:ident, $fmt:literal, $($arg:tt)+) => {
        $crate::S3Error::with_message($crate::S3ErrorCode::$code, format!($fmt, $($arg)+))
    ($code:ident, $($arg:tt)+) => {
        $crate::S3Error::with_message_fmt($crate::S3ErrorCode::$code, format_args!($($arg)+))
    };
}

@@ -234,3 +242,17 @@ impl S3ErrorCode {
        unreachable!()
    }
}

#[cfg(test)]
mod tests {
    // not needed since the macro is globally in scope
    // use super::*;

    #[test]
    fn s3_error_fmt() {
        let bucket = "my_bucket";
        let e = s3_error!(AccessDenied, "access denied for bucket {bucket}");

        assert_eq!(e.message(), Some("access denied for bucket my_bucket"));
    }
}