mdcat/error.rs
1// Copyright 2025 mdcat contributors
2
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7//! Typed errors for the rendering library.
8//!
9//! [`RenderError`] distinguishes IO failures from image-protocol and
10//! SVG rasterisation failures so callers can pattern-match on the
11//! failure category without probing an `ErrorKind`. `std::io::Error`
12//! still drives the state machine internally and auto-wraps via
13//! `#[from]`, so `?` on an `io::Result` works directly in any
14//! function returning [`RenderResult`].
15
16use thiserror::Error;
17
18/// Errors that occur while rendering markdown to a terminal.
19///
20/// This is the error type for the library entry point [`push_tty`][crate::push_tty]
21/// and the render module's internal helpers. Callers typically don't need to
22/// distinguish the variants — printing the error (including its `Display`
23/// implementation, which quotes the inner cause) is enough.
24#[derive(Debug, Error)]
25pub enum RenderError {
26 /// Writing to the output, reading from a resource, or another
27 /// underlying I/O operation failed.
28 #[error("I/O error")]
29 Io(#[from] std::io::Error),
30
31 /// A terminal image protocol failed to encode or emit an image.
32 ///
33 /// `protocol` names the protocol ("kitty", "iterm2", "sixel",
34 /// "terminology") so the caller can see which capability broke.
35 #[error("image protocol {protocol} failed: {source}")]
36 ImageProtocol {
37 /// Name of the image protocol that failed.
38 protocol: &'static str,
39 /// Underlying cause, carried as an opaque error to avoid leaking
40 /// per-protocol error types into the public API.
41 #[source]
42 source: anyhow::Error,
43 },
44
45 /// Rasterising an SVG to PNG via the `svg` feature failed.
46 #[error("SVG rendering failed: {0}")]
47 Svg(#[source] anyhow::Error),
48}
49
50/// `Result` type used throughout the rendering library.
51pub type RenderResult<T> = std::result::Result<T, RenderError>;