Skip to main content

mpv_engine/
error.rs

1use thiserror::Error;
2
3/// Everything this crate's fallible calls can return.
4#[derive(Debug, Error)]
5#[non_exhaustive]
6pub enum Error {
7    /// An error from libmpv itself, via rsmpv (its `Display` carries
8    /// mpv's `mpv_error_string` text plus the numeric code).
9    #[error("mpv: {0}")]
10    Mpv(#[from] rsmpv::Error),
11    /// An attach called while a render context is already live. mpv
12    /// supports exactly one render context per handle, of either backend.
13    #[error("a render context is already attached")]
14    AlreadyAttached,
15    /// A render call against the wrong attached backend (e.g. `render_gl`
16    /// while the software context is attached) — a wiring bug in the
17    /// shell, surfaced loudly rather than silently dropped.
18    #[error("render call does not match the attached render backend")]
19    RenderBackendMismatch,
20    /// Setting up the exported-frame backend's hidden GL context or its
21    /// exportable framebuffers (IOSurface / DMA-BUF / shared D3D11
22    /// texture) failed (`export` feature,
23    /// [`Engine::attach_exported_render`](crate::Engine::attach_exported_render)).
24    /// The common non-bug cause is a session without GPU access —
25    /// no WindowServer on macOS, no readable DRM render node on Linux
26    /// (SSH, bare CI, missing render/video group), no OpenGL ICD or no
27    /// `WGL_NV_DX_interop2` on Windows — treat it like a missing display
28    /// and fall back to another backend or skip.
29    #[cfg(export_backend)]
30    #[error("exported render setup: {0}")]
31    ExportSetup(String),
32    /// Importing an exported frame into a wgpu device failed (`wgpu`
33    /// feature,
34    /// [`ExportedFrame::into_wgpu_texture`](crate::ExportedFrame::into_wgpu_texture))
35    /// — the device isn't on the platform's native backend (Metal on
36    /// macOS, Vulkan on Linux, DX12 on Windows), lacks a required wgpu
37    /// feature (`VULKAN_EXTERNAL_MEMORY_DMA_BUF` on Linux), sits on a
38    /// different GPU than the engine's hidden context (Windows), or the
39    /// driver refused the IOSurface wrap / DMA-BUF import / shared-handle
40    /// open.
41    #[cfg(wgpu_backend)]
42    #[error("wgpu import: {0}")]
43    WgpuImport(String),
44    /// A call that needs a live render context ran before any attach.
45    /// Today only
46    /// [`Engine::set_render_update_callback`](crate::Engine::set_render_update_callback)
47    /// returns this — a callback that could never fire is a wiring bug,
48    /// surfaced loudly like
49    /// [`RenderBackendMismatch`](Self::RenderBackendMismatch). The frame
50    /// path deliberately does *not* use it: unattached,
51    /// `render_gl`/`render_sw` return `Ok` untouched and `render_update`
52    /// returns `false`, because "not attached yet" is an ordinary
53    /// startup state there, not a bug — don't match on this variant to
54    /// detect a missing attach from a draw handler.
55    #[error("no render context is attached")]
56    NotAttached,
57}
58
59/// Shorthand for results carrying this crate's [`enum@Error`].
60pub type Result<T> = std::result::Result<T, Error>;
61
62/// Diagnostic text for a raw `client.h` `mpv_error` code: mpv's own
63/// `mpv_error_string` text plus the numeric code (rsmpv's `Display`
64/// carries both) — not user-facing copy.
65pub(crate) fn describe_code(code: i32) -> String {
66    rsmpv::Error::from_raw(code).to_string()
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72
73    #[test]
74    fn describe_code_carries_mpv_text_and_code() {
75        // -17 is MPV_ERROR_UNKNOWN_FORMAT; the exact wording belongs to
76        // mpv, so pin only that its text came through alongside the code.
77        let msg = describe_code(-17);
78        assert!(msg.contains("format"), "unexpected message: {msg}");
79        assert!(msg.contains("-17"), "unexpected message: {msg}");
80        // Unknown codes still produce a code-bearing string.
81        assert!(describe_code(-99).contains("-99"));
82    }
83}