mant 0.11.0

Local-first TUI, structured CLI, and MCP server for manuals and Markdown
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
//! Maps engine failures onto the CLI's two stable exit-status classes.

use std::io::Write;

use anstyle::{AnsiColor, Style};
use mant_engine::{QueryError, QueryExecutionError, QueryValidationError, ScopeQueryError};
use mant_loader::{LoadError, ScopeLoadError};
use mant_query::{ProjectionError, ScopeExecutionError, SearchError};
use mant_render::sanitize_terminal_text;

const ERROR_STYLE: Style = AnsiColor::Red.on_default().bold();
const WARNING_STYLE: Style = AnsiColor::Yellow.on_default().bold();
const ADVICE_STYLE: Style = AnsiColor::Cyan.on_default().bold();

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum FailureKind {
    Usage,
    Operational,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct Failure {
    kind: FailureKind,
    message: String,
}

impl Failure {
    pub(super) fn usage(message: impl std::fmt::Display) -> Self {
        Self {
            kind: FailureKind::Usage,
            message: sanitized_message(message),
        }
    }

    pub(super) fn operational(message: impl std::fmt::Display) -> Self {
        Self {
            kind: FailureKind::Operational,
            message: sanitized_message(message),
        }
    }

    /// Construct an intentional multi-line usage diagnostic from independently
    /// sanitized lines. Dynamic data may not create a new terminal line.
    pub(super) fn usage_lines<I, T>(first: impl std::fmt::Display, lines: I) -> Self
    where
        I: IntoIterator<Item = T>,
        T: std::fmt::Display,
    {
        Self::with_lines(FailureKind::Usage, first, lines)
    }

    /// Construct an intentional multi-line operational diagnostic from
    /// independently sanitized lines.
    fn operational_lines<I, T>(first: impl std::fmt::Display, lines: I) -> Self
    where
        I: IntoIterator<Item = T>,
        T: std::fmt::Display,
    {
        Self::with_lines(FailureKind::Operational, first, lines)
    }

    fn with_lines<I, T>(kind: FailureKind, first: impl std::fmt::Display, lines: I) -> Self
    where
        I: IntoIterator<Item = T>,
        T: std::fmt::Display,
    {
        let mut message = sanitized_message(first);
        for line in lines {
            message.push('\n');
            message.push_str(&sanitized_message(line));
        }
        Self { kind, message }
    }

    pub(super) fn into_message(self) -> String {
        self.message
    }

    #[cfg(test)]
    pub(super) fn message(&self) -> &str {
        &self.message
    }
}

fn sanitized_message(message: impl std::fmt::Display) -> String {
    let message = message.to_string();
    sanitize_terminal_text(&message).into_owned()
}

pub(super) fn query_failure(error: QueryError) -> Failure {
    match error {
        QueryError::Load(error) => load_failure(error),
        QueryError::QueryValidation(error) => query_validation_failure(error),
    }
}

fn load_failure(error: LoadError) -> Failure {
    match error {
        LoadError::NativeBackendUnavailable {
            tldr_topic: Some(topic),
        } => Failure::operational_lines(
            LoadError::NativeBackendUnavailable { tldr_topic: None },
            [format!(
                "hint: a tldr entry is available; run `mant {topic} --tldr`"
            )],
        ),
        LoadError::EmptyName
        | LoadError::InvalidManualSection
        | LoadError::TldrManualSection { .. }
        | LoadError::InvalidSource
        | LoadError::ConflictingSourceSelectors
        | LoadError::EmptyMarkdownPath
        | LoadError::UnsupportedInputFormat { .. }
        | LoadError::InvalidSelector { .. } => Failure::usage(error),
        LoadError::ManualWithTldr { error, topic } => Failure::operational_lines(
            error,
            [format!(
                "hint: a tldr entry is available; run `mant {topic} --tldr`"
            )],
        ),
        LoadError::Markdown { .. }
        | LoadError::NativeBackendUnavailable { tldr_topic: None }
        | LoadError::EmptyMarkdown { .. }
        | LoadError::Registry { .. }
        | LoadError::Manual(_)
        | LoadError::TldrNotFound { .. }
        | LoadError::Tldr { .. }
        | LoadError::NoReadableContent { .. } => Failure::operational(error),
    }
}

fn query_validation_failure(error: QueryValidationError) -> Failure {
    match error {
        QueryValidationError::EmptySelection
        | QueryValidationError::TooManySelections { .. }
        | QueryValidationError::EmptySelector
        | QueryValidationError::InvalidContentSelector
        | QueryValidationError::InvalidReferenceProjection(_)
        | QueryValidationError::InvalidEntryKinds
        | QueryValidationError::EmptyEntry
        | QueryValidationError::InvalidViewSelector { .. } => Failure::usage(error),
        QueryValidationError::InvalidSearch(error) => search_failure(&error),
        QueryValidationError::InvalidExplanation(mant_query::ExplanationError::MissingContent) => {
            Failure::operational("explanation requires readable content")
        }
        QueryValidationError::InvalidExplanation(error) => Failure::usage(error),
    }
}

fn projection_failure(error: ProjectionError) -> Failure {
    match error {
        ProjectionError::MissingContent { .. } => Failure::operational(error),
        ProjectionError::UnknownSelector { document, selector } => Failure::usage_lines(
            format!("document '{document}' has no outline node '{selector}'"),
            [format!(
                "hint: run `mant {document} --outline --outline-entries all --format json` for available selectors and diagnostics"
            )],
        ),
        ProjectionError::EmptySelection
        | ProjectionError::EmptySelector
        | ProjectionError::InvalidSelector
        | ProjectionError::InvalidReferenceProjection(_)
        | ProjectionError::TooManySelections { .. }
        | ProjectionError::AmbiguousSelector { .. } => Failure::usage(error),
    }
}

pub(super) fn query_execution_failure(error: QueryExecutionError) -> Failure {
    match error {
        QueryExecutionError::Query(error) => query_failure(error),
        QueryExecutionError::Projection(error) => projection_failure(error),
        QueryExecutionError::Search(error) => search_failure(&error),
    }
}

pub(super) fn scope_query_failure(error: ScopeQueryError) -> Failure {
    match error {
        ScopeQueryError::Load(error) => scope_load_failure(error),
        ScopeQueryError::Execution(error) => scope_execution_failure(error),
        ScopeQueryError::InvalidLoadedScope(_) => Failure::operational(error),
        ScopeQueryError::EntrySelector(_) => Failure::usage(error),
        ScopeQueryError::Search(error) => search_failure(&error),
        ScopeQueryError::Explanation(error) => Failure::usage(error),
    }
}

fn scope_execution_failure(error: ScopeExecutionError) -> Failure {
    match error {
        ScopeExecutionError::Explanation(error) => Failure::usage(error),
        ScopeExecutionError::Search(error) => search_failure(&error),
        ScopeExecutionError::NoReadableDocuments { reasons } => {
            // Preserve the CLI's aggregate wording without attributing a pure
            // query execution failure to the source loader's error domain.
            let mut message = "none of the initial documents could be resolved".to_owned();
            if !reasons.is_empty() {
                message.push_str(": ");
                message.push_str(&reasons.join("; "));
            }
            Failure::operational(message)
        }
    }
}

fn scope_load_failure(error: ScopeLoadError) -> Failure {
    match error {
        ScopeLoadError::NoResolvedDocuments { .. } => Failure::operational(error),
        ScopeLoadError::EmptyScope
        | ScopeLoadError::TooManyDocuments
        | ScopeLoadError::DepthLimit
        | ScopeLoadError::DocumentLimit
        | ScopeLoadError::TraversalLimitsRequireLinks
        | ScopeLoadError::DocumentSelector(_) => Failure::usage(error),
    }
}

fn search_failure(error: &SearchError) -> Failure {
    let message = error.to_string();
    let mut lines = message.lines();
    Failure::usage_lines(lines.next().unwrap_or_default(), lines)
}

pub(super) fn report_failure(error: &Failure, diagnostics: &mut dyn Write, color: bool) -> u8 {
    let mut lines = error.message.split('\n');
    let first = lines.next().unwrap_or_default();
    if color {
        let _ = writeln!(diagnostics, "{ERROR_STYLE}mant:{ERROR_STYLE:#} {first}");
    } else {
        let _ = writeln!(diagnostics, "mant: {first}");
    }
    for line in lines {
        let _ = write_diagnostic_line(diagnostics, line, color);
    }
    if error.kind == FailureKind::Usage {
        if color {
            let _ = writeln!(
                diagnostics,
                "{ADVICE_STYLE}Try{ADVICE_STYLE:#} 'mant --help' for more information."
            );
        } else {
            let _ = writeln!(diagnostics, "Try 'mant --help' for more information.");
        }
        2
    } else {
        1
    }
}

fn write_diagnostic_line(
    diagnostics: &mut dyn Write,
    line: &str,
    color: bool,
) -> std::io::Result<()> {
    if !color {
        return writeln!(diagnostics, "{line}");
    }
    for (label, style) in [
        ("warning:", WARNING_STYLE),
        ("hint:", ADVICE_STYLE),
        ("help:", ADVICE_STYLE),
        ("note:", ADVICE_STYLE),
    ] {
        if let Some(message) = line.strip_prefix(label) {
            return writeln!(diagnostics, "{style}{label}{style:#}{message}");
        }
    }
    writeln!(diagnostics, "{line}")
}

/// Preserve clap's actionable usage and suggestion text on the injected stream.
pub(super) fn report_argument_error(error: &clap::Error, diagnostics: &mut dyn Write) -> u8 {
    let rendered = error.to_string();
    let _ = diagnostics.write_all(rendered.as_bytes());
    if !rendered.ends_with('\n') {
        let _ = diagnostics.write_all(b"\n");
    }
    2
}

/// Let clap choose the native stdout/stderr stream and apply its configured
/// terminal color policy. Help and version are successful display results;
/// every other parser diagnostic retains the conventional usage status.
pub(super) fn report_process_argument_error(error: &clap::Error) -> u8 {
    let status = u8::try_from(error.exit_code()).unwrap_or(2);
    let _ = error.print();
    status
}

#[cfg(test)]
mod tests {
    use mant_query::SearchError;

    use super::{Failure, report_failure, search_failure};

    #[test]
    fn loading_and_query_validation_errors_keep_their_exit_categories() {
        use mant_engine::{QueryError, QueryValidationError};
        use mant_loader::LoadError;
        for (error, expected_status, expected_message) in [
            (
                QueryError::Load(LoadError::NativeBackendUnavailable { tldr_topic: None }),
                1,
                "native manual loading requires the 'roff' feature",
            ),
            (
                QueryError::Load(LoadError::EmptyName),
                2,
                "name must not be empty",
            ),
            (
                QueryError::Load(LoadError::InvalidSelector {
                    field: "document source",
                    error: mant_protocol::ScopeTextError::ControlCharacter,
                }),
                2,
                "document source must not contain control characters",
            ),
            (
                QueryError::QueryValidation(QueryValidationError::EmptyEntry),
                2,
                "semantic entry must not be empty",
            ),
            (
                QueryError::QueryValidation(QueryValidationError::InvalidExplanation(
                    mant_query::ExplanationError::MissingContent,
                )),
                1,
                "explanation requires readable content",
            ),
            (
                QueryError::Load(LoadError::Markdown {
                    path: "demo.md".into(),
                    detail: "permission denied".into(),
                }),
                1,
                "could not load Markdown document 'demo.md': permission denied",
            ),
        ] {
            let failure = super::query_failure(error);
            assert_eq!(failure.message(), expected_message);
            assert_eq!(
                report_failure(&failure, &mut Vec::new(), false),
                expected_status
            );
        }
    }

    #[test]
    fn scope_loading_errors_keep_their_exit_categories_and_messages() {
        use mant_engine::ScopeQueryError;
        use mant_loader::ScopeLoadError;

        for (error, expected_status, expected_message) in [
            (
                ScopeLoadError::EmptyScope,
                2,
                "at least one document is required",
            ),
            (
                ScopeLoadError::TraversalLimitsRequireLinks,
                2,
                "maxDepth and maxDocuments require followLinks=true",
            ),
            (
                ScopeLoadError::NoResolvedDocuments {
                    reasons: vec!["missing manual".into()],
                },
                1,
                "none of the initial documents could be resolved: missing manual",
            ),
        ] {
            let failure = super::scope_query_failure(ScopeQueryError::Load(error));
            assert_eq!(failure.message(), expected_message);
            assert_eq!(
                report_failure(&failure, &mut Vec::new(), false),
                expected_status
            );
        }
    }

    #[test]
    fn scope_execution_errors_keep_their_presentation_without_becoming_load_errors() {
        use mant_engine::ScopeQueryError;
        use mant_query::{ExplanationError, ScopeExecutionError};

        for (error, expected_status, expected_message) in [
            (
                ScopeExecutionError::Explanation(ExplanationError::MissingContent),
                2,
                "explanation requires readable content",
            ),
            (
                ScopeExecutionError::Search(SearchError::EmptyPattern),
                2,
                "search pattern must not be empty",
            ),
            (
                ScopeExecutionError::NoReadableDocuments {
                    reasons: vec!["missing body".into()],
                },
                1,
                "none of the initial documents could be resolved: missing body",
            ),
        ] {
            let failure = super::scope_query_failure(ScopeQueryError::Execution(error));
            assert_eq!(failure.message(), expected_message);
            assert_eq!(
                report_failure(&failure, &mut Vec::new(), false),
                expected_status
            );
        }
    }

    #[test]
    fn failure_messages_mask_dynamic_terminal_controls() {
        let error = Failure::operational("bad\u{1b}[2J\nnext\rline");
        assert_eq!(error.message(), "bad�[2J�next�line");

        let error = Failure::usage_lines("first\u{1b}[31m", ["hint: next\tline"]);
        let mut diagnostics = Vec::new();
        assert_eq!(report_failure(&error, &mut diagnostics, false), 2);
        assert_eq!(
            String::from_utf8(diagnostics).expect("diagnostics UTF-8"),
            "mant: first�[31m\nhint: next�line\nTry 'mant --help' for more information.\n"
        );

        let error =
            Failure::operational_lines("could not load topic\nforged", ["hint: retry\nforged"]);
        let mut diagnostics = Vec::new();
        assert_eq!(report_failure(&error, &mut diagnostics, false), 1);
        assert_eq!(
            String::from_utf8(diagnostics).expect("diagnostics UTF-8"),
            "mant: could not load topic�forged\nhint: retry�forged\n"
        );
    }

    #[test]
    fn regex_diagnostics_keep_trusted_line_structure() {
        let error = search_failure(&SearchError::InvalidPattern(
            "regex parse error:\n    (a\n    ^\nerror: unclosed group\u{1b}[31m".to_owned(),
        ));
        let mut diagnostics = Vec::new();

        assert_eq!(report_failure(&error, &mut diagnostics, false), 2);
        assert_eq!(
            String::from_utf8(diagnostics).expect("diagnostics UTF-8"),
            "mant: invalid search pattern: regex parse error:\n    (a\n    ^\nerror: unclosed group�[31m\nTry 'mant --help' for more information.\n"
        );
    }
}