use crate::defs::{MAX_PAPER_LENGTH, MIN_PAPER_LENGTH};
use std::fmt;
use std::path::Path;
pub type Result<T, E = HtopError> = std::result::Result<T, E>;
pub struct HtopError(String);
impl fmt::Debug for HtopError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl HtopError {
pub fn new(message: String) -> Self {
Self(message)
}
}
pub fn err_invalid_paper_format(format_name: &str) -> HtopError {
HtopError::new(format!("invalid paper format '{format_name}'"))
}
pub fn err_invalid_paper_width(paper_width: f64) -> HtopError {
HtopError::new(format!("paper width is out of range ({MIN_PAPER_LENGTH}..{MAX_PAPER_LENGTH} in): {paper_width} in"))
}
pub fn err_invalid_paper_height(paper_height: f64) -> HtopError {
HtopError::new(format!("paper height is out of range ({MIN_PAPER_LENGTH}..{MAX_PAPER_LENGTH} in): {paper_height} in"))
}
pub fn err_headless_chrome(reason: String) -> HtopError {
HtopError::new(format!("headless chrome failed with reason: {reason}"))
}
pub fn err_write_file(file_name: &str, reason: String) -> HtopError {
HtopError::new(format!("writing file {file_name} failed with reason: {reason}"))
}
pub fn err_read_file(file_name: &str, reason: String) -> HtopError {
HtopError::new(format!("reading file {file_name} failed with reason: {reason}"))
}
pub fn err_read_dir(dir_name: &str, reason: String) -> HtopError {
HtopError::new(format!("reading directory {dir_name} failed with reason: {reason}"))
}
pub fn err_canonicalize(path: &Path, reason: String) -> HtopError {
let path = path.to_string_lossy();
HtopError::new(format!("canonicalizing failed for path {path} with reason: {reason}"))
}
pub fn err_invalid_length(length: &str) -> HtopError {
HtopError::new(format!("invalid length: {length}"))
}
pub fn err_invalid_number(number: &str) -> HtopError {
HtopError::new(format!("invalid number: {number}"))
}
pub fn err_invalid_timeout(timeout: &str) -> HtopError {
HtopError::new(format!("invalid timeout: {timeout}"))
}
pub fn err_invalid_width(width: &str) -> HtopError {
HtopError::new(format!("invalid width: {width}"))
}
pub fn err_invalid_height(height: &str) -> HtopError {
HtopError::new(format!("invalid height: {height}"))
}
pub fn err_invalid_margin(margin: &str) -> HtopError {
HtopError::new(format!("invalid margin: {margin}"))
}
pub fn err_invalid_window_size(window_size: &str) -> HtopError {
HtopError::new(format!("invalid window size: {window_size}"))
}
pub fn err_invalid_paper_size(paper_size: &str) -> HtopError {
HtopError::new(format!("invalid paper size: {paper_size}"))
}