Skip to content

Commit 72c884c

Browse files
authored
Get rid of num_enum dependency (#148)
* Get rid of num_enum dependency * Use Error in TryFrom * Use c_int for enums without a type alias
1 parent 4cbf146 commit 72c884c

File tree

12 files changed

+318
-284
lines changed

12 files changed

+318
-284
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ serde = ["dep:serde"]
6060
[dependencies]
6161
mupdf-sys = { version = "0.5.0", path = "mupdf-sys", features = ["zerocopy"] }
6262
once_cell = "1.3.1"
63-
num_enum = "0.7.0"
6463
bitflags = "2.0.2"
6564
serde = { version = "1.0.201", features = ["derive"], optional = true }
6665
zerocopy = { version = "0.8.17", features = ["derive"] }

src/device.rs

Lines changed: 122 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,43 @@
11
use std::ptr;
2-
use std::{ffi::CString, num::NonZero};
2+
use std::{
3+
ffi::{c_int, CString},
4+
num::NonZero,
5+
};
36

47
use bitflags::bitflags;
8+
59
use mupdf_sys::*;
6-
use num_enum::TryFromPrimitive;
710

811
use crate::{
9-
context, ColorParams, Colorspace, DisplayList, Error, FFIWrapper, IRect, Image, Matrix, Path,
10-
Pixmap, Rect, Shade, StrokeState, Text, TextPage, TextPageFlags,
12+
context, from_enum, ColorParams, Colorspace, DisplayList, Error, FFIWrapper, IRect, Image,
13+
Matrix, Path, Pixmap, Rect, Shade, StrokeState, Text, TextPage, TextPageFlags,
1114
};
1215

1316
mod native;
1417
pub use native::NativeDevice;
1518

16-
#[derive(Debug, Clone, Copy, PartialEq, TryFromPrimitive)]
17-
#[repr(u32)]
18-
pub enum BlendMode {
19-
/* PDF 1.4 -- standard separable */
20-
Normal = FZ_BLEND_NORMAL as u32,
21-
Multiply = FZ_BLEND_MULTIPLY as u32,
22-
Screen = FZ_BLEND_SCREEN as u32,
23-
Overlay = FZ_BLEND_OVERLAY as u32,
24-
Darken = FZ_BLEND_DARKEN as u32,
25-
Lighten = FZ_BLEND_LIGHTEN as u32,
26-
ColorDodge = FZ_BLEND_COLOR_DODGE as u32,
27-
ColorBurn = FZ_BLEND_COLOR_BURN as u32,
28-
HardLight = FZ_BLEND_HARD_LIGHT as u32,
29-
SoftLight = FZ_BLEND_SOFT_LIGHT as u32,
30-
Difference = FZ_BLEND_DIFFERENCE as u32,
31-
Exclusion = FZ_BLEND_EXCLUSION as u32,
32-
/* PDF 1.4 -- standard non-separable */
33-
Hue = FZ_BLEND_HUE as u32,
34-
Saturation = FZ_BLEND_SATURATION as u32,
35-
Color = FZ_BLEND_COLOR as u32,
36-
Luminosity = FZ_BLEND_LUMINOSITY as u32,
19+
from_enum! { c_int,
20+
#[derive(Debug, Clone, Copy, PartialEq)]
21+
pub enum BlendMode {
22+
/* PDF 1.4 -- standard separable */
23+
Normal = FZ_BLEND_NORMAL,
24+
Multiply = FZ_BLEND_MULTIPLY,
25+
Screen = FZ_BLEND_SCREEN,
26+
Overlay = FZ_BLEND_OVERLAY,
27+
Darken = FZ_BLEND_DARKEN,
28+
Lighten = FZ_BLEND_LIGHTEN,
29+
ColorDodge = FZ_BLEND_COLOR_DODGE,
30+
ColorBurn = FZ_BLEND_COLOR_BURN,
31+
HardLight = FZ_BLEND_HARD_LIGHT,
32+
SoftLight = FZ_BLEND_SOFT_LIGHT,
33+
Difference = FZ_BLEND_DIFFERENCE,
34+
Exclusion = FZ_BLEND_EXCLUSION,
35+
/* PDF 1.4 -- standard non-separable */
36+
Hue = FZ_BLEND_HUE,
37+
Saturation = FZ_BLEND_SATURATION,
38+
Color = FZ_BLEND_COLOR,
39+
Luminosity = FZ_BLEND_LUMINOSITY,
40+
}
3741
}
3842

3943
bitflags! {
@@ -56,99 +60,101 @@ bitflags! {
5660
}
5761
}
5862

59-
#[derive(Debug, PartialEq, Eq, Clone, Copy, TryFromPrimitive)]
60-
#[repr(i32)]
61-
pub enum Structure {
62-
Invalid = fz_structure_FZ_STRUCTURE_INVALID as _,
63-
64-
/* Grouping elements (PDF 1.7 - Table 10.20) */
65-
Document = fz_structure_FZ_STRUCTURE_DOCUMENT as _,
66-
Part = fz_structure_FZ_STRUCTURE_PART as _,
67-
Art = fz_structure_FZ_STRUCTURE_ART as _,
68-
Sect = fz_structure_FZ_STRUCTURE_SECT as _,
69-
Div = fz_structure_FZ_STRUCTURE_DIV as _,
70-
BlockQuote = fz_structure_FZ_STRUCTURE_BLOCKQUOTE as _,
71-
Caption = fz_structure_FZ_STRUCTURE_CAPTION as _,
72-
TOC = fz_structure_FZ_STRUCTURE_TOC as _,
73-
TOCI = fz_structure_FZ_STRUCTURE_TOCI as _,
74-
Index = fz_structure_FZ_STRUCTURE_INDEX as _,
75-
NonStruct = fz_structure_FZ_STRUCTURE_NONSTRUCT as _,
76-
Private = fz_structure_FZ_STRUCTURE_PRIVATE as _,
77-
/* Grouping elements (PDF 2.0 - Table 364) */
78-
DocumentFragment = fz_structure_FZ_STRUCTURE_DOCUMENTFRAGMENT as _,
79-
/* Grouping elements (PDF 2.0 - Table 365) */
80-
Aside = fz_structure_FZ_STRUCTURE_ASIDE as _,
81-
/* Grouping elements (PDF 2.0 - Table 366) */
82-
Title = fz_structure_FZ_STRUCTURE_TITLE as _,
83-
FENote = fz_structure_FZ_STRUCTURE_FENOTE as _,
84-
/* Grouping elements (PDF 2.0 - Table 367) */
85-
Sub = fz_structure_FZ_STRUCTURE_SUB as _,
86-
87-
/* Paragraphlike elements (PDF 1.7 - Table 10.21) */
88-
P = fz_structure_FZ_STRUCTURE_P as _,
89-
H = fz_structure_FZ_STRUCTURE_H as _,
90-
H1 = fz_structure_FZ_STRUCTURE_H1 as _,
91-
H2 = fz_structure_FZ_STRUCTURE_H2 as _,
92-
H3 = fz_structure_FZ_STRUCTURE_H3 as _,
93-
H4 = fz_structure_FZ_STRUCTURE_H4 as _,
94-
H5 = fz_structure_FZ_STRUCTURE_H5 as _,
95-
H6 = fz_structure_FZ_STRUCTURE_H6 as _,
96-
97-
/* List elements (PDF 1.7 - Table 10.23) */
98-
List = fz_structure_FZ_STRUCTURE_LIST as _,
99-
ListItem = fz_structure_FZ_STRUCTURE_LISTITEM as _,
100-
Label = fz_structure_FZ_STRUCTURE_LABEL as _,
101-
ListBody = fz_structure_FZ_STRUCTURE_LISTBODY as _,
102-
103-
/* Table elements (PDF 1.7 - Table 10.24) */
104-
Table = fz_structure_FZ_STRUCTURE_TABLE as _,
105-
TR = fz_structure_FZ_STRUCTURE_TR as _,
106-
TH = fz_structure_FZ_STRUCTURE_TH as _,
107-
TD = fz_structure_FZ_STRUCTURE_TD as _,
108-
THead = fz_structure_FZ_STRUCTURE_THEAD as _,
109-
TBody = fz_structure_FZ_STRUCTURE_TBODY as _,
110-
TFoot = fz_structure_FZ_STRUCTURE_TFOOT as _,
111-
112-
/* Inline elements (PDF 1.7 - Table 10.25) */
113-
Span = fz_structure_FZ_STRUCTURE_SPAN as _,
114-
Quote = fz_structure_FZ_STRUCTURE_QUOTE as _,
115-
Note = fz_structure_FZ_STRUCTURE_NOTE as _,
116-
Reference = fz_structure_FZ_STRUCTURE_REFERENCE as _,
117-
BibEntry = fz_structure_FZ_STRUCTURE_BIBENTRY as _,
118-
Code = fz_structure_FZ_STRUCTURE_CODE as _,
119-
Link = fz_structure_FZ_STRUCTURE_LINK as _,
120-
Annot = fz_structure_FZ_STRUCTURE_ANNOT as _,
121-
/* Inline elements (PDF 2.0 - Table 368) */
122-
Em = fz_structure_FZ_STRUCTURE_EM as _,
123-
Strong = fz_structure_FZ_STRUCTURE_STRONG as _,
124-
125-
/* Ruby inline element (PDF 1.7 - Table 10.26) */
126-
Ruby = fz_structure_FZ_STRUCTURE_RUBY as _,
127-
RB = fz_structure_FZ_STRUCTURE_RB as _,
128-
RT = fz_structure_FZ_STRUCTURE_RT as _,
129-
RP = fz_structure_FZ_STRUCTURE_RP as _,
130-
131-
/* Warichu inline element (PDF 1.7 - Table 10.26) */
132-
Warichu = fz_structure_FZ_STRUCTURE_WARICHU as _,
133-
WT = fz_structure_FZ_STRUCTURE_WT as _,
134-
WP = fz_structure_FZ_STRUCTURE_WP as _,
135-
136-
/* Illustration elements (PDF 1.7 - Table 10.27) */
137-
Figure = fz_structure_FZ_STRUCTURE_FIGURE as _,
138-
Formula = fz_structure_FZ_STRUCTURE_FORMULA as _,
139-
Form = fz_structure_FZ_STRUCTURE_FORM as _,
140-
141-
/* Artifact structure type (PDF 2.0 - Table 375) */
142-
Artifact = fz_structure_FZ_STRUCTURE_ARTIFACT as _,
63+
from_enum! { fz_structure,
64+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
65+
pub enum Structure {
66+
Invalid = fz_structure_FZ_STRUCTURE_INVALID,
67+
68+
/* Grouping elements (PDF 1.7 - Table 10.20) */
69+
Document = fz_structure_FZ_STRUCTURE_DOCUMENT,
70+
Part = fz_structure_FZ_STRUCTURE_PART,
71+
Art = fz_structure_FZ_STRUCTURE_ART,
72+
Sect = fz_structure_FZ_STRUCTURE_SECT,
73+
Div = fz_structure_FZ_STRUCTURE_DIV,
74+
BlockQuote = fz_structure_FZ_STRUCTURE_BLOCKQUOTE,
75+
Caption = fz_structure_FZ_STRUCTURE_CAPTION,
76+
TOC = fz_structure_FZ_STRUCTURE_TOC,
77+
TOCI = fz_structure_FZ_STRUCTURE_TOCI,
78+
Index = fz_structure_FZ_STRUCTURE_INDEX,
79+
NonStruct = fz_structure_FZ_STRUCTURE_NONSTRUCT,
80+
Private = fz_structure_FZ_STRUCTURE_PRIVATE,
81+
/* Grouping elements (PDF 2.0 - Table 364) */
82+
DocumentFragment = fz_structure_FZ_STRUCTURE_DOCUMENTFRAGMENT,
83+
/* Grouping elements (PDF 2.0 - Table 365) */
84+
Aside = fz_structure_FZ_STRUCTURE_ASIDE,
85+
/* Grouping elements (PDF 2.0 - Table 366) */
86+
Title = fz_structure_FZ_STRUCTURE_TITLE,
87+
FENote = fz_structure_FZ_STRUCTURE_FENOTE,
88+
/* Grouping elements (PDF 2.0 - Table 367) */
89+
Sub = fz_structure_FZ_STRUCTURE_SUB,
90+
91+
/* Paragraphlike elements (PDF 1.7 - Table 10.21) */
92+
P = fz_structure_FZ_STRUCTURE_P,
93+
H = fz_structure_FZ_STRUCTURE_H,
94+
H1 = fz_structure_FZ_STRUCTURE_H1,
95+
H2 = fz_structure_FZ_STRUCTURE_H2,
96+
H3 = fz_structure_FZ_STRUCTURE_H3,
97+
H4 = fz_structure_FZ_STRUCTURE_H4,
98+
H5 = fz_structure_FZ_STRUCTURE_H5,
99+
H6 = fz_structure_FZ_STRUCTURE_H6,
100+
101+
/* List elements (PDF 1.7 - Table 10.23) */
102+
List = fz_structure_FZ_STRUCTURE_LIST,
103+
ListItem = fz_structure_FZ_STRUCTURE_LISTITEM,
104+
Label = fz_structure_FZ_STRUCTURE_LABEL,
105+
ListBody = fz_structure_FZ_STRUCTURE_LISTBODY,
106+
107+
/* Table elements (PDF 1.7 - Table 10.24) */
108+
Table = fz_structure_FZ_STRUCTURE_TABLE,
109+
TR = fz_structure_FZ_STRUCTURE_TR,
110+
TH = fz_structure_FZ_STRUCTURE_TH,
111+
TD = fz_structure_FZ_STRUCTURE_TD,
112+
THead = fz_structure_FZ_STRUCTURE_THEAD,
113+
TBody = fz_structure_FZ_STRUCTURE_TBODY,
114+
TFoot = fz_structure_FZ_STRUCTURE_TFOOT,
115+
116+
/* Inline elements (PDF 1.7 - Table 10.25) */
117+
Span = fz_structure_FZ_STRUCTURE_SPAN,
118+
Quote = fz_structure_FZ_STRUCTURE_QUOTE,
119+
Note = fz_structure_FZ_STRUCTURE_NOTE,
120+
Reference = fz_structure_FZ_STRUCTURE_REFERENCE,
121+
BibEntry = fz_structure_FZ_STRUCTURE_BIBENTRY,
122+
Code = fz_structure_FZ_STRUCTURE_CODE,
123+
Link = fz_structure_FZ_STRUCTURE_LINK,
124+
Annot = fz_structure_FZ_STRUCTURE_ANNOT,
125+
/* Inline elements (PDF 2.0 - Table 368) */
126+
Em = fz_structure_FZ_STRUCTURE_EM,
127+
Strong = fz_structure_FZ_STRUCTURE_STRONG,
128+
129+
/* Ruby inline element (PDF 1.7 - Table 10.26) */
130+
Ruby = fz_structure_FZ_STRUCTURE_RUBY,
131+
RB = fz_structure_FZ_STRUCTURE_RB,
132+
RT = fz_structure_FZ_STRUCTURE_RT,
133+
RP = fz_structure_FZ_STRUCTURE_RP,
134+
135+
/* Warichu inline element (PDF 1.7 - Table 10.26) */
136+
Warichu = fz_structure_FZ_STRUCTURE_WARICHU,
137+
WT = fz_structure_FZ_STRUCTURE_WT,
138+
WP = fz_structure_FZ_STRUCTURE_WP,
139+
140+
/* Illustration elements (PDF 1.7 - Table 10.27) */
141+
Figure = fz_structure_FZ_STRUCTURE_FIGURE,
142+
Formula = fz_structure_FZ_STRUCTURE_FORMULA,
143+
Form = fz_structure_FZ_STRUCTURE_FORM,
144+
145+
/* Artifact structure type (PDF 2.0 - Table 375) */
146+
Artifact = fz_structure_FZ_STRUCTURE_ARTIFACT,
147+
}
143148
}
144149

145-
#[derive(Debug, PartialEq, Eq, Clone, Copy, TryFromPrimitive)]
146-
#[repr(u32)]
147-
pub enum Metatext {
148-
ActualText = fz_metatext_FZ_METATEXT_ACTUALTEXT as _,
149-
Alt = fz_metatext_FZ_METATEXT_ALT as _,
150-
Abbreviation = fz_metatext_FZ_METATEXT_ABBREVIATION as _,
151-
Title = fz_metatext_FZ_METATEXT_TITLE as _,
150+
from_enum! { fz_metatext,
151+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
152+
pub enum Metatext {
153+
ActualText = fz_metatext_FZ_METATEXT_ACTUALTEXT,
154+
Alt = fz_metatext_FZ_METATEXT_ALT,
155+
Abbreviation = fz_metatext_FZ_METATEXT_ABBREVIATION,
156+
Title = fz_metatext_FZ_METATEXT_TITLE,
157+
}
152158
}
153159

154160
pub struct DefaultColorspaces {

src/device/native.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -771,14 +771,12 @@ unsafe extern "C" fn begin_group<D: NativeDevice>(
771771
with_rust_device::<D, _>(dev, |dev| {
772772
let cs = ManuallyDrop::new(Colorspace::from_raw(color_space));
773773

774-
let blendmode = BlendMode::try_from(blendmode as u32).unwrap();
775-
776774
dev.begin_group(
777775
area.into(),
778776
&cs,
779777
isolated != 0,
780778
knockout != 0,
781-
blendmode,
779+
blendmode.try_into().unwrap(),
782780
alpha,
783781
);
784782
});
@@ -891,7 +889,7 @@ unsafe extern "C" fn begin_metatext<D: NativeDevice>(
891889
text: *const c_char,
892890
) {
893891
with_rust_device::<D, _>(dev, |dev| {
894-
let meta = Metatext::try_from(meta as u32).unwrap();
892+
let meta = Metatext::try_from(meta).unwrap();
895893
let text = unsafe { CStr::from_ptr(text) }.to_str().unwrap();
896894

897895
dev.begin_metatext(meta, text);

src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ pub enum Error {
7575
IntConversion(TryFromIntError),
7676
InvalidUtf8,
7777
UnexpectedNullPtr,
78+
UnknownEnumVariant,
7879
}
7980

8081
impl fmt::Display for Error {
@@ -91,6 +92,7 @@ impl fmt::Display for Error {
9192
f,
9293
"An FFI function call returned a null ptr when we expected a non-null ptr"
9394
),
95+
Error::UnknownEnumVariant => write!(f, "unknown enum variant provided"),
9496
}
9597
}
9698
}

src/font.rs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
1-
use std::ffi::{CStr, CString};
1+
use std::ffi::{c_int, CStr, CString};
22
use std::fmt;
33
use std::str::FromStr;
44

55
use mupdf_sys::*;
6-
use num_enum::TryFromPrimitive;
76

8-
use crate::{context, Buffer, Error, Matrix, Path};
7+
use crate::{context, from_enum, Buffer, Error, Matrix, Path};
98

10-
#[derive(Debug, Clone, Copy, PartialEq)]
11-
#[repr(C)]
12-
pub enum SimpleFontEncoding {
13-
Latin,
14-
Greek,
15-
Cyrillic,
9+
from_enum! { c_int,
10+
#[derive(Debug, Clone, Copy, PartialEq)]
11+
pub enum SimpleFontEncoding {
12+
Latin = PDF_SIMPLE_ENCODING_LATIN,
13+
Greek = PDF_SIMPLE_ENCODING_GREEK,
14+
Cyrillic = PDF_SIMPLE_ENCODING_CYRILLIC,
15+
}
1616
}
1717

18-
#[derive(Debug, Clone, Copy, PartialEq, TryFromPrimitive)]
19-
#[repr(u32)]
20-
pub enum WriteMode {
21-
Horizontal = 0,
22-
Vertical = 1,
18+
from_enum! { u32,
19+
#[derive(Debug, Clone, Copy, PartialEq)]
20+
pub enum WriteMode {
21+
Horizontal = 0,
22+
Vertical = 1,
23+
}
2324
}
2425

25-
#[derive(Debug, Clone, Copy, PartialEq)]
26-
#[repr(C)]
27-
pub enum CjkFontOrdering {
28-
AdobeCns,
29-
AdobeGb,
30-
AdobeJapan,
31-
AdobeKorea,
26+
from_enum! { c_int,
27+
#[derive(Debug, Clone, Copy, PartialEq)]
28+
pub enum CjkFontOrdering {
29+
AdobeCns = FZ_ADOBE_CNS,
30+
AdobeGb = FZ_ADOBE_GB,
31+
AdobeJapan = FZ_ADOBE_JAPAN,
32+
AdobeKorea = FZ_ADOBE_KOREA,
33+
}
3234
}
3335

3436
impl FromStr for CjkFontOrdering {

0 commit comments

Comments
 (0)