qx_rs_file 0.0.0

quick file util in rust.
Documentation
use std::{fs::File, io::{self, Read}};

use qx_rs_err::err::*;

pub fn read_file(path: &str) -> Result<Option<File>> {
    _read_file(path)
}

pub fn read_buffer(path: &str) -> Result<Option<Vec<u8>>> {
    if let Some(mut f) = _read_file(path)? {
        let mut buf = Vec::<u8>::new();
        let _ = f.read_to_end(&mut buf).map_err(|err| {
            Error::error(Box::new(err))
        })?;
        Ok(Some(buf))
    } else {
        Ok(None)
    }
}

pub fn read_text(path: &str) -> Result<Option<String>> {
    if let Some(mut f) = _read_file(path)? {
        let mut text = String::new();
        let _ = f.read_to_string(&mut text).map_err(|err| {
            Error::error(Box::new(err))
        })?;
        Ok(Some(text))
    } else {
        Ok(None)
    }
}

fn _read_file(path: &str) -> Result<Option<File>> {
    let res = File::open(path);
    match res {
        Ok(file) => {
            Ok(Some(file))
        },
        Err(err) => {
            match err.kind() {
                io::ErrorKind::NotFound => {
                    Ok(None)
                },
                _ => {
                    Err(Error::error(Box::new(err)))
                }
            }           
        }
    }
}