typed_path/
utils.rs

1use std::convert::TryFrom;
2use std::{env, io};
3
4use crate::{NativePathBuf, Utf8NativePathBuf};
5
6/// Returns the current working directory as [`NativePathBuf`].
7///
8/// # Errors
9///
10/// Returns an [`Err`] if the current working directory value is invalid
11/// or unable to parse the directory with the native encoding.
12///
13/// Possible cases:
14///
15/// * Current directory does not exist.
16/// * There are insufficient permissions to access the current directory.
17/// * The encoding used to parse the current directory failed to parse.
18///
19/// # Examples
20///
21/// ```
22/// fn main() -> std::io::Result<()> {
23///     let path = typed_path::utils::current_dir()?;
24///     println!("The current directory is {}", path.display());
25///     Ok(())
26/// }
27/// ```
28pub fn current_dir() -> io::Result<NativePathBuf> {
29    let std_path = env::current_dir()?;
30    match NativePathBuf::try_from(std_path) {
31        Ok(path) => Ok(path),
32        _ => Err(io::Error::new(io::ErrorKind::InvalidData, "wrong encoding")),
33    }
34}
35
36/// Returns the current working directory as [`Utf8NativePathBuf`].
37///
38/// # Errors
39///
40/// Returns an [`Err`] if the current working directory value is invalid
41/// or unable to parse the directory with the native encoding.
42///
43/// Possible cases:
44///
45/// * Current directory does not exist.
46/// * There are insufficient permissions to access the current directory.
47/// * The encoding used to parse the current directory failed to parse.
48/// * The current directory was not valid UTF8.
49///
50/// # Examples
51///
52/// ```
53/// fn main() -> std::io::Result<()> {
54///     let path = typed_path::utils::utf8_current_dir()?;
55///     println!("The current directory is {}", path);
56///     Ok(())
57/// }
58/// ```
59pub fn utf8_current_dir() -> io::Result<Utf8NativePathBuf> {
60    match Utf8NativePathBuf::from_bytes_path_buf(current_dir()?) {
61        Ok(path) => Ok(path),
62        Err(x) => Err(io::Error::new(io::ErrorKind::InvalidData, x)),
63    }
64}
65
66/// Returns the full filesystem path of the current running executable as [`NativePathBuf`].
67///
68/// # Errors
69///
70/// Returns an [`Err`] if unable to parse the path with the native encoding.
71///
72/// Additionally, returns a [`Err`] if, as [`env::current_exe`] states,
73/// a related filesystem operation or syscall fails.
74///
75///
76/// # Examples
77///
78/// ```
79/// fn main() -> std::io::Result<()> {
80///     let path = typed_path::utils::current_exe()?;
81///     println!("The current executable path is {}", path.display());
82///     Ok(())
83/// }
84/// ```
85pub fn current_exe() -> io::Result<NativePathBuf> {
86    let std_current_exe = env::current_exe()?;
87
88    match NativePathBuf::try_from(std_current_exe) {
89        Ok(path) => Ok(path),
90        Err(_) => Err(io::Error::new(io::ErrorKind::InvalidData, "wrong encoding")),
91    }
92}
93
94/// Returns the full filesystem path of the current running executable as [`Utf8NativePathBuf`].
95///
96/// # Errors
97///
98/// Returns an [`Err`] if unable to parse the path with the native encoding
99/// or the path was not valid UTF8.
100///
101/// Additionally, returns a [`Err`] if, as [`env::current_exe`] states,
102/// a related filesystem operation or syscall fails.
103///
104///
105/// # Examples
106///
107/// ```
108/// fn main() -> std::io::Result<()> {
109///     let path = typed_path::utils::utf8_current_exe()?;
110///     println!("The current executable path is {}", path);
111///     Ok(())
112/// }
113/// ```
114pub fn utf8_current_exe() -> io::Result<Utf8NativePathBuf> {
115    let typed_current_exe = current_exe()?;
116
117    match Utf8NativePathBuf::from_bytes_path_buf(typed_current_exe) {
118        Ok(path) => Ok(path),
119        Err(error) => Err(io::Error::new(io::ErrorKind::InvalidData, error)),
120    }
121}
122
123/// Returns the path of a temporary directory as [`NativePathBuf`].
124///
125/// # Errors
126///
127/// Returns an [`Err`] if unable to parse the path with the native encoding.
128///
129///
130/// # Examples
131///
132/// ```
133/// fn main() -> std::io::Result<()> {
134///     let path = typed_path::utils::temp_dir()?;
135///     println!("The temporary directory path is {}", path.display());
136///     Ok(())
137/// }
138/// ```
139pub fn temp_dir() -> io::Result<NativePathBuf> {
140    let std_temp_dir = env::temp_dir();
141
142    match NativePathBuf::try_from(std_temp_dir) {
143        Ok(path) => Ok(path),
144        Err(_) => Err(io::Error::new(io::ErrorKind::InvalidData, "wrong encoding")),
145    }
146}
147
148/// Returns the path of a temporary directory as [`Utf8NativePathBuf`].
149///
150/// # Errors
151///
152/// Returns an [`Err`] if unable to parse the path with the native encoding
153/// or the path was not valid UTF8.
154///
155///
156/// # Examples
157///
158/// ```
159/// fn main() -> std::io::Result<()> {
160///     let path = typed_path::utils::utf8_temp_dir()?;
161///     println!("The temporary directory path is {}", path);
162///     Ok(())
163/// }
164/// ```
165pub fn utf8_temp_dir() -> io::Result<Utf8NativePathBuf> {
166    let typed_temp_dir = temp_dir()?;
167
168    match Utf8NativePathBuf::from_bytes_path_buf(typed_temp_dir) {
169        Ok(path) => Ok(path),
170        Err(error) => Err(io::Error::new(io::ErrorKind::InvalidData, error)),
171    }
172}