1use std::path::PathBuf;
4
5pub type Result<T, E = Error> = std::result::Result<T, E>;
7
8#[derive(Debug, thiserror::Error)]
10#[non_exhaustive]
11pub enum Error {
12 #[error("failed to load PJRT plugin `{}`", path.display())]
14 Load {
15 path: PathBuf,
17 #[source]
19 source: libloading::Error,
20 },
21
22 #[error("the PJRT plugin does not provide `{0}`")]
27 MissingFunction(&'static str),
28
29 #[error("PJRT error ({code:?}): {message}")]
31 Pjrt {
32 code: ErrorCode,
34 message: String,
36 },
37
38 #[error("host buffer mismatch: {0}")]
40 HostBuffer(String),
41}
42
43#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
47#[non_exhaustive]
48#[allow(missing_docs)]
50pub enum ErrorCode {
51 Cancelled,
52 Unknown,
53 InvalidArgument,
54 DeadlineExceeded,
55 NotFound,
56 AlreadyExists,
57 PermissionDenied,
58 ResourceExhausted,
59 FailedPrecondition,
60 Aborted,
61 OutOfRange,
62 Unimplemented,
63 Internal,
64 Unavailable,
65 DataLoss,
66 Unauthenticated,
67 Other(u32),
69}
70
71impl ErrorCode {
72 pub(crate) fn from_raw(code: u32) -> Self {
73 match code {
74 1 => Self::Cancelled,
75 2 => Self::Unknown,
76 3 => Self::InvalidArgument,
77 4 => Self::DeadlineExceeded,
78 5 => Self::NotFound,
79 6 => Self::AlreadyExists,
80 7 => Self::PermissionDenied,
81 8 => Self::ResourceExhausted,
82 9 => Self::FailedPrecondition,
83 10 => Self::Aborted,
84 11 => Self::OutOfRange,
85 12 => Self::Unimplemented,
86 13 => Self::Internal,
87 14 => Self::Unavailable,
88 15 => Self::DataLoss,
89 16 => Self::Unauthenticated,
90 other => Self::Other(other),
91 }
92 }
93}
94
95#[cfg(test)]
96mod tests {
97 use super::*;
98
99 #[test]
100 fn error_code_from_raw_matches_absl() {
101 assert_eq!(ErrorCode::from_raw(3), ErrorCode::InvalidArgument);
102 assert_eq!(ErrorCode::from_raw(16), ErrorCode::Unauthenticated);
103 assert_eq!(ErrorCode::from_raw(99), ErrorCode::Other(99));
104 }
105}