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
//! Token-filter callbacks and content rewriting.
//!
//! qpdf correspondence: QPDFObjectHandle::TokenFilter callback boundary.
//!
use crate::{
pipeline::{Pipeline, PipelineResult},
tokenizer::Token,
};
/// Rewrite a content stream one lexical token at a time.
///
/// The callback receives the shared tokenizer's parsed/raw token view. It may
/// forward the original token with [`TokenFilterOutput::write_token`], emit
/// replacement bytes, or discard the token by writing nothing. The EOF token
/// is delivered through [`Self::handle_token`] before [`Self::handle_eof`].
///
/// qpdf correspondence: `QPDFObjectHandle::TokenFilter` and
/// `Pl_QPDFTokenizer`.
pub trait TokenFilter {
/// Handle one content token and optionally forward output downstream.
fn handle_token(
&mut self,
token: &Token,
output: &mut TokenFilterOutput<'_>,
) -> PipelineResult<()>;
/// Handle the end of the tokenized content stream.
fn handle_eof(&mut self, _output: &mut TokenFilterOutput<'_>) -> PipelineResult<()> {
Ok(())
}
}
/// Downstream writer exposed to a [`TokenFilter`].
pub struct TokenFilterOutput<'a> {
next: Option<&'a mut dyn Pipeline>,
}
impl<'a> TokenFilterOutput<'a> {
pub(crate) fn new(next: Option<&'a mut dyn Pipeline>) -> Self {
Self { next }
}
/// Forward raw bytes to the optional downstream pipeline.
pub fn write(&mut self, data: &[u8]) -> PipelineResult<()> {
if data.is_empty() {
return Ok(());
}
match self.next.as_deref_mut() {
Some(next) => next.write(data),
None => Ok(()),
}
}
/// Forward the token's original raw spelling to the downstream pipeline.
pub fn write_token(&mut self, token: &Token) -> PipelineResult<()> {
self.write(&token.raw)
}
}