mirror of
https://github.com/deuxfleurs-org/garage.git
synced 2026-08-06 12:57:41 +00:00
refactor: remove unnecessarily wrap value into a Result
this simplify code and remove some unwrap. warning: this function's return value is unnecessarily wrapped by `Result` help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.93.0/index.html#unnecessary_wraps
This commit is contained in:
+8
-8
@@ -126,7 +126,7 @@ fn handle_http_precondition(
|
||||
) -> Result<Option<Response<ResBody>>, Error> {
|
||||
let precondition_headers = PreconditionHeaders::parse(req)?;
|
||||
|
||||
if let Some(status_code) = precondition_headers.check(version, &version_meta.etag)? {
|
||||
if let Some(status_code) = precondition_headers.check(version, &version_meta.etag) {
|
||||
let mut response = object_headers(
|
||||
version,
|
||||
version_meta,
|
||||
@@ -877,7 +877,7 @@ impl PreconditionHeaders {
|
||||
})
|
||||
}
|
||||
|
||||
fn check(&self, v: &ObjectVersion, etag: &str) -> Result<Option<StatusCode>, Error> {
|
||||
fn check(&self, v: &ObjectVersion, etag: &str) -> Option<StatusCode> {
|
||||
// we store date with ms precision, but headers are precise to the second: truncate
|
||||
// the timestamp to handle the same-second edge case
|
||||
let v_date = UNIX_EPOCH + Duration::from_secs(v.timestamp / 1000);
|
||||
@@ -887,32 +887,32 @@ impl PreconditionHeaders {
|
||||
if let Some(im) = &self.if_match {
|
||||
// Step 1: if-match is present
|
||||
if !im.iter().any(|x| x == etag || x == "*") {
|
||||
return Ok(Some(StatusCode::PRECONDITION_FAILED));
|
||||
return Some(StatusCode::PRECONDITION_FAILED);
|
||||
}
|
||||
} else if let Some(ius) = &self.if_unmodified_since {
|
||||
// Step 2: if-unmodified-since is present, and if-match is absent
|
||||
if v_date > *ius {
|
||||
return Ok(Some(StatusCode::PRECONDITION_FAILED));
|
||||
return Some(StatusCode::PRECONDITION_FAILED);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(inm) = &self.if_none_match {
|
||||
// Step 3: if-none-match is present
|
||||
if inm.iter().any(|x| x == etag || x == "*") {
|
||||
return Ok(Some(StatusCode::NOT_MODIFIED));
|
||||
return Some(StatusCode::NOT_MODIFIED);
|
||||
}
|
||||
} else if let Some(ims) = &self.if_modified_since {
|
||||
// Step 4: if-modified-since is present, and if-none-match is absent
|
||||
if v_date <= *ims {
|
||||
return Ok(Some(StatusCode::NOT_MODIFIED));
|
||||
return Some(StatusCode::NOT_MODIFIED);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
None
|
||||
}
|
||||
|
||||
pub(crate) fn check_copy_source(&self, v: &ObjectVersion, etag: &str) -> Result<(), Error> {
|
||||
match self.check(v, etag)? {
|
||||
match self.check(v, etag) {
|
||||
Some(_) => Err(Error::PreconditionFailed),
|
||||
None => Ok(()),
|
||||
}
|
||||
|
||||
+9
-11
@@ -296,7 +296,7 @@ pub async fn handle_list_parts(
|
||||
},
|
||||
);
|
||||
|
||||
let (info, next) = fetch_part_info(query, &mpu)?;
|
||||
let (info, next) = fetch_part_info(query, &mpu);
|
||||
|
||||
let result = s3_xml::ListPartsResult {
|
||||
xmlns: (),
|
||||
@@ -526,7 +526,7 @@ where
|
||||
fn fetch_part_info<'a>(
|
||||
query: &ListPartsQuery,
|
||||
mpu: &'a MultipartUpload,
|
||||
) -> Result<(Vec<PartInfo<'a>>, Option<u64>), Error> {
|
||||
) -> (Vec<PartInfo<'a>>, Option<u64>) {
|
||||
assert!((1..=1000).contains(&query.max_parts)); // see s3/api_server.rs
|
||||
|
||||
// Parse multipart upload part list, removing parts not yet finished
|
||||
@@ -565,10 +565,10 @@ fn fetch_part_info<'a>(
|
||||
if parts.len() > query.max_parts as usize {
|
||||
parts.truncate(query.max_parts as usize);
|
||||
let pagination = Some(parts.last().unwrap().part_number);
|
||||
return Ok((parts, pagination));
|
||||
return (parts, pagination);
|
||||
}
|
||||
|
||||
Ok((parts, None))
|
||||
(parts, None)
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -1255,7 +1255,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fetch_part_info() -> Result<(), Error> {
|
||||
fn test_fetch_part_info() {
|
||||
let mut query = ListPartsQuery {
|
||||
bucket_name: "a".to_string(),
|
||||
key: "a".to_string(),
|
||||
@@ -1267,7 +1267,7 @@ mod tests {
|
||||
let mpu = mpu();
|
||||
|
||||
// Start from the beginning but with limited size to trigger pagination
|
||||
let (info, pagination) = fetch_part_info(&query, &mpu)?;
|
||||
let (info, pagination) = fetch_part_info(&query, &mpu);
|
||||
assert_eq!(pagination.unwrap(), 3);
|
||||
assert_eq!(
|
||||
info,
|
||||
@@ -1291,7 +1291,7 @@ mod tests {
|
||||
|
||||
// Use previous pagination to make a new request
|
||||
query.part_number_marker = Some(pagination.unwrap());
|
||||
let (info, pagination) = fetch_part_info(&query, &mpu)?;
|
||||
let (info, pagination) = fetch_part_info(&query, &mpu);
|
||||
assert!(pagination.is_none());
|
||||
assert_eq!(
|
||||
info,
|
||||
@@ -1315,14 +1315,14 @@ mod tests {
|
||||
|
||||
// Trying to access a part that is way larger than registered ones
|
||||
query.part_number_marker = Some(9999);
|
||||
let (info, pagination) = fetch_part_info(&query, &mpu)?;
|
||||
let (info, pagination) = fetch_part_info(&query, &mpu);
|
||||
assert!(pagination.is_none());
|
||||
assert_eq!(info, vec![]);
|
||||
|
||||
// Try without any limitation
|
||||
query.max_parts = 1000;
|
||||
query.part_number_marker = None;
|
||||
let (info, pagination) = fetch_part_info(&query, &mpu)?;
|
||||
let (info, pagination) = fetch_part_info(&query, &mpu);
|
||||
assert!(pagination.is_none());
|
||||
assert_eq!(
|
||||
info,
|
||||
@@ -1357,7 +1357,5 @@ mod tests {
|
||||
},
|
||||
]
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user