This is an automated email from the ASF dual-hosted git repository.

xuanwo pushed a commit to branch xuanwo/layer-capability
in repository https://gitbox.apache.org/repos/asf/opendal.git

commit 589dd6bf9af14e009ec4aac4d2af16dd091bf150
Author: Xuanwo <[email protected]>
AuthorDate: Mon Dec 8 19:20:24 2025 +0800

    refactor: Split capability check to seperate layer
    
    Signed-off-by: Xuanwo <[email protected]>
---
 core/Cargo.lock                                    |  9 +++++
 core/Cargo.toml                                    |  7 ++--
 core/core/Cargo.toml                               |  2 +-
 core/core/src/layers/correctness_check.rs          | 10 +++---
 core/core/src/layers/mod.rs                        |  2 --
 core/layers/capability-check/Cargo.toml            | 33 +++++++++++++++++++
 .../capability-check/src/lib.rs}                   | 38 ++++++++++++++--------
 core/src/lib.rs                                    |  2 ++
 8 files changed, 80 insertions(+), 23 deletions(-)

diff --git a/core/Cargo.lock b/core/Cargo.lock
index 6f80b77a0..ceb7588f0 100644
--- a/core/Cargo.lock
+++ b/core/Cargo.lock
@@ -5477,6 +5477,7 @@ dependencies = [
  "opendal-core",
  "opendal-layer-async-backtrace",
  "opendal-layer-await-tree",
+ "opendal-layer-capability-check",
  "opendal-service-azblob",
  "opendal-service-azdls",
  "opendal-service-azfile",
@@ -5666,6 +5667,14 @@ dependencies = [
  "opendal-core",
 ]
 
+[[package]]
+name = "opendal-layer-capability-check"
+version = "0.55.0"
+dependencies = [
+ "opendal-core",
+ "tokio",
+]
+
 [[package]]
 name = "opendal-service-azblob"
 version = "0.55.0"
diff --git a/core/Cargo.toml b/core/Cargo.toml
index 7c5ecbfc2..28188089b 100644
--- a/core/Cargo.toml
+++ b/core/Cargo.toml
@@ -27,6 +27,7 @@ members = [
   "services/*",
   "layers/*",
   "layers/await-tree",
+  "layers/capability-check",
 ]
 
 [workspace.package]
@@ -64,6 +65,7 @@ internal-path-cache = ["opendal-core/internal-path-cache"]
 internal-tokio-rt = ["opendal-core/internal-tokio-rt"]
 layers-async-backtrace = ["dep:opendal-layer-async-backtrace"]
 layers-await-tree = ["dep:opendal-layer-await-tree"]
+layers-capability-check = ["dep:opendal-layer-capability-check"]
 layers-chaos = ["opendal-core/layers-chaos"]
 layers-dtrace = ["opendal-core/layers-dtrace"]
 layers-fastmetrics = ["opendal-core/layers-fastmetrics"]
@@ -164,12 +166,13 @@ required-features = ["tests"]
 opendal-core = { path = "core", version = "0.55.0", default-features = false }
 opendal-layer-async-backtrace = { path = "layers/async-backtrace", version = 
"0.55.0", optional = true, default-features = false }
 opendal-layer-await-tree = { path = "layers/await-tree", version = "0.55.0", 
optional = true, default-features = false }
-opendal-service-s3 = { path = "services/s3", version = "0.55.0", optional = 
true, default-features = false }
-opendal-service-moka = { path = "services/moka", version = "0.55.0", optional 
= true, default-features = false }
+opendal-layer-capability-check = { path = "layers/capability-check", version = 
"0.55.0", optional = true, default-features = false }
 opendal-service-azblob = { path = "services/azblob", version = "0.55.0", 
optional = true, default-features = false }
 opendal-service-azdls = { path = "services/azdls", version = "0.55.0", 
optional = true, default-features = false }
 opendal-service-azfile = { path = "services/azfile", version = "0.55.0", 
optional = true, default-features = false }
 opendal-service-ghac = { path = "services/ghac", version = "0.55.0", optional 
= true, default-features = false }
+opendal-service-moka = { path = "services/moka", version = "0.55.0", optional 
= true, default-features = false }
+opendal-service-s3 = { path = "services/s3", version = "0.55.0", optional = 
true, default-features = false }
 
 [dev-dependencies]
 anyhow = { version = "1.0.100", features = ["std"] }
diff --git a/core/core/Cargo.toml b/core/core/Cargo.toml
index a8c3d4924..ec7a51f0c 100644
--- a/core/core/Cargo.toml
+++ b/core/core/Cargo.toml
@@ -189,6 +189,7 @@ anyhow = { version = "1.0.100", features = ["std"] }
 backon = { version = "1.6", features = ["tokio-sleep"] }
 base64 = "0.22"
 bytes = "1.6"
+ctor = "0.6"
 futures = { version = "0.3", default-features = false, features = [
   "std",
   "async-await",
@@ -199,7 +200,6 @@ jiff = { version = "0.2.15", features = ["serde"] }
 log = "0.4"
 md-5 = "0.10"
 mea = { version = "0.5.1" }
-ctor = "0.6"
 percent-encoding = "2"
 quick-xml = { version = "0.38", features = ["serialize", "overlapped-lists"] }
 reqwest = { version = "0.12.24", features = [
diff --git a/core/core/src/layers/correctness_check.rs 
b/core/core/src/layers/correctness_check.rs
index 57b8dada4..38e211def 100644
--- a/core/core/src/layers/correctness_check.rs
+++ b/core/core/src/layers/correctness_check.rs
@@ -51,6 +51,11 @@ impl<A: Access> Layer<A> for CorrectnessCheckLayer {
     }
 }
 
+pub struct CorrectnessAccessor<A: Access> {
+    info: Arc<AccessorInfo>,
+    inner: A,
+}
+
 pub(crate) fn new_unsupported_error(info: &AccessorInfo, op: Operation, args: 
&str) -> Error {
     let scheme = info.scheme();
     let op = op.into_static();
@@ -62,11 +67,6 @@ pub(crate) fn new_unsupported_error(info: &AccessorInfo, op: 
Operation, args: &s
     .with_operation(op)
 }
 
-pub struct CorrectnessAccessor<A: Access> {
-    info: Arc<AccessorInfo>,
-    inner: A,
-}
-
 impl<A: Access> Debug for CorrectnessAccessor<A> {
     fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
         f.debug_struct("CorrectnessCheckAccessor")
diff --git a/core/core/src/layers/mod.rs b/core/core/src/layers/mod.rs
index 09ae0d760..85cccd55b 100644
--- a/core/core/src/layers/mod.rs
+++ b/core/core/src/layers/mod.rs
@@ -120,8 +120,6 @@ pub mod observe;
 
 mod correctness_check;
 pub(crate) use correctness_check::CorrectnessCheckLayer;
-mod capability_check;
-pub use capability_check::CapabilityCheckLayer;
 
 mod http_client;
 pub use http_client::HttpClientLayer;
diff --git a/core/layers/capability-check/Cargo.toml 
b/core/layers/capability-check/Cargo.toml
new file mode 100644
index 000000000..9f895090a
--- /dev/null
+++ b/core/layers/capability-check/Cargo.toml
@@ -0,0 +1,33 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+[package]
+description = "Apache OpenDAL capability-check layer"
+edition = "2024"
+license = "Apache-2.0"
+name = "opendal-layer-capability-check"
+repository = "https://github.com/apache/opendal";
+version = "0.55.0"
+
+[package.metadata.docs.rs]
+all-features = true
+
+[dependencies]
+opendal-core = { path = "../../core", version = "0.55.0", default-features = 
false }
+
+[dev-dependencies]
+tokio = { version = "1.48", features = ["macros", "rt"] }
diff --git a/core/core/src/layers/capability_check.rs 
b/core/layers/capability-check/src/lib.rs
similarity index 84%
rename from core/core/src/layers/capability_check.rs
rename to core/layers/capability-check/src/lib.rs
index d84de68df..5d09ef1ae 100644
--- a/core/core/src/layers/capability_check.rs
+++ b/core/layers/capability-check/src/lib.rs
@@ -19,8 +19,8 @@ use std::fmt::Debug;
 use std::fmt::Formatter;
 use std::sync::Arc;
 
-use crate::layers::correctness_check::new_unsupported_error;
-use crate::raw::*;
+use opendal_core::raw::*;
+use opendal_core::{Error, ErrorKind, Result};
 
 /// Add an extra capability check layer for every operation
 ///
@@ -41,13 +41,13 @@ use crate::raw::*;
 /// # examples
 ///
 /// ```no_run
-/// # use opendal_core::layers::CapabilityCheckLayer;
+/// # use opendal_layer_capability_check::CapabilityCheckLayer;
 /// # use opendal_core::services;
 /// # use opendal_core::Operator;
 /// # use opendal_core::Result;
 ///
 /// # fn main() -> Result<()> {
-/// use opendal_core::layers::CapabilityCheckLayer;
+/// use opendal_layer_capability_check::CapabilityCheckLayer;
 /// let _ = Operator::new(services::Memory::default())?
 ///     .layer(CapabilityCheckLayer)
 ///     .finish();
@@ -66,11 +66,23 @@ impl<A: Access> Layer<A> for CapabilityCheckLayer {
         CapabilityAccessor { info, inner }
     }
 }
+
 pub struct CapabilityAccessor<A: Access> {
     info: Arc<AccessorInfo>,
     inner: A,
 }
 
+fn new_unsupported_error(info: &AccessorInfo, op: Operation, args: &str) -> 
Error {
+    let scheme = info.scheme();
+    let op = op.into_static();
+
+    Error::new(
+        ErrorKind::Unsupported,
+        format!("The service {scheme} does not support the operation {op} with 
the arguments {args}. Please verify if the relevant flags have been enabled, or 
submit an issue if you believe this is incorrect."),
+    )
+    .with_operation(op)
+}
+
 impl<A: Access> Debug for CapabilityAccessor<A> {
     fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
         f.debug_struct("CapabilityCheckAccessor")
@@ -90,11 +102,11 @@ impl<A: Access> LayeredAccess for CapabilityAccessor<A> {
         &self.inner
     }
 
-    async fn read(&self, path: &str, args: OpRead) -> crate::Result<(RpRead, 
Self::Reader)> {
+    async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, 
Self::Reader)> {
         self.inner.read(path, args).await
     }
 
-    async fn write(&self, path: &str, args: OpWrite) -> 
crate::Result<(RpWrite, Self::Writer)> {
+    async fn write(&self, path: &str, args: OpWrite) -> 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_error(
@@ -121,11 +133,11 @@ impl<A: Access> LayeredAccess for CapabilityAccessor<A> {
         self.inner.write(path, args).await
     }
 
-    async fn delete(&self) -> crate::Result<(RpDelete, Self::Deleter)> {
+    async fn delete(&self) -> Result<(RpDelete, Self::Deleter)> {
         self.inner.delete().await
     }
 
-    async fn list(&self, path: &str, args: OpList) -> crate::Result<(RpList, 
Self::Lister)> {
+    async fn list(&self, path: &str, args: OpList) -> Result<(RpList, 
Self::Lister)> {
         let capability = self.info.full_capability();
         if !capability.list_with_versions && args.versions() {
             return Err(new_unsupported_error(
@@ -142,9 +154,9 @@ impl<A: Access> LayeredAccess for CapabilityAccessor<A> {
 #[cfg(test)]
 mod tests {
     use super::*;
-    use crate::Capability;
-    use crate::ErrorKind;
-    use crate::Operator;
+    use opendal_core::Capability;
+    use opendal_core::ErrorKind;
+    use opendal_core::Operator;
 
     #[derive(Debug)]
     struct MockService {
@@ -164,11 +176,11 @@ mod tests {
             info.into()
         }
 
-        async fn write(&self, _: &str, _: OpWrite) -> crate::Result<(RpWrite, 
Self::Writer)> {
+        async fn write(&self, _: &str, _: OpWrite) -> Result<(RpWrite, 
Self::Writer)> {
             Ok((RpWrite::new(), Box::new(())))
         }
 
-        async fn list(&self, _: &str, _: OpList) -> crate::Result<(RpList, 
Self::Lister)> {
+        async fn list(&self, _: &str, _: OpList) -> Result<(RpList, 
Self::Lister)> {
             Ok((RpList {}, Box::new(())))
         }
     }
diff --git a/core/src/lib.rs b/core/src/lib.rs
index 64a3c4adc..bc7d813cc 100644
--- a/core/src/lib.rs
+++ b/core/src/lib.rs
@@ -48,4 +48,6 @@ pub mod layers {
     pub use opendal_layer_async_backtrace::*;
     #[cfg(feature = "layers-await-tree")]
     pub use opendal_layer_await_tree::*;
+    #[cfg(feature = "layers-capability-check")]
+    pub use opendal_layer_capability_check::*;
 }

Reply via email to