Replace with option syntaxic sugar

This commit is contained in:
Quentin 2020-11-11 16:12:42 +01:00
parent 045009da9b
commit 5a5592c176
5 changed files with 35 additions and 82 deletions

View file

@ -21,25 +21,20 @@ pub async fn handle_copy(
source_bucket: &str, source_bucket: &str,
source_key: &str, source_key: &str,
) -> Result<Response<Body>, Error> { ) -> Result<Response<Body>, Error> {
let source_object = match garage let source_object = garage
.object_table .object_table
.get(&source_bucket.to_string(), &source_key.to_string()) .get(&source_bucket.to_string(), &source_key.to_string())
.await? .await?
{ .ok_or(Error::NotFound)?;
None => return Err(Error::NotFound),
Some(o) => o,
};
let source_last_v = match source_object let source_last_v = source_object
.versions() .versions()
.iter() .iter()
.rev() .rev()
.filter(|v| v.is_complete()) .filter(|v| v.is_complete())
.next() .next()
{ .ok_or(Error::NotFound)?;
Some(v) => v,
None => return Err(Error::NotFound),
};
let source_last_state = match &source_last_v.state { let source_last_state = match &source_last_v.state {
ObjectVersionState::Complete(x) => x, ObjectVersionState::Complete(x) => x,
_ => unreachable!(), _ => unreachable!(),
@ -69,10 +64,7 @@ pub async fn handle_copy(
.version_table .version_table
.get(&source_last_v.uuid, &EmptyKey) .get(&source_last_v.uuid, &EmptyKey)
.await?; .await?;
let source_version = match source_version { let source_version = source_version.ok_or(Error::NotFound)?;
Some(v) => v,
None => return Err(Error::NotFound),
};
let dest_version = Version::new( let dest_version = Version::new(
new_uuid, new_uuid,

View file

@ -16,17 +16,11 @@ async fn handle_delete_internal(
bucket: &str, bucket: &str,
key: &str, key: &str,
) -> Result<(UUID, UUID), Error> { ) -> Result<(UUID, UUID), Error> {
let object = match garage let object = garage
.object_table .object_table
.get(&bucket.to_string(), &key.to_string()) .get(&bucket.to_string(), &key.to_string())
.await? .await?
{ .ok_or(Error::NotFound)?; // No need to delete
None => {
// No need to delete
return Err(Error::NotFound);
}
Some(o) => o,
};
let interesting_versions = object.versions().iter().filter(|v| match v.state { let interesting_versions = object.versions().iter().filter(|v| match v.state {
ObjectVersionState::Aborted => false, ObjectVersionState::Aborted => false,
@ -43,10 +37,7 @@ async fn handle_delete_internal(
timestamp = std::cmp::max(timestamp, v.timestamp + 1); timestamp = std::cmp::max(timestamp, v.timestamp + 1);
} }
let deleted_version = match must_delete { let deleted_version = must_delete.ok_or(Error::NotFound)?;
None => return Err(Error::NotFound),
Some(v) => v,
};
let version_uuid = gen_uuid(); let version_uuid = gen_uuid();
@ -142,10 +133,8 @@ fn parse_delete_objects_xml(xml: &roxmltree::Document) -> Result<DeleteRequest,
let mut ret = DeleteRequest { objects: vec![] }; let mut ret = DeleteRequest { objects: vec![] };
let root = xml.root(); let root = xml.root();
let delete = match root.first_child() { let delete = root.first_child().ok_or(format!("Delete tag not found"))?;
Some(del) => del,
None => return Err(format!("Delete tag not found")),
};
if !delete.has_tag_name("Delete") { if !delete.has_tag_name("Delete") {
return Err(format!("Invalid root tag: {:?}", root)); return Err(format!("Invalid root tag: {:?}", root));
} }

View file

@ -41,25 +41,20 @@ pub async fn handle_head(
bucket: &str, bucket: &str,
key: &str, key: &str,
) -> Result<Response<Body>, Error> { ) -> Result<Response<Body>, Error> {
let object = match garage let object = garage
.object_table .object_table
.get(&bucket.to_string(), &key.to_string()) .get(&bucket.to_string(), &key.to_string())
.await? .await?
{ .ok_or(Error::NotFound)?;
None => return Err(Error::NotFound),
Some(o) => o,
};
let version = match object let version = object
.versions() .versions()
.iter() .iter()
.rev() .rev()
.filter(|v| v.is_data()) .filter(|v| v.is_data())
.next() .next()
{ .ok_or(Error::NotFound)?;
Some(v) => v,
None => return Err(Error::NotFound),
};
let version_meta = match &version.state { let version_meta = match &version.state {
ObjectVersionState::Complete(ObjectVersionData::Inline(meta, _)) => meta, ObjectVersionState::Complete(ObjectVersionData::Inline(meta, _)) => meta,
ObjectVersionState::Complete(ObjectVersionData::FirstBlock(meta, _)) => meta, ObjectVersionState::Complete(ObjectVersionData::FirstBlock(meta, _)) => meta,
@ -80,25 +75,20 @@ pub async fn handle_get(
bucket: &str, bucket: &str,
key: &str, key: &str,
) -> Result<Response<Body>, Error> { ) -> Result<Response<Body>, Error> {
let object = match garage let object = garage
.object_table .object_table
.get(&bucket.to_string(), &key.to_string()) .get(&bucket.to_string(), &key.to_string())
.await? .await?
{ .ok_or(Error::NotFound)?;
None => return Err(Error::NotFound),
Some(o) => o,
};
let last_v = match object let last_v = object
.versions() .versions()
.iter() .iter()
.rev() .rev()
.filter(|v| v.is_complete()) .filter(|v| v.is_complete())
.next() .next()
{ .ok_or(Error::NotFound)?;
Some(v) => v,
None => return Err(Error::NotFound),
};
let last_v_data = match &last_v.state { let last_v_data = match &last_v.state {
ObjectVersionState::Complete(x) => x, ObjectVersionState::Complete(x) => x,
_ => unreachable!(), _ => unreachable!(),
@ -146,10 +136,7 @@ pub async fn handle_get(
let get_next_blocks = garage.version_table.get(&last_v.uuid, &EmptyKey); let get_next_blocks = garage.version_table.get(&last_v.uuid, &EmptyKey);
let (first_block, version) = futures::try_join!(read_first_block, get_next_blocks)?; let (first_block, version) = futures::try_join!(read_first_block, get_next_blocks)?;
let version = match version { let version = version.ok_or(Error::NotFound)?;
Some(v) => v,
None => return Err(Error::NotFound),
};
let mut blocks = version let mut blocks = version
.blocks() .blocks()

View file

@ -64,10 +64,9 @@ pub async fn handle_list(
} }
let common_prefix = if delimiter.len() > 0 { let common_prefix = if delimiter.len() > 0 {
let relative_key = &object.key[prefix.len()..]; let relative_key = &object.key[prefix.len()..];
match relative_key.find(delimiter) { relative_key
Some(i) => Some(&object.key[..prefix.len() + i + delimiter.len()]), .find(delimiter)
None => None, .and_then(move |i| Some(&object.key[..prefix.len() + i + delimiter.len()]))
}
} else { } else {
None None
}; };

View file

@ -37,10 +37,7 @@ pub async fn handle_put(
let body = req.into_body(); let body = req.into_body();
let mut chunker = BodyChunker::new(body, garage.config.block_size); let mut chunker = BodyChunker::new(body, garage.config.block_size);
let first_block = match chunker.next().await? { let first_block = chunker.next().await?.unwrap_or(vec![]);
Some(x) => x,
None => vec![],
};
let mut object_version = ObjectVersion { let mut object_version = ObjectVersion {
uuid: version_uuid, uuid: version_uuid,
@ -325,14 +322,9 @@ pub async fn handle_put_part(
let (object, first_block) = futures::try_join!(get_object_fut, get_first_block_fut)?; let (object, first_block) = futures::try_join!(get_object_fut, get_first_block_fut)?;
// Check object is valid and multipart block can be accepted // Check object is valid and multipart block can be accepted
let first_block = match first_block { let first_block = first_block.ok_or(Error::BadRequest(format!("Empty body")))?;
None => return Err(Error::BadRequest(format!("Empty body"))), let object = object.ok_or(Error::BadRequest(format!("Object not found")))?;
Some(x) => x,
};
let object = match object {
None => return Err(Error::BadRequest(format!("Object not found"))),
Some(x) => x,
};
if !object if !object
.versions() .versions()
.iter() .iter()
@ -392,10 +384,8 @@ pub async fn handle_complete_multipart_upload(
garage.object_table.get(&bucket, &key), garage.object_table.get(&bucket, &key),
garage.version_table.get(&version_uuid, &EmptyKey), garage.version_table.get(&version_uuid, &EmptyKey),
)?; )?;
let object = match object { let object = object.ok_or(Error::BadRequest(format!("Object not found")))?;
None => return Err(Error::BadRequest(format!("Object not found"))),
Some(x) => x,
};
let object_version = object let object_version = object
.versions() .versions()
.iter() .iter()
@ -408,10 +398,8 @@ pub async fn handle_complete_multipart_upload(
} }
Some(x) => x.clone(), Some(x) => x.clone(),
}; };
let version = match version { let version = version.ok_or(Error::BadRequest(format!("Version not found")))?;
None => return Err(Error::BadRequest(format!("Version not found"))),
Some(x) => x,
};
if version.blocks().len() == 0 { if version.blocks().len() == 0 {
return Err(Error::BadRequest(format!("No data was uploaded"))); return Err(Error::BadRequest(format!("No data was uploaded")));
} }
@ -474,10 +462,8 @@ pub async fn handle_abort_multipart_upload(
.object_table .object_table
.get(&bucket.to_string(), &key.to_string()) .get(&bucket.to_string(), &key.to_string())
.await?; .await?;
let object = match object { let object = object.ok_or(Error::BadRequest(format!("Object not found")))?;
None => return Err(Error::BadRequest(format!("Object not found"))),
Some(x) => x,
};
let object_version = object let object_version = object
.versions() .versions()
.iter() .iter()