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}
21
22/// Shorthand for results carrying this crate's [`enum@Error`].
23pub type Result<T> = std::result::Result<T, Error>;
24
25/// Diagnostic text for a raw `client.h` `mpv_error` code: mpv's own
26/// `mpv_error_string` text plus the numeric code (rsmpv's `Display`
27/// carries both) — not user-facing copy.
28pub(crate) fn describe_code(code: i32) -> String {
29    rsmpv::Error::from_raw(code).to_string()
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35
36    #[test]
37    fn describe_code_carries_mpv_text_and_code() {
38        // -17 is MPV_ERROR_UNKNOWN_FORMAT; the exact wording belongs to
39        // mpv, so pin only that its text came through alongside the code.
40        let msg = describe_code(-17);
41        assert!(msg.contains("format"), "unexpected message: {msg}");
42        assert!(msg.contains("-17"), "unexpected message: {msg}");
43        // Unknown codes still produce a code-bearing string.
44        assert!(describe_code(-99).contains("-99"));
45    }
46}