Skip to content

Commit

Permalink
Make reqwest an optional (and default) feature
Browse files Browse the repository at this point in the history
Closes #211

This is just the simple solution until
#214 is fixed
  • Loading branch information
emilk authored and asny committed Mar 22, 2022
1 parent 07bbf3b commit fa47567
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
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

0 comments on commit fa47567

Please sign in to comment.