Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature gate object_store and reqwest #71

Merged
merged 15 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,27 @@ flate2 = "1.0.20"
futures = "0.3.31"
jpeg = { package = "jpeg-decoder", version = "0.3.0", default-features = false }
num_enum = "0.7.3"
object_store = "0.12"
object_store = { version = "0.12", optional = true }
# In the future we could make this feature-flagged, but for now we depend on
# object_store which uses reqwest.
reqwest = { version = "0.12", default-features = false }
reqwest = { version = "0.12", default-features = false, optional = true }
thiserror = "1"
tokio = { version = "1.43.0", optional = true }
weezl = "0.1.0"

[dev-dependencies]
tiff = "0.9.1"
tokio = { version = "1.9", features = ["macros", "fs", "rt-multi-thread"] }

[features]
default = ["object_store", "reqwest"]
reqwest = ["dep:reqwest"]
object_store = ["dep:object_store"]

[package.metadata.cargo-all-features]

# If your crate has a large number of optional dependencies, skip them for speed
skip_optional_dependencies = true

# Exclude certain features from the build matrix
denylist = ["default"]
1 change: 1 addition & 0 deletions src/cog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl TIFF {
}
}

#[cfg(feature = "object_store")]
#[cfg(test)]
mod test {
use std::io::BufReader;
Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub enum AsyncTiffError {
JPEGDecodingError(#[from] jpeg::Error),

/// Error while fetching data using object store.
#[cfg(feature = "object_store")]
#[error(transparent)]
ObjectStore(#[from] object_store::Error),

Expand All @@ -32,6 +33,7 @@ pub enum AsyncTiffError {
InternalTIFFError(#[from] crate::tiff::TiffError),

/// Reqwest error
#[cfg(feature = "reqwest")]
#[error(transparent)]
ReqwestError(#[from] reqwest::Error),

Expand Down
11 changes: 7 additions & 4 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use byteorder::{BigEndian, LittleEndian, ReadBytesExt};
use bytes::buf::Reader;
use bytes::{Buf, Bytes};
use futures::future::{BoxFuture, FutureExt, TryFutureExt};
#[cfg(feature = "object_store")]
use object_store::ObjectStore;

use crate::error::{AsyncTiffError, AsyncTiffResult};
Expand Down Expand Up @@ -91,12 +92,13 @@ impl AsyncFileReader for Box<dyn AsyncFileReader + '_> {
// }

/// An AsyncFileReader that reads from an [`ObjectStore`] instance.
#[cfg(feature = "object_store")]
#[derive(Clone, Debug)]
pub struct ObjectReader {
store: Arc<dyn ObjectStore>,
path: object_store::path::Path,
}

#[cfg(feature = "object_store")]
impl ObjectReader {
/// Creates a new [`ObjectReader`] for the provided [`ObjectStore`] and path
///
Expand All @@ -105,7 +107,7 @@ impl ObjectReader {
Self { store, path }
}
}

#[cfg(feature = "object_store")]
impl AsyncFileReader for ObjectReader {
fn get_bytes(&self, range: Range<u64>) -> BoxFuture<'_, AsyncTiffResult<Bytes>> {
let range = range.start as _..range.end as _;
Expand Down Expand Up @@ -134,19 +136,20 @@ impl AsyncFileReader for ObjectReader {
}

/// An AsyncFileReader that reads from a URL using reqwest.
#[cfg(feature = "reqwest")]
#[derive(Debug, Clone)]
pub struct ReqwestReader {
client: reqwest::Client,
url: reqwest::Url,
}

#[cfg(feature = "reqwest")]
impl ReqwestReader {
/// Construct a new ReqwestReader from a reqwest client and URL.
pub fn new(client: reqwest::Client, url: reqwest::Url) -> Self {
Self { client, url }
}
}

#[cfg(feature = "reqwest")]
impl AsyncFileReader for ReqwestReader {
fn get_bytes(&self, range: Range<u64>) -> BoxFuture<'_, AsyncTiffResult<Bytes>> {
let url = self.url.clone();
Expand Down