1pub mod env;
4pub mod envs;
5
6#[cfg(feature = "std")]
7pub mod paths;
8
9#[cfg(feature = "std")]
10pub mod vars;
11
12#[cfg(feature = "std")]
13use cap_std::fs_utf8::Dir;
14
15pub const ASIMOV_HOME: &str = "ASIMOV_HOME";
16
17#[cfg(feature = "std")]
18const CONFIG_PATH: &str = ".config/asimov";
19
20#[cfg(feature = "std")]
22pub fn home_dir() -> std::io::Result<Dir> {
23 use cap_directories::UserDirs;
24
25 let ambient_authority = cap_std::ambient_authority();
26
27 let user_dirs = UserDirs::new().unwrap();
28 user_dirs.home_dir(ambient_authority).map(Dir::from_cap_std)
29}
30
31#[cfg(feature = "std")]
33pub fn config_dir() -> std::io::Result<Dir> {
34 use std::{
35 env::VarError,
36 io::{Error, ErrorKind},
37 };
38
39 let ambient_authority = cap_std::ambient_authority();
40
41 match std::env::var(ASIMOV_HOME) {
42 Err(VarError::NotUnicode(_)) => Err(Error::new(
43 ErrorKind::InvalidData,
44 "ASIMOV_HOME is not valid Unicode",
45 )),
46 Err(VarError::NotPresent) => home_dir()
47 .and_then(|home_dir| {
48 home_dir.create_dir_all(CONFIG_PATH)?;
49 Ok(home_dir)
50 })
51 .and_then(|home_dir| home_dir.open_dir(CONFIG_PATH)),
52 Ok(path) if path.trim().is_empty() => {
53 Err(Error::new(ErrorKind::InvalidData, "ASIMOV_HOME is empty"))
54 },
55 Ok(path) => Dir::create_ambient_dir_all(&path, ambient_authority)
56 .and_then(|_| Dir::open_ambient_dir(&path, ambient_authority)),
57 }
58}