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 /// A call that needs a live render context ran before any attach.
21 /// Today only
22 /// [`Engine::set_render_update_callback`](crate::Engine::set_render_update_callback)
23 /// returns this — a callback that could never fire is a wiring bug,
24 /// surfaced loudly like
25 /// [`RenderBackendMismatch`](Self::RenderBackendMismatch). The frame
26 /// path deliberately does *not* use it: unattached,
27 /// `render_gl`/`render_sw` return `Ok` untouched and `render_update`
28 /// returns `false`, because "not attached yet" is an ordinary
29 /// startup state there, not a bug — don't match on this variant to
30 /// detect a missing attach from a draw handler.
31 #[error("no render context is attached")]
32 NotAttached,
33}
34
35/// Shorthand for results carrying this crate's [`enum@Error`].
36pub type Result<T> = std::result::Result<T, Error>;
37
38/// Diagnostic text for a raw `client.h` `mpv_error` code: mpv's own
39/// `mpv_error_string` text plus the numeric code (rsmpv's `Display`
40/// carries both) — not user-facing copy.
41pub(crate) fn describe_code(code: i32) -> String {
42 rsmpv::Error::from_raw(code).to_string()
43}
44
45#[cfg(test)]
46mod tests {
47 use super::*;
48
49 #[test]
50 fn describe_code_carries_mpv_text_and_code() {
51 // -17 is MPV_ERROR_UNKNOWN_FORMAT; the exact wording belongs to
52 // mpv, so pin only that its text came through alongside the code.
53 let msg = describe_code(-17);
54 assert!(msg.contains("format"), "unexpected message: {msg}");
55 assert!(msg.contains("-17"), "unexpected message: {msg}");
56 // Unknown codes still produce a code-bearing string.
57 assert!(describe_code(-99).contains("-99"));
58 }
59}