typed_path/
utils.rs

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