// Working with files in Rust

use std::fs;

fn main() {
    let input = "Foo";
    let filename = "/tmp/foo.txt";
    fs::write(filename, input).expect("Unable to write file");
    let output = fs::read_to_string(filename).expect("Unable to read file");
    assert_eq!(input, output);

    println!("{:?}", rel_path("foo/foo.log").unwrap());
    println!("{}", exe_name().unwrap());
}

/// A path relative to current excecutable
fn rel_path(path: &str) -> std::io::Result<std::path::PathBuf> {
    let mut dir = std::env::current_exe()?;
    dir.pop();
    dir.push(path);
    Ok(dir)
}

/// Returns executable name without it's path and `.exe` suffix
fn exe_name() -> Option<String> {
    std::env::current_exe()
        .ok()?
        .file_name()?
        .to_str()?
        .to_owned()
        .trim_end_matches(".exe")
        .to_owned()
        .into()
}