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

Make reqwest an optional (and default) feature #215

Merged
merged 1 commit into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[features]
default = ["glutin-window", "canvas", "egui-gui", "obj-io", "gltf-io", "image-io"]
default = ["glutin-window", "canvas", "egui-gui", "obj-io", "gltf-io", "image-io", "reqwest"]
glutin-window = ["glutin"] # Default window for desktop (only available when NOT building for the wasm32 architecture)
canvas = [] # Default window for web (only available when building for the wasm32 architecture)
egui-gui = ["egui"] # Additional GUI features
Expand All @@ -30,7 +30,7 @@ log = "0.4"
cgmath = "0.18"
half = {version="1.8", features=["std", "num-traits", "zerocopy", "serde"]}
thiserror = "1.0"
reqwest = "0.11"
reqwest = { version = "0.11", optional = true }
serde = { version = "1.0", features = ["derive"], optional = true }
gltf = { version = "0.16", features = ["utils"], optional = true }
wavefront_obj = { version = "10.0", optional = true }
Expand Down
6 changes: 5 additions & 1 deletion src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ pub enum IOError {
#[cfg(not(target_arch = "wasm32"))]
#[error("error while loading the file {0}: {1}")]
FailedLoading(String, std::io::Error),
#[error("error while loading the file {0}: {1}")]
#[cfg(feature = "reqwest")]
#[error("error while loading the url {0}: {1}")]
FailedLoadingUrl(String, reqwest::Error),
#[cfg(not(feature = "reqwest"))]
#[error("error while loading the url {0}: feature 'reqwest' not enabled")]
FailedLoadingUrl(String),
#[error("tried to use {0} which was not loaded")]
NotLoaded(String),
}
14 changes: 12 additions & 2 deletions src/io/loader.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::core::*;
use crate::io::*;
use reqwest::Url;
use std::cell::RefCell;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -255,12 +254,13 @@ fn load_from_disk(mut paths: Vec<PathBuf>, loaded: &mut Loaded) -> ThreeDResult<
Ok(())
}

#[cfg(feature = "reqwest")]
async fn load_urls(mut paths: Vec<PathBuf>, loaded: &mut Loaded) -> ThreeDResult<()> {
if paths.len() > 0 {
let mut handles = Vec::new();
let client = reqwest::Client::new();
for path in paths.drain(..) {
let url = Url::parse(path.to_str().unwrap())?;
let url = reqwest::Url::parse(path.to_str().unwrap())?;
handles.push((path, client.get(url).send().await));
}
for (path, handle) in handles.drain(..) {
Expand All @@ -276,6 +276,16 @@ async fn load_urls(mut paths: Vec<PathBuf>, loaded: &mut Loaded) -> ThreeDResult
Ok(())
}

#[cfg(not(feature = "reqwest"))]
async fn load_urls(paths: Vec<PathBuf>, _loaded: &mut Loaded) -> ThreeDResult<()> {
if paths.is_empty() {
Ok(())
} else {
let url = paths[0].to_str().unwrap().to_owned();
Err(Box::new(IOError::FailedLoadingUrl(url)))
}
}

fn is_absolute_url(path: &str) -> bool {
path.find("://").map(|i| i > 0).unwrap_or(false)
|| path.find("//").map(|i| i == 0).unwrap_or(false)
Expand Down