use crate::config::Parse;
use itertools::Itertools;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum S3CopyIfNotExists {
Header(String, String),
HeaderWithStatus(String, String, reqwest::StatusCode),
Multipart,
}
impl std::fmt::Display for S3CopyIfNotExists {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Header(k, v) => write!(f, "header: {k}: {v}"),
Self::HeaderWithStatus(k, v, code) => {
write!(f, "header-with-status: {k}: {v}: {}", code.as_u16())
}
Self::Multipart => f.write_str("multipart"),
}
}
}
impl S3CopyIfNotExists {
fn from_str(s: &str) -> Option<Self> {
if s.trim() == "multipart" {
return Some(Self::Multipart);
};
let (variant, value) = s.split_once(':')?;
match variant.trim() {
"header" => {
let (k, v) = value.split_once(':')?;
Some(Self::Header(k.trim().to_string(), v.trim().to_string()))
}
"header-with-status" => {
let (k, v, status) = value.split(':').collect_tuple()?;
let code = status.trim().parse().ok()?;
Some(Self::HeaderWithStatus(
k.trim().to_string(),
v.trim().to_string(),
code,
))
}
_ => None,
}
}
}
impl Parse for S3CopyIfNotExists {
fn parse(v: &str) -> crate::Result<Self> {
Self::from_str(v).ok_or_else(|| crate::Error::Generic {
store: "Config",
source: format!("Failed to parse \"{v}\" as S3CopyIfNotExists").into(),
})
}
}
#[derive(Debug, Clone, Eq, PartialEq, Default)]
#[allow(missing_copy_implementations)]
#[non_exhaustive]
pub enum S3ConditionalPut {
#[default]
ETagMatch,
Disabled,
}
impl std::fmt::Display for S3ConditionalPut {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::ETagMatch => write!(f, "etag"),
Self::Disabled => write!(f, "disabled"),
}
}
}
impl S3ConditionalPut {
fn from_str(s: &str) -> Option<Self> {
match s.trim() {
"etag" => Some(Self::ETagMatch),
"disabled" => Some(Self::Disabled),
_ => None,
}
}
}
impl Parse for S3ConditionalPut {
fn parse(v: &str) -> crate::Result<Self> {
Self::from_str(v).ok_or_else(|| crate::Error::Generic {
store: "Config",
source: format!("Failed to parse \"{v}\" as S3PutConditional").into(),
})
}
}
#[cfg(test)]
mod tests {
use super::S3CopyIfNotExists;
#[test]
fn parse_s3_copy_if_not_exists_header() {
let input = "header: cf-copy-destination-if-none-match: *";
let expected = Some(S3CopyIfNotExists::Header(
"cf-copy-destination-if-none-match".to_owned(),
"*".to_owned(),
));
assert_eq!(expected, S3CopyIfNotExists::from_str(input));
}
#[test]
fn parse_s3_copy_if_not_exists_header_with_status() {
let input = "header-with-status:key:value:403";
let expected = Some(S3CopyIfNotExists::HeaderWithStatus(
"key".to_owned(),
"value".to_owned(),
reqwest::StatusCode::FORBIDDEN,
));
assert_eq!(expected, S3CopyIfNotExists::from_str(input));
}
#[test]
fn parse_s3_copy_if_not_exists_header_whitespace_invariant() {
let expected = Some(S3CopyIfNotExists::Header(
"cf-copy-destination-if-none-match".to_owned(),
"*".to_owned(),
));
const INPUTS: &[&str] = &[
"header:cf-copy-destination-if-none-match:*",
"header: cf-copy-destination-if-none-match:*",
"header: cf-copy-destination-if-none-match: *",
"header : cf-copy-destination-if-none-match: *",
"header : cf-copy-destination-if-none-match : *",
"header : cf-copy-destination-if-none-match : * ",
];
for input in INPUTS {
assert_eq!(expected, S3CopyIfNotExists::from_str(input));
}
}
#[test]
fn parse_s3_copy_if_not_exists_header_with_status_whitespace_invariant() {
let expected = Some(S3CopyIfNotExists::HeaderWithStatus(
"key".to_owned(),
"value".to_owned(),
reqwest::StatusCode::FORBIDDEN,
));
const INPUTS: &[&str] = &[
"header-with-status:key:value:403",
"header-with-status: key:value:403",
"header-with-status: key: value:403",
"header-with-status: key: value: 403",
"header-with-status : key: value: 403",
"header-with-status : key : value: 403",
"header-with-status : key : value : 403",
"header-with-status : key : value : 403 ",
];
for input in INPUTS {
assert_eq!(expected, S3CopyIfNotExists::from_str(input));
}
}
}