pth/path/
current_path.rs

1/// Define a private namespace for all its items.
2mod private
3{
4
5  #[ allow( clippy::wildcard_imports ) ]
6  use crate::*;
7  #[ cfg( not( feature = "no_std" ) ) ]
8  use std::
9  {
10    env,
11    io,
12  };
13
14  /// Symbolize current path.
15  #[ derive( Clone, Copy, Debug, Default, PartialEq, Eq ) ]
16  pub struct CurrentPath;
17
18  #[ cfg( feature = "path_utf8" ) ]
19  #[ cfg( not( feature = "no_std" ) ) ]
20  impl TryFrom< CurrentPath > for Utf8PathBuf
21  {
22    #[ cfg( not( feature = "no_std" ) ) ]
23    type Error = std::io::Error;
24
25    #[ inline ]
26    fn try_from( src : CurrentPath ) -> Result< Self, Self::Error >
27    {
28      Utf8PathBuf::try_from( PathBuf::try_from( src )? )
29      .map_err
30      (
31        | err |
32        {
33          #[ cfg( not( feature = "no_std" ) ) ]
34          std::io::Error::new
35          (
36            std::io::ErrorKind::NotFound,
37            format!( "Cant convert to utf8 {err}" ),
38          )
39        }
40      )
41    }
42  }
43
44  #[ cfg( not( feature = "no_std" ) ) ]
45  impl TryFrom< CurrentPath > for PathBuf
46  {
47    #[ cfg( not( feature = "no_std" ) ) ]
48    type Error = std::io::Error;
49
50    #[ inline ]
51    fn try_from( _ : CurrentPath ) -> Result< Self, Self::Error >
52    {
53      env::current_dir()
54    }
55  }
56
57  #[ cfg( not( feature = "no_std" ) ) ]
58  impl TryFrom< CurrentPath > for AbsolutePath
59  {
60    #[ cfg( not( feature = "no_std" ) ) ]
61    type Error = std::io::Error;
62
63    #[ inline ]
64    fn try_from( src : CurrentPath ) -> Result< Self, Self::Error >
65    {
66      AbsolutePath::try_from( PathBuf::try_from( src )? )
67    }
68  }
69
70  impl TryIntoPath for &CurrentPath
71  {
72    fn try_into_path( self ) -> Result< PathBuf, io::Error >
73    {
74      env::current_dir()
75    }
76  }
77
78  impl TryIntoPath for CurrentPath
79  {
80    fn try_into_path( self ) -> Result< PathBuf, io::Error >
81    {
82      env::current_dir()
83    }
84  }
85
86  impl< 'a > TryIntoCowPath< 'a > for CurrentPath
87  {
88    fn try_into_cow_path( self ) -> Result< Cow<'a, Path>, io::Error >
89    {
90      let current_dir = env::current_dir()?;
91      Ok( Cow::Owned( current_dir ) )
92    }
93  }
94
95  impl< 'a > TryIntoCowPath< 'a > for &CurrentPath
96  {
97    fn try_into_cow_path( self ) -> Result< Cow<'a, Path>, io::Error >
98    {
99      TryIntoCowPath::try_into_cow_path( *self )
100    }
101  }
102
103}
104
105crate::mod_interface!
106{
107  exposed use CurrentPath;
108}