path.rs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. use std::{
  2. fs,
  3. path::{Path, PathBuf},
  4. };
  5. use crate::{Error, Result};
  6. pub fn expand_path(path: &str) -> Result<PathBuf> {
  7. let ret: PathBuf;
  8. if path.starts_with("~/") {
  9. let homedir = dirs::home_dir().unwrap();
  10. let remains = PathBuf::from(path.strip_prefix("~/").unwrap());
  11. ret = [homedir, remains].iter().collect();
  12. } else if path.starts_with('~') {
  13. ret = dirs::home_dir().unwrap();
  14. } else {
  15. ret = PathBuf::from(path);
  16. }
  17. Ok(ret)
  18. }
  19. pub fn join_config_path(file: &Path) -> Result<PathBuf> {
  20. let mut path = PathBuf::new();
  21. let dfi_path = Path::new("darkfi");
  22. if let Some(v) = dirs::config_dir() {
  23. path.push(v);
  24. }
  25. path.push(dfi_path);
  26. path.push(file);
  27. Ok(path)
  28. }
  29. pub fn load_keypair_to_str(path: PathBuf) -> Result<String> {
  30. if Path::new(&path).exists() {
  31. let key = fs::read(&path)?;
  32. let str_buff = std::str::from_utf8(&key)?;
  33. Ok(str_buff.to_string())
  34. } else {
  35. println!("Could not parse keypair path");
  36. Err(Error::KeypairPathNotFound)
  37. }
  38. }