Skip to content

Commit bd86ff5

Browse files
committed
Hide WASM JS bindings behind feature
This allows building a WASM binary without the JS bindins and their corresponding imports that can be used in a non-JS environment.
1 parent 48f3983 commit bd86ff5

5 files changed

Lines changed: 37 additions & 23 deletions

File tree

Cargo.toml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ inherits = "dev"
2121
opt-level = 0
2222
debug = true
2323

24+
[features]
25+
default = ["jsbindings"]
26+
jsbindings = ["dep:wasm-bindgen", "dep:tsify"]
27+
2428
[dependencies]
2529
serde = { version = "1.0", features = ["derive"] }
2630

2731
#[target.'cfg(target_arch = "wasm32")'.dependencies]
28-
wasm-bindgen = "0.2"
29-
tsify = "0.4.5"
30-
31-
32+
wasm-bindgen = { version = "0.2", optional = true }
33+
tsify = { version = "0.4.5", optional = true }

src/document.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@ use std::fs;
33
use std::io::Read;
44

55
use serde::{Deserialize, Serialize};
6+
#[cfg(feature = "jsbindings")]
67
use wasm_bindgen::prelude::wasm_bindgen;
78

89
use crate::header::RtfHeader;
910
use crate::lexer::Lexer;
1011
use crate::parser::{Parser, StyleBlock};
1112

1213
// Interface to WASM to be used in JS
13-
#[wasm_bindgen]
14+
#[cfg_attr(feature = "jsbindings", wasm_bindgen)]
1415
pub fn parse_rtf(rtf: String) -> RtfDocument {
1516
return RtfDocument::try_from(rtf).unwrap();
1617
}
1718

1819
#[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize)]
19-
#[wasm_bindgen(getter_with_clone)]
20+
#[cfg_attr(feature = "jsbindings", wasm_bindgen(getter_with_clone))]
2021
pub struct RtfDocument {
2122
pub header: RtfHeader,
2223
pub body: Vec<StyleBlock>,

src/header.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use std::collections::HashMap;
22

33
use serde::{Deserialize, Serialize};
4+
#[cfg(feature = "jsbindings")]
45
use tsify::Tsify;
6+
#[cfg(feature = "jsbindings")]
57
use wasm_bindgen::prelude::wasm_bindgen;
68

79
use crate::paragraph::Paragraph;
@@ -33,8 +35,9 @@ pub struct Style {
3335
}
3436

3537
/// Information about the document, including references to fonts & styles
36-
#[derive(Default, Debug, Clone, PartialEq, Deserialize, Serialize, Tsify)]
37-
#[tsify(into_wasm_abi, from_wasm_abi)]
38+
#[derive(Default, Debug, Clone, PartialEq, Deserialize, Serialize)]
39+
#[cfg_attr(feature = "jsbindings", derive(Tsify))]
40+
#[cfg_attr(feature = "jsbindings", tsify(into_wasm_abi, from_wasm_abi))]
3841
pub struct RtfHeader {
3942
pub character_set: CharacterSet,
4043
pub font_table: FontTable,
@@ -43,24 +46,25 @@ pub struct RtfHeader {
4346
}
4447

4548
#[derive(Hash, Default, Clone, Debug, PartialEq, Deserialize, Serialize)]
46-
#[wasm_bindgen(getter_with_clone)]
49+
#[cfg_attr(feature = "jsbindings", wasm_bindgen(getter_with_clone))]
4750
pub struct Font {
4851
pub name: String,
4952
pub character_set: u8,
5053
pub font_family: FontFamily,
5154
}
5255

5356
#[derive(Hash, Default, Clone, Debug, PartialEq, Deserialize, Serialize)]
54-
#[wasm_bindgen]
57+
#[cfg_attr(feature = "jsbindings", wasm_bindgen)]
5558
pub struct Color {
5659
pub red: u8,
5760
pub green: u8,
5861
pub blue: u8,
5962
}
6063

6164
#[allow(dead_code)]
62-
#[derive(Debug, PartialEq, Default, Clone, Hash, Deserialize, Serialize, Tsify)]
63-
#[tsify(into_wasm_abi, from_wasm_abi)]
65+
#[derive(Debug, PartialEq, Default, Clone, Hash, Deserialize, Serialize)]
66+
#[cfg_attr(feature = "jsbindings", derive(Tsify))]
67+
#[cfg_attr(feature = "jsbindings", tsify(into_wasm_abi, from_wasm_abi))]
6468
pub enum CharacterSet {
6569
#[default]
6670
Ansi,
@@ -81,8 +85,9 @@ impl CharacterSet {
8185
}
8286

8387
#[allow(dead_code)]
84-
#[derive(Debug, PartialEq, Hash, Clone, Default, Deserialize, Serialize, Tsify)]
85-
#[tsify(into_wasm_abi, from_wasm_abi)]
88+
#[derive(Debug, PartialEq, Hash, Clone, Default, Deserialize, Serialize)]
89+
#[cfg_attr(feature = "jsbindings", derive(Tsify))]
90+
#[cfg_attr(feature = "jsbindings", tsify(into_wasm_abi, from_wasm_abi))]
8691
pub enum FontFamily {
8792
#[default]
8893
Nil,

src/paragraph.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
/// Define the paragraph related structs and enums
22
use serde::{Deserialize, Serialize};
3+
4+
#[cfg(feature = "jsbindings")]
35
use tsify::Tsify;
6+
#[cfg(feature = "jsbindings")]
47
use wasm_bindgen::prelude::wasm_bindgen;
58

69
use crate::tokens::ControlWord;
710

811
#[derive(Debug, Default, Clone, Copy, PartialEq, Hash, Deserialize, Serialize)]
9-
#[wasm_bindgen]
12+
#[cfg_attr(feature = "jsbindings", wasm_bindgen)]
1013
pub struct Paragraph {
1114
pub alignment: Alignment,
1215
pub spacing: Spacing,
@@ -15,8 +18,9 @@ pub struct Paragraph {
1518
}
1619

1720
/// Alignement of a paragraph (left, right, center, justify)
18-
#[derive(Debug, Default, Clone, Copy, PartialEq, Hash, Deserialize, Serialize, Tsify)]
19-
#[tsify(into_wasm_abi, from_wasm_abi)]
21+
#[derive(Debug, Default, Clone, Copy, PartialEq, Hash, Deserialize, Serialize)]
22+
#[cfg_attr(feature = "jsbindings", derive(Tsify))]
23+
#[cfg_attr(feature = "jsbindings", tsify(into_wasm_abi, from_wasm_abi))]
2024
pub enum Alignment {
2125
#[default]
2226
LeftAligned, // \ql
@@ -39,16 +43,17 @@ impl From<&ControlWord<'_>> for Alignment {
3943

4044
/// The vertical margin before / after a block of text
4145
#[derive(Debug, Default, Clone, Copy, PartialEq, Hash, Deserialize, Serialize)]
42-
#[wasm_bindgen]
46+
#[cfg_attr(feature = "jsbindings", wasm_bindgen)]
4347
pub struct Spacing {
4448
pub before: i32,
4549
pub after: i32,
4650
pub between_line: SpaceBetweenLine,
4751
pub line_multiplier: i32,
4852
}
4953

50-
#[derive(Default, Debug, Clone, Copy, PartialEq, Hash, Deserialize, Serialize, Tsify)]
51-
#[tsify(into_wasm_abi, from_wasm_abi)]
54+
#[derive(Default, Debug, Clone, Copy, PartialEq, Hash, Deserialize, Serialize)]
55+
#[cfg_attr(feature = "jsbindings", derive(Tsify))]
56+
#[cfg_attr(feature = "jsbindings", tsify(into_wasm_abi, from_wasm_abi))]
5257
pub enum SpaceBetweenLine {
5358
Value(i32),
5459
#[default]
@@ -72,7 +77,7 @@ impl From<i32> for SpaceBetweenLine {
7277

7378
// This struct can not be an enum because left-indent and right-ident can both be defined at the same time
7479
#[derive(Default, Debug, Clone, Copy, PartialEq, Hash, Deserialize, Serialize)]
75-
#[wasm_bindgen]
80+
#[cfg_attr(feature = "jsbindings", wasm_bindgen)]
7681
pub struct Indentation {
7782
pub left: i32,
7883
pub right: i32,

src/parser.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::collections::HashMap;
22
use std::{fmt, mem};
33

44
use serde::{Deserialize, Serialize};
5+
#[cfg(feature = "jsbindings")]
56
use wasm_bindgen::prelude::wasm_bindgen;
67

78
use crate::document::RtfDocument;
@@ -20,15 +21,15 @@ macro_rules! header_control_word {
2021
}
2122

2223
#[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)]
23-
#[wasm_bindgen(getter_with_clone)]
24+
#[cfg_attr(feature = "jsbindings", wasm_bindgen(getter_with_clone))]
2425
pub struct StyleBlock {
2526
pub painter: Painter,
2627
pub paragraph: Paragraph,
2728
pub text: String,
2829
}
2930

3031
#[derive(Debug, Clone, PartialEq, Hash, Deserialize, Serialize)]
31-
#[wasm_bindgen]
32+
#[cfg_attr(feature = "jsbindings", wasm_bindgen)]
3233
pub struct Painter {
3334
pub color_ref: ColorRef,
3435
pub font_ref: FontRef,

0 commit comments

Comments
 (0)