Skip to content

Commit 72f42bf

Browse files
itsjunetimevinxv
authored andcommitted
Make serde optional and move serde_json into dev-dependencies (messense#113)
* Make serde optional and move serde_json into dev-dependencies * Remove accidental zerocopy reference * Fix unused import and add serde feature to clippy CI
1 parent ef5692a commit 72f42bf

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

.github/workflows/CI.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
fetch-depth: 500
2424
- uses: dtolnay/rust-toolchain@stable
2525
- run: sudo apt-get -y install libfontconfig1-dev
26-
- run: cargo clippy --tests -- -D warnings
26+
- run: cargo clippy --tests --features serde -- -D warnings
2727

2828
test:
2929
name: Test Suite
@@ -46,12 +46,12 @@ jobs:
4646
- name: Setup msbuild
4747
if: matrix.os == 'windows-2019'
4848
uses: microsoft/setup-msbuild@v1.3.1
49-
- run: cargo test
49+
- run: cargo test --features serde
5050
timeout-minutes: 20
5151
if: matrix.os == 'windows-2019'
5252
env:
5353
LIBCLANG_PATH: "C:\\Program Files\\LLVM\\bin"
54-
- run: cargo test
54+
- run: cargo test --features serde
5555
if: matrix.os != 'windows-2019'
5656
- name: Test package mupdf-sys
5757
if: matrix.os == 'ubuntu-latest'
@@ -82,7 +82,7 @@ jobs:
8282
fetch-depth: 500
8383
- run: |
8484
rustc --version --verbose
85-
cargo test
85+
cargo test --features serde
8686
8787
asan:
8888
name: Address Sanitizer
@@ -96,9 +96,9 @@ jobs:
9696
with:
9797
components: rust-src
9898
- run: sudo apt-get -y install libfontconfig1-dev
99-
- name: cargo test
99+
- name: cargo test --features serde
100100
run: |
101-
cargo test -Zbuild-std --target x86_64-unknown-linux-gnu
101+
cargo test -Zbuild-std --target x86_64-unknown-linux-gnu --features serde
102102
env:
103103
RUSTFLAGS: -Zsanitizer=address
104104

@@ -113,7 +113,7 @@ jobs:
113113
- uses: dtolnay/rust-toolchain@stable
114114
- uses: taiki-e/install-action@valgrind
115115
- run: sudo apt-get -y install libfontconfig1-dev
116-
- run: cargo test
116+
- run: cargo test --features serde
117117
env:
118118
CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUNNER: "valgrind --error-exitcode=1 --track-origins=yes"
119119

Cargo.toml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,15 @@ html = ["mupdf-sys/html"]
4141
epub = ["mupdf-sys/epub"]
4242
all-fonts = ["mupdf-sys/all-fonts"]
4343
system-fonts = ["font-kit"]
44+
# Derive Serialize/Deserialize for a few structs
45+
serde = ["dep:serde"]
4446

4547
[dependencies]
4648
mupdf-sys = { version = "0.4.4", path = "mupdf-sys" }
4749
once_cell = "1.3.1"
4850
num_enum = "0.7.0"
4951
bitflags = "2.0.2"
50-
serde = { version = "1.0.201", features = ["derive"] }
51-
serde_json = "1.0.117"
52+
serde = { version = "1.0.201", features = ["derive"], optional = true }
5253

5354
[dependencies.font-kit]
5455
version = "0.14.1"
@@ -62,3 +63,9 @@ members = [
6263

6364
[dev-dependencies]
6465
crossbeam-utils = "0.8.1"
66+
serde_json = "1.0.117"
67+
68+
[[example]]
69+
name = "extract_stext"
70+
path = "examples/extract_stext.rs"
71+
required-features = ["serde"]

src/page.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ use std::io::Read;
33
use std::ptr;
44
use std::slice;
55

6-
use serde::{Deserialize, Serialize};
7-
86
use mupdf_sys::*;
97

108
use crate::{
@@ -381,7 +379,8 @@ impl Iterator for LinkIter {
381379
}
382380
}
383381

384-
#[derive(Deserialize, Serialize, Debug, Clone)]
382+
#[derive(Debug, Clone)]
383+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
385384
pub struct Font {
386385
pub name: String,
387386
pub family: String,
@@ -390,15 +389,17 @@ pub struct Font {
390389
pub size: u32,
391390
}
392391

393-
#[derive(Deserialize, Serialize, Debug, Clone)]
392+
#[derive(Debug, Clone)]
393+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
394394
pub struct BBox {
395395
pub x: u32,
396396
pub y: u32,
397397
pub w: u32,
398398
pub h: u32,
399399
}
400400

401-
#[derive(Deserialize, Serialize, Debug, Clone)]
401+
#[derive(Debug, Clone)]
402+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
402403
pub struct Line {
403404
pub wmode: u32,
404405
pub bbox: BBox,
@@ -408,25 +409,27 @@ pub struct Line {
408409
pub text: String,
409410
}
410411

411-
#[derive(Deserialize, Serialize, Debug, Clone)]
412+
#[derive(Debug, Clone)]
413+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
412414
pub struct Block {
413415
pub r#type: String,
414416
pub bbox: BBox,
415417
pub lines: Vec<Line>,
416418
}
417419

418420
// StructuredText
419-
#[derive(Deserialize, Serialize, Debug, Clone)]
421+
#[derive(Debug, Clone)]
422+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
420423
pub struct StextPage {
421424
pub blocks: Vec<Block>,
422425
}
423426

424427
#[cfg(test)]
425428
mod test {
426-
use crate::page::StextPage;
427429
use crate::{Document, Matrix};
428430

429431
#[test]
432+
#[cfg(feature = "serde")]
430433
fn test_get_stext_page_as_json() {
431434
let path_to_doc = std::env::current_dir()
432435
.unwrap()
@@ -437,7 +440,7 @@ mod test {
437440
let page = doc.load_page(0).unwrap();
438441
match page.stext_page_as_json_from_page(1.0) {
439442
Ok(stext_json) => {
440-
let stext_page: serde_json::Result<StextPage> =
443+
let stext_page: serde_json::Result<crate::page::StextPage> =
441444
serde_json::from_str(stext_json.as_str());
442445
match stext_page {
443446
Ok(res) => {

0 commit comments

Comments
 (0)