ipp_proto/
builder.rs

1use crate::{
2    attribute::IppAttribute,
3    operation::{CreateJob, GetPrinterAttributes, IppOperation, PrintJob, SendDocument},
4    IppJobSource,
5};
6
7/// Builder to create IPP operations
8pub struct IppOperationBuilder;
9
10impl IppOperationBuilder {
11    /// Create PrintJob operation
12    ///
13    /// * `source` - `IppJobSource`
14    pub fn print_job<T>(source: T) -> PrintJobBuilder
15    where
16        IppJobSource: From<T>,
17    {
18        PrintJobBuilder::new(source.into())
19    }
20
21    /// Create GetPrinterAttributes operation
22    pub fn get_printer_attributes() -> GetPrinterAttributesBuilder {
23        GetPrinterAttributesBuilder::new()
24    }
25
26    /// Create CreateJob operation
27    pub fn create_job() -> CreateJobBuilder {
28        CreateJobBuilder::new()
29    }
30
31    /// Create SendDocument operation
32    ///
33    /// * `job_id` - job id returned by Create-Job operation <br/>
34    /// * `source` - `IppJobSource` <br/>
35    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
43/// Builder to create PrintJob operation
44pub 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    /// Specify requesting-user-name attribute
61    pub fn user_name(mut self, user_name: &str) -> Self {
62        self.user_name = Some(user_name.to_owned());
63        self
64    }
65
66    /// Specify job-name attribute
67    pub fn job_title(mut self, job_title: &str) -> Self {
68        self.job_title = Some(job_title.to_owned());
69        self
70    }
71
72    /// Specify custom job attribute
73    pub fn attribute(mut self, attribute: IppAttribute) -> Self {
74        self.attributes.push(attribute);
75        self
76    }
77
78    /// Build operation
79    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
88/// Builder to create GetPrinterAttributes operation
89pub struct GetPrinterAttributesBuilder {
90    attributes: Vec<String>,
91}
92
93impl GetPrinterAttributesBuilder {
94    fn new() -> GetPrinterAttributesBuilder {
95        GetPrinterAttributesBuilder { attributes: Vec::new() }
96    }
97
98    /// Specify which attribute to retrieve from the printer. Can be repeated.
99    pub fn attribute(mut self, attribute: &str) -> Self {
100        self.attributes.push(attribute.to_owned());
101        self
102    }
103
104    /// Specify which attributes to retrieve from the printer
105    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    /// Build operation
115    pub fn build(self) -> impl IppOperation {
116        GetPrinterAttributes::with_attributes(&self.attributes)
117    }
118}
119
120/// Builder to create CreateJob operation
121pub 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    /// Specify job-name attribute
135    pub fn job_name(mut self, job_name: &str) -> Self {
136        self.job_name = Some(job_name.to_owned());
137        self
138    }
139
140    /// Specify custom job attribute
141    pub fn attribute(mut self, attribute: IppAttribute) -> Self {
142        self.attributes.push(attribute);
143        self
144    }
145
146    /// Build operation
147    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
156/// Builder to create SendDocument operation
157pub 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    /// Specify originating-user-name attribute
175    pub fn user_name(mut self, user_name: &str) -> Self {
176        self.user_name = Some(user_name.to_owned());
177        self
178    }
179
180    /// Parameter which indicates whether this document is a last one
181    pub fn last(mut self, last: bool) -> Self {
182        self.is_last = last;
183        self
184    }
185
186    /// Build operation
187    pub fn build(self) -> impl IppOperation {
188        SendDocument::new(self.job_id, self.source, self.user_name.as_ref(), self.is_last)
189    }
190}