jannie

jannie (mirror)
Log | Files | Refs | README | LICENSE

path_processing.rs (1183B)


      1 use std::{
      2     env::VarError,
      3     path::{Path, PathBuf},
      4 };
      5 
      6 pub fn expand_path(path: &Path) -> Result<PathBuf, Error> {
      7     let path = shellexpand::full(
      8         path.to_str()
      9             .ok_or_else(|| Error::Parse(path.to_path_buf()))?,
     10     )?;
     11     Ok(path.as_ref().into())
     12 }
     13 
     14 pub fn canonicalize_path(path: &Path) -> Result<PathBuf, Error> {
     15     let path = path.canonicalize().map_err(Error::Canonicalize)?;
     16     Ok(path)
     17 }
     18 
     19 /// Expands given path and canonicalizes it
     20 pub fn process_path(path: &Path) -> Result<PathBuf, Error> {
     21     let path = expand_path(path)?;
     22     canonicalize_path(&path)
     23 }
     24 
     25 #[derive(thiserror::Error, Debug)]
     26 pub enum Error {
     27     #[error("Error parsing path: {0:?}")]
     28     Parse(PathBuf),
     29     #[error("Error expanding path: {0}")]
     30     Expand(#[from] shellexpand::LookupError<VarError>),
     31     #[error("Error canonicalizing path: {0}")]
     32     Canonicalize(std::io::Error),
     33 }
     34 
     35 impl Error {
     36     pub fn is_file_not_found(&self) -> bool {
     37         match *self {
     38             Error::Canonicalize(ref error) => match error.kind() {
     39                 std::io::ErrorKind::NotFound => true,
     40                 _ => false,
     41             },
     42             _ => false,
     43         }
     44     }
     45 }