use std::path::{Path, PathBuf}; pub fn ensured_dir>(dir_path: P) -> anyhow::Result { let dir_path = dir_path.as_ref().to_path_buf(); if !dir_path.exists() { std::fs::create_dir_all(&dir_path)?; } Ok(dir_path) } pub fn expand_home>(path: P) -> PathBuf { PathBuf::from(shellexpand::path::tilde(path.as_ref())) } pub fn canonicalize>(path: P) -> anyhow::Result { let path = expand_home(path); let parent = path .parent() .ok_or_else(|| anyhow::anyhow!("path <{}> should have parent", path.display()))?; let file_name = path .file_name() .ok_or_else(|| anyhow::anyhow!("path <{}> should have file name", path.display()))?; let parent = parent.canonicalize()?; Ok(parent.join(file_name)) } pub fn create_dir_parent>(path: P) -> anyhow::Result<()> { let path = expand_home(path); let parent = path .parent() .ok_or_else(|| anyhow::anyhow!("path <{}> should have parent", path.display()))?; std::fs::create_dir_all(parent)?; Ok(()) } // pub fn trim>(path: P) -> anyhow::Result { // let parent = path // .as_ref() // .parent() // .ok_or_else(|| anyhow::anyhow!("path <{}> should have parent", path.as_ref().display()))?; // let file_name = path.as_ref().file_name().ok_or_else(|| { // anyhow::anyhow!("path <{}> should have file name", path.as_ref().display()) // })?; // let file_name = file_name.to_str().ok_or_else(|| { // anyhow::anyhow!( // "file name in path <{}> should be valid utf-8", // path.as_ref().display() // ) // })?; // let file_name = file_name.trim_end_matches('/'); // let res = parent.join(file_name); // Ok(res) // }