1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
/// Internal namespace.
mod private
{
use crate::*;
use std::
{
// borrow::Cow,
path::{ Path, PathBuf },
io,
};
use core::
{
fmt,
ops::
{
Deref,
DerefMut,
},
};
#[ cfg( feature="no_std" ) ]
extern crate std;
#[ cfg( feature="no_std" ) ]
use alloc::string::String;
#[ cfg( feature = "derive_serde" ) ]
use serde::{ Serialize, Deserialize };
#[ cfg( feature = "path_utf8" ) ]
use camino::{ Utf8Path, Utf8PathBuf };
/// Caninical path.
#[ cfg_attr( feature = "derive_serde", derive( Serialize, Deserialize ) ) ]
#[ derive( Debug, Default, Clone, Ord, PartialOrd, Eq, PartialEq, Hash ) ]
pub struct NativePath( PathBuf );
impl NativePath
{
/// Returns the Path without its final component, if there is one.
/// Returns None if the path terminates in a root or prefix, or if it's the empty string.
#[ inline ]
pub fn parent( &self ) -> Option< NativePath >
{
self.0.parent().map( PathBuf::from ).map( NativePath )
}
/// Creates an owned `NativePath` with path adjoined to self.
#[ inline ]
pub fn join< P >( &self, path : P ) -> NativePath
where
P : AsRef< Path >,
{
Self::try_from( self.0.join( path ) ).unwrap()
}
// /// Converts a `NativePath` to a `Cow<str>`
// pub fn to_string_lossy( &self ) -> Cow< '_, str >
// {
// self.0.to_string_lossy()
// }
/// Determines whether base is a prefix of self.
///
/// Only considers whole path components to match.
#[ inline ]
pub fn starts_with< P : AsRef< Path > >( &self, base : P ) -> bool
{
self.0.starts_with( base )
}
/// Returns inner type which is PathBuf.
#[ inline( always ) ]
pub fn inner( self ) -> PathBuf
{
self.0
}
}
impl fmt::Display for NativePath
{
#[ inline ]
fn fmt( &self, f : &mut fmt::Formatter<'_> ) -> fmt::Result
{
write!( f, "{}", self.0.display() )
}
}
// fn is_absolute( path : &Path ) -> bool
// {
// // None - not absolute
// // with `.` or `..` at the first component - not absolute
// !path.components().next().is_some_and( | c | c.as_os_str() == "." || c.as_os_str() == ".." )
// }
impl< 'a > TryFrom< &'a str > for NativePath
{
type Error = std::io::Error;
#[ inline ]
fn try_from( value : &'a str ) -> Result< Self, Self::Error >
{
let path = path::canonicalize( value )?;
// if !is_absolute( &path )
// {
// return Err( io::Error::new( io::ErrorKind::InvalidData, "Path expected to be absolute" ) )
// }
Ok( Self( path ) )
}
}
impl TryFrom< PathBuf > for NativePath
{
type Error = std::io::Error;
#[ inline ]
fn try_from( value : PathBuf ) -> Result< Self, Self::Error >
{
let path = path::canonicalize( value )?;
// if !is_absolute( &path ) { return Err( io::Error::new( io::ErrorKind::InvalidData, "Path expected to be absolute" ) ) }
Ok( Self( path ) )
}
}
impl TryFrom< &PathBuf > for NativePath
{
type Error = std::io::Error;
#[ inline ]
fn try_from( value : &PathBuf ) -> Result< Self, Self::Error >
{
let path = path::canonicalize( value )?;
// if !is_absolute( &path ) { return Err( io::Error::new( io::ErrorKind::InvalidData, "Path expected to be absolute" ) ) }
Ok( Self( path ) )
}
}
// xxx : qqq : use Into< Path >
impl TryFrom< &Path > for NativePath
{
type Error = std::io::Error;
#[ inline ]
fn try_from( value : &Path ) -> Result< Self, Self::Error >
{
let path = path::canonicalize( value )?;
// if !is_absolute( &path ) { return Err( io::Error::new( io::ErrorKind::InvalidData, "Path expected to be absolute" ) ) }
Ok( Self( path ) )
}
}
#[ cfg( feature = "path_utf8" ) ]
impl TryFrom< Utf8PathBuf > for NativePath
{
type Error = std::io::Error;
#[ inline ]
fn try_from( value : Utf8PathBuf ) -> Result< Self, Self::Error >
{
NativePath::try_from( value.as_std_path() )
}
}
#[ cfg( feature = "path_utf8" ) ]
impl TryFrom< &Utf8PathBuf > for NativePath
{
type Error = std::io::Error;
#[ inline ]
fn try_from( value : &Utf8PathBuf ) -> Result< Self, Self::Error >
{
NativePath::try_from( value.as_std_path() )
}
}
#[ cfg( feature = "path_utf8" ) ]
impl TryFrom< &Utf8Path > for NativePath
{
type Error = std::io::Error;
#[ inline ]
fn try_from( value : &Utf8Path ) -> Result< Self, Self::Error >
{
NativePath::try_from( value.as_std_path() )
}
}
impl From< NativePath > for PathBuf
{
#[ inline ]
fn from( src : NativePath ) -> Self
{
src.0
}
}
impl< 'a > TryFrom< &'a NativePath > for &'a str
{
type Error = std::io::Error;
#[ inline ]
fn try_from( src : &'a NativePath ) -> Result< &'a str, Self::Error >
{
src
.to_str()
.ok_or_else
(
move || io::Error::new( io::ErrorKind::Other, format!( "Can't convert &PathBuf into &str {src}" ) )
)
}
}
impl TryFrom< &NativePath > for String
{
type Error = std::io::Error;
#[ inline ]
fn try_from( src : &NativePath ) -> Result< String, Self::Error >
{
let src2 : &str = src.try_into()?;
Ok( src2.into() )
}
}
// impl TryFrom< Utf8PathBuf > for NativePath
// {
// type Error = std::io::Error;
//
// fn try_from( value : Utf8PathBuf ) -> Result< Self, Self::Error >
// {
// NativePath::try_from( value.as_std_path() )
// }
// }
// impl TryFrom< &Utf8Path > for NativePath
// {
// type Error = std::io::Error;
//
// fn try_from( value : &Utf8Path ) -> Result< Self, Self::Error >
// {
// NativePath::try_from( value.as_std_path() )
// }
// }
// // xxx : use derives
// impl AsRef< Path > for NativePath
// {
// fn as_ref( &self ) -> &Path
// {
// self.0.as_ref()
// }
// }
impl AsRef< Path > for NativePath
{
#[ inline ]
fn as_ref( &self ) -> &Path
{
self.0.as_ref()
}
}
impl AsMut< Path > for NativePath
{
#[ inline ]
fn as_mut( &mut self ) -> &mut Path
{
&mut self.0
}
}
impl Deref for NativePath
{
type Target = Path;
#[ inline ]
fn deref( &self ) -> &Self::Target
{
&self.0
}
}
impl DerefMut for NativePath
{
#[ inline ]
fn deref_mut( &mut self ) -> &mut Self::Target
{
&mut self.0
}
}
}
crate::mod_interface!
{
exposed use NativePath;
}