use thiserror::Error;
use aic_sdk_sys::AicErrorCode::{self, *};
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum AicError {
#[error(
"Parameter value is outside the acceptable range. Check documentation for valid values."
)]
ParameterOutOfRange,
#[error("Handle must be initialized before calling this operation.")]
NotInitialized,
#[error("Audio configuration (sample_rate, block_size) is not supported by the model")]
AudioConfigUnsupported,
#[error("Audio block configuration differs from the one provided during initialization")]
AudioConfigMismatch,
#[error(
"Processing is not allowed because the SDK key was not authorized or usage reporting failed."
)]
ProcessingNotAllowed,
#[error("Internal error occurred. Contact support.")]
Internal,
#[error("License key format is invalid or corrupted. Verify the key was copied correctly.")]
LicenseFormatInvalid,
#[error(
"License version is not compatible with the SDK version. Update SDK or contact support."
)]
LicenseVersionUnsupported,
#[error("License key has expired. Renew your license to continue.")]
LicenseExpired,
#[error(
"Updating the token is only supported when both the original and new keys are JWT-form licenses."
)]
TokenUpdateUnsupported,
#[error("The model file is invalid or corrupted. Verify the file is correct.")]
ModelInvalid,
#[error("The model file version is not compatible with this SDK version.")]
ModelVersionUnsupported,
#[error("The model type is not supported by the requested API.")]
ModelTypeUnsupported,
#[error("The file path is invalid.")]
FilePathInvalid,
#[error(
"The model file cannot be opened due to a filesystem error. Verify that the file exists."
)]
FileSystemError,
#[error("The model data is not aligned to 64 bytes.")]
ModelDataUnaligned,
#[error("Model download error: {0}")]
ModelDownload(String),
#[error("Unknown error code: {0}")]
Unknown(AicErrorCode::Type),
}
impl From<AicErrorCode::Type> for AicError {
fn from(error_code: AicErrorCode::Type) -> Self {
match error_code {
AIC_ERROR_CODE_NULL_POINTER => {
panic!(
"Unexpected null pointer error from C library - this is a bug in the Rust wrapper"
);
}
AIC_ERROR_CODE_PARAMETER_OUT_OF_RANGE => AicError::ParameterOutOfRange,
AIC_ERROR_CODE_NOT_INITIALIZED => AicError::NotInitialized,
AIC_ERROR_CODE_AUDIO_CONFIG_UNSUPPORTED => AicError::AudioConfigUnsupported,
AIC_ERROR_CODE_AUDIO_CONFIG_MISMATCH => AicError::AudioConfigMismatch,
AIC_ERROR_CODE_PROCESSING_NOT_ALLOWED => AicError::ProcessingNotAllowed,
AIC_ERROR_CODE_INTERNAL_ERROR => AicError::Internal,
AIC_ERROR_CODE_LICENSE_FORMAT_INVALID => AicError::LicenseFormatInvalid,
AIC_ERROR_CODE_LICENSE_VERSION_UNSUPPORTED => AicError::LicenseVersionUnsupported,
AIC_ERROR_CODE_LICENSE_EXPIRED => AicError::LicenseExpired,
AIC_ERROR_CODE_TOKEN_UPDATE_UNSUPPORTED => AicError::TokenUpdateUnsupported,
AIC_ERROR_CODE_MODEL_INVALID => AicError::ModelInvalid,
AIC_ERROR_CODE_MODEL_VERSION_UNSUPPORTED => AicError::ModelVersionUnsupported,
AIC_ERROR_CODE_MODEL_TYPE_UNSUPPORTED => AicError::ModelTypeUnsupported,
AIC_ERROR_CODE_FILE_PATH_INVALID => AicError::FilePathInvalid,
AIC_ERROR_CODE_FILE_SYSTEM_ERROR => AicError::FileSystemError,
AIC_ERROR_CODE_MODEL_DATA_UNALIGNED => AicError::ModelDataUnaligned,
code => AicError::Unknown(code),
}
}
}
pub(crate) fn handle_error(error_code: AicErrorCode::Type) -> Result<(), AicError> {
match error_code {
AIC_ERROR_CODE_SUCCESS => Ok(()),
code => Err(AicError::from(code)),
}
}
pub(crate) fn assert_success(error_code: AicErrorCode::Type, message: &str) {
assert_eq!(error_code, AIC_ERROR_CODE_SUCCESS, "{}", message);
}