awpak_utils/
file_utils.rs

1use std::{io::Error};
2
3pub fn path_for_file( str_path : &str ) -> Result<std::path::PathBuf, Error>
4{
5    let path = std::path::Path::new( str_path );
6
7    path_for_file_from_path( path )
8}
9
10pub fn path_for_file_from_path( path : &std::path::Path ) -> Result<std::path::PathBuf, Error>
11{
12    path_exists( path )?;
13    path_is_file( path )?;
14
15    Ok( path.into() )
16}
17
18pub fn path_exists( path : &std::path::Path ) -> Result<(), Error>
19{
20    match path.exists()
21    {
22        true => Ok( () ),
23        false => Err( std::io::Error::new( std::io::ErrorKind::NotFound, format!( "{:?} not exists.", path ) ) )
24    }
25}
26
27pub fn path_is_dir( path : &std::path::Path ) -> Result<(), Error>
28{
29    match path.is_dir()
30    {
31        true => Ok( () ),
32        false => Err( std::io::Error::new( std::io::ErrorKind::NotADirectory, format!( "{:?} is not a directory", path ) ) )
33    }
34}
35
36pub fn path_is_file( path : &std::path::Path ) -> Result<(), Error>
37{
38    match path.is_file()
39    {
40        true => Ok( () ),
41        false => Err( std::io::Error::new( std::io::ErrorKind::InvalidFilename, format!( "{:?} is not a file", path ) ) )
42    }
43}
44
45// fn path_is_exec( path : &std::path::Path ) -> Result<(), impl Error>
46// {
47//     match path.is_executable()
48//     {
49//         true => Ok( () ),
50//         false => Err( Error::NotExec( path.to_str().unwrap_or( "" ).to_string() ) )  
51//     }
52// }