is_file_example/
is_file_example.rs

1// Show usage of is_* functions
2use std::{
3    env,
4    fs::{self, File},
5    io,
6};
7
8use permissions::{is_executable, is_readable, is_removable, is_writable};
9
10fn main() -> io::Result<()> {
11    // Asserts with files
12    let this_program_path = env::args().next().unwrap();
13    assert!(is_executable(&this_program_path)?);
14
15    let this_file = "examples/example2.rs";
16    assert!(is_readable(&this_file)?);
17
18    let temp_file = "temp.txt";
19    File::create(temp_file)?;
20    assert!(is_writable(&temp_file)?);
21    assert!(is_removable(&temp_file)?);
22    fs::remove_file(temp_file)?;
23
24    // -------------------------
25
26    // Let's try with directories too
27    let temp_directory = "temp/";
28    fs::create_dir(temp_directory)?;
29    assert!(is_readable(&temp_directory)?);
30    assert!(is_writable(&temp_directory)?);
31    assert!(is_executable(&temp_directory)?);
32    assert!(is_removable(&temp_directory)?);
33    fs::remove_dir(temp_directory)?;
34
35    println!("Finished.");
36    Ok(())
37}