pth/
try_into_path.rs

1/// Define a private namespace for all its items.
2mod private
3{
4  #[ allow( unused_imports, clippy::wildcard_imports ) ]
5  use crate::*;
6  use std::
7  {
8    io,
9    path::{ Component, Path, PathBuf },
10  };
11  // use camino::{ Utf8Path, Utf8PathBuf };
12
13  /// A trait for converting various types into a `PathBuf`.
14  ///
15  /// This trait is used to convert any path-like type into an owned `PathBuf`.
16  /// Unlike `TryIntoCowPath`, it always returns an owned `PathBuf`, so there is no need to differentiate between borrowed and owned paths at runtime.
17  /// Unlike `AsPath`, it is implemented for a wider range of path-like types, similar to `TryIntoCowPath`.
18  pub trait TryIntoPath
19  {
20    /// Converts the implementing type into a `PathBuf`.
21    ///
22    /// # Returns
23    ///
24    /// * `Ok(PathBuf)` - The owned path buffer.
25    /// * `Err(io::Error)` - An error if the conversion fails.
26    /// # Errors
27    /// qqq: doc
28    fn try_into_path( self ) -> Result< PathBuf, io::Error >;
29  }
30
31  /// Implementation of `TryIntoPath` for `&str`.
32  impl TryIntoPath for &str
33  {
34    fn try_into_path( self ) -> Result< PathBuf, io::Error >
35    {
36      Ok( PathBuf::from( self ) )
37    }
38  }
39
40  /// Implementation of `TryIntoPath` for `String`.
41  impl TryIntoPath for String
42  {
43    fn try_into_path( self ) -> Result< PathBuf, io::Error >
44    {
45      Ok( PathBuf::from( self ) )
46    }
47  }
48
49  /// Implementation of `TryIntoPath` for a reference to `Path`.
50  impl TryIntoPath for &Path
51  {
52    fn try_into_path( self ) -> Result< PathBuf, io::Error >
53    {
54      Ok( self.to_path_buf() )
55    }
56  }
57
58  /// Implementation of `TryIntoPath` for `PathBuf`.
59  impl TryIntoPath for PathBuf
60  {
61    fn try_into_path( self ) -> Result< PathBuf, io::Error >
62    {
63      Ok( self )
64    }
65  }
66
67  /// Implementation of `TryIntoPath` for a reference to `Utf8Path`.
68  #[cfg( feature = "path_utf8" )]
69  impl TryIntoPath for &Utf8Path
70  {
71    fn try_into_path( self ) -> Result< PathBuf, io::Error >
72    {
73      Ok( self.as_std_path().to_path_buf() )
74    }
75  }
76
77  /// Implementation of `TryIntoPath` for `Utf8PathBuf`.
78  #[cfg( feature = "path_utf8" )]
79  impl TryIntoPath for Utf8PathBuf
80  {
81    fn try_into_path( self ) -> Result< PathBuf, io::Error >
82    {
83      Ok( self.as_std_path().to_path_buf() )
84    }
85  }
86
87  /// Implementation of `TryIntoPath` for `std::path::Component`.
88  impl TryIntoPath for Component<'_>
89  {
90    fn try_into_path( self ) -> Result< PathBuf, io::Error >
91    {
92      Ok( self.as_os_str().into() )
93    }
94  }
95
96  /// Blanket implementation of `TryIntoPath` for references to types implementing `AsRef<Path>`.
97  impl< T > TryIntoPath for &T
98  where
99    T : AsRef< Path >,
100  {
101    fn try_into_path( self ) -> Result< PathBuf, io::Error >
102    {
103      Ok( self.as_ref().to_path_buf() )
104    }
105  }
106}
107
108crate::mod_interface!
109{
110  orphan use TryIntoPath;
111}