liftoff 0.1.1

Get your coding project off the ground fast. See repo
Documentation
use crate::{perror, psuccess, error::KickError};
use leg;
use std::io::prelude::*;
use std::{fs, path::Path};

pub fn touch<P: AsRef<Path>>(path: &P) -> Result<(), KickError> {
    match fs::OpenOptions::new().create(true).write(true).open(path) {
        Err(e) => {
            perror!("Can't create file {}", path.as_ref().display());
            Err(KickError::FileError(e))
        }
        Ok(_) => {
            psuccess!("Created file      {}", path.as_ref().display());
            Ok(())
        }
    }
}

// TODO need double strangely
pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: &P, to: &Q) -> Result<(), KickError> {
    let from = from.as_ref();
    let to = to.as_ref();

    match fs::copy(from, to) {
        Err(e) => {
            perror!("Can't copy file {} from {}", to.display(), from.display());
            Err(KickError::FileError(e))
        }
        Ok(_) => {
            psuccess!("Copied  file      {} to {}", from.display(), to.display());
            Ok(())
        }
    }
}

pub fn mv<P: AsRef<Path>>(from: &P, to: &P) -> Result<(), KickError> {
    let from = from.as_ref();
    let to = to.as_ref();

    match fs::copy(from, to) {
        Err(e) => {
            perror!("Can't move file {} from {}", to.display(), from.display());
            Err(KickError::FileError(e))
        }
        Ok(_) => {
            fs::remove_file(from)?;
            psuccess!("Moved   file       {} from {}", to.display(), from.display());
            Ok(())
        }
    }
}

pub fn echo<S: AsRef<str>, P: AsRef<Path>>(text: S, path: &P) -> Result<(), KickError> {
    let text = text.as_ref();
    let path = path.as_ref();

    match fs::File::create(path) {
        Err(e) => {
            perror!("Can't write \"{}\" to {}", text, path.display());
            Err(KickError::FileError(e))
        }
        Ok(mut f) => {
            f.write_all(text.as_bytes())?;
            Ok(())
        },
    }
}

pub fn mkdir<P: AsRef<Path>>(path: &P) -> Result<(), KickError> {
    match fs::create_dir_all(path) {
        Err(e) => {
            perror!("Can't create directory {}", path.as_ref().display());
            Err(KickError::FileError(e))
        }
        Ok(_) => {
            psuccess!("Created directory {}", path.as_ref().display());
            Ok(())
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_mkdir() {
        let path = Path::new(".");
        let dir = path.join("testuytuyg34");
        mkdir(&dir).unwrap();
        assert!(Path::exists(&dir));
        fs::remove_dir(&dir).unwrap();
    }

    #[test]
    fn test_touch() {
        let path = Path::new(".");
        let file = path.join("testtouch.txt");
        touch(&file).unwrap();
        assert!(Path::exists(&file));
        fs::remove_file(&file).unwrap();
    }

    #[test]
    fn test_mv() {
        let path = Path::new(".");
        let file = path.join("testmv.txt");
        println!("HERE {}", file.display());
        touch(&file).unwrap();
        assert!(Path::exists(&file));
        let new_file = path.join("test2mv.txt");
        mv(&file, &new_file).unwrap();
        assert!(!Path::exists(&file));
        assert!(Path::exists(&new_file));
        fs::remove_file(&new_file).unwrap();
    }

    #[test]
    fn test_copy() {
        let path = Path::new(".");
        let file = path.join("testcopy.txt");
        touch(&file).unwrap();
        assert!(Path::exists(&file));
        let new_file = path.join("test2copy.txt");
        copy(&file, &new_file).unwrap();
        assert!(Path::exists(&file));
        assert!(Path::exists(&new_file));
        fs::remove_file(&new_file).unwrap();
        fs::remove_file(&file).unwrap();
    }

    #[test]
    fn test_echo() {
        let path = Path::new(".");
        let file = path.join("testecho.txt");
        echo("test", &file).unwrap();
        assert!(Path::exists(&file));
        assert_eq!(
            fs::read_to_string("testecho.txt").unwrap(),
            "test".to_string()
        );
        fs::remove_file(&file).unwrap();
    }
}