Skip to content

Commit

Permalink
0.9: remove deprecated methods, made StorageService trait object safe.
Browse files Browse the repository at this point in the history
  • Loading branch information
auguwu committed Jun 28, 2024
1 parent 963dab4 commit 3356144
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 100 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ resolver = "2"
members = ["crates/*", "remi"]

[workspace.package]
version = "0.8.0"
version = "0.9.0"
repository = "https://github.com/Noelware/remi-rs"
license = "MIT"
edition = "2021"
rust-version = "1.76"

[workspace.dependencies]
bollard = "0.16.1"
testcontainers = "0.18.0"
6 changes: 3 additions & 3 deletions crates/azure/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ azure_storage_blobs = "0.20.0"
bytes = "1.6.0"
futures-util = "0.3.30"
log = { version = "0.4.21", optional = true }
remi = { path = "../../remi", version = "0.8.0" }
remi = { path = "../../remi", version = "0.9.0" }
serde = { version = "1.0.200", features = ["derive"], optional = true }
tracing = { version = "0.1.40", optional = true }

[dev-dependencies]
bollard = "0.16.1"
testcontainers = "0.16.7"
bollard.workspace = true
testcontainers.workspace = true
tokio = { version = "1.37.0", features = ["macros", "rt"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
Expand Down
30 changes: 13 additions & 17 deletions crates/azure/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use azure_storage_blobs::prelude::ContainerClient;
use bytes::Bytes;
use futures_util::StreamExt;
use remi::{Blob, File, ListBlobsRequest, UploadRequest};
use std::{ops::Deref, path::Path, time::SystemTime};
use std::{borrow::Cow, ops::Deref, path::Path, time::SystemTime};

#[derive(Debug, Clone)]
pub struct StorageService {
Expand Down Expand Up @@ -77,7 +77,10 @@ impl Deref for StorageService {
#[async_trait]
impl remi::StorageService for StorageService {
type Error = azure_core::Error;
const NAME: &'static str = "remi:azure";

fn name(&self) -> Cow<'static, str> {
Cow::Borrowed("remi:azure")
}

#[cfg_attr(
feature = "tracing",
Expand Down Expand Up @@ -436,19 +439,18 @@ impl remi::StorageService for StorageService {
}

#[cfg(test)]
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
mod tests {
use crate::{Credential, StorageConfig};
use azure_storage::CloudLocation;
use bollard::Docker;
use remi::{StorageService, UploadRequest};
use testcontainers::{runners::AsyncRunner, GenericImage};
use testcontainers::{runners::AsyncRunner, GenericImage, ImageExt};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

const IMAGE: &str = "mcr.microsoft.com/azure-storage/azurite";

// renovate: image="microsoft-azure-storage-azurite"
const TAG: &str = "3.29.0";
const TAG: &str = "3.31.0";

fn container() -> GenericImage {
GenericImage::new(IMAGE, TAG)
Expand Down Expand Up @@ -488,17 +490,11 @@ mod tests {
.with(tracing_subscriber::fmt::layer())
.set_default();

let container = (
container(),
vec![
From::from("azurite-blob"),
From::from("--blobHost"),
From::from("0.0.0.0"),
],
)
.start()
.await;
let req: ::testcontainers::ContainerRequest<GenericImage> = container()
.with_cmd(["azurite-blob", "--blobHost", "0.0.0.0"])
.into();

let container = req.start().await.expect("failed to start container");
let $storage = crate::StorageService::new(StorageConfig {
container: String::from("test-container"),
credentials: Credential::AccessKey {
Expand All @@ -508,8 +504,8 @@ mod tests {
),
},
location: CloudLocation::Emulator {
address: container.get_host().await.to_string(),
port: container.get_host_port_ipv4(10000).await,
address: container.get_host().await.expect("failed to get host ip for container").to_string(),
port: container.get_host_port_ipv4(10000).await.expect("failed to get mapped port `10000`"),
},
});

Expand Down
2 changes: 1 addition & 1 deletion crates/fs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ dirs = "5.0.1"
file-format = { version = "0.25.0", features = ["reader-txt"], optional = true }
infer = { version = "0.16.0", default-features = false, optional = true }
log = { version = "0.4.21", optional = true }
remi = { path = "../../remi", version = "0.8.0" }
remi = { path = "../../remi", version = "0.9.0" }
serde = { version = "1.0.200", features = ["derive"], optional = true }
serde_json = { version = "1.0.116", optional = true }
serde_yaml = { version = "0.9.34", optional = true }
Expand Down
9 changes: 6 additions & 3 deletions crates/fs/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use crate::{default_resolver, Config, ContentTypeResolver};
use async_trait::async_trait;
use bytes::Bytes;
use remi::{Blob, Directory, File, ListBlobsRequest, StorageService as RemiStorageService, UploadRequest};
use remi::{Blob, Directory, File, ListBlobsRequest, StorageService as _, UploadRequest};
use std::{
borrow::Cow,
io,
Expand Down Expand Up @@ -242,9 +242,12 @@ impl StorageService {
}

#[async_trait]
impl RemiStorageService for StorageService {
impl remi::StorageService for StorageService {
type Error = io::Error;
const NAME: &'static str = "remi:fs";

fn name(&self) -> Cow<'static, str> {
Cow::Borrowed("remi:gridfs")
}

#[cfg_attr(
feature = "tracing",
Expand Down
8 changes: 4 additions & 4 deletions crates/gridfs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ async-trait = "0.1.80"
bytes = "1.6.0"
futures-util = "0.3.30"
log = { version = "0.4.21", optional = true }
mongodb = "2.8.2"
remi = { path = "../../remi", version = "0.8.0" }
mongodb = "3.0.0"
remi = { path = "../../remi", version = "0.9.0" }
serde = { version = "1.0.200", features = ["derive"], optional = true }
tokio-util = "0.7.11"
tracing = { version = "0.1.40", optional = true }

[dev-dependencies]
bollard = "0.16.1"
testcontainers = "0.16.7"
bollard.workspace = true
testcontainers.workspace = true
tokio = { version = "1.37.0", features = ["rt", "macros"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
Expand Down
52 changes: 25 additions & 27 deletions crates/gridfs/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ use bytes::{Bytes, BytesMut};
use futures_util::{AsyncWriteExt, StreamExt};
use mongodb::{
bson::{doc, raw::ValueAccessErrorKind, Bson, Document, RawDocument},
options::{GridFsFindOptions, GridFsUploadOptions},
Client, Database, GridFsBucket,
gridfs::GridFsBucket,
options::GridFsUploadOptions,
Client, Database,
};
use remi::{Blob, File, ListBlobsRequest, UploadRequest};
use std::{collections::HashMap, io, path::Path};
use std::{borrow::Cow, collections::HashMap, io, path::Path};
use tokio_util::{compat::FuturesAsyncReadCompatExt, io::ReaderStream};

fn value_access_err_to_error(error: mongodb::bson::raw::ValueAccessError) -> mongodb::error::Error {
Expand Down Expand Up @@ -176,7 +177,10 @@ impl StorageService {
#[async_trait]
impl remi::StorageService for StorageService {
type Error = mongodb::error::Error;
const NAME: &'static str = "remi:gridfs";

fn name(&self) -> Cow<'static, str> {
Cow::Borrowed("remi:gridfs")
}

#[cfg_attr(
feature = "tracing",
Expand All @@ -198,11 +202,7 @@ impl remi::StorageService for StorageService {
#[cfg(feature = "log")]
::log::info!("opening file [{}]", path);

let mut cursor = self
.bucket
.find(doc! { "filename": &path }, GridFsFindOptions::default())
.await?;

let mut cursor = self.bucket.find(doc! { "filename": &path }).await?;
let advanced = cursor.advance().await?;
if !advanced {
#[cfg(feature = "tracing")]
Expand Down Expand Up @@ -267,12 +267,9 @@ impl remi::StorageService for StorageService {

let mut cursor = self
.bucket
.find(
doc! {
"filename": &path,
},
GridFsFindOptions::default(),
)
.find(doc! {
"filename": &path,
})
.await?;

// has_advanced returns false if there is no entries that have that filename
Expand Down Expand Up @@ -326,7 +323,7 @@ impl remi::StorageService for StorageService {
return Ok(vec![]);
}

let mut cursor = self.bucket.find(doc!(), GridFsFindOptions::default()).await?;
let mut cursor = self.bucket.find(doc!()).await?;
let mut blobs = vec![];
while cursor.advance().await? {
let doc = cursor.current();
Expand Down Expand Up @@ -388,12 +385,9 @@ impl remi::StorageService for StorageService {

let mut cursor = self
.bucket
.find(
doc! {
"filename": &path,
},
GridFsFindOptions::default(),
)
.find(doc! {
"filename": &path,
})
.await?;

// has_advanced returns false if there is no entries that have that filename
Expand Down Expand Up @@ -474,7 +468,7 @@ impl remi::StorageService for StorageService {
.metadata(metadata)
.build();

let mut stream = self.bucket.open_upload_stream(path, Some(opts));
let mut stream = self.bucket.open_upload_stream(path).with_options(opts).await?;
stream.write_all(&options.data[..]).await?;
stream.close().await.map_err(From::from)
}
Expand All @@ -495,7 +489,7 @@ mod tests {
const TAG: &str = "7.0.9";

fn container() -> GenericImage {
GenericImage::new(IMAGE, TAG).with_exposed_port(27017)
GenericImage::new(IMAGE, TAG)
}

#[test]
Expand All @@ -518,7 +512,7 @@ mod tests {
) => {
$(
#[cfg_attr(target_os = "linux", tokio::test)]
#[cfg_attr(not(target_os = "linux"), ignore = "azurite image can be only used on Linux")]
#[cfg_attr(not(target_os = "linux"), ignore = "`mongo` image can be only used on Linux")]
$(#[$meta])*
async fn $name() {
if ::bollard::Docker::connect_with_defaults().is_err() {
Expand All @@ -530,9 +524,13 @@ mod tests {
.with(tracing_subscriber::fmt::layer())
.set_default();

let container = container().start().await;
let container = container().start().await.expect("failed to start container");
let $storage = crate::StorageService::from_conn_string(
format!("mongodb://{}:{}", container.get_host().await,container.get_host_port_ipv4(27017).await),
format!(
"mongodb://{}:{}",
container.get_host().await.expect("failed to get host ip"),
container.get_host_port_ipv4(27017).await.expect("failed to get port mapping: 27017")
),
$crate::StorageConfig {
database: Some(String::from("remi")),
bucket: String::from("fs"),
Expand Down
9 changes: 8 additions & 1 deletion crates/s3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,16 @@ aws-sdk-s3 = { version = "1.25.0", features = ["behavior-version-latest"] }
aws-smithy-runtime-api = "1.5.0"
bytes = "1.6.0"
log = { version = "0.4.21", optional = true }
remi = { path = "../../remi", version = "0.8.0" }
remi = { path = "../../remi", version = "0.9.0" }
serde = { version = "1.0.200", features = ["derive"], optional = true }
tracing = { version = "0.1.40", optional = true }

[dev-dependencies]
bollard.workspace = true
testcontainers.workspace = true
tokio = { version = "1.37.0", features = ["macros", "rt"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"

[package.metadata.docs.rs]
all-features = true
13 changes: 9 additions & 4 deletions crates/s3/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ use aws_sdk_s3::{
Client, Config,
};
use bytes::Bytes;
use remi::{Blob, Directory, File, ListBlobsRequest, StorageService as RemiStorageService, UploadRequest};
use std::path::Path;
use remi::{Blob, Directory, File, ListBlobsRequest, UploadRequest};
use std::{borrow::Cow, path::Path};

const DEFAULT_CONTENT_TYPE: &str = "application/octet; charset=utf-8";

Expand Down Expand Up @@ -82,6 +82,8 @@ impl StorageService {
}

async fn s3_obj_to_blob(&self, entry: &Object) -> crate::Result<Option<Blob>> {
use remi::StorageService;

match entry.key() {
Some(key) if key.ends_with('/') => Ok(Some(Blob::Directory(Directory {
created_at: None,
Expand All @@ -96,13 +98,16 @@ impl StorageService {
}

#[async_trait]
impl RemiStorageService for StorageService {
impl remi::StorageService for StorageService {
// this has to stay `io::Error` since `SdkError` requires too much information
// and this can narrow down.
//
// TODO(@auguwu): this can be a flat error if we could do?
type Error = crate::Error;
const NAME: &'static str = "remi:s3";

fn name(&self) -> Cow<'static, str> {
Cow::Borrowed("remi:s3")
}

#[cfg_attr(
feature = "tracing",
Expand Down
Loading

0 comments on commit 3356144

Please sign in to comment.