Skip to content

Commit

Permalink
Merge pull request #15 from pvichivanives/master
Browse files Browse the repository at this point in the history
Remove Deriative
  • Loading branch information
d0rianb authored Oct 14, 2024
2 parents 0a2fe13 + ac27f1a commit ad2a660
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 14 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ opt-level = 0
debug = true

[dependencies]
derivative = "2.2.0"
serde = { version = "1.0", features = ["derive"] }

#[target.'cfg(target_arch = "wasm32")'.dependencies]
Expand Down
2 changes: 1 addition & 1 deletion src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::parser::{Parser, StyleBlock};
// Interface to WASM to be used in JS
#[wasm_bindgen]
pub fn parse_rtf(rtf: String) -> RtfDocument {
return RtfDocument::try_from(rtf).unwrap()
return RtfDocument::try_from(rtf).unwrap();
}

#[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize)]
Expand Down
2 changes: 1 addition & 1 deletion src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,4 @@ if (a == b) \{\
[OpeningBracket, PlainText("je suis une b"), ControlSymbol((Unicode, Value(234))), PlainText("te"), ClosingBracket,]
);
}
}
}
1 change: 0 additions & 1 deletion src/paragraph.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/// Define the paragraph related structs and enums

use serde::{Deserialize, Serialize};
use tsify::Tsify;
use wasm_bindgen::prelude::wasm_bindgen;
Expand Down
40 changes: 31 additions & 9 deletions src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::collections::HashMap;
use std::{fmt, mem};

use derivative::Derivative;
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::wasm_bindgen;

Expand All @@ -28,13 +27,11 @@ pub struct StyleBlock {
pub text: String,
}

#[derive(Derivative, Debug, Clone, PartialEq, Hash, Deserialize, Serialize)]
#[derivative(Default)]
#[derive(Debug, Clone, PartialEq, Hash, Deserialize, Serialize)]
#[wasm_bindgen]
pub struct Painter {
pub color_ref: ColorRef,
pub font_ref: FontRef,
#[derivative(Default(value = "12"))]
pub font_size: u16,
pub bold: bool,
pub italic: bool,
Expand All @@ -45,6 +42,23 @@ pub struct Painter {
pub strike: bool,
}

impl Default for Painter {
fn default() -> Self {
Self {
color_ref: Default::default(),
font_ref: Default::default(),
font_size: 12,
bold: Default::default(),
italic: Default::default(),
underline: Default::default(),
superscript: Default::default(),
subscript: Default::default(),
smallcaps: Default::default(),
strike: Default::default(),
}
}
}

#[derive(Debug, Clone)]
pub enum ParserError {
InvalidToken(String),
Expand Down Expand Up @@ -78,15 +92,23 @@ impl fmt::Display for ParserError {
}

// This state keeps track of each value that depends on the scope nesting
#[derive(Derivative, Debug, Clone, PartialEq, Hash)]
#[derivative(Default)]
#[derive(Debug, Clone, PartialEq, Hash)]
struct ParserState {
pub painter: Painter,
pub paragraph: Paragraph,
#[derivative(Default(value = "1"))]
pub unicode_ignore_count: i32,
}

impl Default for ParserState {
fn default() -> Self {
Self {
painter: Default::default(),
paragraph: Default::default(),
unicode_ignore_count: 1,
}
}
}

pub struct Parser<'a> {
tokens: Vec<Token<'a>>,
parsed_item: Vec<bool>,
Expand Down Expand Up @@ -129,7 +151,7 @@ impl<'a> Parser<'a> {
pub fn parse(&mut self) -> Result<RtfDocument, ParserError> {
self.check_document_validity()?;
let mut document = RtfDocument::default(); // Init empty document
// Traverse the document and consume the header groups (FontTable, StyleSheet, etc ...)
// Traverse the document and consume the header groups (FontTable, StyleSheet, etc ...)
document.header = self.parse_header()?;
// Init the state of the docuement. the stack is used to keep track of the different scope changes.
let mut state_stack: Vec<ParserState> = vec![ParserState::default()];
Expand Down Expand Up @@ -748,4 +770,4 @@ pub mod tests {
assert_eq!(doc1.body, doc2.body);
assert_eq!(doc3.body, doc2.body);
}
}
}
2 changes: 1 addition & 1 deletion src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,4 @@ mod tests {
let input = r"\rtf-1";
assert_eq!(ControlWord::from(input).unwrap(), (ControlWord::Rtf, Property::Value(-1)))
}
}
}

0 comments on commit ad2a660

Please sign in to comment.