rxing/pdf417/encoder/
compaction.rs

1/*
2 * Copyright 2011 ZXing authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use crate::Exceptions;
18
19/**
20 * Represents possible PDF417 barcode compaction types.
21 */
22#[derive(Clone, Copy, PartialEq, Eq)]
23pub enum Compaction {
24    AUTO = 0,
25    TEXT = 1,
26    BYTE = 2,
27    NUMERIC = 3,
28}
29
30impl TryFrom<&String> for Compaction {
31    type Error = Exceptions;
32
33    fn try_from(value: &String) -> Result<Self, Self::Error> {
34        if let Ok(num_val) = value.parse::<u8>() {
35            match num_val {
36                0 => return Ok(Compaction::AUTO),
37                1 => return Ok(Compaction::TEXT),
38                2 => return Ok(Compaction::BYTE),
39                3 => return Ok(Compaction::NUMERIC),
40                _ => {}
41            }
42        }
43        Err(Exceptions::format_with(format!(
44            "Compaction must be 0-3 (inclusivie). Found: {value}"
45        )))
46    }
47}