1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use std::convert::TryFrom;
use std::{env, io};

use crate::{NativePathBuf, Utf8NativePathBuf};

/// Returns the current working directory as [`NativePathBuf`].
///
/// # Errors
///
/// Returns an [`Err`] if the current working directory value is invalid
/// or unable to parse the directory with the native encoding.
///
/// Possible cases:
///
/// * Current directory does not exist.
/// * There are insufficient permissions to access the current directory.
/// * The encoding used to parse the current directory failed to parse.
///
/// # Examples
///
/// ```
/// fn main() -> std::io::Result<()> {
///     let path = typed_path::utils::current_dir()?;
///     println!("The current directory is {}", path.display());
///     Ok(())
/// }
/// ```
pub fn current_dir() -> io::Result<NativePathBuf> {
    let std_path = env::current_dir()?;
    match NativePathBuf::try_from(std_path) {
        Ok(path) => Ok(path),
        _ => Err(io::Error::new(io::ErrorKind::InvalidData, "wrong encoding")),
    }
}

/// Returns the current working directory as [`Utf8NativePathBuf`].
///
/// # Errors
///
/// Returns an [`Err`] if the current working directory value is invalid
/// or unable to parse the directory with the native encoding.
///
/// Possible cases:
///
/// * Current directory does not exist.
/// * There are insufficient permissions to access the current directory.
/// * The encoding used to parse the current directory failed to parse.
/// * The current directory was not valid UTF8.
///
/// # Examples
///
/// ```
/// fn main() -> std::io::Result<()> {
///     let path = typed_path::utils::utf8_current_dir()?;
///     println!("The current directory is {}", path);
///     Ok(())
/// }
/// ```
pub fn utf8_current_dir() -> io::Result<Utf8NativePathBuf> {
    match Utf8NativePathBuf::from_bytes_path_buf(current_dir()?) {
        Ok(path) => Ok(path),
        Err(x) => Err(io::Error::new(io::ErrorKind::InvalidData, x)),
    }
}