mod private
{
use crate :: *;
use std ::
{
borrow ::Cow,
path :: { Path, PathBuf },
io,
};
use core ::
{
fmt,
ops ::
{
Deref,
DerefMut,
},
};
#[ cfg( feature = "derive_serde" ) ]
use serde :: { Serialize, Deserialize };
#[ cfg_attr( feature = "derive_serde", derive( Serialize, Deserialize ) ) ]
#[ derive( Debug, Default, Clone, Ord, PartialOrd, Eq, PartialEq, Hash ) ]
pub struct NormalizedPath( PathBuf );
impl NormalizedPath
{
#[ inline ]
pub fn parent( &self ) -> Option< NormalizedPath >
{
self.0.parent().map( PathBuf ::from ).map( NormalizedPath )
}
#[ inline ]
pub fn join< P >( &self, path: P ) -> Result< NormalizedPath, io::Error >
where
P: AsRef< Path >,
{
Self ::try_from( self.0.join( path ) )
}
#[ inline ]
pub fn starts_with< P: AsRef< Path > >( &self, base: P ) -> bool
{
self.0.starts_with( base )
}
#[ inline( always ) ]
#[ must_use ]
pub fn inner( self ) -> PathBuf
{
self.0
}
}
impl fmt ::Display for NormalizedPath
{
#[ inline ]
fn fmt( &self, f: &mut fmt ::Formatter< '_ > ) -> fmt ::Result
{
write!( f, "{}", self.0.display() )
}
}
impl< 'a > TryFrom< &'a str > for NormalizedPath
{
type Error = std ::io ::Error;
#[ inline ]
fn try_from( value: &'a str ) -> Result< Self, Self ::Error >
{
let path = path ::normalize_unchecked( value );
Ok( Self( path ) )
}
}
impl< 'a > TryFrom< &'a String > for NormalizedPath
{
type Error = std ::io ::Error;
#[ inline ]
fn try_from( src: &'a String ) -> Result< Self, Self ::Error >
{
< Self as TryFrom< &Path > > ::try_from( src.as_ref() )
}
}
#[ allow( clippy ::extra_unused_lifetimes ) ]
impl< 'a > TryFrom< String > for NormalizedPath
{
type Error = std ::io ::Error;
#[ inline ]
fn try_from( src: String ) -> Result< Self, Self ::Error >
{
< Self as TryFrom< &Path > > ::try_from( src.as_ref() )
}
}
impl TryFrom< PathBuf > for NormalizedPath
{
type Error = std ::io ::Error;
#[ inline ]
fn try_from( value: PathBuf ) -> Result< Self, Self ::Error >
{
let path = path ::normalize_unchecked( value );
Ok( Self( path ) )
}
}
impl TryFrom< &Path > for NormalizedPath
{
type Error = std ::io ::Error;
#[ inline ]
fn try_from( value: &Path ) -> Result< Self, Self ::Error >
{
let path = path ::normalize_unchecked( value );
Ok( Self( path ) )
}
}
#[ cfg( feature = "path_utf8" ) ]
impl TryFrom< Utf8PathBuf > for NormalizedPath
{
type Error = std ::io ::Error;
#[ inline ]
fn try_from( value: Utf8PathBuf ) -> Result< Self, Self ::Error >
{
NormalizedPath ::try_from( value.as_std_path() )
}
}
#[ cfg( feature = "path_utf8" ) ]
impl TryFrom< &Utf8PathBuf > for NormalizedPath
{
type Error = std ::io ::Error;
#[ inline ]
fn try_from( value: &Utf8PathBuf ) -> Result< Self, Self ::Error >
{
NormalizedPath ::try_from( value.as_std_path() )
}
}
#[ cfg( feature = "path_utf8" ) ]
impl TryFrom< &Utf8Path > for NormalizedPath
{
type Error = std ::io ::Error;
#[ inline ]
fn try_from( value: &Utf8Path ) -> Result< Self, Self ::Error >
{
NormalizedPath ::try_from( value.as_std_path() )
}
}
impl From< NormalizedPath > for PathBuf
{
#[ inline ]
fn from( src: NormalizedPath ) -> Self
{
src.0
}
}
impl< 'a > TryFrom< &'a NormalizedPath > for &'a str
{
type Error = std ::io ::Error;
#[ inline ]
fn try_from( src: &'a NormalizedPath ) -> Result< &'a str, Self ::Error >
{
src
.to_str()
.ok_or_else
(
move || io ::Error ::other( format!( "Can't convert &PathBuf into &str {}", src.display() ) )
)
}
}
impl TryFrom< &NormalizedPath > for String
{
type Error = std ::io ::Error;
#[ inline ]
fn try_from( src: &NormalizedPath ) -> Result< String, Self ::Error >
{
let src2: &str = src.try_into()?;
Ok( src2.into() )
}
}
impl TryIntoPath for NormalizedPath
{
#[ inline ]
fn try_into_path( self ) -> Result< PathBuf, io ::Error >
{
Ok( self.0 )
}
}
impl< 'a > TryIntoCowPath< 'a > for NormalizedPath
{
#[ inline ]
fn try_into_cow_path( self ) -> Result< Cow<'a, Path >, io ::Error >
{
Ok( Cow ::Owned( self.0 ) )
}
}
impl AsRef< Path > for NormalizedPath
{
#[ inline ]
fn as_ref( &self ) -> &Path
{
self.0.as_ref()
}
}
impl AsMut< Path > for NormalizedPath
{
#[ inline ]
fn as_mut( &mut self ) -> &mut Path
{
&mut self.0
}
}
impl Deref for NormalizedPath
{
type Target = Path;
#[ inline ]
fn deref( &self ) -> &Self ::Target
{
&self.0
}
}
impl DerefMut for NormalizedPath
{
#[ inline ]
fn deref_mut( &mut self ) -> &mut Self::Target
{
&mut self.0
}
}
}
crate ::mod_interface!
{
exposed use NormalizedPath;
}