Skip to content

Commit 53718e9

Browse files
authored
Merge pull request #20 from heilerich/master
Hide WASM JS bindings behind feature
2 parents 48f3983 + c1ae4ed commit 53718e9

File tree

5 files changed

+32
-23
lines changed

5 files changed

+32
-23
lines changed

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: 10 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,8 @@ 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), tsify(into_wasm_abi, from_wasm_abi))]
3840
pub struct RtfHeader {
3941
pub character_set: CharacterSet,
4042
pub font_table: FontTable,
@@ -43,24 +45,24 @@ pub struct RtfHeader {
4345
}
4446

4547
#[derive(Hash, Default, Clone, Debug, PartialEq, Deserialize, Serialize)]
46-
#[wasm_bindgen(getter_with_clone)]
48+
#[cfg_attr(feature = "jsbindings", wasm_bindgen(getter_with_clone))]
4749
pub struct Font {
4850
pub name: String,
4951
pub character_set: u8,
5052
pub font_family: FontFamily,
5153
}
5254

5355
#[derive(Hash, Default, Clone, Debug, PartialEq, Deserialize, Serialize)]
54-
#[wasm_bindgen]
56+
#[cfg_attr(feature = "jsbindings", wasm_bindgen)]
5557
pub struct Color {
5658
pub red: u8,
5759
pub green: u8,
5860
pub blue: u8,
5961
}
6062

6163
#[allow(dead_code)]
62-
#[derive(Debug, PartialEq, Default, Clone, Hash, Deserialize, Serialize, Tsify)]
63-
#[tsify(into_wasm_abi, from_wasm_abi)]
64+
#[derive(Debug, PartialEq, Default, Clone, Hash, Deserialize, Serialize)]
65+
#[cfg_attr(feature = "jsbindings", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
6466
pub enum CharacterSet {
6567
#[default]
6668
Ansi,
@@ -81,8 +83,8 @@ impl CharacterSet {
8183
}
8284

8385
#[allow(dead_code)]
84-
#[derive(Debug, PartialEq, Hash, Clone, Default, Deserialize, Serialize, Tsify)]
85-
#[tsify(into_wasm_abi, from_wasm_abi)]
86+
#[derive(Debug, PartialEq, Hash, Clone, Default, Deserialize, Serialize)]
87+
#[cfg_attr(feature = "jsbindings", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
8688
pub enum FontFamily {
8789
#[default]
8890
Nil,

src/paragraph.rs

Lines changed: 10 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,8 @@ 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), tsify(into_wasm_abi, from_wasm_abi))]
2023
pub enum Alignment {
2124
#[default]
2225
LeftAligned, // \ql
@@ -39,16 +42,16 @@ impl From<&ControlWord<'_>> for Alignment {
3942

4043
/// The vertical margin before / after a block of text
4144
#[derive(Debug, Default, Clone, Copy, PartialEq, Hash, Deserialize, Serialize)]
42-
#[wasm_bindgen]
45+
#[cfg_attr(feature = "jsbindings", wasm_bindgen)]
4346
pub struct Spacing {
4447
pub before: i32,
4548
pub after: i32,
4649
pub between_line: SpaceBetweenLine,
4750
pub line_multiplier: i32,
4851
}
4952

50-
#[derive(Default, Debug, Clone, Copy, PartialEq, Hash, Deserialize, Serialize, Tsify)]
51-
#[tsify(into_wasm_abi, from_wasm_abi)]
53+
#[derive(Default, Debug, Clone, Copy, PartialEq, Hash, Deserialize, Serialize)]
54+
#[cfg_attr(feature = "jsbindings", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
5255
pub enum SpaceBetweenLine {
5356
Value(i32),
5457
#[default]
@@ -72,7 +75,7 @@ impl From<i32> for SpaceBetweenLine {
7275

7376
// This struct can not be an enum because left-indent and right-ident can both be defined at the same time
7477
#[derive(Default, Debug, Clone, Copy, PartialEq, Hash, Deserialize, Serialize)]
75-
#[wasm_bindgen]
78+
#[cfg_attr(feature = "jsbindings", wasm_bindgen)]
7679
pub struct Indentation {
7780
pub left: i32,
7881
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)