1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
use std::path::PathBuf;

use camino::Utf8PathBuf;
use miette::miette;
use miette::Context;
use miette::IntoDiagnostic;

/// Get the current working directory of the process with [`std::env::current_dir`].
pub fn current_dir() -> miette::Result<PathBuf> {
    std::env::current_dir()
        .into_diagnostic()
        .wrap_err("Failed to get current directory")
}

/// Get the current working directory of the process as a [`Utf8PathBuf`].
pub fn current_dir_utf8() -> miette::Result<Utf8PathBuf> {
    current_dir()?
        .try_into()
        .map_err(|path| miette!("Current directory isn't valid UTF-8: {path:?}"))
}