Skip to content

Commit

Permalink
remove useless checks
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorgan committed Dec 4, 2024
1 parent d8608c0 commit 3507309
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 441 deletions.
201 changes: 42 additions & 159 deletions core/src/layers/capability_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

use crate::layers::correctness_check::new_unsupported_args_error;
use crate::layers::correctness_check::new_unsupported_error;
use crate::raw::*;
use std::fmt::{Debug, Formatter};
use std::sync::Arc;
Expand Down Expand Up @@ -93,61 +93,40 @@ impl<A: Access> LayeredAccess for CapabilityAccessor<A> {
}

async fn read(&self, path: &str, args: OpRead) -> crate::Result<(RpRead, Self::Reader)> {
let capability = self.info.full_capability();
if !capability.read_with_version && args.version().is_some() {
return Err(new_unsupported_args_error(
self.info.as_ref(),
Operation::Read,
"version",
));
}

self.inner.read(path, args).await
}

async fn write(&self, path: &str, args: OpWrite) -> crate::Result<(RpWrite, Self::Writer)> {
let capability = self.info.full_capability();
if !capability.write_with_content_type && args.content_type().is_some() {
return Err(new_unsupported_args_error(
return Err(new_unsupported_error(
self.info.as_ref(),
Operation::Write,
"content_type",
));
}

self.inner.write(path, args).await
}

async fn stat(&self, path: &str, args: OpStat) -> crate::Result<RpStat> {
let capability = self.info.full_capability();
if !capability.stat_with_version && args.version().is_some() {
return Err(new_unsupported_args_error(
if !capability.write_with_cache_control && args.cache_control().is_some() {
return Err(new_unsupported_error(
self.info.as_ref(),
Operation::Stat,
"version",
Operation::Write,
"cache_control",
));
}

self.inner.stat(path, args).await
}

async fn delete(&self, path: &str, args: OpDelete) -> crate::Result<RpDelete> {
let capability = self.info.full_capability();
if !capability.delete_with_version && args.version().is_some() {
return Err(new_unsupported_args_error(
if !capability.write_with_content_disposition && args.content_disposition().is_some() {
return Err(new_unsupported_error(
self.info.as_ref(),
Operation::Delete,
"version",
Operation::Write,
"content_disposition",
));
}

self.inner.delete(path, args).await
self.inner.write(path, args).await
}

async fn list(&self, path: &str, args: OpList) -> crate::Result<(RpList, Self::Lister)> {
let capability = self.info.full_capability();
if !capability.list_with_version && args.version() {
return Err(new_unsupported_args_error(
return Err(new_unsupported_error(
self.info.as_ref(),
Operation::List,
"version",
Expand All @@ -162,16 +141,7 @@ impl<A: Access> LayeredAccess for CapabilityAccessor<A> {
path: &str,
args: OpRead,
) -> crate::Result<(RpRead, Self::BlockingReader)> {
let capability = self.info.full_capability();
if !capability.read_with_version && args.version().is_some() {
return Err(new_unsupported_args_error(
self.info.as_ref(),
Operation::BlockingRead,
"version",
));
}

self.inner.blocking_read(path, args)
self.inner().blocking_read(path, args)
}

fn blocking_write(
Expand All @@ -181,40 +151,28 @@ impl<A: Access> LayeredAccess for CapabilityAccessor<A> {
) -> crate::Result<(RpWrite, Self::BlockingWriter)> {
let capability = self.info.full_capability();
if !capability.write_with_content_type && args.content_type().is_some() {
return Err(new_unsupported_args_error(
return Err(new_unsupported_error(
self.info.as_ref(),
Operation::BlockingWrite,
"content_type",
));
}

self.inner.blocking_write(path, args)
}

fn blocking_stat(&self, path: &str, args: OpStat) -> crate::Result<RpStat> {
let capability = self.info.full_capability();
if !capability.stat_with_version && args.version().is_some() {
return Err(new_unsupported_args_error(
if !capability.write_with_cache_control && args.cache_control().is_some() {
return Err(new_unsupported_error(
self.info.as_ref(),
Operation::BlockingStat,
"version",
Operation::BlockingWrite,
"cache_control",
));
}

self.inner.blocking_stat(path, args)
}

fn blocking_delete(&self, path: &str, args: OpDelete) -> crate::Result<RpDelete> {
let capability = self.info.full_capability();
if !capability.delete_with_version && args.version().is_some() {
return Err(new_unsupported_args_error(
if !capability.write_with_content_disposition && args.content_disposition().is_some() {
return Err(new_unsupported_error(
self.info.as_ref(),
Operation::BlockingDelete,
"version",
Operation::BlockingWrite,
"content_disposition",
));
}

self.inner.blocking_delete(path, args)
self.inner.blocking_write(path, args)
}

fn blocking_list(
Expand All @@ -224,7 +182,7 @@ impl<A: Access> LayeredAccess for CapabilityAccessor<A> {
) -> crate::Result<(RpList, Self::BlockingLister)> {
let capability = self.info.full_capability();
if !capability.list_with_version && args.version() {
return Err(new_unsupported_args_error(
return Err(new_unsupported_error(
self.info.as_ref(),
Operation::BlockingList,
"version",
Expand All @@ -238,13 +196,7 @@ impl<A: Access> LayeredAccess for CapabilityAccessor<A> {
#[cfg(test)]
mod tests {
use super::*;
use crate::raw::{
oio, OpCopy, OpCreateDir, OpPresign, OpRename, PresignedRequest, RpCopy, RpCreateDir,
RpPresign, RpRename,
};
use crate::{Capability, EntryMode, ErrorKind, Metadata, Operator};
use http::HeaderMap;
use http::Method as HttpMethod;
use crate::{Capability, ErrorKind, Operator};

#[derive(Debug)]
struct MockService {
Expand All @@ -266,45 +218,13 @@ mod tests {
info.into()
}

async fn create_dir(&self, _: &str, _: OpCreateDir) -> crate::Result<RpCreateDir> {
Ok(RpCreateDir {})
}

async fn stat(&self, _: &str, _: OpStat) -> crate::Result<RpStat> {
Ok(RpStat::new(Metadata::new(EntryMode::Unknown)))
}

async fn read(&self, _: &str, _: OpRead) -> crate::Result<(RpRead, Self::Reader)> {
Ok((RpRead::new(), Box::new(bytes::Bytes::new())))
}

async fn write(&self, _: &str, _: OpWrite) -> crate::Result<(RpWrite, Self::Writer)> {
Ok((RpWrite::new(), Box::new(())))
}

async fn delete(&self, _: &str, _: OpDelete) -> crate::Result<RpDelete> {
Ok(RpDelete {})
}

async fn list(&self, _: &str, _: OpList) -> crate::Result<(RpList, Self::Lister)> {
Ok((RpList {}, Box::new(())))
}

async fn copy(&self, _: &str, _: &str, _: OpCopy) -> crate::Result<RpCopy> {
Ok(RpCopy {})
}

async fn rename(&self, _: &str, _: &str, _: OpRename) -> crate::Result<RpRename> {
Ok(RpRename {})
}

async fn presign(&self, _: &str, _: OpPresign) -> crate::Result<RpPresign> {
Ok(RpPresign::new(PresignedRequest::new(
HttpMethod::POST,
"https://example.com/presign".parse().expect("should parse"),
HeaderMap::new(),
)))
}
}

fn new_test_operator(capability: Capability) -> Operator {
Expand All @@ -314,78 +234,41 @@ mod tests {
}

#[tokio::test]
async fn test_read_with() {
async fn test_writer_with() {
let op = new_test_operator(Capability {
read: true,
write: true,
..Default::default()
});
let res = op.read_with("path").version("version").await;
let res = op.writer_with("path").content_type("type").await;
assert!(res.is_err());
assert_eq!(res.unwrap_err().kind(), ErrorKind::Unsupported);

let op = new_test_operator(Capability {
read: true,
read_with_version: true,
..Default::default()
});
let res = op.read_with("path").version("version").await;
assert!(res.is_ok())
}

#[tokio::test]
async fn test_stat_with() {
let op = new_test_operator(Capability {
stat: true,
..Default::default()
});
let res = op.stat_with("path").version("version").await;
let res = op.writer_with("path").cache_control("cache").await;
assert!(res.is_err());
assert_eq!(res.unwrap_err().kind(), ErrorKind::Unsupported);

let op = new_test_operator(Capability {
stat: true,
stat_with_version: true,
..Default::default()
});
let res = op.stat_with("path").version("version").await;
assert!(res.is_ok())
}

#[tokio::test]
async fn test_writer_with() {
let op = new_test_operator(Capability {
write: true,
..Default::default()
});
let res = op.writer_with("path").content_type("type").await;
let res = op
.writer_with("path")
.content_disposition("disposition")
.await;
assert!(res.is_err());

let op = new_test_operator(Capability {
write: true,
write_with_content_type: true,
write_with_cache_control: true,
write_with_content_disposition: true,
..Default::default()
});
let res = op.writer_with("path").content_type("type").await;
assert!(res.is_ok());
}

#[tokio::test]
async fn test_delete_with() {
let op = new_test_operator(Capability {
delete: true,
..Default::default()
});
let res = op.delete_with("path").version("version").await;
assert!(res.is_err());
assert_eq!(res.unwrap_err().kind(), ErrorKind::Unsupported);
let res = op.writer_with("path").cache_control("cache").await;
assert!(res.is_ok());

let op = new_test_operator(Capability {
delete: true,
delete_with_version: true,
..Default::default()
});
let res = op.delete_with("path").version("version").await;
assert!(res.is_ok())
let res = op
.writer_with("path")
.content_disposition("disposition")
.await;
assert!(res.is_ok());
}

#[tokio::test]
Expand Down
4 changes: 2 additions & 2 deletions core/src/layers/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl<A: Access> CompleteAccessor<A> {
return Ok(RpCreateDir::default());
}

return self.inner.create_dir(path, args).await;
self.inner.create_dir(path, args).await
}

fn complete_blocking_create_dir(&self, path: &str, args: OpCreateDir) -> Result<RpCreateDir> {
Expand All @@ -151,7 +151,7 @@ impl<A: Access> CompleteAccessor<A> {
return Ok(RpCreateDir::default());
}

return self.inner.blocking_create_dir(path, args);
self.inner.blocking_create_dir(path, args)
}

async fn complete_stat(&self, path: &str, args: OpStat) -> Result<RpStat> {
Expand Down
Loading

0 comments on commit 3507309

Please sign in to comment.