-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors.rs
More file actions
106 lines (85 loc) · 3.63 KB
/
errors.rs
File metadata and controls
106 lines (85 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//! # Error definitions
use crate::defs::{MAX_PAPER_LENGTH, MIN_PAPER_LENGTH};
use std::fmt;
use std::path::Path;
/// Common result type.
pub type Result<T, E = HtopError> = std::result::Result<T, E>;
/// Common error definition.
pub struct HtopError(String);
impl fmt::Debug for HtopError {
/// Implements [Debug](fmt::Debug) trait for [HtopError].
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl HtopError {
/// Creates a new [HtopError] with specified message.
pub fn new(message: String) -> Self {
Self(message)
}
}
/// Creates invalid paper format error.
pub fn err_invalid_paper_format(format_name: &str) -> HtopError {
HtopError::new(format!("invalid paper format '{format_name}'"))
}
/// Creates invalid paper width error.
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"))
}
/// Creates invalid paper height error.
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"))
}
/// Creates an error with failure reason message from headless chrome.
pub fn err_headless_chrome(reason: String) -> HtopError {
HtopError::new(format!("headless chrome failed with reason: {reason}"))
}
/// Creates an error with file writing failure reason.
pub fn err_write_file(file_name: &str, reason: String) -> HtopError {
HtopError::new(format!("writing file {file_name} failed with reason: {reason}"))
}
/// Creates an error with file reading failure reason.
pub fn err_read_file(file_name: &str, reason: String) -> HtopError {
HtopError::new(format!("reading file {file_name} failed with reason: {reason}"))
}
/// Creates an error with directory reading failure reason.
pub fn err_read_dir(dir_name: &str, reason: String) -> HtopError {
HtopError::new(format!("reading directory {dir_name} failed with reason: {reason}"))
}
/// Creates an error when canonicalizing a path fails.
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}"))
}
/// Creates an error when invalid length was encountered.
pub fn err_invalid_length(length: &str) -> HtopError {
HtopError::new(format!("invalid length: {length}"))
}
/// Creates an error when invalid number was encountered.
pub fn err_invalid_number(number: &str) -> HtopError {
HtopError::new(format!("invalid number: {number}"))
}
/// Creates an error when invalid timeout was encountered.
pub fn err_invalid_timeout(timeout: &str) -> HtopError {
HtopError::new(format!("invalid timeout: {timeout}"))
}
/// Creates an error when invalid width was encountered.
pub fn err_invalid_width(width: &str) -> HtopError {
HtopError::new(format!("invalid width: {width}"))
}
/// Creates an error when invalid height was encountered.
pub fn err_invalid_height(height: &str) -> HtopError {
HtopError::new(format!("invalid height: {height}"))
}
/// Creates an error when invalid margin was encountered.
pub fn err_invalid_margin(margin: &str) -> HtopError {
HtopError::new(format!("invalid margin: {margin}"))
}
/// Creates an error when invalid window size was encountered.
pub fn err_invalid_window_size(window_size: &str) -> HtopError {
HtopError::new(format!("invalid window size: {window_size}"))
}
/// Creates an error when invalid paper size was encountered.
pub fn err_invalid_paper_size(paper_size: &str) -> HtopError {
HtopError::new(format!("invalid paper size: {paper_size}"))
}