utils.rs 668 B

12345678910111213141516171819202122232425
  1. use std::path::PathBuf;
  2. use anyhow::{anyhow, Result};
  3. /// 检查目录是否存在
  4. pub(super) fn check_dir_exists<'a>(path: &'a PathBuf) -> Result<&'a PathBuf> {
  5. if !path.exists() {
  6. return Err(anyhow!("Path '{}' not exists", path.display()));
  7. }
  8. if !path.is_dir() {
  9. return Err(anyhow!("Path '{}' is not a directory", path.display()));
  10. }
  11. return Ok(path);
  12. }
  13. /// 获取给定路径的绝对路径
  14. pub fn abs_path(path: &PathBuf) -> PathBuf {
  15. if path.is_absolute() {
  16. path.to_path_buf()
  17. } else {
  18. let origin = std::env::current_dir().unwrap().join(path);
  19. origin.canonicalize().unwrap_or(origin)
  20. }
  21. }