1use thiserror::Error;
2
3use aic_sdk_sys::AicErrorCode::{self, *};
4
5#[derive(Debug, Clone, PartialEq, Eq, Error)]
7pub enum AicError {
8 #[error(
9 "Parameter value is outside the acceptable range. Check documentation for valid values."
10 )]
11 ParameterOutOfRange,
12 #[error(
13 "Processor must be initialized before calling this operation. Call `Processor::initialize` first."
14 )]
15 ProcessorNotInitialized,
16 #[error(
17 "Audio configuration (samplerate, num_channels, num_frames) is not supported by the model"
18 )]
19 AudioConfigUnsupported,
20 #[error("Audio buffer configuration differs from the one provided during initialization")]
21 AudioConfigMismatch,
22 #[error(
23 "SDK key was not authorized or process failed to report usage. Check if you have internet connection."
24 )]
25 EnhancementNotAllowed,
26 #[error("Internal error occurred. Contact support.")]
27 Internal,
28 #[error("License key format is invalid or corrupted. Verify the key was copied correctly.")]
29 LicenseFormatInvalid,
30 #[error(
31 "License version is not compatible with the SDK version. Update SDK or contact support."
32 )]
33 LicenseVersionUnsupported,
34 #[error("License key has expired. Renew your license to continue.")]
35 LicenseExpired,
36 #[error("The model file is invalid or corrupted. Verify the file is correct.")]
37 ModelInvalid,
38 #[error("The model file version is not compatible with this SDK version.")]
39 ModelVersionUnsupported,
40 #[error("The path to the model file is invalid")]
41 ModelFilePathInvalid,
42 #[error(
43 "The model file cannot be opened due to a filesystem error. Verify that the file exists."
44 )]
45 FileSystemError,
46 #[error("The model data is not aligned to 64 bytes.")]
47 ModelDataUnaligned,
48 #[error("Model download error: {0}")]
49 ModelDownload(String),
50 #[error("Unknown error code: {0}")]
51 Unknown(AicErrorCode::Type),
52}
53
54impl From<AicErrorCode::Type> for AicError {
55 fn from(error_code: AicErrorCode::Type) -> Self {
56 match error_code {
57 AIC_ERROR_CODE_NULL_POINTER => {
58 panic!(
61 "Unexpected null pointer error from C library - this is a bug in the Rust wrapper"
62 );
63 }
64 AIC_ERROR_CODE_PARAMETER_OUT_OF_RANGE => AicError::ParameterOutOfRange,
65 AIC_ERROR_CODE_PROCESSOR_NOT_INITIALIZED => AicError::ProcessorNotInitialized,
66 AIC_ERROR_CODE_AUDIO_CONFIG_UNSUPPORTED => AicError::AudioConfigUnsupported,
67 AIC_ERROR_CODE_AUDIO_CONFIG_MISMATCH => AicError::AudioConfigMismatch,
68 AIC_ERROR_CODE_ENHANCEMENT_NOT_ALLOWED => AicError::EnhancementNotAllowed,
69 AIC_ERROR_CODE_INTERNAL_ERROR => AicError::Internal,
70 AIC_ERROR_CODE_LICENSE_FORMAT_INVALID => AicError::LicenseFormatInvalid,
71 AIC_ERROR_CODE_LICENSE_VERSION_UNSUPPORTED => AicError::LicenseVersionUnsupported,
72 AIC_ERROR_CODE_LICENSE_EXPIRED => AicError::LicenseExpired,
73 AIC_ERROR_CODE_MODEL_INVALID => AicError::ModelInvalid,
74 AIC_ERROR_CODE_MODEL_VERSION_UNSUPPORTED => AicError::ModelVersionUnsupported,
75 AIC_ERROR_CODE_MODEL_FILE_PATH_INVALID => AicError::ModelFilePathInvalid,
76 AIC_ERROR_CODE_FILE_SYSTEM_ERROR => AicError::FileSystemError,
77 AIC_ERROR_CODE_MODEL_DATA_UNALIGNED => AicError::ModelDataUnaligned,
78 code => AicError::Unknown(code),
79 }
80 }
81}
82
83pub(crate) fn handle_error(error_code: AicErrorCode::Type) -> Result<(), AicError> {
85 match error_code {
86 AIC_ERROR_CODE_SUCCESS => Ok(()),
87 code => Err(AicError::from(code)),
88 }
89}
90
91pub(crate) fn assert_success(error_code: AicErrorCode::Type, message: &str) {
92 assert_eq!(error_code, AIC_ERROR_CODE_SUCCESS, "{}", message);
93}