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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use std::{
fmt, io,
path::{Path, PathBuf},
};
use ipp_proto::{ipp::StatusCode, ParseError};
pub use crate::client::IppClient;
pub mod client;
#[derive(Debug)]
pub enum IppError {
HttpError(reqwest::Error),
IOError(::std::io::Error),
StatusError(StatusCode),
PrinterStateError(Vec<String>),
PrinterStopped,
ParamError(String),
ParseError(ParseError),
MissingAttribute,
InvalidAttributeType,
}
impl fmt::Display for IppError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
IppError::HttpError(ref e) => write!(f, "{}", e),
IppError::IOError(ref e) => write!(f, "{}", e),
IppError::StatusError(ref e) => write!(f, "IPP status error: {}", e),
IppError::ParamError(ref e) => write!(f, "IPP param error: {}", e),
IppError::PrinterStateError(ref e) => write!(f, "IPP printer state error: {:?}", e),
IppError::PrinterStopped => write!(f, "IPP printer stopped"),
IppError::ParseError(ref e) => write!(f, "{}", e),
IppError::MissingAttribute => write!(f, "Missing attribute in response"),
IppError::InvalidAttributeType => write!(f, "Invalid attribute type"),
}
}
}
impl From<io::Error> for IppError {
fn from(error: io::Error) -> Self {
IppError::IOError(error)
}
}
impl From<StatusCode> for IppError {
fn from(code: StatusCode) -> Self {
IppError::StatusError(code)
}
}
impl From<reqwest::Error> for IppError {
fn from(error: reqwest::Error) -> Self {
IppError::HttpError(error)
}
}
impl From<ParseError> for IppError {
fn from(error: ParseError) -> Self {
IppError::ParseError(error)
}
}
impl std::error::Error for IppError {}
pub struct IppClientBuilder {
uri: String,
ca_certs: Vec<PathBuf>,
verify_hostname: bool,
verify_certificate: bool,
timeout: u64,
}
impl IppClientBuilder {
pub fn new(uri: &str) -> Self {
IppClientBuilder {
uri: uri.to_owned(),
ca_certs: Vec::new(),
verify_hostname: true,
verify_certificate: true,
timeout: 0,
}
}
pub fn ca_cert<P>(mut self, path: P) -> Self
where
P: AsRef<Path>,
{
self.ca_certs.push(path.as_ref().to_owned());
self
}
pub fn ca_certs<I, P>(mut self, paths: I) -> Self
where
I: IntoIterator<Item = P>,
P: AsRef<Path>,
{
self.ca_certs.extend(paths.into_iter().map(|p| p.as_ref().to_owned()));
self
}
pub fn verify_hostname(mut self, verify: bool) -> Self {
self.verify_hostname = verify;
self
}
pub fn verify_certificate(mut self, verify: bool) -> Self {
self.verify_certificate = verify;
self
}
pub fn timeout(mut self, timeout: u64) -> Self {
self.timeout = timeout;
self
}
pub fn build(self) -> IppClient {
IppClient {
uri: self.uri,
ca_certs: self.ca_certs,
verify_hostname: self.verify_hostname,
verify_certificate: self.verify_certificate,
timeout: self.timeout,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_builder() {
let mut builder = IppClientBuilder::new("foobar");
assert_eq!(builder.uri, "foobar");
let cert = PathBuf::from("mycert");
builder = builder.ca_cert(&cert);
assert_eq!(builder.ca_certs, vec![cert.clone()]);
builder = builder.ca_certs(&[cert.clone()]);
assert_eq!(builder.ca_certs, vec![cert.clone(), cert.clone()]);
builder = builder.verify_hostname(false);
assert!(!builder.verify_hostname);
builder = builder.verify_certificate(false);
assert!(!builder.verify_certificate);
builder = builder.timeout(100);
assert_eq!(builder.timeout, 100);
let _ = builder.build();
}
}