Skip to content

Commit fbbca22

Browse files
authored
Wasm feature gate (#622)
* conditional wasm * derive ts * ts export * incl wasm feature
1 parent 971fceb commit fbbca22

38 files changed

+124
-74
lines changed

docx-core/Cargo.toml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,22 @@ keywords = [
1717
name = "docx_rs"
1818
path = "src/lib.rs"
1919

20+
[features]
21+
wasm = ["wasm-bindgen", "ts-rs"]
22+
2023
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
2124

2225
[dependencies]
2326
xml-rs = "0.8.4"
24-
wasm-bindgen = "0.2.78"
2527
thiserror = "1.0"
2628
zip = { version = "0.6.3", default-features = false, features = ["deflate"] }
2729
serde = { version = "1.0", features = ["derive"] }
28-
serde_json = "1.0"
30+
serde_json = {version = "1.0" }
2931
base64 = "0.13.1"
3032
image = { version = "0.24.4", default-features = false, features=["gif", "jpeg", "png", "bmp", "tiff"] }
31-
ts-rs = "6.1"
33+
wasm-bindgen = { version = "0.2.78", optional = true }
34+
ts-rs = { version = "6.1", optional = true }
3235

3336
[dev-dependencies]
3437
pretty_assertions = "1.3.0"
35-
insta = "1.16"
38+
insta = "1.16"

docx-core/src/documents/elements/fld_char.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ use crate::documents::*;
44
use crate::types::*;
55
use crate::xml_builder::*;
66

7-
#[derive(Serialize, Debug, Clone, PartialEq, ts_rs::TS)]
8-
#[ts(export)]
7+
#[derive(Serialize, Debug, Clone, PartialEq)]
8+
#[cfg_attr(feature = "wasm", derive(ts_rs::TS))]
9+
#[cfg_attr(feature = "wasm", ts(export))]
910
#[serde(rename_all = "camelCase")]
1011
pub struct FieldChar {
1112
pub field_char_type: FieldCharType,

docx-core/src/documents/elements/font_scheme.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
use serde::Serialize;
22

3-
#[derive(Debug, Clone, PartialEq, Serialize, ts_rs::TS)]
4-
#[ts(export)]
3+
#[derive(Debug, Clone, PartialEq, Serialize)]
4+
#[cfg_attr(feature = "wasm", derive(ts_rs::TS))]
5+
#[cfg_attr(feature = "wasm", ts(export))]
56
#[serde(rename_all = "camelCase")]
67
pub struct FontSchemeFont {
78
pub script: String,
89
pub typeface: String,
910
}
1011

11-
#[derive(Debug, Clone, PartialEq, Serialize, Default, ts_rs::TS)]
12-
#[ts(export)]
12+
#[derive(Debug, Clone, PartialEq, Serialize, Default)]
13+
#[cfg_attr(feature = "wasm", derive(ts_rs::TS))]
14+
#[cfg_attr(feature = "wasm", ts(export))]
1315
#[serde(rename_all = "camelCase")]
1416
pub struct FontGroup {
1517
pub latin: String,
@@ -18,8 +20,9 @@ pub struct FontGroup {
1820
pub fonts: Vec<FontSchemeFont>,
1921
}
2022

21-
#[derive(Debug, Clone, PartialEq, Serialize, Default, ts_rs::TS)]
22-
#[ts(export)]
23+
#[derive(Debug, Clone, PartialEq, Serialize, Default)]
24+
#[cfg_attr(feature = "wasm", derive(ts_rs::TS))]
25+
#[cfg_attr(feature = "wasm", ts(export))]
2326
#[serde(rename_all = "camelCase")]
2427
pub struct FontScheme {
2528
pub major_font: FontGroup,

docx-core/src/documents/elements/instr_hyperlink.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use serde::Serialize;
22

33
// https://c-rex.net/projects/samples/ooxml/e1/Part4/OOXML_P4_DOCX_HYPERLINKHYPERLINK_topic_ID0EFYG1.html
4-
#[derive(Serialize, Debug, Clone, PartialEq, Default, ts_rs::TS)]
5-
#[ts(export)]
4+
#[derive(Serialize, Debug, Clone, PartialEq, Default)]
5+
#[cfg_attr(feature = "wasm", derive(ts_rs::TS))]
6+
#[cfg_attr(feature = "wasm", ts(export))]
67
#[serde(rename_all = "camelCase")]
78
pub struct InstrHyperlink {
89
pub target: String,

docx-core/src/documents/elements/instr_toc.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ use serde::Serialize;
22

33
use crate::documents::*;
44

5-
#[derive(Serialize, Debug, Clone, PartialEq, Default, ts_rs::TS)]
6-
#[ts(export)]
5+
#[derive(Serialize, Debug, Clone, PartialEq, Default)]
6+
#[cfg_attr(feature = "wasm", derive(ts_rs::TS))]
7+
#[cfg_attr(feature = "wasm", ts(export))]
78
pub struct StyleWithLevel(pub (String, usize));
89

910
impl StyleWithLevel {
@@ -12,8 +13,9 @@ impl StyleWithLevel {
1213
}
1314
}
1415
// https://c-rex.net/projects/samples/ooxml/e1/Part4/OOXML_P4_DOCX_TOCTOC_topic_ID0ELZO1.html
15-
#[derive(Serialize, Debug, Clone, PartialEq, Default, ts_rs::TS)]
16-
#[ts(export)]
16+
#[derive(Serialize, Debug, Clone, PartialEq, Default)]
17+
#[cfg_attr(feature = "wasm", derive(ts_rs::TS))]
18+
#[cfg_attr(feature = "wasm", ts(export))]
1719
#[serde(rename_all = "camelCase")]
1820
pub struct InstrToC {
1921
// \o If no heading range is specified, all heading levels used in the document are listed.

docx-core/src/documents/elements/pic.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ use crate::documents::*;
55
use crate::types::*;
66
use crate::xml_builder::*;
77

8-
#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
9-
#[ts(export)]
8+
#[derive(Debug, Clone, Serialize, PartialEq)]
9+
#[cfg_attr(feature = "wasm", derive(ts_rs::TS))]
10+
#[cfg_attr(feature = "wasm", ts(export))]
1011
#[serde(rename_all = "camelCase")]
1112
pub struct Pic {
1213
pub id: String,

docx-core/src/documents/elements/shape.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use serde::Serialize;
22

3-
#[derive(Serialize, Debug, Clone, PartialEq, Default, ts_rs::TS)]
4-
#[ts(export)]
3+
#[derive(Serialize, Debug, Clone, PartialEq, Default)]
4+
#[cfg_attr(feature = "wasm", derive(ts_rs::TS))]
5+
#[cfg_attr(feature = "wasm", ts(export))]
56
#[serde(rename_all = "camelCase")]
67
pub struct Shape {
78
#[serde(skip_serializing_if = "Option::is_none")]
@@ -11,8 +12,9 @@ pub struct Shape {
1112
}
1213
// Experimental, For now reader only.
1314

14-
#[derive(Serialize, Debug, Clone, PartialEq, Default, ts_rs::TS)]
15-
#[ts(export)]
15+
#[derive(Serialize, Debug, Clone, PartialEq, Default)]
16+
#[cfg_attr(feature = "wasm", derive(ts_rs::TS))]
17+
#[cfg_attr(feature = "wasm", ts(export))]
1618
#[serde(rename_all = "camelCase")]
1719
pub struct ImageData {
1820
pub id: String,

docx-core/src/documents/elements/table_cell_property.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
use wasm_bindgen::prelude::*;
21
use serde::Serialize;
2+
#[cfg(feature = "wasm")]
3+
use wasm_bindgen::prelude::*;
34

45
use super::*;
56
use crate::documents::BuildXML;
67
use crate::types::*;
78
use crate::xml_builder::*;
89

9-
#[wasm_bindgen]
10+
#[cfg_attr(feature = "wasm", wasm_bindgen)]
1011
#[derive(Serialize, Debug, Clone, PartialEq)]
1112
#[serde(rename_all = "camelCase")]
1213
pub struct TableCellProperty {

docx-core/src/documents/elements/table_property.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use serde::Serialize;
2+
#[cfg(feature = "wasm")]
23
use wasm_bindgen::prelude::*;
34

45
use super::*;
56
use crate::documents::BuildXML;
67
use crate::types::*;
78
use crate::xml_builder::*;
89

9-
#[wasm_bindgen]
10+
#[cfg_attr(feature = "wasm", wasm_bindgen)]
1011
#[derive(Debug, Clone, PartialEq, Serialize)]
1112
#[serde(rename_all = "camelCase")]
1213
pub struct TableProperty {

docx-core/src/documents/theme.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ use serde::Serialize;
22

33
use super::*;
44

5-
#[derive(Debug, Clone, PartialEq, Serialize, Default, ts_rs::TS)]
6-
#[ts(export)]
5+
#[derive(Debug, Clone, PartialEq, Serialize, Default)]
6+
#[cfg_attr(feature = "wasm", derive(ts_rs::TS))]
7+
#[cfg_attr(feature = "wasm", ts(export))]
78
#[serde(rename_all = "camelCase")]
89
pub struct Theme {
910
pub font_schema: FontScheme,

docx-core/src/types/alignment_type.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use std::fmt;
22
use std::str::FromStr;
3+
#[cfg(feature = "wasm")]
34
use wasm_bindgen::prelude::*;
45

56
use super::errors;
67

7-
#[wasm_bindgen]
8+
#[cfg_attr(feature = "wasm", wasm_bindgen)]
89
#[derive(Copy, Clone, Debug)]
910
pub enum AlignmentType {
1011
Both,

docx-core/src/types/border_position.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use serde::Serialize;
2+
#[cfg(feature = "wasm")]
23
use wasm_bindgen::prelude::*;
34

4-
#[wasm_bindgen]
5+
#[cfg_attr(feature = "wasm", wasm_bindgen)]
56
#[derive(Debug, Clone, PartialEq, Serialize)]
67
#[serde(rename_all = "camelCase")]
78
pub enum TableBorderPosition {
@@ -13,7 +14,7 @@ pub enum TableBorderPosition {
1314
InsideV,
1415
}
1516

16-
#[wasm_bindgen]
17+
#[cfg_attr(feature = "wasm", wasm_bindgen)]
1718
#[derive(Debug, Clone, PartialEq, Serialize)]
1819
#[serde(rename_all = "camelCase")]
1920
pub enum TableCellBorderPosition {

docx-core/src/types/border_type.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
//
44
use serde::{Deserialize, Serialize};
55
use std::fmt;
6+
#[cfg(feature = "wasm")]
67
use wasm_bindgen::prelude::*;
78

89
use super::errors;
910
use std::str::FromStr;
1011

11-
#[wasm_bindgen]
12+
#[cfg_attr(feature = "wasm", wasm_bindgen)]
1213
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
1314
#[serde(rename_all = "camelCase")]
1415
pub enum BorderType {

docx-core/src/types/break_type.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ use serde::{Deserialize, Serialize};
66

77
use std::fmt;
88
use std::str::FromStr;
9+
#[cfg(feature = "wasm")]
910
use wasm_bindgen::prelude::*;
1011

1112
use super::errors;
1213

13-
#[wasm_bindgen]
14+
#[cfg_attr(feature = "wasm", wasm_bindgen)]
1415
#[derive(Copy, Clone, Debug, Deserialize, Serialize, PartialEq)]
1516
pub enum BreakType {
1617
Page,

docx-core/src/types/doc_grid_type.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ use serde::{Deserialize, Serialize};
22

33
use std::fmt;
44
use std::str::FromStr;
5+
#[cfg(feature = "wasm")]
56
use wasm_bindgen::prelude::*;
67

78
use super::errors;
89

9-
#[wasm_bindgen]
10+
#[cfg_attr(feature = "wasm", wasm_bindgen)]
1011
#[derive(Copy, Clone, Debug, Deserialize, Serialize, PartialEq)]
1112
#[serde(rename_all = "camelCase")]
1213
pub enum DocGridType {

docx-core/src/types/drawing_position.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
use serde::Serialize;
22
use std::fmt;
33

4+
#[cfg(feature = "wasm")]
45
use wasm_bindgen::prelude::*;
56

6-
#[wasm_bindgen]
7-
#[derive(Debug, Clone, Copy, Serialize, PartialEq, ts_rs::TS)]
8-
#[ts(export)]
7+
#[cfg_attr(feature = "wasm", wasm_bindgen)]
8+
#[derive(Debug, Clone, Copy, Serialize, PartialEq)]
9+
#[cfg_attr(feature = "wasm", derive(ts_rs::TS))]
10+
#[cfg_attr(feature = "wasm", ts(export))]
911
#[serde(rename_all = "camelCase")]
1012
pub enum DrawingPositionType {
1113
Anchor,
1214
Inline,
1315
}
1416

15-
#[wasm_bindgen]
16-
#[derive(Debug, Clone, Copy, Serialize, PartialEq, ts_rs::TS)]
17-
#[ts(export)]
17+
#[cfg_attr(feature = "wasm", wasm_bindgen)]
18+
#[derive(Debug, Clone, Copy, Serialize, PartialEq)]
19+
#[cfg_attr(feature = "wasm", derive(ts_rs::TS))]
20+
#[cfg_attr(feature = "wasm", ts(export))]
1821
#[serde(rename_all = "camelCase")]
1922
pub enum PicAlign {
2023
Left,
@@ -36,8 +39,9 @@ impl fmt::Display for PicAlign {
3639
}
3740
}
3841

39-
#[derive(Debug, Clone, Copy, Serialize, PartialEq, ts_rs::TS)]
40-
#[ts(export)]
42+
#[derive(Debug, Clone, Copy, Serialize, PartialEq)]
43+
#[cfg_attr(feature = "wasm", derive(ts_rs::TS))]
44+
#[cfg_attr(feature = "wasm", ts(export))]
4145
#[serde(rename_all = "camelCase")]
4246
pub enum DrawingPosition {
4347
Offset(i32),

docx-core/src/types/field_char_type.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ use serde::{Deserialize, Serialize};
55
//
66
use std::fmt;
77
use std::str::FromStr;
8+
#[cfg(feature = "wasm")]
89
use wasm_bindgen::prelude::*;
910

1011
use super::errors;
1112

12-
#[wasm_bindgen]
13-
#[derive(Copy, Clone, Debug, Deserialize, Serialize, PartialEq, ts_rs::TS)]
14-
#[ts(export)]
13+
#[cfg_attr(feature = "wasm", wasm_bindgen)]
14+
#[derive(Copy, Clone, Debug, Deserialize, Serialize, PartialEq)]
15+
#[cfg_attr(feature = "wasm", derive(ts_rs::TS))]
16+
#[cfg_attr(feature = "wasm", ts(export))]
1517
#[serde(rename_all = "camelCase")]
1618
pub enum FieldCharType {
1719
Begin,

docx-core/src/types/font_pitch_type.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use std::fmt;
2+
#[cfg(feature = "wasm")]
23
use wasm_bindgen::prelude::*;
34

4-
#[wasm_bindgen]
5+
#[cfg_attr(feature = "wasm", wasm_bindgen)]
56
#[derive(Copy, Clone, Debug)]
67
pub enum FontPitchType {
78
Default,

docx-core/src/types/height_rule.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use std::fmt;
2+
#[cfg(feature = "wasm")]
23
use wasm_bindgen::prelude::*;
34

45
use serde::Serialize;
56

67
use super::errors;
78
use std::str::FromStr;
89

9-
#[wasm_bindgen]
10+
#[cfg_attr(feature = "wasm", wasm_bindgen)]
1011
#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
1112
#[serde(rename_all = "camelCase")]
1213
pub enum HeightRule {

docx-core/src/types/hyperlink_type.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
use std::fmt;
2+
#[cfg(feature = "wasm")]
23
use wasm_bindgen::prelude::*;
34

45
use serde::Serialize;
56

67
use super::errors;
78
use std::str::FromStr;
89

9-
#[wasm_bindgen]
10-
#[derive(Debug, Clone, PartialEq, Serialize, ts_rs::TS)]
10+
#[cfg_attr(feature = "wasm", wasm_bindgen)]
11+
#[derive(Debug, Clone, PartialEq, Serialize)]
12+
#[cfg_attr(feature = "wasm", derive(ts_rs::TS))]
1113
#[serde(rename_all = "camelCase")]
12-
#[ts(export)]
14+
#[cfg_attr(feature = "wasm", ts(export))]
1315
pub enum HyperlinkType {
1416
Anchor,
1517
External,

docx-core/src/types/level_suffix_type.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use std::fmt;
2+
#[cfg(feature = "wasm")]
23
use wasm_bindgen::prelude::*;
34

45
use serde::Serialize;
56

67
use super::errors;
78
use std::str::FromStr;
89

9-
#[wasm_bindgen]
10+
#[cfg_attr(feature = "wasm", wasm_bindgen)]
1011
#[derive(Debug, Clone, PartialEq, Serialize)]
1112
#[serde(rename_all = "camelCase")]
1213
pub enum LevelSuffixType {

docx-core/src/types/line_spacing_type.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ use crate::types::errors;
22
use crate::TypeError;
33
use serde::*;
44
use std::str::FromStr;
5+
#[cfg(feature = "wasm")]
56
use wasm_bindgen::prelude::*;
67

7-
#[wasm_bindgen]
8+
#[cfg_attr(feature = "wasm", wasm_bindgen)]
89
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
910
#[serde(rename_all = "camelCase")]
1011
pub enum LineSpacingType {

0 commit comments

Comments
 (0)