alnyan/yggdrasil: implement join_paths()/split_paths()
This commit is contained in:
parent
127b7e16a5
commit
82cfeff7e1
@ -32,6 +32,7 @@ mod env {
|
|||||||
use super::env_imp;
|
use super::env_imp;
|
||||||
use crate::collections::hash_map;
|
use crate::collections::hash_map;
|
||||||
use crate::ffi::{OsStr, OsString};
|
use crate::ffi::{OsStr, OsString};
|
||||||
|
use crate::sys_common::AsInner;
|
||||||
use crate::{fmt, io};
|
use crate::{fmt, io};
|
||||||
|
|
||||||
pub struct Env {
|
pub struct Env {
|
||||||
@ -71,21 +72,27 @@ mod env {
|
|||||||
Env { iter }
|
Env { iter }
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO check for = in name
|
|
||||||
pub fn getenv(name: &OsStr) -> Option<OsString> {
|
pub fn getenv(name: &OsStr) -> Option<OsString> {
|
||||||
|
if name.as_inner().as_encoded_bytes().contains(&b'=') {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
let env = env_imp::read();
|
let env = env_imp::read();
|
||||||
env.get(name).cloned()
|
env.get(name).cloned()
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO check for = in name
|
|
||||||
pub unsafe fn setenv(name: &OsStr, value: &OsStr) -> io::Result<()> {
|
pub unsafe fn setenv(name: &OsStr, value: &OsStr) -> io::Result<()> {
|
||||||
|
if name.as_inner().as_encoded_bytes().contains(&b'=') {
|
||||||
|
return Err(io::Error::new(io::ErrorKind::Uncategorized, "Invalid env var name"));
|
||||||
|
}
|
||||||
let mut env = env_imp::write();
|
let mut env = env_imp::write();
|
||||||
env.insert(name.to_owned(), value.to_owned());
|
env.insert(name.to_owned(), value.to_owned());
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO check for = in name
|
|
||||||
pub unsafe fn unsetenv(name: &OsStr) -> io::Result<()> {
|
pub unsafe fn unsetenv(name: &OsStr) -> io::Result<()> {
|
||||||
|
if name.as_inner().as_encoded_bytes().contains(&b'=') {
|
||||||
|
return Err(io::Error::new(io::ErrorKind::Uncategorized, "Invalid env var name"));
|
||||||
|
}
|
||||||
let mut env = env_imp::write();
|
let mut env = env_imp::write();
|
||||||
env.remove(name);
|
env.remove(name);
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -97,10 +104,13 @@ mod paths {
|
|||||||
use crate::ffi::{OsStr, OsString};
|
use crate::ffi::{OsStr, OsString};
|
||||||
use crate::marker::PhantomData;
|
use crate::marker::PhantomData;
|
||||||
use crate::path::{Path, PathBuf};
|
use crate::path::{Path, PathBuf};
|
||||||
use crate::sys::{cvt_io, run_with_path_str};
|
use crate::sys::{cvt_io, os_str, run_with_path_str};
|
||||||
use crate::{fmt, io};
|
use crate::sys_common::{AsInner, FromInner};
|
||||||
|
use crate::{fmt, io, iter, mem, slice};
|
||||||
|
|
||||||
pub struct SplitPaths<'a>(!, PhantomData<&'a ()>);
|
pub struct SplitPaths<'a> {
|
||||||
|
iter: iter::Map<slice::Split<'a, u8, fn(&u8) -> bool>, fn(&'a [u8]) -> PathBuf>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct JoinPathsError;
|
pub struct JoinPathsError;
|
||||||
@ -108,34 +118,61 @@ mod paths {
|
|||||||
impl<'a> Iterator for SplitPaths<'a> {
|
impl<'a> Iterator for SplitPaths<'a> {
|
||||||
type Item = PathBuf;
|
type Item = PathBuf;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<PathBuf> {
|
||||||
todo!()
|
self.iter.next()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||||
|
self.iter.size_hint()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for JoinPathsError {
|
impl fmt::Display for JoinPathsError {
|
||||||
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
todo!()
|
write!(f, "path segment contains separator `/`")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StdError for JoinPathsError {
|
impl StdError for JoinPathsError {
|
||||||
#[allow(deprecated)]
|
#[allow(deprecated)]
|
||||||
fn description(&self) -> &str {
|
fn description(&self) -> &str {
|
||||||
todo!()
|
"failed to join paths"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn split_paths(_path: &OsStr) -> SplitPaths<'_> {
|
pub fn split_paths(unparsed: &OsStr) -> SplitPaths<'_> {
|
||||||
todo!()
|
fn bytes_to_path(b: &[u8]) -> PathBuf {
|
||||||
|
PathBuf::from(unsafe { mem::transmute::<_, &OsStr>(b) })
|
||||||
|
}
|
||||||
|
fn is_separator(b: &u8) -> bool {
|
||||||
|
*b == b'/'
|
||||||
|
}
|
||||||
|
let unparsed = &unparsed.as_inner().inner;
|
||||||
|
SplitPaths {
|
||||||
|
iter: unparsed
|
||||||
|
.split(is_separator as fn(&u8) -> bool)
|
||||||
|
.map(bytes_to_path as fn(&[u8]) -> PathBuf),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn join_paths<I, T>(_paths: I) -> Result<OsString, JoinPathsError>
|
pub fn join_paths<I, T>(paths: I) -> Result<OsString, JoinPathsError>
|
||||||
where
|
where
|
||||||
I: Iterator<Item = T>,
|
I: Iterator<Item = T>,
|
||||||
T: AsRef<OsStr>,
|
T: AsRef<OsStr>,
|
||||||
{
|
{
|
||||||
todo!()
|
let mut joined = Vec::new();
|
||||||
|
|
||||||
|
for (i, path) in paths.enumerate() {
|
||||||
|
let path = &path.as_ref().as_inner().inner;
|
||||||
|
if i > 0 {
|
||||||
|
joined.push(b'/')
|
||||||
|
}
|
||||||
|
if path.contains(&b'/') {
|
||||||
|
return Err(JoinPathsError);
|
||||||
|
}
|
||||||
|
joined.extend_from_slice(path);
|
||||||
|
}
|
||||||
|
Ok(FromInner::from_inner(os_str::Buf { inner: joined }))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn current_exe() -> io::Result<PathBuf> {
|
pub fn current_exe() -> io::Result<PathBuf> {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user