From 2158772a4d94ea45c1168f09804f836c3dd7bd6b Mon Sep 17 00:00:00 2001 From: suyanhanx Date: Thu, 13 Jul 2023 16:35:31 +0800 Subject: [PATCH] feat(core): delete_with on blocking operator Signed-off-by: suyanhanx --- core/src/types/operator/blocking_operator.rs | 36 +++++++++++++++++-- core/src/types/operator/operator_functions.rs | 19 ++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/core/src/types/operator/blocking_operator.rs b/core/src/types/operator/blocking_operator.rs index 57144817e5b8..e6badc7211d0 100644 --- a/core/src/types/operator/blocking_operator.rs +++ b/core/src/types/operator/blocking_operator.rs @@ -673,11 +673,43 @@ impl BlockingOperator { /// # } /// ``` pub fn delete(&self, path: &str) -> Result<()> { + self.delete_with(path).call()?; + + Ok(()) + } + + /// Delete given path with options. + /// + /// # Notes + /// + /// - Delete not existing error won't return errors. + /// + /// # Examples + /// + /// ```no_run + /// # use anyhow::Result; + /// # use futures::io; + /// # use opendal::BlockingOperator; + /// # fn test(op: BlockingOperator) -> Result<()> { + /// let _ = op + /// .delete_with("path/to/file") + /// .version("example_version").call()?; + /// # Ok(()) + /// # } + /// ``` + pub fn delete_with(&self, path: &str) -> FunctionDelete { let path = normalize_path(path); - let _ = self.inner().blocking_delete(&path, OpDelete::new())?; + FunctionDelete(OperatorFunction::new( + self.inner().clone(), + path, + OpDelete::new(), + |inner, path, args| { + let _ = inner.blocking_delete(&path, args)?; - Ok(()) + Ok(()) + }, + )) } /// remove will remove files via the given paths. diff --git a/core/src/types/operator/operator_functions.rs b/core/src/types/operator/operator_functions.rs index 282d097c22fc..0d97e2fedab0 100644 --- a/core/src/types/operator/operator_functions.rs +++ b/core/src/types/operator/operator_functions.rs @@ -95,3 +95,22 @@ impl FunctionWrite { self.0.call() } } + +/// Function that generated by [`BlockingOperator::delete_with`]. +/// +/// Users can add more options by public functions provided by this struct. +pub struct FunctionDelete(pub(crate) OperatorFunction); + +impl FunctionDelete { + /// Set the version for this operation. + pub fn version(mut self, v: &str) -> Self { + self.0 = self.0.map_args(|args| args.with_version(v)); + self + } + + /// Call the function to consume all the input and generate a + /// result. + pub fn call(self) -> Result<()> { + self.0.call() + } +}