Skip to content

Commit

Permalink
Add features serde
Browse files Browse the repository at this point in the history
  • Loading branch information
zyk-mjzs committed Apr 4, 2024
1 parent 8a8b976 commit 216c8ca
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 16 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ repository = "https://github.com/d0rianb/rtf-parser"
version = "0.2.1"
edition = "2021"
license = "MIT"
keywords = ["rtf", "rich" ,"text", "format", "parser"]
keywords = ["rtf", "rich", "text", "format", "parser"]
categories = ["parsing", "parser-implementations"]
exclude = ["*.rtf", ".idea"]

Expand All @@ -15,4 +15,7 @@ opt-level = 3

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

[features]
serde_support = ["serde"]
3 changes: 2 additions & 1 deletion src/document.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::header::RtfHeader;
use crate::parser::StyleBlock;
use serde::{Deserialize, Serialize};

#[derive(Debug, Default, Clone, PartialEq)]
#[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize)]
pub struct RtfDocument {
pub header: RtfHeader,
pub body: Vec<StyleBlock>,
Expand Down
13 changes: 7 additions & 6 deletions src/header.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashMap;
use serde::{Deserialize, Serialize};

use crate::paragraph::Paragraph;
use crate::parser::Painter;
Expand All @@ -14,37 +15,37 @@ pub type StyleRef = u16;
pub type StyleSheet = HashMap<StyleRef, Style>;

/// Style for the StyleSheet
#[derive(Hash, Default, Debug, Clone, PartialEq)]
#[derive(Hash, Default, Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct Style {
painter: Painter,
paragraph: Paragraph,
}

/// Information about the document, including references to fonts & styles
#[derive(Default, Debug, Clone, PartialEq)]
#[derive(Default, Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct RtfHeader {
pub character_set: CharacterSet,
pub font_table: FontTable,
pub color_table: ColorTable,
pub stylesheet: StyleSheet,
}

#[derive(Hash, Default, Clone, Debug, PartialEq)]
#[derive(Hash, Default, Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct Font {
pub name: String,
pub character_set: u8,
pub font_family: FontFamily,
}

#[derive(Hash, Default, Clone, Debug, PartialEq)]
#[derive(Hash, Default, Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct Color {
pub red: u16,
pub green: u16,
pub blue: u16,
}

#[allow(dead_code)]
#[derive(Debug, PartialEq, Default, Clone)]
#[derive(Debug, PartialEq, Default, Clone, Deserialize, Serialize)]
pub enum CharacterSet {
#[default]
Ansi,
Expand All @@ -65,7 +66,7 @@ impl CharacterSet {
}

#[allow(dead_code)]
#[derive(Debug, PartialEq, Hash, Clone, Default)]
#[derive(Debug, PartialEq, Hash, Clone, Default, Deserialize, Serialize)]
pub enum FontFamily {
#[default]
Nil,
Expand Down
12 changes: 6 additions & 6 deletions src/paragraph.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Define the paragraph related structs and enums

use serde::{Deserialize, Serialize};
use crate::tokens::ControlWord;

#[derive(Debug, Default, Clone, PartialEq, Hash)]
#[derive(Debug, Default, Clone, PartialEq, Hash, Deserialize, Serialize)]
pub struct Paragraph {
pub alignment: Alignment,
pub spacing: Spacing,
Expand All @@ -11,7 +11,7 @@ pub struct Paragraph {
}

/// Alignement of a paragraph (left, right, center, justify)
#[derive(Debug, Default, Clone, Copy, PartialEq, Hash)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Hash, Deserialize, Serialize)]
pub enum Alignment {
#[default]
LeftAligned, // \ql
Expand All @@ -33,15 +33,15 @@ impl From<&ControlWord<'_>> for Alignment {
}

/// The vertical margin before / after a block of text
#[derive(Debug, Default, Clone, PartialEq, Hash)]
#[derive(Debug, Default, Clone, PartialEq, Hash, Deserialize, Serialize)]
pub struct Spacing {
pub before: i32,
pub after: i32,
pub between_line: SpaceBetweenLine,
pub line_multiplier: i32,
}

#[derive(Default, Debug, Clone, PartialEq, Hash)]
#[derive(Default, Debug, Clone, PartialEq, Hash, Deserialize, Serialize)]
pub enum SpaceBetweenLine {
Value(i32),
#[default]
Expand All @@ -64,7 +64,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, PartialEq, Hash)]
#[derive(Default, Debug, Clone, PartialEq, Hash, Deserialize, Serialize)]
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 derivative::Derivative;
use serde::{Deserialize, Serialize};

use crate::document::RtfDocument;
use crate::header::{CharacterSet, Color, ColorRef, ColorTable, Font, FontFamily, FontRef, FontTable, RtfHeader, StyleSheet};
Expand All @@ -18,14 +19,14 @@ macro_rules! header_control_word {
};
}

#[derive(Debug, Default, PartialEq, Clone)]
#[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)]
pub struct StyleBlock {
pub painter: Painter,
pub paragraph: Paragraph,
pub text: String,
}

#[derive(Derivative, Debug, Clone, PartialEq, Hash)]
#[derive(Derivative, Debug, Clone, PartialEq, Hash, Deserialize, Serialize)]
#[derivative(Default)]
pub struct Painter {
pub color_ref: ColorRef,
Expand Down

0 comments on commit 216c8ca

Please sign in to comment.