Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
5 changes: 3 additions & 2 deletions src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ use std::fs;
use std::io::Read;

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

use crate::header::RtfHeader;
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<StyleBlock>,
Expand Down
18 changes: 10 additions & 8 deletions src/header.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -43,24 +45,24 @@ 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,
pub font_family: FontFamily,
}

#[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,
pub blue: u8,
}

#[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,
Expand All @@ -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,
Expand Down
17 changes: 10 additions & 7 deletions src/paragraph.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand All @@ -39,16 +42,16 @@ 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,
pub between_line: SpaceBetweenLine,
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]
Expand All @@ -72,7 +75,7 @@ impl From<i32> 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,
Expand Down
5 changes: 3 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,15 +21,15 @@ 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,
pub text: String,
}

#[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,
Expand Down