1use crate::{
2 attribute::IppAttribute,
3 operation::{CreateJob, GetPrinterAttributes, IppOperation, PrintJob, SendDocument},
4 IppJobSource,
5};
6
7pub struct IppOperationBuilder;
9
10impl IppOperationBuilder {
11 pub fn print_job<T>(source: T) -> PrintJobBuilder
15 where
16 IppJobSource: From<T>,
17 {
18 PrintJobBuilder::new(source.into())
19 }
20
21 pub fn get_printer_attributes() -> GetPrinterAttributesBuilder {
23 GetPrinterAttributesBuilder::new()
24 }
25
26 pub fn create_job() -> CreateJobBuilder {
28 CreateJobBuilder::new()
29 }
30
31 pub fn send_document<T>(job_id: i32, source: T) -> SendDocumentBuilder
36 where
37 IppJobSource: From<T>,
38 {
39 SendDocumentBuilder::new(job_id, source.into())
40 }
41}
42
43pub struct PrintJobBuilder {
45 source: IppJobSource,
46 user_name: Option<String>,
47 job_title: Option<String>,
48 attributes: Vec<IppAttribute>,
49}
50
51impl PrintJobBuilder {
52 fn new(source: IppJobSource) -> PrintJobBuilder {
53 PrintJobBuilder {
54 source,
55 user_name: None,
56 job_title: None,
57 attributes: Vec::new(),
58 }
59 }
60 pub fn user_name(mut self, user_name: &str) -> Self {
62 self.user_name = Some(user_name.to_owned());
63 self
64 }
65
66 pub fn job_title(mut self, job_title: &str) -> Self {
68 self.job_title = Some(job_title.to_owned());
69 self
70 }
71
72 pub fn attribute(mut self, attribute: IppAttribute) -> Self {
74 self.attributes.push(attribute);
75 self
76 }
77
78 pub fn build(self) -> impl IppOperation {
80 let op = PrintJob::new(self.source, self.user_name.as_ref(), self.job_title.as_ref());
81 self.attributes.into_iter().fold(op, |mut op, attr| {
82 op.add_attribute(attr);
83 op
84 })
85 }
86}
87
88pub struct GetPrinterAttributesBuilder {
90 attributes: Vec<String>,
91}
92
93impl GetPrinterAttributesBuilder {
94 fn new() -> GetPrinterAttributesBuilder {
95 GetPrinterAttributesBuilder { attributes: Vec::new() }
96 }
97
98 pub fn attribute(mut self, attribute: &str) -> Self {
100 self.attributes.push(attribute.to_owned());
101 self
102 }
103
104 pub fn attributes<T>(mut self, attributes: &[T]) -> Self
106 where
107 T: AsRef<str>,
108 {
109 self.attributes
110 .extend(attributes.iter().map(|s| s.as_ref().to_string()));
111 self
112 }
113
114 pub fn build(self) -> impl IppOperation {
116 GetPrinterAttributes::with_attributes(&self.attributes)
117 }
118}
119
120pub struct CreateJobBuilder {
122 job_name: Option<String>,
123 attributes: Vec<IppAttribute>,
124}
125
126impl CreateJobBuilder {
127 fn new() -> CreateJobBuilder {
128 CreateJobBuilder {
129 job_name: None,
130 attributes: Vec::new(),
131 }
132 }
133
134 pub fn job_name(mut self, job_name: &str) -> Self {
136 self.job_name = Some(job_name.to_owned());
137 self
138 }
139
140 pub fn attribute(mut self, attribute: IppAttribute) -> Self {
142 self.attributes.push(attribute);
143 self
144 }
145
146 pub fn build(self) -> impl IppOperation {
148 let op = CreateJob::new(self.job_name.as_ref());
149 self.attributes.into_iter().fold(op, |mut op, attr| {
150 op.add_attribute(attr);
151 op
152 })
153 }
154}
155
156pub struct SendDocumentBuilder {
158 job_id: i32,
159 source: IppJobSource,
160 user_name: Option<String>,
161 is_last: bool,
162}
163
164impl SendDocumentBuilder {
165 fn new(job_id: i32, source: IppJobSource) -> SendDocumentBuilder {
166 SendDocumentBuilder {
167 job_id,
168 source,
169 user_name: None,
170 is_last: true,
171 }
172 }
173
174 pub fn user_name(mut self, user_name: &str) -> Self {
176 self.user_name = Some(user_name.to_owned());
177 self
178 }
179
180 pub fn last(mut self, last: bool) -> Self {
182 self.is_last = last;
183 self
184 }
185
186 pub fn build(self) -> impl IppOperation {
188 SendDocument::new(self.job_id, self.source, self.user_name.as_ref(), self.is_last)
189 }
190}