1use thiserror::Error;
2
3use aic_sdk_sys::AicErrorCode::{self, *};
4
5#[derive(Debug, Clone, PartialEq, Eq, Error)]
7pub enum AicError {
8 #[error(
10 "Parameter value is outside the acceptable range. Check documentation for valid values."
11 )]
12 ParameterOutOfRange,
13 #[error("Handle must be initialized before calling this operation.")]
15 NotInitialized,
16 #[error("Audio configuration (sample_rate, block_size) is not supported by the model")]
18 AudioConfigUnsupported,
19 #[error("Audio block configuration differs from the one provided during initialization")]
21 AudioConfigMismatch,
22 #[error(
24 "Processing is not allowed because the SDK key was not authorized or usage reporting failed."
25 )]
26 ProcessingNotAllowed,
27 #[error("Internal error occurred. Contact support.")]
29 Internal,
30 #[error("License key format is invalid or corrupted. Verify the key was copied correctly.")]
32 LicenseFormatInvalid,
33 #[error(
35 "License version is not compatible with the SDK version. Update SDK or contact support."
36 )]
37 LicenseVersionUnsupported,
38 #[error("License key has expired. Renew your license to continue.")]
40 LicenseExpired,
41 #[error(
43 "Updating the token is only supported when both the original and new keys are JWT-form licenses."
44 )]
45 TokenUpdateUnsupported,
46 #[error("The model file is invalid or corrupted. Verify the file is correct.")]
48 ModelInvalid,
49 #[error("The model file version is not compatible with this SDK version.")]
51 ModelVersionUnsupported,
52 #[error("The model type is not supported by the requested API.")]
54 ModelTypeUnsupported,
55 #[error("The file path is invalid.")]
57 FilePathInvalid,
58 #[error(
60 "The model file cannot be opened due to a filesystem error. Verify that the file exists."
61 )]
62 FileSystemError,
63 #[error("The model data is not aligned to 64 bytes.")]
65 ModelDataUnaligned,
66 #[error("Model download error: {0}")]
68 ModelDownload(String),
69 #[error("Unknown error code: {0}")]
71 Unknown(AicErrorCode::Type),
72}
73
74impl From<AicErrorCode::Type> for AicError {
75 fn from(error_code: AicErrorCode::Type) -> Self {
76 match error_code {
77 AIC_ERROR_CODE_NULL_POINTER => {
78 panic!(
81 "Unexpected null pointer error from C library - this is a bug in the Rust wrapper"
82 );
83 }
84 AIC_ERROR_CODE_PARAMETER_OUT_OF_RANGE => AicError::ParameterOutOfRange,
85 AIC_ERROR_CODE_NOT_INITIALIZED => AicError::NotInitialized,
86 AIC_ERROR_CODE_AUDIO_CONFIG_UNSUPPORTED => AicError::AudioConfigUnsupported,
87 AIC_ERROR_CODE_AUDIO_CONFIG_MISMATCH => AicError::AudioConfigMismatch,
88 AIC_ERROR_CODE_PROCESSING_NOT_ALLOWED => AicError::ProcessingNotAllowed,
89 AIC_ERROR_CODE_INTERNAL_ERROR => AicError::Internal,
90 AIC_ERROR_CODE_LICENSE_FORMAT_INVALID => AicError::LicenseFormatInvalid,
91 AIC_ERROR_CODE_LICENSE_VERSION_UNSUPPORTED => AicError::LicenseVersionUnsupported,
92 AIC_ERROR_CODE_LICENSE_EXPIRED => AicError::LicenseExpired,
93 AIC_ERROR_CODE_TOKEN_UPDATE_UNSUPPORTED => AicError::TokenUpdateUnsupported,
94 AIC_ERROR_CODE_MODEL_INVALID => AicError::ModelInvalid,
95 AIC_ERROR_CODE_MODEL_VERSION_UNSUPPORTED => AicError::ModelVersionUnsupported,
96 AIC_ERROR_CODE_MODEL_TYPE_UNSUPPORTED => AicError::ModelTypeUnsupported,
97 AIC_ERROR_CODE_FILE_PATH_INVALID => AicError::FilePathInvalid,
98 AIC_ERROR_CODE_FILE_SYSTEM_ERROR => AicError::FileSystemError,
99 AIC_ERROR_CODE_MODEL_DATA_UNALIGNED => AicError::ModelDataUnaligned,
100 code => AicError::Unknown(code),
101 }
102 }
103}
104
105pub(crate) fn handle_error(error_code: AicErrorCode::Type) -> Result<(), AicError> {
107 match error_code {
108 AIC_ERROR_CODE_SUCCESS => Ok(()),
109 code => Err(AicError::from(code)),
110 }
111}
112
113pub(crate) fn assert_success(error_code: AicErrorCode::Type, message: &str) {
114 assert_eq!(error_code, AIC_ERROR_CODE_SUCCESS, "{}", message);
115}