pdf_rs/
vpdf.rs

1use std::fmt::Display;
2use std::str::FromStr;
3use crate::error::PDFError;
4
5macro_rules! pdf_version {
6    ($(($name:ident,$version:literal)),+$(,)?) => {
7        #[derive(PartialEq,Debug)]
8        pub enum PDFVersion{
9        $(
10            $name,
11        )+
12        }
13
14        impl FromStr for PDFVersion {
15            type Err = PDFError;
16            fn from_str(value: &str) -> Result<Self, Self::Err> {
17                match value {
18                    $(
19                        $version => Ok(PDFVersion::$name),
20                    )+
21                    _ => Err(PDFError::NotSupportPDFVersion(value.to_string())),
22                }
23            }
24        }
25
26        impl TryFrom<String> for PDFVersion{
27            type Error = PDFError;
28            fn try_from(value: String) -> Result<Self, Self::Error> {
29                Self::from_str(&value)
30            }
31        }
32
33        impl Display for PDFVersion {
34            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35                match self{
36                    $(
37                        PDFVersion::$name=>write!(f,"{}",$version),
38                    )+
39                }
40            }
41        }
42    }
43}
44
45pdf_version!(
46    (V1_0, "1.0"),
47    (V1_1, "1.1"),
48    (V1_2, "1.2"),
49    (V1_3, "1.3"),
50    (V1_4, "1.4"),
51    (V1_5, "1.5"),
52    (V1_6, "1.6"),
53    (V1_7, "1.7"),
54    (V2_0, "2.0")
55);