Skip to content

Commit

Permalink
feat(s3s): handle fmt message with implicit arguments in s3_error mac…
Browse files Browse the repository at this point in the history
…ro (#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
  • Loading branch information
lperlaki authored Dec 17, 2024
1 parent 787319c commit 9555e11
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions crates/s3s/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)+))
};
}

Expand All @@ -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"));
}
}

0 comments on commit 9555e11

Please sign in to comment.