diff --git a/Cargo.toml b/Cargo.toml index c5c835b..fe6008c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,11 +21,13 @@ inherits = "dev" opt-level = 0 debug = true +[features] +default = ["jsbindings"] +jsbindings = ["dep:wasm-bindgen", "dep:tsify"] + [dependencies] serde = { version = "1.0", features = ["derive"] } #[target.'cfg(target_arch = "wasm32")'.dependencies] -wasm-bindgen = "0.2" -tsify = "0.4.5" - - +wasm-bindgen = { version = "0.2", optional = true } +tsify = { version = "0.4.5", optional = true } diff --git a/src/document.rs b/src/document.rs index fd1524b..4da33c4 100644 --- a/src/document.rs +++ b/src/document.rs @@ -3,6 +3,7 @@ use std::fs; use std::io::Read; use serde::{Deserialize, Serialize}; +#[cfg(feature = "jsbindings")] use wasm_bindgen::prelude::wasm_bindgen; use crate::header::RtfHeader; @@ -10,13 +11,13 @@ use crate::lexer::Lexer; use crate::parser::{Parser, StyleBlock}; // Interface to WASM to be used in JS -#[wasm_bindgen] +#[cfg_attr(feature = "jsbindings", wasm_bindgen)] pub fn parse_rtf(rtf: String) -> RtfDocument { return RtfDocument::try_from(rtf).unwrap(); } #[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize)] -#[wasm_bindgen(getter_with_clone)] +#[cfg_attr(feature = "jsbindings", wasm_bindgen(getter_with_clone))] pub struct RtfDocument { pub header: RtfHeader, pub body: Vec, diff --git a/src/header.rs b/src/header.rs index 2af2891..f683edb 100644 --- a/src/header.rs +++ b/src/header.rs @@ -1,7 +1,9 @@ use std::collections::HashMap; use serde::{Deserialize, Serialize}; +#[cfg(feature = "jsbindings")] use tsify::Tsify; +#[cfg(feature = "jsbindings")] use wasm_bindgen::prelude::wasm_bindgen; use crate::paragraph::Paragraph; @@ -33,8 +35,8 @@ pub struct Style { } /// Information about the document, including references to fonts & styles -#[derive(Default, Debug, Clone, PartialEq, Deserialize, Serialize, Tsify)] -#[tsify(into_wasm_abi, from_wasm_abi)] +#[derive(Default, Debug, Clone, PartialEq, Deserialize, Serialize)] +#[cfg_attr(feature = "jsbindings", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))] pub struct RtfHeader { pub character_set: CharacterSet, pub font_table: FontTable, @@ -43,7 +45,7 @@ pub struct RtfHeader { } #[derive(Hash, Default, Clone, Debug, PartialEq, Deserialize, Serialize)] -#[wasm_bindgen(getter_with_clone)] +#[cfg_attr(feature = "jsbindings", wasm_bindgen(getter_with_clone))] pub struct Font { pub name: String, pub character_set: u8, @@ -51,7 +53,7 @@ pub struct Font { } #[derive(Hash, Default, Clone, Debug, PartialEq, Deserialize, Serialize)] -#[wasm_bindgen] +#[cfg_attr(feature = "jsbindings", wasm_bindgen)] pub struct Color { pub red: u8, pub green: u8, @@ -59,8 +61,8 @@ pub struct Color { } #[allow(dead_code)] -#[derive(Debug, PartialEq, Default, Clone, Hash, Deserialize, Serialize, Tsify)] -#[tsify(into_wasm_abi, from_wasm_abi)] +#[derive(Debug, PartialEq, Default, Clone, Hash, Deserialize, Serialize)] +#[cfg_attr(feature = "jsbindings", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))] pub enum CharacterSet { #[default] Ansi, @@ -81,8 +83,8 @@ impl CharacterSet { } #[allow(dead_code)] -#[derive(Debug, PartialEq, Hash, Clone, Default, Deserialize, Serialize, Tsify)] -#[tsify(into_wasm_abi, from_wasm_abi)] +#[derive(Debug, PartialEq, Hash, Clone, Default, Deserialize, Serialize)] +#[cfg_attr(feature = "jsbindings", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))] pub enum FontFamily { #[default] Nil, diff --git a/src/paragraph.rs b/src/paragraph.rs index 50a4383..c47af0b 100644 --- a/src/paragraph.rs +++ b/src/paragraph.rs @@ -1,12 +1,15 @@ /// Define the paragraph related structs and enums use serde::{Deserialize, Serialize}; + +#[cfg(feature = "jsbindings")] use tsify::Tsify; +#[cfg(feature = "jsbindings")] use wasm_bindgen::prelude::wasm_bindgen; use crate::tokens::ControlWord; #[derive(Debug, Default, Clone, Copy, PartialEq, Hash, Deserialize, Serialize)] -#[wasm_bindgen] +#[cfg_attr(feature = "jsbindings", wasm_bindgen)] pub struct Paragraph { pub alignment: Alignment, pub spacing: Spacing, @@ -15,8 +18,8 @@ pub struct Paragraph { } /// Alignement of a paragraph (left, right, center, justify) -#[derive(Debug, Default, Clone, Copy, PartialEq, Hash, Deserialize, Serialize, Tsify)] -#[tsify(into_wasm_abi, from_wasm_abi)] +#[derive(Debug, Default, Clone, Copy, PartialEq, Hash, Deserialize, Serialize)] +#[cfg_attr(feature = "jsbindings", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))] pub enum Alignment { #[default] LeftAligned, // \ql @@ -39,7 +42,7 @@ impl From<&ControlWord<'_>> for Alignment { /// The vertical margin before / after a block of text #[derive(Debug, Default, Clone, Copy, PartialEq, Hash, Deserialize, Serialize)] -#[wasm_bindgen] +#[cfg_attr(feature = "jsbindings", wasm_bindgen)] pub struct Spacing { pub before: i32, pub after: i32, @@ -47,8 +50,8 @@ pub struct Spacing { pub line_multiplier: i32, } -#[derive(Default, Debug, Clone, Copy, PartialEq, Hash, Deserialize, Serialize, Tsify)] -#[tsify(into_wasm_abi, from_wasm_abi)] +#[derive(Default, Debug, Clone, Copy, PartialEq, Hash, Deserialize, Serialize)] +#[cfg_attr(feature = "jsbindings", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))] pub enum SpaceBetweenLine { Value(i32), #[default] @@ -72,7 +75,7 @@ impl From for SpaceBetweenLine { // This struct can not be an enum because left-indent and right-ident can both be defined at the same time #[derive(Default, Debug, Clone, Copy, PartialEq, Hash, Deserialize, Serialize)] -#[wasm_bindgen] +#[cfg_attr(feature = "jsbindings", wasm_bindgen)] pub struct Indentation { pub left: i32, pub right: i32, diff --git a/src/parser.rs b/src/parser.rs index 577f5d5..3ffadd3 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -2,6 +2,7 @@ use std::collections::HashMap; use std::{fmt, mem}; use serde::{Deserialize, Serialize}; +#[cfg(feature = "jsbindings")] use wasm_bindgen::prelude::wasm_bindgen; use crate::document::RtfDocument; @@ -20,7 +21,7 @@ macro_rules! header_control_word { } #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)] -#[wasm_bindgen(getter_with_clone)] +#[cfg_attr(feature = "jsbindings", wasm_bindgen(getter_with_clone))] pub struct StyleBlock { pub painter: Painter, pub paragraph: Paragraph, @@ -28,7 +29,7 @@ pub struct StyleBlock { } #[derive(Debug, Clone, PartialEq, Hash, Deserialize, Serialize)] -#[wasm_bindgen] +#[cfg_attr(feature = "jsbindings", wasm_bindgen)] pub struct Painter { pub color_ref: ColorRef, pub font_ref: FontRef,