escl 0.3.0

A Rust library for discovering and using scanners via the eSCL protocol (scanning over network)
Documentation
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
use serde::{de::Visitor, Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct ScannerCapabilities {
    pub version: String,
    pub make_and_model: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub manufacturer: Option<String>,
    pub serial_number: String,
    #[serde(rename = "UUID")]
    pub uuid: String,
    #[serde(rename = "AdminURI")]
    pub admin_uri: String,
    #[serde(rename = "IconURI")]
    pub icon_uri: String,
    #[serde(default, skip_serializing_if = "Certifications::is_empty")]
    pub certifications: Certifications,
    pub platen: Platen,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub adf: Option<Adf>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub compression_factor_support: Option<CompressionFactorSupport>,
    #[serde(default, skip_serializing_if = "SupportedMediaTypes::is_empty")]
    pub supported_media_types: SupportedMediaTypes,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub sharpen_support: Option<SharpenSupport>,
}

#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct Certifications {
    pub certification: Vec<Certification>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct Certification {
    pub name: String,
    pub version: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct Platen {
    pub platen_input_caps: InputCaps,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct Adf {
    pub adf_simplex_input_caps: InputCaps,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct InputCaps {
    pub min_width: u32,
    pub max_width: u32,
    pub min_height: u32,
    pub max_height: u32,
    pub max_scan_regions: u32,
    pub setting_profiles: SettingProfiles,
    pub supported_intents: SupportedIntents,
    pub max_optical_x_resolution: u32,
    pub max_optical_y_resolution: u32,
    pub risky_left_margin: u32,
    pub risky_right_margin: u32,
    pub risky_top_margin: u32,
    pub risky_bottom_margin: u32,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct SettingProfiles {
    pub setting_profile: SettingProfile,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct SettingProfile {
    pub color_modes: ColorModes,
    #[serde(default, skip_serializing_if = "ContentTypes::is_empty")]
    pub content_types: ContentTypes,
    pub document_formats: DocumentFormats,
    pub supported_resolutions: SupportedResolutions,
    pub color_spaces: ColorSpaces,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ccd_channels: Option<CcdChannels>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct ColorModes {
    pub color_mode: Vec<ColorMode>,
}

#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct ContentTypes {
    pub content_type: Vec<ContentType>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct DocumentFormats {
    pub document_format: Vec<String>,
    pub document_format_ext: Vec<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct SupportedResolutions {
    pub discrete_resolutions: DiscreteResolutions,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct DiscreteResolutions {
    pub discrete_resolution: Vec<DiscreteResolution>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct DiscreteResolution {
    pub x_resolution: u32,
    pub y_resolution: u32,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct ColorSpaces {
    pub color_space: Vec<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct CcdChannels {
    pub ccd_channel: Vec<CcdChannel>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct SupportedIntents {
    pub intent: Vec<ScanIntent>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct CompressionFactorSupport {
    pub min: u32,
    pub max: u32,
    pub normal: u32,
    pub step: u32,
}

#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct SupportedMediaTypes {
    pub media_type: Vec<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct SharpenSupport {
    pub min: u32,
    pub max: u32,
    pub normal: u32,
    pub step: u32,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ColorMode {
    /// Binary monochrome scanning. Valid only for certain DocumentFormat/DocumentFormatExt values -
    /// like 'application/octet-stream', 'image/tiff' that can support single-bit scans. For
    /// document format not supporting BlackAndWhite1 color mode, scanner SHOULD report a 409
    /// Conflict error.
    BlackAndWhite1,
    /// 8-bit grayscale
    Grayscale8,
    /// 16-bit grayscale
    Grayscale16,
    /// 8-bit per channel RGB
    RGB24,
    /// 16-bit per channel RGB
    RGB48,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ContentType {
    Photo,
    Text,
    TextAndPhoto,
    LineArt,
    Magazine,
    Halftone,
    Auto,
    Custom(String),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CcdChannel {
    /// Use the Red CCD
    Red,
    /// Use the Green CCD
    Green,
    /// Use the Blue CCD
    Blue,
    /// Weighted combination of the three color channels optimized for photos
    NTSC,
    /// A dedicated Gray CCD array in the hardware (optimized for documents)
    GrayCcd,
    /// An emulated Gray CCD mode where each CCD line are given even weight (1/3 R, 1/3 G, 1/3 B)
    /// (optimized for documents).
    GrayCcdEmulated,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ScanIntent {
    /// Scanning optimized for text.
    Document,
    /// A composite document with mixed text/graphic/photo content.
    TextAndGraphic,
    /// Scanning optimized for photo
    Photo,
    /// Scanning optimized for performance (fast output)
    Preview,
    /// Scanning optimized for 3 dimensional objects - objects with depth
    Object,
    /// Scanning optimized for a business card
    BusinessCard,
    Custom(String),
}

struct ColorModeVisitor;
struct ContentTypeVisitor;
struct CcdChannelVisitor;
struct ScanIntentVisitor;

impl Certifications {
    fn is_empty(&self) -> bool {
        self.certification.is_empty()
    }
}

impl ContentTypes {
    fn is_empty(&self) -> bool {
        self.content_type.is_empty()
    }
}

impl SupportedMediaTypes {
    fn is_empty(&self) -> bool {
        self.media_type.is_empty()
    }
}

impl ColorModes {
    /// Gets the highest support quality RGB color mode. If no RGB color mode is supported, `None`
    // is returned.
    pub fn color(&self) -> Option<ColorMode> {
        if self.color_mode.contains(&ColorMode::RGB48) {
            Some(ColorMode::RGB48)
        } else if self.color_mode.contains(&ColorMode::RGB24) {
            Some(ColorMode::RGB24)
        } else {
            None
        }
    }
}

impl Serialize for ColorMode {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(match self {
            Self::BlackAndWhite1 => "BlackAndWhite1",
            Self::Grayscale8 => "Grayscale8",
            Self::Grayscale16 => "Grayscale16",
            Self::RGB24 => "RGB24",
            Self::RGB48 => "RGB48",
        })
    }
}

impl<'de> Deserialize<'de> for ColorMode {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        deserializer.deserialize_str(ColorModeVisitor)
    }
}

impl<'de> Visitor<'de> for ColorModeVisitor {
    type Value = ColorMode;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(formatter, "string")
    }

    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(match v {
            "BlackAndWhite1" => ColorMode::BlackAndWhite1,
            "Grayscale8" => ColorMode::Grayscale8,
            "Grayscale16" => ColorMode::Grayscale16,
            "RGB24" => ColorMode::RGB24,
            "RGB48" => ColorMode::RGB48,
            _ => {
                return Err(serde::de::Error::invalid_value(
                    serde::de::Unexpected::Str(v),
                    &"valid ColorMode value",
                ))
            }
        })
    }
}

impl Serialize for ContentType {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(match self {
            Self::Photo => "Photo",
            Self::Text => "Text",
            Self::TextAndPhoto => "TextAndPhoto",
            Self::LineArt => "LineArt",
            Self::Magazine => "Magazine",
            Self::Halftone => "Halftone",
            Self::Auto => "Auto",
            Self::Custom(custom) => custom,
        })
    }
}

impl<'de> Deserialize<'de> for ContentType {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        deserializer.deserialize_str(ContentTypeVisitor)
    }
}

impl<'de> Visitor<'de> for ContentTypeVisitor {
    type Value = ContentType;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(formatter, "string")
    }

    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(match v {
            "Photo" => ContentType::Photo,
            "Text" => ContentType::Text,
            "TextAndPhoto" => ContentType::TextAndPhoto,
            "LineArt" => ContentType::LineArt,
            "Magazine" => ContentType::Magazine,
            "Halftone" => ContentType::Halftone,
            "Auto" => ContentType::Auto,
            custom => ContentType::Custom(custom.to_owned()),
        })
    }
}

impl Serialize for CcdChannel {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(match self {
            CcdChannel::Red => "Red",
            CcdChannel::Green => "Green",
            CcdChannel::Blue => "Blue",
            CcdChannel::NTSC => "NTSC",
            CcdChannel::GrayCcd => "GrayCcd",
            CcdChannel::GrayCcdEmulated => "GrayCcdEmulated",
        })
    }
}

impl<'de> Deserialize<'de> for CcdChannel {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        deserializer.deserialize_str(CcdChannelVisitor)
    }
}

impl<'de> Visitor<'de> for CcdChannelVisitor {
    type Value = CcdChannel;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(formatter, "string")
    }

    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(match v {
            "Red" => CcdChannel::Red,
            "Green" => CcdChannel::Green,
            "Blue" => CcdChannel::Blue,
            "NTSC" => CcdChannel::NTSC,
            "GrayCcd" => CcdChannel::GrayCcd,
            "GrayCcdEmulated" => CcdChannel::GrayCcdEmulated,
            _ => {
                return Err(serde::de::Error::invalid_value(
                    serde::de::Unexpected::Str(v),
                    &"valid ColorMode value",
                ))
            }
        })
    }
}

impl Serialize for ScanIntent {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(match self {
            Self::Document => "Document",
            Self::TextAndGraphic => "TextAndGraphic",
            Self::Photo => "Photo",
            Self::Preview => "Preview",
            Self::Object => "Object",
            Self::BusinessCard => "BusinessCard",
            Self::Custom(custom) => custom,
        })
    }
}

impl<'de> Deserialize<'de> for ScanIntent {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        deserializer.deserialize_str(ScanIntentVisitor)
    }
}

impl<'de> Visitor<'de> for ScanIntentVisitor {
    type Value = ScanIntent;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(formatter, "string")
    }

    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(match v {
            "Document" => ScanIntent::Document,
            "TextAndGraphic" => ScanIntent::TextAndGraphic,
            "Photo" => ScanIntent::Photo,
            "Preview" => ScanIntent::Preview,
            "Object" => ScanIntent::Object,
            "BusinessCard" => ScanIntent::BusinessCard,
            custom => ScanIntent::Custom(custom.to_owned()),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    pub fn test_capabilities_deser() {
        for raw_xml in [
            include_str!("../test-data/capabilities/brother_mfc_j497dw.xml"),
            include_str!("../test-data/capabilities/canon_ts5300_series.xml"),
            include_str!("../test-data/capabilities/canon_ts7450.xml"),
        ]
        .into_iter()
        {
            serde_xml_rs::from_str::<ScannerCapabilities>(raw_xml)
                .expect("capabilities deserializing failure");
        }
    }
}