forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiBcbbd6e2Tahinli2 months ago2369 commits

error.rs148 lines, 5.0 KiBHistoryRawDownload
1//! Typed errors. Every `VAStatus` that is not `VA_STATUS_SUCCESS` becomes an
2//! [`Error::Va`] carrying the failing entry point, the raw status and libva's
3//! own description of it — a bare status number in a log is a bug report
4//! waiting to happen.
5
6use std::ffi::CStr;
7use std::fmt;
8
9use crate::sys;
10
11/// Result alias for this crate.
12pub type Result<T> = std::result::Result<T, Error>;
13
14/// Everything that can go wrong between here and the driver.
15#[derive(Debug)]
16#[non_exhaustive]
17pub enum Error {
18 /// A libva entry point returned a failing `VAStatus`.
19 Va {
20 /// The libva function that failed, e.g. `"vaCreateConfig"`.
21 op: &'static str,
22 /// The raw `VAStatus`.
23 status: sys::VAStatus,
24 /// `vaErrorStr(status)`.
25 message: String,
26 },
27 /// Opening a DRM render node failed.
28 Drm {
29 /// The device path that was tried.
30 path: String,
31 /// The underlying `open(2)` failure.
32 source: std::io::Error,
33 },
34 /// No `/dev/dri/renderD*` node could be opened as a VA display.
35 NoDevice,
36 /// The runtime libva is older than the ABI this crate transcribes.
37 ///
38 /// This is the drift guard: the struct layouts in [`crate::sys`] are
39 /// compile-time transcriptions of the 1.23 headers, so refusing to talk to
40 /// an older runtime is the only sound answer.
41 Version {
42 /// Runtime major version reported by `vaInitialize`.
43 major: i32,
44 /// Runtime minor version reported by `vaInitialize`.
45 minor: i32,
46 },
47 /// The driver returned something this FFI cannot interpret.
48 ///
49 /// Not a bitstream error and not a status code — a shape violation, e.g. a
50 /// surface attribute whose value type contradicts its documented type.
51 Protocol(String),
52}
53
54impl Error {
55 /// Build an [`Error::Va`] for `op`, looking the status string up in libva.
56 pub(crate) fn va(op: &'static str, status: sys::VAStatus) -> Self {
57 Error::Va {
58 op,
59 status,
60 message: status_str(status),
61 }
62 }
63
64 /// The raw `VAStatus`, when this error came from a libva call.
65 pub fn status(&self) -> Option<sys::VAStatus> {
66 match self {
67 Error::Va { status, .. } => Some(*status),
68 _ => None,
69 }
70 }
71}
72
73/// `vaErrorStr` as an owned Rust string.
74pub(crate) fn status_str(status: sys::VAStatus) -> String {
75 // SAFETY: `vaErrorStr` is documented (va.h:401) to return a pointer to a
76 // static, NUL-terminated english description for any input value —
77 // unknown codes map to a fallback string rather than NULL. It reads no
78 // global state we mutate, so it is safe to call at any time, including
79 // before vaInitialize. The pointer is static, so building a CStr from it
80 // and copying is sound; the copy outlives the borrow.
81 let ptr = unsafe { sys::vaErrorStr(status) };
82 if ptr.is_null() {
83 return format!("VAStatus 0x{status:08x}");
84 }
85 // SAFETY: non-NULL and NUL-terminated per the contract above.
86 unsafe { CStr::from_ptr(ptr) }
87 .to_string_lossy()
88 .into_owned()
89}
90
91/// Turn a `VAStatus` into a `Result`, tagging it with the entry point name.
92pub(crate) fn check(op: &'static str, status: sys::VAStatus) -> Result<()> {
93 if status == sys::VA_STATUS_SUCCESS {
94 Ok(())
95 } else {
96 Err(Error::va(op, status))
97 }
98}
99
100impl fmt::Display for Error {
101 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
102 match self {
103 Error::Va {
104 op,
105 status,
106 message,
107 } => write!(f, "{op} failed: {message} (VAStatus 0x{status:08x})"),
108 Error::Drm { path, source } => write!(f, "opening DRM node {path}: {source}"),
109 Error::NoDevice => write!(f, "no usable /dev/dri/renderD* VA display"),
110 Error::Version { major, minor } => write!(
111 f,
112 "libva {major}.{minor} is older than the {}.{} ABI this build transcribes",
113 crate::MIN_VA_MAJOR,
114 crate::MIN_VA_MINOR
115 ),
116 Error::Protocol(what) => write!(f, "libva returned an uninterpretable value: {what}"),
117 }
118 }
119}
120
121impl std::error::Error for Error {
122 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
123 match self {
124 Error::Drm { source, .. } => Some(source),
125 _ => None,
126 }
127 }
128}
129
130#[cfg(test)]
131mod tests {
132 use super::*;
133
134 #[test]
135 fn status_strings_come_from_libva() {
136 assert_eq!(status_str(sys::VA_STATUS_SUCCESS), "success (no error)");
137 // Any unknown code must still produce a string, never a panic or NULL deref.
138 assert!(!status_str(0x7fff_0000).is_empty());
139 }
140
141 #[test]
142 fn check_maps_success_and_failure() {
143 assert!(check("vaFake", sys::VA_STATUS_SUCCESS).is_ok());
144 let err = check("vaFake", sys::VA_STATUS_ERROR_UNIMPLEMENTED).unwrap_err();
145 assert_eq!(err.status(), Some(sys::VA_STATUS_ERROR_UNIMPLEMENTED));
146 assert!(err.to_string().contains("vaFake"));
147 }
148}