diagprint 0.8.1

A Rust diagnostics lifecycle framework for structured diagnostics, rendering, remediation, CI, editors, and telemetry.
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
use crate::DocumentationLink;
use reqwest::blocking::Client;
use scraper::{Html, Selector};
use std::{error::Error, fmt, time::Duration};
use syntect::{
    easy::HighlightLines,
    highlighting::{Theme as SyntectTheme, ThemeSet},
    parsing::SyntaxSet,
    util::{LinesWithEndings, as_24_bit_terminal_escaped},
};

const DEFAULT_MAX_DOCUMENT_BYTES: usize = 2 * 1024 * 1024;

#[derive(Debug)]
pub enum TerminalDocError {
    Http(reqwest::Error),
    Html(html2text::Error),
    InvalidUrl(String),
    UnsupportedScheme(String),
    Selector(String),
    Highlight(String),

    DocumentTooLarge { limit: usize, actual: usize },
}

impl fmt::Display for TerminalDocError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Http(error) => write!(f, "{error}"),
            Self::Html(error) => write!(f, "{error}"),

            Self::InvalidUrl(error) => {
                write!(f, "invalid documentation URL: {error}")
            }

            Self::UnsupportedScheme(scheme) => {
                write!(f, "unsupported documentation URL scheme `{scheme}`")
            }

            Self::Selector(error) => write!(f, "{error}"),
            Self::Highlight(error) => write!(f, "{error}"),

            Self::DocumentTooLarge { limit, actual } => write!(
                f,
                "documentation response is too large: {actual} bytes \
                 exceeds the {limit}-byte limit"
            ),
        }
    }
}

impl Error for TerminalDocError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            Self::Http(error) => Some(error),
            Self::Html(error) => Some(error),
            _ => None,
        }
    }
}

impl From<reqwest::Error> for TerminalDocError {
    fn from(value: reqwest::Error) -> Self {
        Self::Http(value)
    }
}

impl From<html2text::Error> for TerminalDocError {
    fn from(value: html2text::Error) -> Self {
        Self::Html(value)
    }
}

#[derive(Debug, Clone)]
pub struct TerminalDocViewer {
    width: usize,
    max_code_blocks: usize,
    max_document_bytes: usize,
    timeout: Duration,
    syntax_theme: String,
}

impl Default for TerminalDocViewer {
    fn default() -> Self {
        Self {
            width: 88,
            max_code_blocks: 8,
            max_document_bytes: DEFAULT_MAX_DOCUMENT_BYTES,
            timeout: Duration::from_secs(15),
            syntax_theme: "base16-ocean.dark".into(),
        }
    }
}

impl TerminalDocViewer {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn width(mut self, width: usize) -> Self {
        self.width = width.max(40);
        self
    }

    pub fn max_code_blocks(mut self, count: usize) -> Self {
        self.max_code_blocks = count;
        self
    }

    pub fn max_document_bytes(mut self, bytes: usize) -> Self {
        self.max_document_bytes = bytes.max(1024);
        self
    }

    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    pub fn syntax_theme(mut self, theme: impl Into<String>) -> Self {
        self.syntax_theme = theme.into();
        self
    }

    /// Fetches and renders a documentation link synchronously.
    ///
    /// This uses Reqwest's blocking client. Applications already running inside
    /// an async runtime should execute this method from an appropriate blocking
    /// worker instead of calling it directly on the async runtime thread.
    pub fn open(&self, link: &DocumentationLink) -> Result<String, TerminalDocError> {
        let url = reqwest::Url::parse(&link.url)
            .map_err(|error| TerminalDocError::InvalidUrl(error.to_string()))?;

        if !matches!(url.scheme(), "http" | "https") {
            return Err(TerminalDocError::UnsupportedScheme(url.scheme().to_owned()));
        }

        let client = Client::builder()
            .timeout(self.timeout)
            .user_agent(concat!(
                "diagprint/",
                env!("CARGO_PKG_VERSION"),
                " terminal-doc-viewer"
            ))
            .build()?;

        let response = client.get(url).send()?.error_for_status()?;

        if let Some(length) = response.content_length() {
            let length = usize::try_from(length).unwrap_or(usize::MAX);

            if length > self.max_document_bytes {
                return Err(TerminalDocError::DocumentTooLarge {
                    limit: self.max_document_bytes,
                    actual: length,
                });
            }
        }

        let content_type = response
            .headers()
            .get(reqwest::header::CONTENT_TYPE)
            .and_then(|value| value.to_str().ok())
            .unwrap_or("")
            .to_ascii_lowercase();

        let body = response.text()?;

        if body.len() > self.max_document_bytes {
            return Err(TerminalDocError::DocumentTooLarge {
                limit: self.max_document_bytes,
                actual: body.len(),
            });
        }

        let body = sanitize_terminal_input(&body);
        let label = sanitize_terminal_input(&link.label);
        let display_url = sanitize_terminal_input(&link.url);

        let mut output = String::new();

        output.push_str("\x1b[1;36mDOCS\x1b[0m  ");
        output.push_str(&label);
        output.push('\n');

        output.push_str("\x1b[2m");
        output.push_str(&display_url);
        output.push_str("\x1b[0m\n\n");

        let markdown = content_type.contains("markdown")
            || content_type.contains("text/plain")
            || link.url.ends_with(".md")
            || link.url.ends_with(".markdown");

        if markdown {
            output.push_str(&self.render_markdown(&body, link.language_hint.as_deref())?);
        } else {
            output.push_str(&self.render_html(&body, link.language_hint.as_deref())?);
        }

        Ok(output)
    }

    pub fn open_and_print(&self, link: &DocumentationLink) -> Result<(), TerminalDocError> {
        print!("{}", self.open(link)?);
        Ok(())
    }

    fn render_html(
        &self,
        html: &str,
        language_hint: Option<&str>,
    ) -> Result<String, TerminalDocError> {
        let document = Html::parse_document(html);

        let main_selector = Selector::parse("main")
            .map_err(|error| TerminalDocError::Selector(error.to_string()))?;

        let code_selector = Selector::parse("pre code")
            .map_err(|error| TerminalDocError::Selector(error.to_string()))?;

        let (content, code_blocks) = if let Some(main) = document.select(&main_selector).next() {
            let code_blocks = main
                .select(&code_selector)
                .take(self.max_code_blocks)
                .map(|element| {
                    let language = element
                        .value()
                        .attr("class")
                        .and_then(language_from_class)
                        .or(language_hint)
                        .unwrap_or("rust")
                        .to_owned();

                    let source = element.text().collect::<String>();

                    (language, source)
                })
                .collect::<Vec<_>>();

            (main.html(), code_blocks)
        } else {
            let code_blocks = document
                .select(&code_selector)
                .take(self.max_code_blocks)
                .map(|element| {
                    let language = element
                        .value()
                        .attr("class")
                        .and_then(language_from_class)
                        .or(language_hint)
                        .unwrap_or("rust")
                        .to_owned();

                    let source = element.text().collect::<String>();

                    (language, source)
                })
                .collect::<Vec<_>>();

            (html.to_owned(), code_blocks)
        };

        let mut output = html2text::from_read(content.as_bytes(), self.width)?;

        if !code_blocks.is_empty() {
            let syntaxes = SyntaxSet::load_defaults_newlines();
            let themes = ThemeSet::load_defaults();

            let theme = select_syntax_theme(&themes, &self.syntax_theme)?;

            output.push_str("\n\n\x1b[1;35mCODE EXAMPLES\x1b[0m\n");

            for (index, (language, code)) in code_blocks.iter().enumerate() {
                if code.trim().is_empty() {
                    continue;
                }

                output.push_str(&format!("\n\x1b[2m[{}] {}\x1b[0m\n", index + 1, language));

                output.push_str(&highlight_code(code, language, &syntaxes, theme)?);
            }
        }

        if !output.ends_with('\n') {
            output.push('\n');
        }

        Ok(output)
    }

    fn render_markdown(
        &self,
        markdown: &str,
        language_hint: Option<&str>,
    ) -> Result<String, TerminalDocError> {
        let syntaxes = SyntaxSet::load_defaults_newlines();
        let themes = ThemeSet::load_defaults();

        let theme = select_syntax_theme(&themes, &self.syntax_theme)?;

        let mut output = String::new();
        let mut code = String::new();
        let mut in_code = false;
        let mut language = String::new();

        for line in markdown.lines() {
            if let Some(fence) = line.trim_start().strip_prefix("```") {
                if in_code {
                    output.push_str(&highlight_code(
                        &code,
                        if language.is_empty() {
                            language_hint.unwrap_or("txt")
                        } else {
                            &language
                        },
                        &syntaxes,
                        theme,
                    )?);

                    code.clear();
                    language.clear();
                    in_code = false;
                } else {
                    language = fence.trim().to_owned();
                    in_code = true;
                }

                continue;
            }

            if in_code {
                code.push_str(line);
                code.push('\n');
                continue;
            }

            if let Some(heading) = line.strip_prefix("### ") {
                output.push_str(&format!("\x1b[1;36m{heading}\x1b[0m\n"));
            } else if let Some(heading) = line.strip_prefix("## ") {
                output.push_str(&format!("\n\x1b[1;35m{heading}\x1b[0m\n"));
            } else if let Some(heading) = line.strip_prefix("# ") {
                output.push_str(&format!("\n\x1b[1;33m{heading}\x1b[0m\n"));
            } else {
                output.push_str(line);
                output.push('\n');
            }
        }

        if in_code && !code.is_empty() {
            output.push_str(&highlight_code(
                &code,
                if language.is_empty() {
                    language_hint.unwrap_or("txt")
                } else {
                    &language
                },
                &syntaxes,
                theme,
            )?);
        }

        Ok(output)
    }
}

fn select_syntax_theme<'a>(
    themes: &'a ThemeSet,
    requested: &str,
) -> Result<&'a SyntectTheme, TerminalDocError> {
    themes
        .themes
        .get(requested)
        .or_else(|| themes.themes.get("base16-ocean.dark"))
        .or_else(|| themes.themes.values().next())
        .ok_or_else(|| {
            TerminalDocError::Highlight("syntect did not provide a terminal theme".into())
        })
}

fn language_from_class(class: &str) -> Option<&str> {
    class
        .split_whitespace()
        .find_map(|part| part.strip_prefix("language-"))
}

fn highlight_code(
    source: &str,
    language: &str,
    syntaxes: &SyntaxSet,
    theme: &SyntectTheme,
) -> Result<String, TerminalDocError> {
    let syntax = syntaxes
        .find_syntax_by_token(language)
        .or_else(|| syntaxes.find_syntax_by_extension(language))
        .or_else(|| {
            if matches!(language, "rs" | "rust") {
                syntaxes.find_syntax_by_extension("rs")
            } else {
                None
            }
        })
        .unwrap_or_else(|| syntaxes.find_syntax_plain_text());

    let mut highlighter = HighlightLines::new(syntax, theme);
    let mut output = String::new();

    for line in LinesWithEndings::from(source) {
        let ranges = highlighter
            .highlight_line(line, syntaxes)
            .map_err(|error| TerminalDocError::Highlight(error.to_string()))?;

        output.push_str(&as_24_bit_terminal_escaped(&ranges, false));

        output.push_str("\x1b[0m");
    }

    if !output.ends_with('\n') {
        output.push('\n');
    }

    Ok(output)
}

fn sanitize_terminal_input(input: &str) -> String {
    input
        .chars()
        .filter_map(|character| match character {
            '\n' | '\t' => Some(character),
            '\r' => None,
            '\x1b' => Some('␛'),
            character if character.is_control() => Some('�'),
            character => Some(character),
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::{TerminalDocViewer, sanitize_terminal_input};

    #[test]
    fn sanitizes_terminal_escape_sequences() {
        let input = "safe\x1b[31munsafe\x07text";

        let sanitized = sanitize_terminal_input(input);

        assert!(!sanitized.contains('\x1b'));
        assert!(!sanitized.contains('\x07'));
        assert!(sanitized.contains('␛'));
        assert!(sanitized.contains('�'));
    }

    #[test]
    fn renders_markdown_code_without_network() {
        let viewer = TerminalDocViewer::new();

        let Ok(rendered) = viewer.render_markdown(
            "# Example\n\n```rust\nfn main() {\n    println!(\"hello\");\n}\n```\n",
            None,
        ) else {
            panic!("offline Markdown documentation rendering failed");
        };

        assert!(rendered.contains("Example"));
        assert!(rendered.contains("fn"));
        assert!(rendered.contains("println"));
        assert!(rendered.contains("\x1b["));
    }

    #[test]
    fn renders_html_main_content_without_network() {
        let viewer = TerminalDocViewer::new();

        let Ok(rendered) = viewer.render_html(
            r#"
                <html>
                    <body>
                        <nav>navigation noise</nav>
                        <main>
                            <h1>Diagnostic docs</h1>
                            <p>Useful explanation.</p>
                            <pre><code class="language-rust">fn demo() {}</code></pre>
                        </main>
                    </body>
                </html>
                "#,
            None,
        ) else {
            panic!("offline HTML documentation rendering failed");
        };

        assert!(rendered.contains("Diagnostic docs"));
        assert!(rendered.contains("Useful explanation"));
        assert!(rendered.contains("CODE EXAMPLES"));
        assert!(rendered.contains("fn"));
    }

    #[test]
    fn falls_back_when_syntax_theme_is_unknown() {
        let viewer = TerminalDocViewer::new().syntax_theme("__does_not_exist__");

        let Ok(rendered) = viewer.render_markdown("```rust\nlet value = 42;\n```\n", None) else {
            panic!("fallback syntax-theme rendering failed");
        };

        assert!(rendered.contains("value"));
    }
}