techy 0.1.3

A fast, extensible LaTeX parser for 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
//! The sources table: [`SourceSerdeDriver`], its driver, and [`SourceIndex`], its
//! position type.
//!
//! The driver asks the caller how each source's text is written — embedded in the entry
//! or kept outside and referenced ([`SourceTextPolicy`], [`SourceTextForm`]) — and, when
//! a source is referenced, asks the caller for the text and for the verification of the
//! recorded digest ([`SourceTextSupplier`], [`ReferencedSource`], [`SourceDigest`]).
//!
//! The value conversion of spans is here too: a span refers to its source by position in
//! this table.

use alloc::string::String;
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::fmt;
use core::marker::PhantomData;

use crate::source::{Source, SourceOrigin, SourceProvenance, SourceSpan};
use crate::state::Lang;

use super::super::engine::{DeserializeContext, ObjectSerdeDriver, SerializeContext};
use super::super::error::{DeserializeError, SerializeError};
use super::super::object::{DeserializableValue, SerializableLang, SerializableValue};
use super::super::value::{SerialEntry, SerialValue};
use super::super::wire::source::{
    WireDigest, WireProvenance, WireSource, WireSourceReference, WireSourceText, WireSpan,
};
use super::super::wire::{FromSerialValue, SerialBytes, ToSerialValue};
use super::standard::StandardTableReading;
use super::{StandardTableInterning, SOURCES_TABLE, SOURCE_IDENTIFIER};

crate::serial_index! {
    /// A position in the sources table — the `Index` type of [`SourceSerdeDriver`]:
    /// the serialized reference to a [`Source`].
    pub struct SourceIndex;
}

/// A digest of a source's text: a fixed-size fingerprint computed by a hash function.
///
/// `algorithm` names the hash function — the writer's choice, `"sha256"` say — and
/// `bytes` is that function's output for the text.
///
/// The crate neither chooses nor implements any hash function. A [`SourceTextPolicy`]
/// computes digests when it decides to reference a source, and a [`SourceTextSupplier`]
/// verifies them when the source is read back; the crate stores the pair and asks the
/// caller's supplier to check it.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SourceDigest {
    /// The hash function's name.
    pub algorithm: String,
    /// The hash function's output for the text.
    pub bytes: Vec<u8>,
}

impl SourceDigest {
    /// A digest computed by the hash function named `algorithm`, with output `bytes`.
    pub fn new(algorithm: impl Into<String>, bytes: impl Into<Vec<u8>>) -> SourceDigest {
        SourceDigest { algorithm: algorithm.into(), bytes: bytes.into() }
    }
}

/// How a source's text is written: embedded in the source's entry, or kept outside
/// the serialized form and *referenced*.
///
/// When the text is referenced, the entry records the text's length and, optionally,
/// a [`SourceDigest`], and the reading side obtains the text from its
/// [`SourceTextSupplier`].
///
/// The [`SourceTextPolicy`] of the writing session's source driver decides per
/// source.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SourceTextForm {
    /// The text is written into the entry: the serialized form is self-contained.
    Embedded,
    /// The text is not written; the entry records its length and `digest` (when
    /// given), and the reading side must supply the text.
    Referenced {
        /// A digest of the text, recorded so that the reading side can verify the
        /// text it supplies is the one that was serialized; `None` records the length
        /// only.
        digest: Option<SourceDigest>,
    },
}

/// The writing side's per-source choice between embedding and referencing the text (see
/// [`SourceTextForm`]).
///
/// A policy is configured on the source driver with
/// [`SourceSerdeDriver::with_text_policy`]. Without one, every source is embedded.
pub trait SourceTextPolicy<O: SourceOrigin>: Send + Sync {
    /// Decide how `source`'s text is written — computing the digest here, when the
    /// text is referenced with one.
    ///
    /// # Errors
    ///
    /// The policy cannot decide or cannot compute the digest; the source is not
    /// serialized and the error is reported through the interning call.
    fn text_form(&self, source: &Source<O>) -> Result<SourceTextForm, SerializeError>;
}

/// The reading side's supply of the text of a *referenced* source — one whose entry
/// holds no text (see [`SourceTextForm::Referenced`]) — and its verification of recorded
/// digests.
///
/// A supplier is configured on the source driver with
/// [`SourceSerdeDriver::with_text_supplier`]. Without one, a referenced source is an
/// error ([`DeserializeError::NoSourceTextSupplier`]).
///
/// The driver calls [`source_text`](Self::source_text), checks the text's length against
/// the recorded one ([`DeserializeError::SourceLengthMismatch`]), and, when the entry
/// records a digest, calls [`digest_matches`](Self::digest_matches)
/// ([`DeserializeError::SourceDigestMismatch`] on `false`). So a text that has changed
/// since it was serialized is an error, never a source with wrong offsets.
pub trait SourceTextSupplier<O: SourceOrigin>: Send + Sync {
    /// The text of `source`, as it was when the source was serialized.
    ///
    /// `source` holds everything the entry recorded about it: its origin, its
    /// provenance, the text's length, and the digest.
    ///
    /// # Errors
    ///
    /// The text cannot be obtained (typically [`DeserializeError::failed`] with the
    /// reason).
    fn source_text(&self, source: &ReferencedSource<O>) -> Result<String, DeserializeError>;

    /// Whether `text` matches `digest`: `Ok(true)` when the digest of `text` under
    /// the algorithm the digest names equals the digest's bytes, `Ok(false)` when it
    /// does not (the driver then reports [`DeserializeError::SourceDigestMismatch`]).
    ///
    /// # Errors
    ///
    /// The digest cannot be checked — an algorithm this supplier does not know, say
    /// (a supplier is free to refuse any algorithm; the source is then not
    /// deserialized).
    fn digest_matches(&self, digest: &SourceDigest, text: &str) -> Result<bool, DeserializeError>;
}

/// What a source's entry records about a referenced source — one whose text is kept
/// outside the serialized form.
///
/// This is the argument of [`SourceTextSupplier::source_text`].
#[derive(Clone, Debug)]
pub struct ReferencedSource<O: SourceOrigin> {
    origin: O,
    provenance: SourceProvenance<O>,
    length: usize,
    digest: Option<SourceDigest>,
    line_number_offset: usize,
    column_number_offset: usize,
}

impl<O: SourceOrigin> ReferencedSource<O> {
    /// The source's origin ([`Source::origin`]) — for the default origin type, the
    /// URL or path the text was obtained from, if it was recorded.
    pub fn origin(&self) -> &O {
        &self.origin
    }

    /// How the source entered the parse ([`Source::provenance`]).
    pub fn provenance(&self) -> &SourceProvenance<O> {
        &self.provenance
    }

    /// The text's length in bytes, as recorded.
    pub fn length(&self) -> usize {
        self.length
    }

    /// The text's digest, if the writer recorded one.
    pub fn digest(&self) -> Option<&SourceDigest> {
        self.digest.as_ref()
    }

    /// The source's line number offset ([`Source::line_number_offset`]).
    pub fn line_number_offset(&self) -> usize {
        self.line_number_offset
    }

    /// The source's column number offset ([`Source::column_number_offset`]).
    pub fn column_number_offset(&self) -> usize {
        self.column_number_offset
    }
}

/// The driver of the sources table (table name `sources`, one kind of object —
/// identifier `core.source`): how a [`Source`] is serialized and rebuilt.
///
/// A source's entry records its origin (through the language's `SourceOrigin` value
/// conversion), its provenance, its line and column number offsets, and its text. A
/// resolved or synthesized source refers to the source its triggering location lies in
/// by position, so provenance chains are serialized as references between entries of
/// this table.
///
/// The text is written in one of two forms:
///
/// - *Embedded*: the text is written into the entry, and the serialized form is
///   self-contained. The default for every source.
/// - *Referenced*: the text is kept outside; the entry records the text's length
///   and, optionally, a [`SourceDigest`] — a fixed-size fingerprint of the text
///   computed by a hash function of the writer's choice, stored as the function's
///   name and output. On reading, the driver asks its [`SourceTextSupplier`] for the
///   text, checks the length, and asks the supplier to verify the digest; a text
///   that changed since it was serialized is an error, never a source with wrong
///   offsets. The crate neither chooses nor implements a hash function.
///
/// The choice is the [`SourceTextPolicy`]'s ([`with_text_policy`](Self::with_text_policy)),
/// per source; the supply is the [`SourceTextSupplier`]'s
/// ([`with_text_supplier`](Self::with_text_supplier)). Without a policy every source
/// is embedded; without a supplier a referenced source is an error.
///
/// Sharing survives: every span of a stream that points into one `Arc<Source>`
/// refers to one entry, and reading the entries back yields one `Arc<Source>` per
/// entry — [`SourceSpan::same_source`] holds after a round trip exactly where it
/// held before.
///
/// Registered by [`SerdeSession::new`](crate::serialize::SerdeSession::new); a
/// configured driver is registered through
/// [`SerdeSession::with_source_driver`](crate::serialize::SerdeSession::with_source_driver).
pub struct SourceSerdeDriver<L: Lang> {
    text_policy: Option<Arc<dyn SourceTextPolicy<L::SourceOrigin>>>,
    text_supplier: Option<Arc<dyn SourceTextSupplier<L::SourceOrigin>>>,
    lang: PhantomData<fn() -> L>,
}

impl<L: Lang> SourceSerdeDriver<L> {
    /// The driver with the defaults: every source embedded on writing; referenced
    /// sources an error on reading.
    pub fn new() -> SourceSerdeDriver<L> {
        SourceSerdeDriver { text_policy: None, text_supplier: None, lang: PhantomData }
    }

    /// Use `policy` to decide, per source, whether the text is embedded or
    /// referenced (see [`SourceTextForm`]).
    pub fn with_text_policy(mut self, policy: Arc<dyn SourceTextPolicy<L::SourceOrigin>>) -> Self {
        self.text_policy = Some(policy);
        self
    }

    /// Use `supplier` to obtain the text of referenced sources and to verify their
    /// digests.
    pub fn with_text_supplier(mut self, supplier: Arc<dyn SourceTextSupplier<L::SourceOrigin>>) -> Self {
        self.text_supplier = Some(supplier);
        self
    }
}

impl<L: Lang> Default for SourceSerdeDriver<L> {
    fn default() -> Self {
        SourceSerdeDriver::new()
    }
}

impl<L: Lang> fmt::Debug for SourceSerdeDriver<L> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("SourceSerdeDriver")
            .field("text_policy", &self.text_policy.as_ref().map(|_| "set"))
            .field("text_supplier", &self.text_supplier.as_ref().map(|_| "set"))
            .finish()
    }
}

/// The origin's label, owned, for error messages.
fn origin_label<O: SourceOrigin>(origin: &O) -> Option<String> {
    origin.label().map(|label| label.into_owned())
}

impl<L: SerializableLang> ObjectSerdeDriver<L> for SourceSerdeDriver<L> {
    type Object = Source<L::SourceOrigin>;
    type Index = SourceIndex;

    fn table_name(&self) -> &'static str {
        SOURCES_TABLE
    }

    fn homogeneous_identifier(&self) -> Option<&'static str> {
        Some(SOURCE_IDENTIFIER)
    }

    fn serialize_object(
        &self,
        source: &Arc<Source<L::SourceOrigin>>,
        cx: &mut SerializeContext<'_, L>,
    ) -> Result<SerialEntry, SerializeError> {
        let form = match &self.text_policy {
            Some(policy) => policy.text_form(source)?,
            None => SourceTextForm::Embedded,
        };
        let origin = source.origin().serialize_value(cx)?;
        let provenance = match source.provenance() {
            SourceProvenance::Primary => WireProvenance::Primary,
            SourceProvenance::Resolved { reference, triggered_at } => WireProvenance::Resolved {
                reference: reference.clone(),
                triggered_at: serialize_span(triggered_at, cx)?,
            },
            SourceProvenance::Synthesized { description, triggered_at } => WireProvenance::Synthesized {
                description: description.clone(),
                triggered_at: serialize_span(triggered_at, cx)?,
            },
        };
        let text = match form {
            SourceTextForm::Embedded => WireSourceText::Embedded(String::from(source.content())),
            SourceTextForm::Referenced { digest } => WireSourceText::Referenced(WireSourceReference {
                length: source.content().len(),
                digest: digest.map(|digest| WireDigest {
                    algorithm: digest.algorithm,
                    bytes: SerialBytes(digest.bytes),
                }),
            }),
        };
        let wire = WireSource {
            origin,
            provenance,
            line_number_offset: source.line_number_offset(),
            column_number_offset: source.column_number_offset(),
            text,
        };
        Ok(SerialEntry { identifier: SOURCE_IDENTIFIER.into(), data: wire.to_serial_value()? })
    }

    fn deserialize_object(
        &self,
        entry: &SerialEntry,
        cx: &mut DeserializeContext<'_, L>,
    ) -> Result<Arc<Source<L::SourceOrigin>>, DeserializeError> {
        let wire = WireSource::from_serial_value(&entry.data)?;
        let origin = <L::SourceOrigin as DeserializableValue<L>>::deserialize_value(&wire.origin, cx)?;
        let provenance = match wire.provenance {
            WireProvenance::Primary => SourceProvenance::Primary,
            WireProvenance::Resolved { reference, triggered_at } => SourceProvenance::Resolved {
                reference,
                triggered_at: deserialize_span(triggered_at, cx)?,
            },
            WireProvenance::Synthesized { description, triggered_at } => SourceProvenance::Synthesized {
                description,
                triggered_at: deserialize_span(triggered_at, cx)?,
            },
        };
        let text = match wire.text {
            WireSourceText::Embedded(text) => text,
            WireSourceText::Referenced(reference) => {
                let label = origin_label(&origin);
                let Some(supplier) = &self.text_supplier else {
                    return Err(DeserializeError::NoSourceTextSupplier { origin: label });
                };
                let digest = reference
                    .digest
                    .map(|digest| SourceDigest { algorithm: digest.algorithm, bytes: digest.bytes.0 });
                let referenced = ReferencedSource {
                    origin: origin.clone(),
                    provenance: provenance.clone(),
                    length: reference.length,
                    digest: digest.clone(),
                    line_number_offset: wire.line_number_offset,
                    column_number_offset: wire.column_number_offset,
                };
                let text = supplier.source_text(&referenced)?;
                if text.len() != reference.length {
                    return Err(DeserializeError::SourceLengthMismatch {
                        origin: label,
                        expected: reference.length,
                        found: text.len(),
                    });
                }
                if let Some(digest) = &digest {
                    if !supplier.digest_matches(digest, &text)? {
                        return Err(DeserializeError::SourceDigestMismatch {
                            origin: label,
                            algorithm: digest.algorithm.clone(),
                        });
                    }
                }
                text
            }
        };
        let source = Source::new(text)
            .with_origin(origin)
            .with_provenance(provenance)
            .with_line_column_number_offsets(wire.line_number_offset, wire.column_number_offset);
        Ok(Arc::new(source))
    }
}

/// The serialized form of `span`: its source interned into the sources table, and
/// its byte range.
pub(crate) fn serialize_span<L: SerializableLang>(
    span: &SourceSpan<L::SourceOrigin>,
    cx: &mut SerializeContext<'_, L>,
) -> Result<WireSpan, SerializeError> {
    let source = cx.intern_source(span.source())?;
    Ok(WireSpan { source, start: span.start(), end: span.end() })
}

/// The span `wire` describes, over the source read back from the sources table.
/// Validates the range against the source before constructing the span (the range
/// must lie within the text and on character boundaries).
pub(crate) fn deserialize_span<L: SerializableLang>(
    wire: WireSpan,
    cx: &mut DeserializeContext<'_, L>,
) -> Result<SourceSpan<L::SourceOrigin>, DeserializeError> {
    let source = cx.source(wire.source)?;
    let content = source.content();
    let (start, end) = (wire.start, wire.end);
    if start > end || end > content.len() {
        return Err(DeserializeError::SpanOutOfBounds { start, end, len: content.len() });
    }
    if !content.is_char_boundary(start) || !content.is_char_boundary(end) {
        return Err(DeserializeError::SpanNotOnCharBoundary { start, end });
    }
    // Validated above: within bounds and on character boundaries, so the constructor's
    // precondition holds.
    Ok(SourceSpan::new(&source, start..end))
}

/// A span is the value `{"source": <position>, "start": <int>, "end": <int>}`: its
/// source is interned into the sources table (a source
/// interned before yields its existing position, so spans into one source share one
/// entry), and its byte range is embedded.
impl<L: Lang> SerializableValue<L> for SourceSpan<L::SourceOrigin> {
    fn serialize_value(&self, cx: &mut SerializeContext<'_, L>) -> Result<SerialValue, SerializeError>
    where
        L: SerializableLang,
    {
        Ok(serialize_span(self, cx)?.to_serial_value()?)
    }
}

/// A span is read back over the source at the recorded position (the same `Arc` for
/// every span into that source), with its byte range validated against the source's
/// text ([`DeserializeError::SpanOutOfBounds`],
/// [`DeserializeError::SpanNotOnCharBoundary`]).
impl<L: Lang> DeserializableValue<L> for SourceSpan<L::SourceOrigin> {
    fn deserialize_value(
        value: &SerialValue,
        cx: &mut DeserializeContext<'_, L>,
    ) -> Result<Self, DeserializeError>
    where
        L: SerializableLang,
    {
        deserialize_span(WireSpan::from_serial_value(value)?, cx)
    }
}