easypdf-core 0.1.1

Core types, traits, enums, converters, and errors for easypdf-rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
//! PDF 语义内容块。

use crate::{ImageData, ListItem, PdfBlockType, SourceLocation};

/// 从 PDF 页面识别出的语义内容块。
///
/// `#[non_exhaustive]` 保证未来新增变体不会破坏下游代码。
/// 消费方应始终包含通配分支(`_ => ...`)或使用 [`block_type`](Self::block_type)
/// 进行分发。
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum PdfBlock {
    /// 分级标题。
    Heading {
        /// 标题级别,范围为 1 到 6。
        level: u8,
        /// 标题文本。
        text: String,
        /// 源位置。
        source: SourceLocation,
    },
    /// 普通段落。
    Paragraph {
        /// 段落文本。
        text: String,
        /// 源位置。
        source: SourceLocation,
    },
    /// 有序或无序列表。
    List {
        /// 是否为有序列表。
        ordered: bool,
        /// 列表项(支持嵌套)。
        items: Vec<ListItem>,
        /// 源位置。
        source: SourceLocation,
    },
    /// 表格数据。
    Table {
        /// 表头。
        headers: Vec<String>,
        /// 表格行。
        rows: Vec<Vec<String>>,
        /// 源位置。
        source: SourceLocation,
    },
    /// 图片引用。
    Image {
        /// 图片元数据。
        data: ImageData,
        /// 源位置。
        source: SourceLocation,
    },
    /// 代码块。
    Code {
        /// 代码语言标识(如 `"rust"`、`"python"`)。
        language: Option<String>,
        /// 代码文本。
        text: String,
        /// 源位置。
        source: SourceLocation,
    },
    /// 数学公式(LaTeX 语法)。
    Formula {
        /// LaTeX 源码。
        latex: String,
        /// 源位置。
        source: SourceLocation,
    },
    /// 分页符。
    PageBreak {
        /// 源位置。
        source: SourceLocation,
    },
    /// 脚注。
    Footnote {
        /// 脚注引用标识。
        reference_id: String,
        /// 脚注正文。
        text: String,
        /// 源位置。
        source: SourceLocation,
    },
    /// 表格单元格(细粒度识别)。
    TableCell {
        /// 行跨度。
        row_span: u32,
        /// 列跨度。
        col_span: u32,
        /// 单元格文本。
        text: String,
        /// 源位置。
        source: SourceLocation,
    },
    /// 引用块。
    BlockQuote {
        /// 引用文本。
        text: String,
        /// 源位置。
        source: SourceLocation,
    },
    /// 水平分隔线。
    HorizontalRule {
        /// 源位置。
        source: SourceLocation,
    },
    /// 超链接。
    Link {
        /// 链接地址。
        url: String,
        /// 链接显示文本。
        text: String,
        /// 源位置。
        source: SourceLocation,
    },
    /// 无法识别的内容。
    Unknown {
        /// 原始文本。
        raw: String,
        /// 源位置。
        source: SourceLocation,
    },
}

impl PdfBlock {
    // ------------------------------------------------------------------
    //  便捷构造方法
    // ------------------------------------------------------------------

    /// 创建分级标题。
    #[must_use]
    pub fn heading(level: u8, text: impl Into<String>, source: SourceLocation) -> Self {
        Self::Heading {
            level,
            text: text.into(),
            source,
        }
    }

    /// 创建普通段落。
    #[must_use]
    pub fn paragraph(text: impl Into<String>, source: SourceLocation) -> Self {
        Self::Paragraph {
            text: text.into(),
            source,
        }
    }

    /// 创建有序或无序列表。
    #[must_use]
    pub fn list(ordered: bool, items: Vec<ListItem>, source: SourceLocation) -> Self {
        Self::List {
            ordered,
            items,
            source,
        }
    }

    /// 创建表格。
    #[must_use]
    pub fn table(headers: Vec<String>, rows: Vec<Vec<String>>, source: SourceLocation) -> Self {
        Self::Table {
            headers,
            rows,
            source,
        }
    }

    /// 创建图片引用。
    #[must_use]
    pub fn image(data: ImageData, source: SourceLocation) -> Self {
        Self::Image { data, source }
    }

    /// 创建代码块。
    #[must_use]
    pub fn code(text: impl Into<String>, source: SourceLocation) -> Self {
        Self::Code {
            language: None,
            text: text.into(),
            source,
        }
    }

    /// 创建带语言标识的代码块。
    #[must_use]
    pub fn code_with_language(
        language: impl Into<String>,
        text: impl Into<String>,
        source: SourceLocation,
    ) -> Self {
        Self::Code {
            language: Some(language.into()),
            text: text.into(),
            source,
        }
    }

    /// 创建数学公式。
    #[must_use]
    pub fn formula(latex: impl Into<String>, source: SourceLocation) -> Self {
        Self::Formula {
            latex: latex.into(),
            source,
        }
    }

    /// 创建分页符。
    #[must_use]
    pub const fn page_break(source: SourceLocation) -> Self {
        Self::PageBreak { source }
    }

    /// 创建脚注。
    #[must_use]
    pub fn footnote(
        ref_id: impl Into<String>,
        text: impl Into<String>,
        source: SourceLocation,
    ) -> Self {
        Self::Footnote {
            reference_id: ref_id.into(),
            text: text.into(),
            source,
        }
    }

    /// 创建表格单元格。
    #[must_use]
    pub fn table_cell(
        row_span: u32,
        col_span: u32,
        text: impl Into<String>,
        source: SourceLocation,
    ) -> Self {
        Self::TableCell {
            row_span,
            col_span,
            text: text.into(),
            source,
        }
    }

    /// 创建引用块。
    #[must_use]
    pub fn block_quote(text: impl Into<String>, source: SourceLocation) -> Self {
        Self::BlockQuote {
            text: text.into(),
            source,
        }
    }

    /// 创建水平分隔线。
    #[must_use]
    pub const fn horizontal_rule(source: SourceLocation) -> Self {
        Self::HorizontalRule { source }
    }

    /// 创建超链接。
    #[must_use]
    pub fn link(url: impl Into<String>, text: impl Into<String>, source: SourceLocation) -> Self {
        Self::Link {
            url: url.into(),
            text: text.into(),
            source,
        }
    }

    /// 创建无法识别的内容。
    #[must_use]
    pub fn unknown(raw: impl Into<String>, source: SourceLocation) -> Self {
        Self::Unknown {
            raw: raw.into(),
            source,
        }
    }

    // ------------------------------------------------------------------
    //  查询方法
    // ------------------------------------------------------------------

    /// 返回内容块的源位置。
    #[must_use]
    pub const fn source(&self) -> &SourceLocation {
        match self {
            Self::Heading { source, .. }
            | Self::Paragraph { source, .. }
            | Self::List { source, .. }
            | Self::Table { source, .. }
            | Self::Image { source, .. }
            | Self::Code { source, .. }
            | Self::Formula { source, .. }
            | Self::PageBreak { source }
            | Self::Footnote { source, .. }
            | Self::TableCell { source, .. }
            | Self::BlockQuote { source, .. }
            | Self::HorizontalRule { source }
            | Self::Link { source, .. }
            | Self::Unknown { source, .. } => source,
        }
    }

    /// 返回内容块的语义分类。
    ///
    /// # Examples
    ///
    /// ```
    /// use easypdf_core::{PdfBlock, PdfBlockType, SourceLocation};
    /// use easypdf_core::PageIndex;
    ///
    /// let loc = SourceLocation::new(PageIndex::new(0), 1.0);
    /// let block = PdfBlock::heading(1, "Title", loc);
    /// assert_eq!(block.block_type(), PdfBlockType::Heading);
    /// ```
    #[must_use]
    pub const fn block_type(&self) -> PdfBlockType {
        match self {
            Self::Heading { .. } => PdfBlockType::Heading,
            Self::Paragraph { .. } => PdfBlockType::Paragraph,
            Self::List { .. } => PdfBlockType::List,
            Self::Table { .. } => PdfBlockType::Table,
            Self::Image { .. } => PdfBlockType::Image,
            Self::Code { .. } => PdfBlockType::Code,
            Self::Formula { .. } => PdfBlockType::Formula,
            Self::PageBreak { .. } => PdfBlockType::PageBreak,
            Self::Footnote { .. } => PdfBlockType::Footnote,
            Self::TableCell { .. } => PdfBlockType::TableCell,
            Self::BlockQuote { .. } => PdfBlockType::BlockQuote,
            Self::HorizontalRule { .. } => PdfBlockType::HorizontalRule,
            Self::Link { .. } => PdfBlockType::Link,
            Self::Unknown { .. } => PdfBlockType::Unknown,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::PageIndex;

    fn loc() -> SourceLocation {
        SourceLocation::new(PageIndex::new(0), 1.0)
    }

    #[test]
    fn heading_construction() {
        let b = PdfBlock::heading(2, "Section", loc());
        assert_eq!(b.block_type(), PdfBlockType::Heading);
        assert_eq!(b.source().page_index().value(), 0);
    }

    #[test]
    fn paragraph_construction() {
        let b = PdfBlock::paragraph("Hello", loc());
        assert_eq!(b.block_type(), PdfBlockType::Paragraph);
    }

    #[test]
    fn list_construction() {
        let items = vec![ListItem::new("A"), ListItem::new("B")];
        let b = PdfBlock::list(false, items, loc());
        assert_eq!(b.block_type(), PdfBlockType::List);
    }

    #[test]
    fn table_construction() {
        let b = PdfBlock::table(vec!["H".into()], vec![vec!["C".into()]], loc());
        assert_eq!(b.block_type(), PdfBlockType::Table);
    }

    #[test]
    fn image_construction() {
        let data = ImageData::new(crate::ImageFormat::Png);
        let b = PdfBlock::image(data, loc());
        assert_eq!(b.block_type(), PdfBlockType::Image);
    }

    #[test]
    fn code_construction() {
        let b = PdfBlock::code("fn main() {}", loc());
        assert_eq!(b.block_type(), PdfBlockType::Code);
    }

    #[test]
    fn code_with_language_construction() {
        let b = PdfBlock::code_with_language("rust", "fn main() {}", loc());
        assert_eq!(b.block_type(), PdfBlockType::Code);
    }

    #[test]
    fn formula_construction() {
        let b = PdfBlock::formula("E = mc^2", loc());
        assert_eq!(b.block_type(), PdfBlockType::Formula);
    }

    #[test]
    fn page_break_construction() {
        let b = PdfBlock::page_break(loc());
        assert_eq!(b.block_type(), PdfBlockType::PageBreak);
    }

    #[test]
    fn footnote_construction() {
        let b = PdfBlock::footnote("1", "See page 5", loc());
        assert_eq!(b.block_type(), PdfBlockType::Footnote);
    }

    #[test]
    fn table_cell_construction() {
        let b = PdfBlock::table_cell(2, 1, "Merged", loc());
        assert_eq!(b.block_type(), PdfBlockType::TableCell);
    }

    #[test]
    fn block_quote_construction() {
        let b = PdfBlock::block_quote("A wise saying", loc());
        assert_eq!(b.block_type(), PdfBlockType::BlockQuote);
    }

    #[test]
    fn horizontal_rule_construction() {
        let b = PdfBlock::horizontal_rule(loc());
        assert_eq!(b.block_type(), PdfBlockType::HorizontalRule);
    }

    #[test]
    fn link_construction() {
        let b = PdfBlock::link("https://example.com", "Example", loc());
        assert_eq!(b.block_type(), PdfBlockType::Link);
    }

    #[test]
    fn unknown_construction() {
        let b = PdfBlock::unknown("???binary???", loc());
        assert_eq!(b.block_type(), PdfBlockType::Unknown);
    }

    #[test]
    fn source_returns_correct_location() {
        let loc2 = SourceLocation::new(PageIndex::new(3), 0.85);
        let b = PdfBlock::paragraph("test", loc2);
        assert_eq!(b.source().page_index().value(), 3);
        assert!((b.source().confidence() - 0.85).abs() < f32::EPSILON);
    }

    #[test]
    fn block_type_covers_all_variants() {
        let loc = loc();
        let variants = [
            PdfBlock::heading(1, "h", loc),
            PdfBlock::paragraph("p", loc),
            PdfBlock::list(false, vec![], loc),
            PdfBlock::table(vec![], vec![], loc),
            PdfBlock::image(ImageData::new(crate::ImageFormat::Png), loc),
            PdfBlock::code("c", loc),
            PdfBlock::formula("f", loc),
            PdfBlock::page_break(loc),
            PdfBlock::footnote("1", "t", loc),
            PdfBlock::table_cell(1, 1, "t", loc),
            PdfBlock::block_quote("q", loc),
            PdfBlock::horizontal_rule(loc),
            PdfBlock::link("u", "t", loc),
            PdfBlock::unknown("r", loc),
        ];
        let types: Vec<_> = variants.iter().map(PdfBlock::block_type).collect();
        assert_eq!(types.len(), 14);
        // 每个变体应映射到不同分类
        for i in 0..types.len() {
            for j in (i + 1)..types.len() {
                assert_ne!(types[i], types[j], "duplicate at {i} vs {j}");
            }
        }
    }

    #[test]
    fn clone_and_eq() {
        let a = PdfBlock::paragraph("hello", loc());
        let b = a.clone();
        assert_eq!(a, b);
    }
}