Skip to content

Commit

Permalink
Rearrange files
Browse files Browse the repository at this point in the history
  • Loading branch information
dylni committed Nov 4, 2023
1 parent 81ac84b commit 7140754
Show file tree
Hide file tree
Showing 15 changed files with 243 additions and 223 deletions.
41 changes: 41 additions & 0 deletions src/common/convert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::borrow::Cow;
use std::convert::Infallible;
use std::ffi::OsStr;
use std::ffi::OsString;
use std::result;

#[cfg(all(target_vendor = "fortanix", target_env = "sgx"))]
use std::os::fortanix_sgx as os;
#[cfg(target_os = "hermit")]
use std::os::hermit as os;
#[cfg(target_os = "solid_asp3")]
use std::os::solid as os;
#[cfg(unix)]
use std::os::unix as os;
#[cfg(target_os = "wasi")]
use std::os::wasi as os;
#[cfg(target_os = "xous")]
use std::os::xous as os;

use os::ffi::OsStrExt;
use os::ffi::OsStringExt;

pub(crate) type EncodingError = Infallible;

pub(crate) type Result<T> = result::Result<T, EncodingError>;

pub(crate) fn os_str_from_bytes(string: &[u8]) -> Result<Cow<'_, OsStr>> {
Ok(Cow::Borrowed(OsStrExt::from_bytes(string)))
}

pub(crate) fn os_str_to_bytes(os_string: &OsStr) -> Cow<'_, [u8]> {
Cow::Borrowed(OsStrExt::as_bytes(os_string))
}

pub(crate) fn os_string_from_vec(string: Vec<u8>) -> Result<OsString> {
Ok(OsStringExt::from_vec(string))
}

pub(crate) fn os_string_into_vec(os_string: OsString) -> Vec<u8> {
OsStringExt::into_vec(os_string)
}
44 changes: 3 additions & 41 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,7 @@
use std::borrow::Cow;
use std::convert::Infallible;
use std::ffi::OsStr;
use std::ffi::OsString;
use std::result;

#[cfg(all(target_vendor = "fortanix", target_env = "sgx"))]
use std::os::fortanix_sgx as os;
#[cfg(target_os = "hermit")]
use std::os::hermit as os;
#[cfg(target_os = "solid_asp3")]
use std::os::solid as os;
#[cfg(unix)]
use std::os::unix as os;
#[cfg(target_os = "wasi")]
use std::os::wasi as os;
#[cfg(target_os = "xous")]
use std::os::xous as os;

use os::ffi::OsStrExt;
use os::ffi::OsStringExt;
if_conversions! {
pub(super) mod convert;
}

if_raw_str! {
pub(super) mod raw;
}

pub(super) type EncodingError = Infallible;

pub(super) type Result<T> = result::Result<T, EncodingError>;

pub(super) fn os_str_from_bytes(string: &[u8]) -> Result<Cow<'_, OsStr>> {
Ok(Cow::Borrowed(OsStrExt::from_bytes(string)))
}

pub(super) fn os_str_to_bytes(os_string: &OsStr) -> Cow<'_, [u8]> {
Cow::Borrowed(OsStrExt::as_bytes(os_string))
}

pub(super) fn os_string_from_vec(string: Vec<u8>) -> Result<OsString> {
Ok(OsStringExt::from_vec(string))
}

pub(super) fn os_string_into_vec(os_string: OsString) -> Vec<u8> {
OsStringExt::into_vec(os_string)
}
23 changes: 13 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@ if_conversions! {
}
}

#[allow(dead_code)]
#[cfg_attr(
all(target_family = "wasm", target_os = "unknown"),
path = "wasm/mod.rs"
Expand All @@ -287,7 +286,11 @@ if_conversions! {
)]
mod imp;

#[cfg(any(feature = "raw_os_str", windows))]
if_conversions! {
use imp::convert;
}

#[cfg(any(all(feature = "conversions", windows), feature = "raw_os_str"))]
mod util;

if_raw_str! {
Expand Down Expand Up @@ -325,7 +328,7 @@ if_checked_conversions! {
os_str_bytes_docs_rs,
doc(cfg(feature = "checked_conversions"))
)]
pub struct EncodingError(imp::EncodingError);
pub struct EncodingError(convert::EncodingError);

impl Display for EncodingError {
#[inline]
Expand All @@ -342,14 +345,14 @@ if_checked_conversions! {
}

if_conversions! {
fn from_raw_bytes<'a, S>(string: S) -> imp::Result<Cow<'a, OsStr>>
fn from_raw_bytes<'a, S>(string: S) -> convert::Result<Cow<'a, OsStr>>
where
S: Into<Cow<'a, [u8]>>,
{
match string.into() {
Cow::Borrowed(string) => imp::os_str_from_bytes(string),
Cow::Borrowed(string) => convert::os_str_from_bytes(string),
Cow::Owned(string) => {
imp::os_string_from_vec(string).map(Cow::Owned)
convert::os_string_from_vec(string).map(Cow::Owned)
}
}
}
Expand Down Expand Up @@ -482,7 +485,7 @@ if_conversions! {

#[inline]
fn to_raw_bytes(&self) -> Cow<'_, [u8]> {
imp::os_str_to_bytes(self)
convert::os_str_to_bytes(self)
}
}

Expand Down Expand Up @@ -1055,19 +1058,19 @@ if_conversions! {
impl OsStringBytes for OsString {
#[inline]
fn assert_from_raw_vec(string: Vec<u8>) -> Self {
expect_encoded!(imp::os_string_from_vec(string))
expect_encoded!(convert::os_string_from_vec(string))
}

if_checked_conversions! {
#[inline]
fn from_raw_vec(string: Vec<u8>) -> Result<Self> {
imp::os_string_from_vec(string).map_err(EncodingError)
convert::os_string_from_vec(string).map_err(EncodingError)
}
}

#[inline]
fn into_raw_vec(self) -> Vec<u8> {
imp::os_string_into_vec(self)
convert::os_string_into_vec(self)
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/raw_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ if_checked_conversions! {
}

if_conversions! {
use super::imp;
use super::convert;
}

#[cfg(not(feature = "memchr"))]
Expand Down Expand Up @@ -181,8 +181,8 @@ impl RawOsStr {
if_conversions! {
fn cow_from_raw_bytes_checked(
string: &[u8],
) -> imp::Result<Cow<'_, Self>> {
imp::os_str_from_bytes(string).map(RawOsStrCow::from_os_str)
) -> convert::Result<Cow<'_, Self>> {
convert::os_str_from_bytes(string).map(RawOsStrCow::from_os_str)
}
}

Expand Down Expand Up @@ -730,7 +730,7 @@ impl RawOsStr {
#[inline]
#[must_use]
pub fn to_raw_bytes(&self) -> Cow<'_, [u8]> {
imp::os_str_to_bytes(self.as_os_str())
convert::os_str_to_bytes(self.as_os_str())
}
}

Expand Down Expand Up @@ -1087,8 +1087,8 @@ impl RawOsString {
}

if_conversions! {
fn from_raw_vec_checked(string: Vec<u8>) -> imp::Result<Self> {
imp::os_string_from_vec(string).map(Self::new)
fn from_raw_vec_checked(string: Vec<u8>) -> convert::Result<Self> {
convert::os_string_from_vec(string).map(Self::new)
}
}

Expand Down Expand Up @@ -1271,7 +1271,7 @@ impl RawOsString {
#[inline]
#[must_use]
pub fn into_raw_vec(self) -> Vec<u8> {
imp::os_string_into_vec(self.into_os_string())
convert::os_string_into_vec(self.into_os_string())
}
}

Expand Down
54 changes: 54 additions & 0 deletions src/wasm/convert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use std::borrow::Cow;
use std::error::Error;
use std::ffi::OsStr;
use std::ffi::OsString;
use std::fmt;
use std::fmt::Display;
use std::fmt::Formatter;
use std::result;
use std::str;
use std::str::Utf8Error;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct EncodingError(Utf8Error);

impl Display for EncodingError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "os_str_bytes: {}", self.0)
}
}

impl Error for EncodingError {}

pub(crate) type Result<T> = result::Result<T, EncodingError>;

macro_rules! expect_utf8 {
( $result:expr ) => {
$result.expect(
"platform string contains invalid UTF-8, which should not be \
possible",
)
};
}

fn from_bytes(string: &[u8]) -> Result<&str> {
str::from_utf8(string).map_err(EncodingError)
}

pub(crate) fn os_str_from_bytes(string: &[u8]) -> Result<Cow<'_, OsStr>> {
from_bytes(string).map(|x| Cow::Borrowed(OsStr::new(x)))
}

pub(crate) fn os_str_to_bytes(os_string: &OsStr) -> Cow<'_, [u8]> {
Cow::Borrowed(expect_utf8!(os_string.to_str()).as_bytes())
}

pub(crate) fn os_string_from_vec(string: Vec<u8>) -> Result<OsString> {
String::from_utf8(string)
.map(Into::into)
.map_err(|x| EncodingError(x.utf8_error()))
}

pub(crate) fn os_string_into_vec(os_string: OsString) -> Vec<u8> {
expect_utf8!(os_string.into_string()).into_bytes()
}
57 changes: 3 additions & 54 deletions src/wasm/mod.rs
Original file line number Diff line number Diff line change
@@ -1,58 +1,7 @@
use std::borrow::Cow;
use std::error::Error;
use std::ffi::OsStr;
use std::ffi::OsString;
use std::fmt;
use std::fmt::Display;
use std::fmt::Formatter;
use std::result;
use std::str;
use std::str::Utf8Error;
if_conversions! {
pub(super) mod convert;
}

if_raw_str! {
pub(super) mod raw;
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) struct EncodingError(Utf8Error);

impl Display for EncodingError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "os_str_bytes: {}", self.0)
}
}

impl Error for EncodingError {}

pub(super) type Result<T> = result::Result<T, EncodingError>;

macro_rules! expect_utf8 {
( $result:expr ) => {
$result.expect(
"platform string contains invalid UTF-8, which should not be \
possible",
)
};
}

fn from_bytes(string: &[u8]) -> Result<&str> {
str::from_utf8(string).map_err(EncodingError)
}

pub(super) fn os_str_from_bytes(string: &[u8]) -> Result<Cow<'_, OsStr>> {
from_bytes(string).map(|x| Cow::Borrowed(OsStr::new(x)))
}

pub(super) fn os_str_to_bytes(os_string: &OsStr) -> Cow<'_, [u8]> {
Cow::Borrowed(expect_utf8!(os_string.to_str()).as_bytes())
}

pub(super) fn os_string_from_vec(string: Vec<u8>) -> Result<OsString> {
String::from_utf8(string)
.map(Into::into)
.map_err(|x| EncodingError(x.utf8_error()))
}

pub(super) fn os_string_into_vec(os_string: OsString) -> Vec<u8> {
expect_utf8!(os_string.into_string()).into_bytes()
}
1 change: 1 addition & 0 deletions src/wasm/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fmt::Formatter;

use crate::RawOsStr;

#[allow(dead_code)]
#[path = "../common/raw.rs"]
mod common_raw;
#[cfg(feature = "uniquote")]
Expand Down
Loading

0 comments on commit 7140754

Please sign in to comment.