Skip to content

Commit

Permalink
💚 Editor errors are avoided by using public enumeration parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
langyo committed Aug 16, 2024
1 parent 9bfa84a commit 968d5a3
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 218 deletions.
68 changes: 33 additions & 35 deletions packages/database/src/init/bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,42 @@ pub enum InitBucketParams {

#[async_trait::async_trait]
#[allow(unused_variables)]
impl Init<Box<dyn BucketStore>> for InitBucketParams {
async fn init(self) -> Result<Box<dyn BucketStore>> {
match self {
InitBucketParams::Cloudflare((env, bucket_name)) => {
#[cfg(feature = "cloudflare")]
{
Ok(Box::new(
tairitsu_database_driver_cloudflare::bucket::init_bucket(env, bucket_name)
.await?,
))
impl Init<Box<crate::prelude::ProxyBucket>> for InitBucketParams {
async fn init(self) -> Result<Box<crate::prelude::ProxyBucket>> {
cfg_if::cfg_if! {
if #[cfg(feature = "cloudflare")] {
match self {
InitBucketParams::Cloudflare((env, bucket_name)) => {
Ok(Box::new(
tairitsu_database_driver_cloudflare::bucket::init_bucket(env, bucket_name)
.await?,
))
}

_ => Err(anyhow!("Only allow one platform at a time")),
}

#[cfg(not(feature = "cloudflare"))]
Err(anyhow!("Cloudflare feature not enabled"))
}

InitBucketParams::Native(bucket_name) => {
#[cfg(feature = "native")]
{
Ok(Box::new(
tairitsu_database_driver_native::bucket::init_bucket(bucket_name).await?,
))
} else if #[cfg(feature = "native")] {
match self {
InitBucketParams::Native(bucket_name) => {
Ok(Box::new(
tairitsu_database_driver_native::bucket::init_bucket(bucket_name).await?,
))
}

_ => Err(anyhow!("Only allow one platform at a time")),
}

#[cfg(not(feature = "native"))]
Err(anyhow!("Native feature not enabled"))
}

InitBucketParams::WASI(bucket_name) => {
#[cfg(feature = "wasi")]
{
Ok(Box::new(
tairitsu_database_driver_wasi::bucket::init_bucket(bucket_name).await?,
))
} else if #[cfg(feature = "wasi")] {
match self {
InitBucketParams::WASI(bucket_name) => {
Ok(Box::new(
tairitsu_database_driver_wasi::bucket::init_bucket(bucket_name).await?,
))
}

_ => Err(anyhow!("Only allow one platform at a time")),
}

#[cfg(not(feature = "wasi"))]
Err(anyhow!("WASI feature not enabled"))
} else {
Err(anyhow!("No platform feature enabled"))
}
}
}
Expand Down
67 changes: 33 additions & 34 deletions packages/database/src/init/kv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,42 @@ pub enum InitKVParams {

#[async_trait::async_trait]
#[allow(unused_variables)]
impl Init<Box<dyn KVStore>> for InitKVParams {
async fn init(self) -> Result<Box<dyn KVStore>> {
match self {
InitKVParams::Cloudflare((env, bucket_name)) => {
#[cfg(feature = "cloudflare")]
{
Ok(Box::new(
tairitsu_database_driver_cloudflare::kv::init_kv(env, bucket_name).await?,
))
impl Init<Box<crate::prelude::ProxyKV>> for InitKVParams {
async fn init(self) -> Result<Box<crate::prelude::ProxyKV>> {
cfg_if::cfg_if! {
if #[cfg(feature = "cloudflare")] {
match self {
InitKVParams::Cloudflare((env, kv_name)) => {
Ok(Box::new(
tairitsu_database_driver_cloudflare::kv::init_kv(env, kv_name)
.await?,
))
}

_ => Err(anyhow!("Only allow one platform at a time")),
}

#[cfg(not(feature = "cloudflare"))]
Err(anyhow!("Cloudflare feature not enabled"))
}

InitKVParams::Native(bucket_name) => {
#[cfg(feature = "native")]
{
Ok(Box::new(
tairitsu_database_driver_native::kv::init_kv(bucket_name).await?,
))
} else if #[cfg(feature = "native")] {
match self {
InitKVParams::Native(kv_name) => {
Ok(Box::new(
tairitsu_database_driver_native::kv::init_kv(kv_name).await?,
))
}

_ => Err(anyhow!("Only allow one platform at a time")),
}

#[cfg(not(feature = "native"))]
Err(anyhow!("Native feature not enabled"))
}

InitKVParams::WASI(bucket_name) => {
#[cfg(feature = "wasi")]
{
Ok(Box::new(
tairitsu_database_driver_wasi::kv::init_kv(bucket_name).await?,
))
} else if #[cfg(feature = "wasi")] {
match self {
InitKVParams::WASI(kv_name) => {
Ok(Box::new(
tairitsu_database_driver_wasi::kv::init_kv(kv_name).await?,
))
}

_ => Err(anyhow!("Only allow one platform at a time")),
}

#[cfg(not(feature = "wasi"))]
Err(anyhow!("WASI feature not enabled"))
} else {
Err(anyhow!("No platform feature enabled"))
}
}
}
Expand Down
63 changes: 31 additions & 32 deletions packages/database/src/init/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,40 @@ pub enum InitSQLParams {
#[allow(unused_variables)]
impl Init<Box<DatabaseConnection>> for InitSQLParams {
async fn init(self) -> Result<Box<DatabaseConnection>> {
match self {
InitSQLParams::Cloudflare((env, bucket_name)) => {
#[cfg(feature = "cloudflare")]
{
Ok(Box::new(
tairitsu_database_driver_cloudflare::sql::init_db(env, bucket_name).await?,
))
cfg_if::cfg_if! {
if #[cfg(feature = "cloudflare")] {
match self {
InitSQLParams::Cloudflare((env, db_name)) => {
Ok(Box::new(
tairitsu_database_driver_cloudflare::sql::init_sql(env, db_name)
.await?,
))
}

_ => Err(anyhow!("Only allow one platform at a time")),
}

#[cfg(not(feature = "cloudflare"))]
Err(anyhow!("Cloudflare feature not enabled"))
}

InitSQLParams::Native(bucket_name) => {
#[cfg(feature = "native")]
{
Ok(Box::new(
tairitsu_database_driver_native::sql::init_db(bucket_name).await?,
))
} else if #[cfg(feature = "native")] {
match self {
InitSQLParams::Native(db_name) => {
Ok(Box::new(
tairitsu_database_driver_native::sql::init_sql(db_name).await?,
))
}

_ => Err(anyhow!("Only allow one platform at a time")),
}

#[cfg(not(feature = "native"))]
Err(anyhow!("Native feature not enabled"))
}

InitSQLParams::WASI => {
#[cfg(feature = "wasi")]
{
Ok(Box::new(
tairitsu_database_driver_wasi::sql::init_db().await?,
))
} else if #[cfg(feature = "wasi")] {
match self {
InitSQLParams::WASI => {
Ok(Box::new(
tairitsu_database_driver_wasi::sql::init_sql().await?,
))
}

_ => Err(anyhow!("Only allow one platform at a time")),
}

#[cfg(not(feature = "wasi"))]
Err(anyhow!("WASI feature not enabled"))
} else {
Err(anyhow!("No platform feature enabled"))
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/database/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ pub mod prelude {
pub use super::init::*;
pub use tairitsu_database_types::providers::{bucket::*, kv::*};

pub async fn init_bucket(param: impl Into<InitBucketParams>) -> Result<Box<dyn BucketStore>> {
pub async fn init_bucket(param: impl Into<InitBucketParams>) -> Result<Box<ProxyBucket>> {
let param: InitBucketParams = param.into();
param.init().await
}

pub async fn init_kv(param: impl Into<InitKVParams>) -> Result<Box<dyn KVStore>> {
pub async fn init_kv(param: impl Into<InitKVParams>) -> Result<Box<ProxyKV>> {
let param: InitKVParams = param.into();
param.init().await
}
Expand Down
20 changes: 20 additions & 0 deletions packages/database/src/mock/bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,24 @@ impl BucketStore for ProxyBucket {
async fn delete(&self, _key: String) -> Result<()> {
unimplemented!()
}

async fn create_multipart_upload(&self) -> Result<String> {
todo!()
}

async fn append_multipart_upload(&self, _upload_id: String, _data: Bytes) -> Result<()> {
todo!()
}

async fn complete_multipart_upload(
&self,
_upload_id: String,
_final_data_key: Option<String>,
) -> Result<BucketMultipartUploadResult> {
todo!()
}

async fn abort_multipart_upload(&self, _upload_id: String) -> Result<()> {
todo!()
}
}
33 changes: 16 additions & 17 deletions packages/database_driver_cloudflare/src/bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ impl BucketStore for ProxyBucket {
async fn set(&self, key: String, value: Bytes) -> Result<()> {
let env = self.env.bucket(self.bucket_name.as_str())?;

SendFuture::new(async move {
let _ = env
.put(key.to_string().as_str(), worker::Data::Bytes(value.into()))
let _ = SendFuture::new(async move {
env.put(key.to_string().as_str(), worker::Data::Bytes(value.into()))
.execute()
.await
.map_err(|err| anyhow!("Failed to set key-value pair: {:?}", err));
.map_err(|err| anyhow!("Failed to set key-value pair: {:?}", err))
})
.await;
.await?;

Ok(())
}
Expand All @@ -49,9 +48,9 @@ impl BucketStore for ProxyBucket {
Err(err) => Err(anyhow!("Failed to get key-value pair: {:?}", err)),
}
})
.await;
.await?;

ret
Ok(ret)
}

async fn delete(&self, key: String) -> Result<()> {
Expand All @@ -62,9 +61,9 @@ impl BucketStore for ProxyBucket {
.await
.map_err(|err| anyhow!("Failed to delete key-value pair: {:?}", err))
})
.await;
.await?;

ret
Ok(ret)
}

async fn create_multipart_upload(&self) -> Result<String> {
Expand All @@ -90,9 +89,9 @@ impl BucketStore for ProxyBucket {
Err(err) => Err(anyhow!("Failed to create multipart upload: {:?}", err)),
}
})
.await;
.await?;

ret
Ok(ret)
}

async fn append_multipart_upload(&self, upload_id: String, data: Bytes) -> Result<()> {
Expand Down Expand Up @@ -148,9 +147,9 @@ impl BucketStore for ProxyBucket {
Err(err) => Err(anyhow!("Failed to resume multipart upload: {:?}", err)),
}
})
.await;
.await?;

ret
Ok(ret)
}

async fn complete_multipart_upload(
Expand Down Expand Up @@ -231,9 +230,9 @@ impl BucketStore for ProxyBucket {
Err(err) => Err(anyhow!("Failed to resume multipart upload: {:?}", err)),
}
})
.await;
.await?;

ret
Ok(ret)
}

async fn abort_multipart_upload(&self, upload_id: String) -> Result<()> {
Expand All @@ -259,9 +258,9 @@ impl BucketStore for ProxyBucket {
Err(err) => Err(anyhow!("Failed to resume multipart upload: {:?}", err)),
}
})
.await;
.await?;

ret
Ok(ret)
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/database_driver_cloudflare/src/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ impl ProxyDatabaseTrait for ProxyDb {
}
}

pub async fn init_db(env: Arc<Env>, db_name: impl ToString) -> Result<DatabaseConnection> {
pub async fn init_sql(env: Arc<Env>, db_name: impl ToString) -> Result<DatabaseConnection> {
let db = Database::connect_proxy(
DbBackend::Sqlite,
Arc::new(Box::new(ProxyDb {
Expand Down
20 changes: 20 additions & 0 deletions packages/database_driver_native/src/bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@ impl BucketStore for ProxyBucket {
async fn delete(&self, _key: String) -> Result<()> {
todo!()
}

async fn create_multipart_upload(&self) -> Result<String> {
todo!()
}

async fn append_multipart_upload(&self, _upload_id: String, _data: Bytes) -> Result<()> {
todo!()
}

async fn complete_multipart_upload(
&self,
_upload_id: String,
_final_data_key: Option<String>,
) -> Result<BucketMultipartUploadResult> {
todo!()
}

async fn abort_multipart_upload(&self, _upload_id: String) -> Result<()> {
todo!()
}
}

pub async fn init_bucket(path: impl ToString) -> Result<ProxyBucket> {
Expand Down
Loading

0 comments on commit 968d5a3

Please sign in to comment.