libmandoc-rs 0.9.1

Safe Rust interface to the vendored libmandoc parser and reference renderers
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
482
//! Optional bounded wrappers around libmandoc's reference renderers.

use std::{
    ffi::CString,
    fmt,
    fs::File,
    io,
    path::{Path, PathBuf},
};

#[cfg(windows)]
use std::io::Read;

use crate::{
    Compression, Diagnostic, ParseError, ParseErrorKind, Parser, RawRender, SourceBundle,
    compression, diagnostics, ffi, parser::IncludeSettings,
};

/// Default maximum bytes retained for one render call.
pub const DEFAULT_RENDER_OUTPUT_BYTES: usize = 8 * 1024 * 1024;

/// Default terminal width used by ASCII and UTF-8 output.
pub const DEFAULT_RENDER_WIDTH: usize = 78;

/// Hard maximum accepted output budget for one render call.
pub const MAX_RENDER_OUTPUT_BYTES: usize = 64 * 1024 * 1024;

/// Smallest supported terminal width.
pub const MIN_RENDER_WIDTH: usize = 20;

/// Largest supported terminal width.
pub const MAX_RENDER_WIDTH: usize = 1_000;

/// Reference output format produced by [`Renderer`].
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum RenderFormat {
    /// Portable 7-bit terminal text using backspace overstrikes for styling.
    Ascii,
    /// Deterministic UTF-8 terminal text with Unicode cell widths.
    Utf8,
    /// UTF-8 HTML, either a full document or a caller-selected fragment.
    Html,
}

/// Successful bounded reference rendering and non-fatal parser findings.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RenderReport {
    /// Complete rendered output. Limit failures never return a partial value.
    pub output: String,
    /// Non-fatal findings emitted while parsing the source.
    pub diagnostics: Vec<Diagnostic>,
}

/// Stable category for a failed render call.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum RenderErrorKind {
    /// A source path cannot cross the native boundary.
    InvalidPath,
    /// Source bytes could not be read.
    Read,
    /// Compressed source bytes could not be decoded.
    Decompression,
    /// The parser configuration is unavailable on this platform.
    Unsupported,
    /// A width or output limit is outside the documented range.
    InvalidOptions,
    /// The complete output exceeds the configured byte budget.
    OutputLimit,
    /// libmandoc could not parse or render the source.
    Render,
    /// The native renderer returned invalid UTF-8.
    Encoding,
}

/// Failure from an optional reference renderer.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RenderError {
    /// Logical or physical source associated with the failure.
    pub path: PathBuf,
    /// Stable category suitable for programmatic handling.
    pub kind: RenderErrorKind,
    /// Human-readable detail.
    pub message: String,
}

impl fmt::Display for RenderError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "{}: {}", self.path.display(), self.message)
    }
}

impl std::error::Error for RenderError {}

/// Bounded, thread-safe access to libmandoc's ASCII, UTF-8, and HTML renderers.
///
/// Rendering reparses the source and formats the native tree in one call;
/// it does not reconstruct private libmandoc state from the owned Rust AST.
/// Output is captured by a per-thread native sink and never uses process
/// standard output. Recursive re-entry on one OS thread remains unsupported.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Renderer {
    parser: Parser,
    format: RenderFormat,
    width: usize,
    max_output_bytes: usize,
    html_fragment: bool,
}

impl Renderer {
    /// Create a renderer with the default parser, width 78, and an 8 MiB cap.
    #[must_use]
    pub fn new(format: RenderFormat) -> Self {
        Self {
            parser: Parser::default(),
            format,
            width: DEFAULT_RENDER_WIDTH,
            max_output_bytes: DEFAULT_RENDER_OUTPUT_BYTES,
            html_fragment: false,
        }
    }

    /// Use an explicit parser policy and input-format selection.
    #[must_use]
    pub fn with_parser(mut self, parser: Parser) -> Self {
        self.parser = parser;
        self
    }

    /// Set the terminal width used by ASCII and UTF-8 output.
    #[must_use]
    pub const fn with_width(mut self, width: usize) -> Self {
        self.width = width;
        self
    }

    /// Set the maximum complete output size retained by one call.
    #[must_use]
    pub const fn with_max_output_bytes(mut self, max_output_bytes: usize) -> Self {
        self.max_output_bytes = max_output_bytes;
        self
    }

    /// Select an HTML fragment instead of a complete HTML document.
    #[must_use]
    pub const fn with_html_fragment(mut self, html_fragment: bool) -> Self {
        self.html_fragment = html_fragment;
        self
    }

    /// Return the parser configuration used by this renderer.
    #[must_use]
    pub const fn parser(&self) -> &Parser {
        &self.parser
    }

    /// Return the selected output format.
    #[must_use]
    pub const fn format(&self) -> RenderFormat {
        self.format
    }

    /// Return the configured terminal width.
    #[must_use]
    pub const fn width(&self) -> usize {
        self.width
    }

    /// Return the maximum complete output size retained by one call.
    #[must_use]
    pub const fn max_output_bytes(&self) -> usize {
        self.max_output_bytes
    }

    /// Return whether HTML output is configured as a fragment.
    #[must_use]
    pub const fn html_fragment(&self) -> bool {
        self.html_fragment
    }

    /// Render one source path.
    ///
    /// # Errors
    ///
    /// Returns [`RenderError`] for invalid options or paths, transport and
    /// decompression failures, parser/renderer failures, or output overflow.
    pub fn render_file(&self, path: impl AsRef<Path>) -> Result<RenderReport, RenderError> {
        let path = path.as_ref();
        self.validate(path)?;
        match self.parser.options().compression {
            Compression::Auto if path.extension().is_some_and(|extension| extension == "zst") => {
                self.render_zstd_file(path)
            }
            Compression::Auto => self.render_auto_file(path),
            Compression::Plain => {
                let source = std::fs::read(path).map_err(|error| read_error(path, &error))?;
                self.render_plain_bytes(path, &source)
            }
            Compression::Zstd => self.render_zstd_file(path),
        }
    }

    /// Render caller-owned source bytes under a logical path.
    ///
    /// # Errors
    ///
    /// Returns [`RenderError`] under the same conditions as
    /// [`Renderer::render_file`].
    pub fn render_bytes(
        &self,
        source_path: impl AsRef<Path>,
        source: &[u8],
    ) -> Result<RenderReport, RenderError> {
        let path = source_path.as_ref();
        self.validate(path)?;
        match self.parser.options().compression {
            Compression::Auto if has_zstd_magic(source) => self.render_zstd_bytes(path, source),
            Compression::Auto | Compression::Plain => self.render_plain_bytes(path, source),
            Compression::Zstd => self.render_zstd_bytes(path, source),
        }
    }

    /// Render one root from a bounded virtual source tree.
    ///
    /// # Errors
    ///
    /// Returns [`RenderError`] if the root is absent or invalid, options are
    /// invalid, parsing/rendering fails, or output exceeds the byte budget.
    pub fn render_bundle(
        &self,
        root: impl AsRef<Path>,
        bundle: &SourceBundle,
    ) -> Result<RenderReport, RenderError> {
        let root = root.as_ref();
        self.validate(root)?;
        let root_label = root.to_str().ok_or_else(|| RenderError {
            path: root.to_path_buf(),
            kind: RenderErrorKind::InvalidPath,
            message: "source bundle roots must be UTF-8 logical paths".into(),
        })?;
        if bundle.get(root_label).is_none() {
            return Err(RenderError {
                path: root.to_path_buf(),
                kind: RenderErrorKind::Read,
                message: "source bundle does not contain the requested root".into(),
            });
        }
        let c_path = native_path(root)?;
        Self::finish(
            root,
            ffi::render_bundle(
                &c_path,
                bundle,
                self.parser.input_format(),
                self.parser.mdoc_operating_system(),
                format_code(self.format),
                self.width,
                self.html_fragment,
                self.max_output_bytes,
            ),
        )
    }

    #[cfg(unix)]
    fn render_auto_file(&self, path: &Path) -> Result<RenderReport, RenderError> {
        let c_path = native_path(path)?;
        let includes = self.include_settings(path)?;
        Self::finish(
            path,
            ffi::render_file(
                &c_path,
                includes.root.as_deref(),
                includes.allow_includes,
                self.parser.input_format(),
                self.parser.mdoc_operating_system(),
                format_code(self.format),
                self.width,
                self.html_fragment,
                self.max_output_bytes,
            ),
        )
    }

    #[cfg(windows)]
    fn render_auto_file(&self, path: &Path) -> Result<RenderReport, RenderError> {
        let (source, gzip) =
            compression::open_auto_file(path).map_err(|error| read_error(path, &error))?;
        if gzip {
            let decoded = compression::decode_gzip(source).map_err(|error| RenderError {
                path: path.to_path_buf(),
                kind: RenderErrorKind::Decompression,
                message: format!("could not decompress gzip manual source: {error}"),
            })?;
            self.render_plain_bytes(path, &decoded)
        } else {
            let mut source = source;
            let mut bytes = Vec::new();
            source
                .read_to_end(&mut bytes)
                .map_err(|error| read_error(path, &error))?;
            self.render_plain_bytes(path, &bytes)
        }
    }

    fn render_zstd_file(&self, path: &Path) -> Result<RenderReport, RenderError> {
        let source = File::open(path)
            .and_then(compression::decode_zstd)
            .map_err(|error| decompression_error(path, &error))?;
        self.render_plain_bytes(path, &source)
    }

    fn render_zstd_bytes(&self, path: &Path, source: &[u8]) -> Result<RenderReport, RenderError> {
        let source =
            compression::decode_zstd(source).map_err(|error| decompression_error(path, &error))?;
        self.render_plain_bytes(path, &source)
    }

    #[cfg(unix)]
    fn render_plain_bytes(&self, path: &Path, source: &[u8]) -> Result<RenderReport, RenderError> {
        let c_path = native_path(path)?;
        let includes = self.include_settings(path)?;
        Self::finish(
            path,
            ffi::render_buffer(
                &c_path,
                source,
                includes.root.as_deref(),
                includes.allow_includes,
                self.parser.input_format(),
                self.parser.mdoc_operating_system(),
                format_code(self.format),
                self.width,
                self.html_fragment,
                self.max_output_bytes,
            ),
        )
    }

    #[cfg(windows)]
    fn render_plain_bytes(&self, path: &Path, source: &[u8]) -> Result<RenderReport, RenderError> {
        let c_path = native_path(path)?;
        let includes = self.include_settings(path)?;
        Self::finish(
            path,
            ffi::render_buffer(
                &c_path,
                source,
                includes.root.as_deref(),
                includes.allow_includes,
                self.parser.input_format(),
                self.parser.mdoc_operating_system(),
                format_code(self.format),
                self.width,
                self.html_fragment,
                self.max_output_bytes,
            ),
        )
    }

    fn finish(
        path: &Path,
        rendered: Result<RawRender, ffi::NativeRenderError>,
    ) -> Result<RenderReport, RenderError> {
        let raw = rendered.map_err(|error| RenderError {
            path: path.to_path_buf(),
            kind: if error.status == 1 {
                RenderErrorKind::OutputLimit
            } else {
                RenderErrorKind::Render
            },
            message: error.message,
        })?;
        let output = String::from_utf8(raw.output).map_err(|error| RenderError {
            path: path.to_path_buf(),
            kind: RenderErrorKind::Encoding,
            message: format!("native renderer returned invalid UTF-8: {error}"),
        })?;
        Ok(RenderReport {
            output,
            diagnostics: diagnostics::parse_diagnostics(&raw.diagnostics),
        })
    }

    fn validate(&self, path: &Path) -> Result<(), RenderError> {
        if self.format != RenderFormat::Html
            && !(MIN_RENDER_WIDTH..=MAX_RENDER_WIDTH).contains(&self.width)
        {
            return Err(RenderError {
                path: path.to_path_buf(),
                kind: RenderErrorKind::InvalidOptions,
                message: format!(
                    "render width must be between {MIN_RENDER_WIDTH} and {MAX_RENDER_WIDTH}"
                ),
            });
        }
        if !(1..=MAX_RENDER_OUTPUT_BYTES).contains(&self.max_output_bytes) {
            return Err(RenderError {
                path: path.to_path_buf(),
                kind: RenderErrorKind::InvalidOptions,
                message: format!(
                    "render output limit must be between 1 and {MAX_RENDER_OUTPUT_BYTES} bytes"
                ),
            });
        }
        Ok(())
    }

    fn include_settings(&self, source_path: &Path) -> Result<IncludeSettings, RenderError> {
        self.parser
            .include_settings(source_path)
            .map_err(map_parse_error)
    }
}

fn native_path(path: &Path) -> Result<CString, RenderError> {
    crate::parser::path_label(path).map_err(|_| RenderError {
        path: path.to_path_buf(),
        kind: RenderErrorKind::InvalidPath,
        message: "manual source path contains a NUL byte".into(),
    })
}

fn map_parse_error(error: ParseError) -> RenderError {
    RenderError {
        path: error.path,
        kind: match error.kind {
            ParseErrorKind::InvalidPath => RenderErrorKind::InvalidPath,
            ParseErrorKind::Read => RenderErrorKind::Read,
            ParseErrorKind::Decompression => RenderErrorKind::Decompression,
            ParseErrorKind::Unsupported => RenderErrorKind::Unsupported,
            ParseErrorKind::Parse => RenderErrorKind::Render,
        },
        message: error.message,
    }
}

const fn format_code(format: RenderFormat) -> i32 {
    match format {
        RenderFormat::Ascii => 1,
        RenderFormat::Html => 2,
        RenderFormat::Utf8 => 3,
    }
}

fn has_zstd_magic(source: &[u8]) -> bool {
    source.starts_with(&[0x28, 0xb5, 0x2f, 0xfd])
}

fn read_error(path: &Path, error: &io::Error) -> RenderError {
    RenderError {
        path: path.to_path_buf(),
        kind: RenderErrorKind::Read,
        message: error.to_string(),
    }
}

fn decompression_error(path: &Path, error: &io::Error) -> RenderError {
    RenderError {
        path: path.to_path_buf(),
        kind: RenderErrorKind::Decompression,
        message: format!("could not decompress zstd manual source: {error}"),
    }
}

#[cfg(test)]
mod tests {
    use crate::{RenderFormat, Renderer, ffi};

    #[test]
    fn utf8_rendering_does_not_change_process_ctype_locale() {
        let before = ffi::ctype_locale();
        let report = Renderer::new(RenderFormat::Utf8)
            .render_bytes(
                "locale.1",
                ".TH LOCALE 1\n.SH NAME\ncafé \\(em 日本 😀\n".as_bytes(),
            )
            .expect("render deterministic UTF-8");
        let after = ffi::ctype_locale();

        assert_eq!(after, before);
        assert!(report.output.contains("café — 日本 😀"));
    }
}