Skip to content

Commit

Permalink
Update noelware-remi crates to v0.5.0, added assertion tests in `no…
Browse files Browse the repository at this point in the history
…elware-log`
  • Loading branch information
auguwu committed Jan 16, 2024
1 parent 35f5144 commit e88075f
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 47 deletions.
32 changes: 30 additions & 2 deletions crates/log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ where
/// [`WriteFn`] to go alongside with this type.
pub struct WriteLayer {
writer: RwLock<Box<dyn Write + Send + Sync>>,
write_fn: Option<Box<dyn WriteFn>>,
write_fn: Option<Box<dyn WriteFn + Send + Sync>>,
}

impl WriteLayer {
Expand All @@ -74,7 +74,10 @@ impl WriteLayer {
}

/// Creates a new [`WriteLayer`] with a specified [`WriteFn`].
pub fn new_with<W: Write + Send + Sync + 'static, F: WriteFn + 'static>(writer: W, fn_: F) -> WriteLayer {
pub fn new_with<W: Write + Send + Sync + 'static, F: WriteFn + Send + Sync + 'static>(
writer: W,
fn_: F,
) -> WriteLayer {
WriteLayer {
writer: RwLock::new(Box::new(writer)),
write_fn: Some(Box::new(fn_)),
Expand Down Expand Up @@ -150,3 +153,28 @@ impl<S: Subscriber + for<'l> LookupSpan<'l>> Layer<S> for WriteLayer {
}
}
}

#[cfg(test)]
mod tests {
use crate::WriteLayer;
use std::io;
use tracing::Dispatch;
use tracing_subscriber::{layer::SubscriberExt, registry, Layer, Registry};

fn __assert_is_layer<S>(_: &dyn Layer<S>) {
/* no body here */
}

fn __assert_is_dispatchable(_: impl Into<Dispatch>) {
/* no body here :3 */
}

#[test]
fn assertions() {
__assert_is_layer::<Registry>(&WriteLayer::new(io::stdout()));
__assert_is_dispatchable(registry().with(WriteLayer::new(io::stdout())));

#[cfg(feature = "writers")]
__assert_is_dispatchable(registry().with(WriteLayer::new_with(io::stdout(), crate::writers::default)))
}
}
35 changes: 22 additions & 13 deletions crates/remi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,35 @@ rust-version.workspace = true
default = []
file-format = ["remi-fs/file-format"]
async-std = ["remi-fs/async-std"]
#serde_json = ["remi-fs/serde_json"]
#serde_yaml = ["remi-fs/serde_yaml"]
#tracing = ["remi-fs/tracing", "remi-s3/tracing", "remi-gcs/tracing", "remi-azure/tracing", "remi-gridfs/tracing"]
serde_json = ["remi-fs/serde_json"]
serde_yaml = ["remi-fs/serde_yaml"]
tracing = [
"remi-fs/tracing",
"remi-s3/tracing",
"remi-azure/tracing",
"remi-gridfs/tracing",
]
gridfs = ["dep:remi-gridfs"]
#azure = ["dep:remi-azure"]
#serde = ["dep:serde", "remi-fs/serde", "remi-s3/serde", "remi-gcs/serde", "remi-azure/serde", "remi-gridfs/serde"]
serde = ["dep:serde", "remi-fs/serde", "remi-s3/serde", "remi-gridfs/serde"]
#log = ["remi-fs/log", "remi-s3/log", "remi-gcs/log", "remi-azure/log", "remi-gridfs/log"]
log = ["remi-fs/log", "remi-s3/log"]
azure = ["dep:remi-azure"]
serde = [
"dep:serde",
"remi-fs/serde",
"remi-s3/serde",
"remi-azure/serde",
"remi-gridfs/serde",
]
log = ["remi-fs/log", "remi-s3/log", "remi-azure/log", "remi-gridfs/log"]
all = ["gridfs", "s3", "fs"]
#gcs = ["dep:remi-gcs"]
s3 = ["dep:remi-s3"]
fs = ["dep:remi-fs"]

[dependencies]
bytes = "1.5.0"
remi-core = "0.4.3"
remi-fs = { version = "0.4.3", optional = true }
remi-gridfs = { version = "0.4.3", optional = true }
remi-s3 = { version = "0.4.3", optional = true }
remi = "0.5.0"
remi-azure = { version = "0.5.0", optional = true }
remi-fs = { version = "0.5.0", optional = true }
remi-gridfs = { version = "0.5.0", optional = true }
remi-s3 = { version = "0.5.0", optional = true }
serde = { version = "1.0.195", optional = true }

[package.metadata.docs.rs]
Expand Down
9 changes: 3 additions & 6 deletions crates/remi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Enables the use to detect JSON files when resolving content types. This implicit
Enables the use to detect YAML files when resolving content types. This implicitilly enables the `serde_yaml` feature in [`remi-fs`](https://docs.rs/remi-fs).

### `tracing` (disabled by default)
Enables the use of [`tracing`](https://docs.rs/tracing) for logging and instrumentation. This will add spans to each method on the [`StorageService`](https://docs.rs/remi) trait when enabled. This implicitilly enables the `tracing` feature in [`remi-fs`](https://docs.rs/remi-fs), [`remi-s3`](https://docs.rs/remi-s3), [`remi-gcs`](https://docs.rs/remi-gcs), [`remi-gridfs`](https://docs.rs/remi-gridfs), and [`remi-azure`](https://docs.rs/remi-azure) crates.
Enables the use of [`tracing`](https://docs.rs/tracing) for logging and instrumentation. This will add spans to each method on the [`StorageService`](https://docs.rs/remi) trait when enabled. This implicitilly enables the `tracing` feature in [`remi-fs`](https://docs.rs/remi-fs), [`remi-s3`](https://docs.rs/remi-s3), [`remi-gridfs`](https://docs.rs/remi-gridfs), and [`remi-azure`](https://docs.rs/remi-azure) crates.

### `gridfs` (disabled by default)
Enables [`remi-gridfs`](https://docs.rs/remi-gridfs) in the `StorageService` enum of this crate.
Expand All @@ -47,17 +47,14 @@ Enables [`remi-gridfs`](https://docs.rs/remi-gridfs) in the `StorageService` enu
Enables [`remi-azure`](https://docs.rs/remi-azure) in the `StorageService` enum of this crate.

### `serde` (disabled by default)
Enables the use of [`serde`](https://docs.rs/serde) to serialize and deserialize configuration structs that each Remi-based crate. This will enable the use of `serde` on the `Config` enum of this crate and implicitilly enable the `serde` feature in [`remi-fs`](https://docs.rs/remi-fs), [`remi-s3`](https://docs.rs/remi-s3), [`remi-gcs`](https://docs.rs/remi-gcs), [`remi-gridfs`](https://docs.rs/remi-gridfs), and [`remi-azure`](https://docs.rs/remi-azure) crates.
Enables the use of [`serde`](https://docs.rs/serde) to serialize and deserialize configuration structs that each Remi-based crate. This will enable the use of `serde` on the `Config` enum of this crate and implicitilly enable the `serde` feature in [`remi-fs`](https://docs.rs/remi-fs), [`remi-s3`](https://docs.rs/remi-s3), [`remi-gridfs`](https://docs.rs/remi-gridfs), and [`remi-azure`](https://docs.rs/remi-azure) crates.

### `log` (disabled by default)
Enables the use of [`log`](https://docs.rs/log) to provide logging statements that might be useful. This will implicitilly enable the `log` feature in [`remi-fs`](https://docs.rs/remi-fs), [`remi-s3`](https://docs.rs/remi-s3), [`remi-gcs`](https://docs.rs/remi-gcs), [`remi-gridfs`](https://docs.rs/remi-gridfs), and [`remi-azure`](https://docs.rs/remi-azure) crates.
Enables the use of [`log`](https://docs.rs/log) to provide logging statements that might be useful. This will implicitilly enable the `log` feature in [`remi-fs`](https://docs.rs/remi-fs), [`remi-s3`](https://docs.rs/remi-s3), [`remi-gridfs`](https://docs.rs/remi-gridfs), and [`remi-azure`](https://docs.rs/remi-azure) crates.

### `all` (disabled by default)
Enables the `gridfs`, `azure`, `gcs`, `s3`, and `fs` features.

### `gcs` (disabled by default)
Enables [`remi-gcs`](https://docs.rs/remi-gcs) in the `StorageService` enum of this crate.

### `s3` (disabled by default)
Enables [`remi-s3`](https://docs.rs/remi-s3) in the `StorageService` enum of this crate.

Expand Down
68 changes: 42 additions & 26 deletions crates/remi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@
#![cfg_attr(docsrs, feature(doc_cfg))]
#![allow(deprecated)] // all #[deprecated] are all the non exhaustive types

pub use remi_core as remi;
pub use remi;

#[cfg(feature = "gridfs")]
#[cfg_attr(docsrs, doc(cfg(feature = "gridfs")))]
pub use remi_gridfs as gridfs;

#[cfg(feature = "azure")]
#[cfg_attr(docsrs, doc(cfg(feature = "azure")))]
pub use remi_azure as azure;

#[cfg(feature = "s3")]
#[cfg_attr(docsrs, doc(cfg(feature = "s3")))]
pub use remi_s3 as s3;
Expand All @@ -38,10 +42,8 @@ pub use remi_s3 as s3;
#[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
pub use remi_fs as fs;

use bytes::Bytes;

#[allow(unused)]
use remi_core::{async_trait, Blob, ListBlobsRequest, StorageService as _, UploadRequest};
use remi::{async_trait, Blob, Bytes, ListBlobsRequest, StorageService as _, UploadRequest};
use std::{io::Result, path::Path};

/// Union-like enum for [`StorageService`].
Expand All @@ -55,6 +57,9 @@ pub enum StorageService {
#[cfg(feature = "gridfs")]
GridFS(remi_gridfs::GridfsStorageService),

#[cfg(feature = "azure")]
Azure(remi_azure::StorageService),

#[cfg(feature = "s3")]
S3(remi_s3::S3StorageService),

Expand All @@ -65,21 +70,8 @@ pub enum StorageService {

#[async_trait]
#[allow(unused)]
impl remi_core::StorageService for StorageService {
fn name(self) -> &'static str {
match self {
#[cfg(feature = "fs")]
Self::Filesystem(fs) => fs.name(),

#[cfg(feature = "gridfs")]
Self::GridFS(gridfs) => gridfs.name(),

#[cfg(feature = "s3")]
Self::S3(s3) => s3.name(),

_ => "remi:disabled",
}
}
impl remi::StorageService for StorageService {
const NAME: &'static str = "noelware:remi";

async fn init(&self) -> Result<()> {
match self {
Expand All @@ -89,46 +81,55 @@ impl remi_core::StorageService for StorageService {
#[cfg(feature = "gridfs")]
Self::GridFS(gridfs) => gridfs.init().await,

#[cfg(feature = "azure")]
Self::Azure(azure) => azure.init().await,

#[cfg(feature = "s3")]
Self::S3(s3) => s3.init().await,

_ => Ok(()),
}
}

async fn open(&self, path: impl AsRef<Path> + Send) -> Result<Option<Bytes>> {
async fn open<P: AsRef<Path> + Send>(&self, path: P) -> Result<Option<Bytes>> {
match self {
#[cfg(feature = "fs")]
Self::Filesystem(fs) => fs.open(path).await,

#[cfg(feature = "gridfs")]
Self::GridFS(gridfs) => gridfs.open(path).await,

#[cfg(feature = "azure")]
Self::Azure(azure) => azure.open(path).await,

#[cfg(feature = "s3")]
Self::S3(s3) => s3.open(path).await,

_ => Ok(None),
}
}

async fn blob(&self, path: impl AsRef<Path> + Send) -> Result<Option<Blob>> {
async fn blob<P: AsRef<Path> + Send>(&self, path: P) -> Result<Option<Blob>> {
match self {
#[cfg(feature = "fs")]
Self::Filesystem(fs) => fs.blob(path).await,

#[cfg(feature = "gridfs")]
Self::GridFS(gridfs) => gridfs.blob(path).await,

#[cfg(feature = "azure")]
Self::Azure(azure) => azure.blob(path).await,

#[cfg(feature = "s3")]
Self::S3(s3) => s3.blob(path).await,

_ => Ok(None),
}
}

async fn blobs(
async fn blobs<P: AsRef<Path> + Send>(
&self,
path: Option<impl AsRef<Path> + Send>,
path: Option<P>,
options: Option<ListBlobsRequest>,
) -> Result<Vec<Blob>> {
match self {
Expand All @@ -138,51 +139,63 @@ impl remi_core::StorageService for StorageService {
#[cfg(feature = "gridfs")]
Self::GridFS(gridfs) => gridfs.blobs(path, options).await,

#[cfg(feature = "azure")]
Self::Azure(azure) => azure.blobs(path, options).await,

#[cfg(feature = "s3")]
Self::S3(s3) => s3.blobs(path, options).await,

_ => Ok(vec![]),
}
}

async fn delete(&self, path: impl AsRef<Path> + Send) -> Result<()> {
async fn delete<P: AsRef<Path> + Send>(&self, path: P) -> Result<()> {
match self {
#[cfg(feature = "fs")]
Self::Filesystem(fs) => fs.delete(path).await,

#[cfg(feature = "gridfs")]
Self::GridFS(gridfs) => gridfs.delete(path).await,

#[cfg(feature = "azure")]
Self::Azure(azure) => azure.delete(path).await,

#[cfg(feature = "s3")]
Self::S3(s3) => s3.delete(path).await,

_ => Ok(()),
}
}

async fn exists(&self, path: impl AsRef<Path> + Send) -> Result<bool> {
async fn exists<P: AsRef<Path> + Send>(&self, path: P) -> Result<bool> {
match self {
#[cfg(feature = "fs")]
Self::Filesystem(fs) => fs.exists(path).await,

#[cfg(feature = "gridfs")]
Self::GridFS(gridfs) => gridfs.exists(path).await,

#[cfg(feature = "azure")]
Self::Azure(azure) => azure.exists(path).await,

#[cfg(feature = "s3")]
Self::S3(s3) => s3.exists(path).await,

_ => Ok(false),
}
}

async fn upload(&self, path: impl AsRef<Path> + Send, options: UploadRequest) -> Result<()> {
async fn upload<P: AsRef<Path> + Send>(&self, path: P, options: UploadRequest) -> Result<()> {
match self {
#[cfg(feature = "fs")]
Self::Filesystem(fs) => fs.upload(path, options).await,

#[cfg(feature = "gridfs")]
Self::GridFS(gridfs) => gridfs.upload(path, options).await,

#[cfg(feature = "azure")]
Self::Azure(azure) => azure.upload(path, options).await,

#[cfg(feature = "s3")]
Self::S3(s3) => s3.upload(path, options).await,

Expand All @@ -205,6 +218,9 @@ pub enum Config {
#[cfg(feature = "gridfs")]
GridFS(remi_gridfs::GridfsStorageConfig),

#[cfg(feature = "azure")]
Azure(remi_azure::Config),

#[cfg(feature = "s3")]
S3(remi_s3::S3StorageConfig),

Expand Down

0 comments on commit e88075f

Please sign in to comment.