Skip to main content

bear_cli/
config.rs

1use std::env;
2use std::path::PathBuf;
3
4use anyhow::{Result, anyhow};
5
6pub fn expand_tilde(path: &str) -> Result<PathBuf> {
7    if let Some(rest) = path.strip_prefix("~/") {
8        let home = env::var_os("HOME").ok_or_else(|| anyhow!("$HOME is not set"))?;
9        return Ok(PathBuf::from(home).join(rest));
10    }
11    Ok(PathBuf::from(path))
12}
13
14pub fn app_support_dir() -> Result<PathBuf> {
15    let home = env::var_os("HOME").ok_or_else(|| anyhow!("$HOME is not set"))?;
16    Ok(PathBuf::from(home)
17        .join("Library")
18        .join("Application Support")
19        .join("bear-cli"))
20}
21
22#[cfg(test)]
23mod tests {
24    use super::expand_tilde;
25
26    #[test]
27    fn expands_tilde() {
28        let expanded = expand_tilde("~/tmp").expect("tilde should expand");
29        assert!(expanded.to_string_lossy().ends_with("/tmp"));
30    }
31}