Skip to content

Commit

Permalink
fix: allow building with no-default-features (#565)
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-Aaron-Bloom committed Mar 6, 2024
1 parent fe594e9 commit 7cf4044
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 25 deletions.
2 changes: 1 addition & 1 deletion crates/taplo-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ schemars = "0.8"
serde = "1"
serde_json = "1"
taplo = { version = "0.13.1", path = "../taplo", features = ["serde"] }
taplo-common = { version = "0.5.1", path = "../taplo-common", default-features = false }
taplo-common = { version = "0.5.1", path = "../taplo-common" }
taplo-lsp = { version = "0.7.1", path = "../taplo-lsp", default-features = false, optional = true }
time = { version = "0.3", features = ["parsing"] }
toml = "0.7"
Expand Down
4 changes: 0 additions & 4 deletions crates/taplo-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ tokio = { version = "1.24.2", features = [
], default-features = false }

[features]
# We only set a default such that `cargo publish` doesn't complain. Crates
# depending on taplo-common should disable default features and explicitly set
# the features they need.
default = ["rustls-tls"]
# default-tls enables native-tls but without enabling native-tls specific features.
native-tls = ["reqwest/default-tls"]
rustls-tls = ["reqwest/rustls-tls"]
Expand Down
49 changes: 33 additions & 16 deletions crates/taplo-common/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,23 +124,26 @@ pub(crate) fn normalize_str(s: &str) -> Cow<str> {
#[cfg(not(target_arch = "wasm32"))]
#[tracing::instrument]
pub fn get_reqwest_client(timeout: std::time::Duration) -> Result<reqwest::Client, reqwest::Error> {
fn get_cert(path: impl AsRef<Path>) -> Result<reqwest::Certificate, anyhow::Error> {
let path = path.as_ref();
let is_der = path.extension().map_or(false, |ext| ext == "der");
let buf = std::fs::read(path)?;
tracing::info!(
"Found a custom CA {}. Reading the CA...",
path.to_string_lossy()
);
if is_der {
Ok(reqwest::Certificate::from_der(&buf)?)
} else {
Ok(reqwest::Certificate::from_pem(&buf)?)
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))]
fn get_certs(
mut builder: reqwest::ClientBuilder,
path: std::ffi::OsString,
) -> reqwest::ClientBuilder {
fn get_cert(path: &Path) -> Result<reqwest::Certificate, anyhow::Error> {
let is_der = path.extension().map_or(false, |ext| ext == "der");
let buf = std::fs::read(path)?;
tracing::info!(
"Found a custom CA {}. Reading the CA...",
path.to_string_lossy()
);
if is_der {
Ok(reqwest::Certificate::from_der(&buf)?)
} else {
Ok(reqwest::Certificate::from_pem(&buf)?)
}
}
}
let mut builder = reqwest::Client::builder().timeout(timeout);
if let Some(path) = std::env::var_os("TAPLO_EXTRA_CA_CERTS") {
match get_cert(&path) {

match get_cert(path.as_ref()) {
Ok(cert) => {
builder = builder.add_root_certificate(cert);
tracing::info!(?path, "Added the custom CA");
Expand All @@ -149,6 +152,20 @@ pub fn get_reqwest_client(timeout: std::time::Duration) -> Result<reqwest::Clien
tracing::error!(error = %err, "Could not parse the custom CA");
}
}
builder
}
#[cfg(not(any(feature = "native-tls", feature = "rustls-tls")))]
fn get_certs(
builder: reqwest::ClientBuilder,
path: std::ffi::OsString,
) -> reqwest::ClientBuilder {
tracing::error!(?path, "Could not load certs, taplo was built without TLS");
builder
}

let mut builder = reqwest::Client::builder().timeout(timeout);
if let Some(path) = std::env::var_os("TAPLO_EXTRA_CA_CERTS") {
builder = get_certs(builder, path)
}
builder.build()
}
2 changes: 1 addition & 1 deletion crates/taplo-lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", features = ["preserve_order"] }
tap = "1.0.1"
taplo = { version = "0.13.1", path = "../taplo", features = ["serde"] }
taplo-common = { version = "0.5.1", path = "../taplo-common", default-features = false }
taplo-common = { version = "0.5.1", path = "../taplo-common" }
time = { version = "0.3", features = ["formatting", "parsing"] }
toml = "0.7"
tracing = "0.1.29"
Expand Down
4 changes: 1 addition & 3 deletions crates/taplo-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ serde = { version = "1.0.137", features = ["derive"] }
serde_json = "1.0.81"
taplo = { path = "../taplo" }
taplo-cli = { path = "../taplo-cli", optional = true }
taplo-common = { path = "../taplo-common", default-features = false, features = [
"rustls-tls",
] }
taplo-common = { path = "../taplo-common", features = ["rustls-tls"] }
taplo-lsp = { path = "../taplo-lsp", optional = true }
time = { version = "0.3.9", features = ["parsing"] }
tokio = { version = "1.19.2", default-features = false }
Expand Down

0 comments on commit 7cf4044

Please sign in to comment.