Skip to content

Commit

Permalink
Fix feature scoping to support the wasm32-unknown-unknown target
Browse files Browse the repository at this point in the history
  • Loading branch information
konstin committed Oct 30, 2024
1 parent e94454a commit 830f92f
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 25 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,12 @@ jobs:
- uses: Swatinem/rust-cache@v2
- run: cargo test
- run: cargo test --all-features

check-wasm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
- run: rustup target add wasm32-unknown-unknown
- run: cargo check --target wasm32-unknown-unknown

2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pep508_rs"
version = "0.9.0"
version = "0.9.1"
description = "A library for python dependency specifiers, better known as PEP 508"
edition = "2021"
include = ["/src", "Changelog.md", "License-Apache", "License-BSD", "Readme.md", "pyproject.toml"]
Expand Down
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.9.1

- Fix feature scoping to support the wasm32-unknown-unknown target

# 0.9.0

- Rewritten markers for sounds arithmetic operation and being much faster
Expand Down
2 changes: 2 additions & 0 deletions src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::path::{Component, Path, PathBuf};
///
/// When a relative path is provided with `..` components that extend beyond the base directory.
/// For example, `./a/../../b` cannot be normalized because it escapes the base directory.
#[cfg_attr(not(feature = "non-pep508-extensions"), allow(dead_code))]
pub fn normalize_absolute_path(path: &Path) -> Result<PathBuf, std::io::Error> {
let mut components = path.components().peekable();
let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().copied() {
Expand Down Expand Up @@ -58,6 +59,7 @@ pub fn normalize_absolute_path(path: &Path) -> Result<PathBuf, std::io::Error> {
/// `/C:/Users/ferris/wheel-0.42.0.tar.gz`.
///
/// On other platforms, this is a no-op.
#[cfg_attr(not(feature = "non-pep508-extensions"), allow(dead_code))]
pub fn normalize_url_path(path: &str) -> Cow<'_, str> {
// Apply percent-decoding to the URL.
let path = urlencoding::decode(path).unwrap_or(Cow::Borrowed(path));
Expand Down
2 changes: 2 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,7 @@ fn error_invalid_extra_unnamed_url() {

/// Check that the relative path support feature toggle works.
#[test]
#[cfg(feature = "non-pep508-extensions")]
fn non_pep508_paths() {
let requirements = &[
"foo @ file://./foo",
Expand Down Expand Up @@ -748,6 +749,7 @@ fn no_space_after_operator() {
}

#[test]
#[cfg(feature = "non-pep508-extensions")]
fn path_with_fragment() {
let requirements = if cfg!(windows) {
&[
Expand Down
67 changes: 44 additions & 23 deletions src/verbatim_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::sync::LazyLock;
use thiserror::Error;
use url::{ParseError, Url};

#[cfg(feature = "non-pep508-extensions")]
use crate::path::{normalize_absolute_path, normalize_url_path};
use crate::Pep508Url;

Expand Down Expand Up @@ -80,6 +81,7 @@ impl VerbatimUrl {
}

/// Parse a URL from an absolute path.
#[cfg(feature = "non-pep508-extensions")]
pub fn from_absolute_path(path: impl AsRef<Path>) -> Result<Self, VerbatimUrlError> {
let path = path.as_ref();

Expand Down Expand Up @@ -139,6 +141,7 @@ impl VerbatimUrl {
}

/// Return the underlying [`Path`], if the URL is a file URL.
#[cfg(feature = "non-pep508-extensions")]
pub fn as_path(&self) -> Result<PathBuf, VerbatimUrlError> {
self.url
.to_file_path()
Expand Down Expand Up @@ -209,11 +212,8 @@ impl Pep508Url for VerbatimUrl {
type Err = VerbatimUrlError;

/// Create a `VerbatimUrl` to represent the requirement.
fn parse_url(
url: &str,
#[cfg_attr(not(feature = "non-pep508-extensions"), allow(unused_variables))]
working_dir: Option<&Path>,
) -> Result<Self, Self::Err> {
#[cfg_attr(not(feature = "non-pep508-extensions"), allow(unused_variables))]
fn parse_url(url: &str, working_dir: Option<&Path>) -> Result<Self, Self::Err> {
// Expand environment variables in the URL.
let expanded = expand_env_vars(url);

Expand All @@ -222,18 +222,24 @@ impl Pep508Url for VerbatimUrl {
// Ex) `file:///home/ferris/project/scripts/...`, `file://localhost/home/ferris/project/scripts/...`, or `file:../ferris/`
Some(Scheme::File) => {
// Strip the leading slashes, along with the `localhost` host, if present.
let path = strip_host(path);

// Transform, e.g., `/C:/Users/ferris/wheel-0.42.0.tar.gz` to `C:\Users\ferris\wheel-0.42.0.tar.gz`.
let path = normalize_url_path(path);

#[cfg(feature = "non-pep508-extensions")]
if let Some(working_dir) = working_dir {
return Ok(VerbatimUrl::from_path(path.as_ref(), working_dir)?
.with_given(url.to_string()));
}
{
let path = strip_host(path);

let path = normalize_url_path(path);

Ok(VerbatimUrl::from_absolute_path(path.as_ref())?.with_given(url.to_string()))
if let Some(working_dir) = working_dir {
return Ok(VerbatimUrl::from_path(path.as_ref(), working_dir)?
.with_given(url.to_string()));
}

Ok(VerbatimUrl::from_absolute_path(path.as_ref())?
.with_given(url.to_string()))
}
#[cfg(not(feature = "non-pep508-extensions"))]
Ok(VerbatimUrl::parse_url(expanded)?.with_given(url.to_string()))
}

// Ex) `https://download.pytorch.org/whl/torch_stable.html`
Expand All @@ -245,24 +251,33 @@ impl Pep508Url for VerbatimUrl {
// Ex) `C:\Users\ferris\wheel-0.42.0.tar.gz`
_ => {
#[cfg(feature = "non-pep508-extensions")]
if let Some(working_dir) = working_dir {
return Ok(VerbatimUrl::from_path(expanded.as_ref(), working_dir)?
.with_given(url.to_string()));
{
if let Some(working_dir) = working_dir {
return Ok(VerbatimUrl::from_path(expanded.as_ref(), working_dir)?
.with_given(url.to_string()));
}

Ok(VerbatimUrl::from_absolute_path(expanded.as_ref())?
.with_given(url.to_string()))
}

Ok(VerbatimUrl::from_absolute_path(expanded.as_ref())?
.with_given(url.to_string()))
#[cfg(not(feature = "non-pep508-extensions"))]
Err(Self::Err::NotAUrl(expanded.to_string()))
}
}
} else {
// Ex) `../editable/`
#[cfg(feature = "non-pep508-extensions")]
if let Some(working_dir) = working_dir {
return Ok(VerbatimUrl::from_path(expanded.as_ref(), working_dir)?
.with_given(url.to_string()));
{
if let Some(working_dir) = working_dir {
return Ok(VerbatimUrl::from_path(expanded.as_ref(), working_dir)?
.with_given(url.to_string()));
}

Ok(VerbatimUrl::from_absolute_path(expanded.as_ref())?.with_given(url.to_string()))
}

Ok(VerbatimUrl::from_absolute_path(expanded.as_ref())?.with_given(url.to_string()))
#[cfg(not(feature = "non-pep508-extensions"))]
Err(Self::Err::NotAUrl(expanded.to_string()))
}
}
}
Expand All @@ -285,6 +300,11 @@ pub enum VerbatimUrlError {
/// Received a path that could not be normalized.
#[error("path could not be normalized: {0}")]
Normalization(PathBuf, #[source] std::io::Error),

/// Received a path that could not be normalized.
#[cfg(not(feature = "non-pep508-extensions"))]
#[error("Not a URL (missing scheme): {0}")]
NotAUrl(String),
}

/// Expand all available environment variables.
Expand Down Expand Up @@ -379,6 +399,7 @@ pub fn strip_host(path: &str) -> &str {
///
/// For example, given `file:///home/ferris/project/scripts#hash=somehash`, returns
/// `("/home/ferris/project/scripts", Some("hash=somehash"))`.
#[cfg_attr(not(feature = "non-pep508-extensions"), allow(dead_code))]
fn split_fragment(path: &Path) -> (Cow<Path>, Option<&str>) {
let Some(s) = path.to_str() else {
return (Cow::Borrowed(path), None);
Expand Down

0 comments on commit 830f92f

Please sign in to comment.