Skip to content

Commit

Permalink
Update convert to exclude native types when compiling for wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
chipsenkbeil committed Feb 16, 2024
1 parent 9105185 commit e356c5a
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 15 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
components: rustfmt
- run: cargo fmt --all -- --check
tests:
name: "Test Rust ${{ matrix.rust }} (${{ matrix.os }}, ${{ matrix.flags }})"
name: "Test Rust ${{ matrix.rust }} (${{ matrix.os }}, ${{ matrix.target }} ${{ matrix.flags }})"
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
Expand All @@ -69,6 +69,8 @@ jobs:
- { rust: stable, os: macos-latest, flags: "--no-default-features" }
- { rust: stable, os: ubuntu-latest, flags: "--all-features" }
- { rust: stable, os: ubuntu-latest, flags: "--no-default-features" }
- { rust: stable, os: ubuntu-latest, target: wasm32-unknown-unknown, flags: "--all-features" }
- { rust: stable, os: ubuntu-latest, target: wasm32-unknown-unknown, flags: "--no-default-features" }
- { rust: 1.58.1, os: ubuntu-latest, flags: "--all-features" }
- { rust: 1.58.1, os: ubuntu-latest, flags: "--no-default-features" }
steps:
Expand All @@ -79,7 +81,12 @@ jobs:
profile: minimal
toolchain: ${{ matrix.rust }}
target: ${{ matrix.target }}
- run: cargo test ${{ matrix.flags }} --target ${{ matrix.target }}
if: matrix.target != 0 && matrix.target != 'wasm32-unknown-unknown'
- run: cargo build ${{ matrix.flags }} --target ${{ matrix.target }}
if: matrix.target != 0 && matrix.target == 'wasm32-unknown-unknown'
- run: cargo test ${{ matrix.flags }}
if: matrix.target == 0
examples:
name: "Run examples using Rust ${{ matrix.rust }} (${{ matrix.os }})"
runs-on: ${{ matrix.os }}
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.7.1] - 2024-02-15

* Support `wasm` family for compilation

## [0.7.0] - 2023-11-04

* Support `no_std` environments, when `default-features = false` is set for the crate
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "typed-path"
description = "Provides typed variants of Path and PathBuf for Unix and Windows"
version = "0.7.0"
version = "0.7.1"
edition = "2021"
authors = ["Chip Senkbeil <[email protected]>"]
categories = ["development-tools", "filesystem", "os"]
Expand Down
2 changes: 1 addition & 1 deletion src/common/non_utf8/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ where
/// let path = cwd.join(Path::new("a/b/../c/./d"));
/// assert_eq!(path.absolutize().unwrap(), cwd.join(Path::new("a/c/d")));
/// ```
#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(target_family = "wasm")))]
pub fn absolutize(&self) -> std::io::Result<PathBuf<T>> {
if self.is_absolute() {
Ok(self.normalize())
Expand Down
2 changes: 1 addition & 1 deletion src/common/utf8/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ where
/// let path = cwd.join(Utf8Path::new("a/b/../c/./d"));
/// assert_eq!(path.absolutize().unwrap(), cwd.join(Utf8Path::new("a/c/d")));
/// ```
#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(target_family = "wasm")))]
pub fn absolutize(&self) -> std::io::Result<Utf8PathBuf<T>> {
if self.is_absolute() {
Ok(self.normalize())
Expand Down
12 changes: 7 additions & 5 deletions src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ use std::{

#[cfg(feature = "std")]
use crate::{
native::{Utf8NativePath, Utf8NativePathBuf},
unix::UnixComponent,
windows::{WindowsComponent, WindowsPrefixComponent},
Encoding, Path, PathBuf,
};

#[cfg(all(feature = "std", not(target_family = "wasm")))]
use crate::native::{Utf8NativePath, Utf8NativePathBuf};

/// Interface to try to perform a cheap reference-to-reference conversion.
pub trait TryAsRef<T: ?Sized> {
fn try_as_ref(&self) -> Option<&T>;
Expand Down Expand Up @@ -306,7 +308,7 @@ impl<'a> TryFrom<StdComponent<'a>> for WindowsComponent<'a> {
}
}

#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(target_family = "wasm")))]
impl AsRef<StdPath> for Utf8NativePath {
/// Converts a native utf8 path (based on compilation family) into [`std::path::Path`].
///
Expand All @@ -324,7 +326,7 @@ impl AsRef<StdPath> for Utf8NativePath {
}
}

#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(target_family = "wasm")))]
impl AsRef<StdPath> for Utf8NativePathBuf {
/// Converts a native utf8 pathbuf (based on compilation family) into [`std::path::Path`].
///
Expand All @@ -342,7 +344,7 @@ impl AsRef<StdPath> for Utf8NativePathBuf {
}
}

#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(target_family = "wasm")))]
impl<'a> From<&'a Utf8NativePath> for StdPathBuf {
/// Converts a native utf8 path (based on compilation family) into [`std::path::PathBuf`].
///
Expand All @@ -360,7 +362,7 @@ impl<'a> From<&'a Utf8NativePath> for StdPathBuf {
}
}

#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(target_family = "wasm")))]
impl From<Utf8NativePathBuf> for StdPathBuf {
/// Converts a native utf8 pathbuf (based on compilation family) into [`std::path::PathBuf`].
///
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub struct ReadmeDoctests;
extern crate alloc;

mod no_std_compat {
#[allow(unused_imports)]
pub use alloc::{
boxed::Box,
string::{String, ToString},
Expand All @@ -19,10 +20,11 @@ mod no_std_compat {
#[macro_use]
mod common;
mod convert;
#[cfg(not(target_family = "wasm"))]
mod native;
mod typed;
mod unix;
#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(target_family = "wasm")))]
pub mod utils;
mod windows;

Expand All @@ -33,6 +35,7 @@ mod private {

pub use common::*;
pub use convert::*;
#[cfg(not(target_family = "wasm"))]
pub use native::*;
pub use typed::*;
pub use unix::*;
Expand Down
7 changes: 5 additions & 2 deletions src/typed/non_utf8/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use alloc::borrow::Cow;
use core::fmt;

#[cfg(feature = "std")]
use std::{io, path::Path};
use std::path::Path;

#[cfg(all(feature = "std", not(target_family = "wasm")))]
use std::io;

use crate::common::StripPrefixError;
use crate::convert::TryAsRef;
Expand Down Expand Up @@ -534,7 +537,7 @@ impl<'a> TypedPath<'a> {
/// let path = cwd.join("a/b/../c/./d");
/// assert_eq!(path.absolutize().unwrap(), cwd.join("a/c/d"));
/// ```
#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(target_family = "wasm")))]
pub fn absolutize(&self) -> io::Result<TypedPathBuf> {
Ok(match self {
Self::Unix(path) => TypedPathBuf::Unix(path.absolutize()?),
Expand Down
2 changes: 1 addition & 1 deletion src/typed/non_utf8/pathbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ impl TypedPathBuf {
/// let path = cwd.join("a/b/../c/./d");
/// assert_eq!(path.absolutize().unwrap(), cwd.join("a/c/d"));
/// ```
#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(target_family = "wasm")))]
pub fn absolutize(&self) -> io::Result<TypedPathBuf> {
self.to_path().absolutize()
}
Expand Down
2 changes: 1 addition & 1 deletion src/typed/utf8/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ impl<'a> Utf8TypedPath<'a> {
/// let path = cwd.join("a/b/../c/./d");
/// assert_eq!(path.absolutize().unwrap(), cwd.join("a/c/d"));
/// ```
#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(target_family = "wasm")))]
pub fn absolutize(&self) -> std::io::Result<Utf8TypedPathBuf> {
Ok(match self {
Self::Unix(path) => Utf8TypedPathBuf::Unix(path.absolutize()?),
Expand Down
2 changes: 1 addition & 1 deletion src/typed/utf8/pathbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ impl Utf8TypedPathBuf {
/// let path = cwd.join("a/b/../c/./d");
/// assert_eq!(path.absolutize().unwrap(), cwd.join("a/c/d"));
/// ```
#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(target_family = "wasm")))]
pub fn absolutize(&self) -> std::io::Result<Utf8TypedPathBuf> {
self.to_path().absolutize()
}
Expand Down

0 comments on commit e356c5a

Please sign in to comment.