1use std::path::PathBuf;
4
5use baracuda_types::{CudaStatus, CudaVersion};
6use thiserror::Error;
7
8#[derive(Debug, Error)]
15pub enum LoaderError {
16 #[error("could not load {library}: tried {candidates:?} across {search_paths} path(s)")]
19 LibraryNotFound {
20 library: &'static str,
21 candidates: Vec<&'static str>,
22 search_paths: usize,
23 },
24
25 #[error("library '{library}' is missing symbol '{symbol}'")]
27 SymbolNotFound {
28 library: &'static str,
29 symbol: &'static str,
30 },
31
32 #[error("symbol '{symbol}' requires {required} but baracuda's driver loader sees {installed}")]
36 VersionTooOld {
37 symbol: &'static str,
38 required: CudaVersion,
39 installed: CudaVersion,
40 },
41
42 #[error("{0}")]
46 Libloading(#[from] libloading::Error),
47
48 #[error("baracuda does not support {platform}; NVIDIA driver is only available on Linux and Windows")]
50 UnsupportedPlatform { platform: &'static str },
51}
52
53impl LoaderError {
54 pub fn library_not_found(library: &'static str, candidates: &[&'static str]) -> Self {
57 Self::LibraryNotFound {
58 library,
59 candidates: candidates.to_vec(),
60 search_paths: 0,
61 }
62 }
63
64 pub fn library_not_found_with_search(
66 library: &'static str,
67 candidates: &[&'static str],
68 search_path_count: usize,
69 ) -> Self {
70 Self::LibraryNotFound {
71 library,
72 candidates: candidates.to_vec(),
73 search_paths: search_path_count,
74 }
75 }
76}
77
78#[derive(Debug, Error)]
82pub enum Error<S>
83where
84 S: CudaStatus + Send + Sync + 'static,
85{
86 #[error("{} returned {} ({}): {}", .status.library(), .status.name(), .status.code(), .status.description())]
88 Status { status: S },
89
90 #[error(transparent)]
92 Loader(#[from] LoaderError),
93
94 #[error("{api} requires {since}; install a newer driver to use it")]
96 FeatureNotSupported {
97 api: &'static str,
98 since: CudaVersion,
99 },
100}
101
102impl<S> Error<S>
103where
104 S: CudaStatus + Send + Sync + 'static,
105{
106 pub fn check(status: S) -> Result<(), Self> {
109 if status.is_success() {
110 Ok(())
111 } else {
112 Err(Self::Status { status })
113 }
114 }
115}
116
117#[derive(Debug, Error)]
120pub enum BaracudaError {
121 #[error("{library} returned {name} ({code}): {description}")]
123 Status {
124 library: &'static str,
125 name: &'static str,
126 description: &'static str,
127 code: i32,
128 },
129
130 #[error(transparent)]
132 Loader(#[from] LoaderError),
133
134 #[error("{api} requires {since}; install a newer driver to use it")]
136 FeatureNotSupported {
137 api: &'static str,
138 since: CudaVersion,
139 },
140
141 #[error("{context}")]
144 Context { context: &'static str },
145}
146
147impl<S> From<Error<S>> for BaracudaError
148where
149 S: CudaStatus + Send + Sync + 'static,
150{
151 fn from(err: Error<S>) -> Self {
152 match err {
153 Error::Status { status } => BaracudaError::Status {
154 library: status.library(),
155 name: status.name(),
156 description: status.description(),
157 code: status.code(),
158 },
159 Error::Loader(l) => BaracudaError::Loader(l),
160 Error::FeatureNotSupported { api, since } => {
161 BaracudaError::FeatureNotSupported { api, since }
162 }
163 }
164 }
165}
166
167#[allow(dead_code)]
170pub(crate) type PathList = Vec<PathBuf>;