Skip to content

Commit

Permalink
feat: Mark Scheme non_exhaustive and extendable
Browse files Browse the repository at this point in the history
Signed-off-by: Xuanwo <github@xuanwo.io>
  • Loading branch information
Xuanwo committed Aug 18, 2022
1 parent 447fc7f commit b60e0b6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
1 change: 0 additions & 1 deletion examples/gcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use std::env;

use anyhow::Result;
use log::info;

use opendal::services::gcs::Builder;
use opendal::Operator;

Expand Down
10 changes: 10 additions & 0 deletions src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::HashMap;
use std::env;
use std::io::ErrorKind;
use std::io::Result;
use std::sync::Arc;

use anyhow::anyhow;
use futures::StreamExt;
use futures::TryStreamExt;
use log::debug;

use crate::error::other;
use crate::error::BackendError;
use crate::io_util::BottomUpWalker;
use crate::io_util::TopDownWalker;
use crate::services;
Expand Down Expand Up @@ -130,6 +134,12 @@ impl Operator {
Scheme::Memory => services::memory::Builder::default().build()?.into(),
Scheme::Gcs => services::gcs::Backend::from_iter(it)?.into(),
Scheme::S3 => services::s3::Backend::from_iter(it)?.into(),
Scheme::Custom(v) => {
return Err(other(BackendError::new(
HashMap::default(),
anyhow!("custom service {v} is not supported"),
)))
}
};

Ok(op)
Expand Down
24 changes: 13 additions & 11 deletions src/scheme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,33 @@ use std::fmt::Formatter;
use std::io;
use std::str::FromStr;

use anyhow::anyhow;

use crate::error::other;
use crate::error::BackendError;

/// Backends that OpenDAL supports
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Scheme {
/// [azblob][crate::services::azblob]: Azure Storage Blob services.
Azblob,
/// [fs][crate::services::fs]: POSIX alike file system.
Fs,
/// [gcs][crate::services::gcs]: Google Cloud Storage backend.
Gcs,
/// [hdfs][crate::services::hdfs]: Hadoop Distributed File System.
#[cfg(feature = "services-hdfs")]
Hdfs,
/// [http][crate::services::http]: HTTP backend.
#[cfg(feature = "services-http")]
Http,
/// [gcs][crate::services::gcs]: Google Cloud Storage backend.
Gcs,
/// [memory][crate::services::memory]: In memory backend support.
Memory,
/// [s3][crate::services::s3]: AWS S3 alike services.
S3,
/// Custom that allow users to implement services outside of OpenDAL.
///
/// # NOTE
///
/// - Custom must not overwrite any existing services name.
/// - Custom must be lower cases.
Custom(&'static str),
}

impl Scheme {
Expand Down Expand Up @@ -67,6 +70,7 @@ impl Display for Scheme {
Scheme::Http => write!(f, "http"),
Scheme::Memory => write!(f, "memory"),
Scheme::S3 => write!(f, "s3"),
Scheme::Custom(v) => write!(f, "{v}"),
}
}
}
Expand All @@ -86,10 +90,7 @@ impl FromStr for Scheme {
"gcs" => Ok(Scheme::Gcs),
"memory" => Ok(Scheme::Memory),
"s3" => Ok(Scheme::S3),
v => Err(other(BackendError::new(
Default::default(),
anyhow!("{} is not supported", v),
))),
_ => Ok(Scheme::Custom(Box::leak(s.into_boxed_str()))),
}
}
}
Expand All @@ -106,6 +107,7 @@ impl From<Scheme> for &'static str {
Scheme::Gcs => "gcs",
Scheme::Memory => "memory",
Scheme::S3 => "s3",
Scheme::Custom(v) => v,
}
}
}
5 changes: 2 additions & 3 deletions src/services/gcs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
//! ```
//! ```no_run
//! use anyhow::Result;
//! use opendal::Object;
//! use opendal::Operator;
//! use opendal::Scheme;
//! use opendal::Object;
//!
//! #[tokio::main]
//! async fn main() -> Result<()> {
Expand All @@ -63,10 +63,9 @@
//!
//! ```no_run
//! use anyhow::Result;
//!
//! use opendal::services::gcs;
//! use opendal::Object;
//! use opendal::Operator;
//! use opendal::services::gcs;
//!
//! #[tokio::main]
//! async fn main() -> Result<()> {
Expand Down

1 comment on commit b60e0b6

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for opendal ready!

✅ Preview
https://opendal-qzlfug8m6-databend.vercel.app

Built with commit b60e0b6.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.