Skip to main content

bookforge_core/
ir.rs

1use std::path::PathBuf;
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
6pub struct BookId(pub String);
7
8#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
9pub struct SectionId(pub String);
10
11#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
12pub struct BlockId(pub String);
13
14#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct Book {
16    pub source_path: Option<PathBuf>,
17    pub id: BookId,
18    pub format: BookFormat,
19    pub metadata: Metadata,
20    pub manifest: Vec<Resource>,
21    pub spine: Vec<SpineItem>,
22    pub sections: Vec<Section>,
23    pub blocks: Vec<Block>,
24}
25
26#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
27pub enum BookFormat {
28    Epub,
29}
30
31#[derive(Debug, Clone, Default, Serialize, Deserialize)]
32pub struct Metadata {
33    pub title: Option<String>,
34    pub creators: Vec<String>,
35    pub language: Option<String>,
36}
37
38#[derive(Debug, Clone, Serialize, Deserialize)]
39pub struct Resource {
40    pub id: String,
41    pub href: String,
42    pub media_type: String,
43    pub properties: Vec<String>,
44}
45
46#[derive(Debug, Clone, Serialize, Deserialize)]
47pub struct SpineItem {
48    pub idref: String,
49    pub href: Option<String>,
50    pub linear: bool,
51}
52
53#[derive(Debug, Clone, Serialize, Deserialize)]
54pub struct Section {
55    pub id: SectionId,
56    pub href: String,
57    pub spine_index: usize,
58    pub title: Option<String>,
59    pub heading_level: Option<u8>,
60    pub block_ids: Vec<BlockId>,
61    pub prev: Option<SectionId>,
62    pub next: Option<SectionId>,
63}
64
65#[derive(Debug, Clone, Serialize, Deserialize)]
66pub struct Block {
67    pub id: BlockId,
68    pub section_id: SectionId,
69    pub kind: BlockKind,
70    pub dom_path: DomPath,
71    pub text_runs: Vec<TextRun>,
72    pub inline_marks: Vec<InlineMark>,
73    pub protected_spans: Vec<ProtectedSpan>,
74    pub token_estimate: usize,
75}
76
77#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
78pub enum BlockKind {
79    Heading(u8),
80    Paragraph,
81    ListItem,
82    Quote,
83    TableCell,
84    TableRow,
85    Footnote,
86    Caption,
87    Code,
88    Unknown,
89}
90
91#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
92pub struct DomPath(pub Vec<usize>);
93
94#[derive(Debug, Clone, Serialize, Deserialize)]
95pub struct TextRun {
96    pub id: String,
97    pub text: String,
98}
99
100#[derive(Debug, Clone, Serialize, Deserialize)]
101pub struct InlineMark {
102    pub id: String,
103    pub kind: String,
104}
105
106#[derive(Debug, Clone, Serialize, Deserialize)]
107pub struct ProtectedSpan {
108    pub kind: ProtectedSpanKind,
109    pub text: String,
110}
111
112#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
113pub enum ProtectedSpanKind {
114    Url,
115    Email,
116    Code,
117    Math,
118    Number,
119    Filename,
120    InternalAnchor,
121    Citation,
122    FootnoteReference,
123}