148 lines
4.3 KiB
Rust
148 lines
4.3 KiB
Rust
use std::{boxed::Box, io::{BufReader, BufWriter, Read, Write}, path::PathBuf};
|
|
|
|
pub mod bits;
|
|
pub mod raw;
|
|
|
|
pub type Output = Box<dyn Write>;
|
|
pub type Input = Box<dyn Read>;
|
|
|
|
pub trait ErrorString {
|
|
fn to_string(self) -> String;
|
|
}
|
|
|
|
pub struct Error {
|
|
pub exit_code: i32,
|
|
pub action: String,
|
|
pub text: String,
|
|
}
|
|
|
|
impl Error {
|
|
const DEFAULT_EXITCODE:i32 = -1;
|
|
|
|
pub fn from_str(str: &str) -> Error {
|
|
Self::from_text(str.to_owned())
|
|
}
|
|
|
|
pub fn from_text(text: String) -> Error {
|
|
Error{exit_code: Self::DEFAULT_EXITCODE, action: String::new(), text}
|
|
}
|
|
|
|
pub fn from_error<T>(error: T) -> Error where T: std::fmt::Display {
|
|
Error::from_text(error.to_string())
|
|
}
|
|
|
|
pub fn from_core_error<T>(error: T) -> Error where T: core::fmt::Display {
|
|
Error::from_text(error.to_string())
|
|
}
|
|
|
|
pub fn from_callback<F>(callback: F) -> Error where F: Fn() -> String {
|
|
Error::from_text(callback())
|
|
}
|
|
|
|
pub fn not_implemented(function: &str) -> Error {
|
|
Error::from_text(format!("{} not implemented yet", function))
|
|
}
|
|
|
|
pub fn try_or_new<T, S>(result: std::result::Result<T, S>) -> Result<T, Error> where S: std::fmt::Display {
|
|
Self::try_or_new_with_action(String::new(), result)
|
|
}
|
|
|
|
pub fn try_or_new_with_action<T, S>(action: String, result: std::result::Result<T, S>) -> Result<T, Error> where S: std::fmt::Display {
|
|
match result {
|
|
Ok(value) => Ok(value),
|
|
Err(error) => Err(Error{exit_code: Self::DEFAULT_EXITCODE, action, text: error.to_string()}),
|
|
}
|
|
}
|
|
|
|
pub fn ok_or_new<T, F>(option: Option<T>, error_text: F) -> Result<T, Error> where F: Fn () -> String{
|
|
Ok(option.ok_or(Error::from_callback(error_text))?)
|
|
}
|
|
|
|
pub fn with_action(mut self, action: &str) -> Error {
|
|
self.action = action.to_owned();
|
|
self
|
|
}
|
|
}
|
|
|
|
impl std::fmt::Display for Error {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
if self.action.is_empty() {
|
|
write!(f, "{}", self.text)
|
|
}
|
|
|
|
else {
|
|
write!(f, "{}:{}", self.action, self.text)
|
|
}
|
|
}
|
|
}
|
|
|
|
impl std::convert::From<std::io::Error> for Error {
|
|
fn from(error: std::io::Error) -> Self {
|
|
Error{exit_code: -1, action: String::new(), text: error.to_string()}
|
|
}
|
|
}
|
|
|
|
impl std::convert::From<cdtypes::Error> for Error {
|
|
fn from(error: cdtypes::Error) -> Self {
|
|
Error::from_error(error)
|
|
}
|
|
}
|
|
|
|
impl std::convert::From<std::convert::Infallible> for Error {
|
|
fn from(error: std::convert::Infallible) -> Self {
|
|
Error::from_error(error)
|
|
}
|
|
}
|
|
|
|
impl<T: ErrorString> std::convert::From<T> for Error {
|
|
fn from(error: T) -> Self {
|
|
Error::from_text(error.to_string())
|
|
}
|
|
}
|
|
|
|
pub fn open_output_file(output_path: PathBuf) -> Result<BufWriter<std::fs::File>, Error> {
|
|
Ok(std::io::BufWriter::new(std::fs::File::create(output_path)?))
|
|
}
|
|
|
|
pub fn open_output(output_file: Option<PathBuf>) -> Result<Output, Error> {
|
|
match output_file {
|
|
Some(output_path) => Ok(Box::new(open_output_file(output_path)?)),
|
|
None => Ok(Box::new(BufWriter::new(std::io::stdout()))),
|
|
}
|
|
}
|
|
|
|
pub fn open_input(input_file: Option<PathBuf>) -> Result<Input, Error> {
|
|
match input_file {
|
|
Some(input_path) => Ok(Box::new(BufReader::new(std::fs::File::open(input_path)?))),
|
|
None => Ok(Box::new(BufReader::new(std::io::stdin()))),
|
|
}
|
|
}
|
|
|
|
pub fn os_str_to_string(input: &std::ffi::OsStr, name: &str) -> Result<String, Error> {
|
|
Ok(Error::ok_or_new(input.to_str(), ||format!("Converting {} to UTF-8 failed", name))?.to_owned())
|
|
}
|
|
|
|
pub fn get_file_name_from_path_buf(input: &PathBuf, name: &str) -> Result<String, Error> {
|
|
os_str_to_string(Error::ok_or_new(input.file_name(), ||format!("No {} file name found", name))?, name)
|
|
}
|
|
|
|
pub fn input_to_vec(input: Input) -> Result<Vec<u8>, Error> {
|
|
let mut data = Vec::new();
|
|
|
|
for byte in input.bytes() {
|
|
data.push(byte?);
|
|
}
|
|
|
|
Ok(data)
|
|
}
|
|
|
|
pub fn read_file(file_path: PathBuf) -> Result<Vec<u8>, Error> {
|
|
match std::fs::read(&file_path) {
|
|
Ok(data) => {
|
|
Ok(data)
|
|
},
|
|
Err(error) => {
|
|
Err(Error::from_text(format!("Failed reading file {} with error: \"{}\"", file_path.display(), error)))
|
|
}
|
|
}
|
|
} |