aic_sdk/
error.rs

1use thiserror::Error;
2
3use aic_sdk_sys::AicErrorCode::{self, *};
4
5/// Error type for AIC SDK operations.
6#[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("The requested parameter is read-only for this model type and cannot be modified.")]
29    ParameterFixed,
30    #[error("License key format is invalid or corrupted. Verify the key was copied correctly.")]
31    LicenseFormatInvalid,
32    #[error(
33        "License version is not compatible with the SDK version. Update SDK or contact support."
34    )]
35    LicenseVersionUnsupported,
36    #[error("License key has expired. Renew your license to continue.")]
37    LicenseExpired,
38    #[error("The model file is invalid or corrupted. Verify the file is correct.")]
39    ModelInvalid,
40    #[error("The model file version is not compatible with this SDK version.")]
41    ModelVersionUnsupported,
42    #[error("The path to the model file is invalid")]
43    ModelFilePathInvalid,
44    #[error(
45        "The model file cannot be opened due to a filesystem error. Verify that the file exists."
46    )]
47    FileSystemError,
48    #[error("The model data is not aligned to 64 bytes.")]
49    ModelDataUnaligned,
50    #[error("Model download error: {0}")]
51    ModelDownload(String),
52    #[error("Unknown error code: {0}")]
53    Unknown(AicErrorCode::Type),
54}
55
56impl From<AicErrorCode::Type> for AicError {
57    fn from(error_code: AicErrorCode::Type) -> Self {
58        match error_code {
59            AIC_ERROR_CODE_NULL_POINTER => {
60                // This should never happen in our Rust wrapper, but if it does,
61                // it indicates a serious bug in our wrapper logic
62                panic!(
63                    "Unexpected null pointer error from C library - this is a bug in the Rust wrapper"
64                );
65            }
66            AIC_ERROR_CODE_PARAMETER_OUT_OF_RANGE => AicError::ParameterOutOfRange,
67            AIC_ERROR_CODE_PROCESSOR_NOT_INITIALIZED => AicError::ProcessorNotInitialized,
68            AIC_ERROR_CODE_AUDIO_CONFIG_UNSUPPORTED => AicError::AudioConfigUnsupported,
69            AIC_ERROR_CODE_AUDIO_CONFIG_MISMATCH => AicError::AudioConfigMismatch,
70            AIC_ERROR_CODE_ENHANCEMENT_NOT_ALLOWED => AicError::EnhancementNotAllowed,
71            AIC_ERROR_CODE_INTERNAL_ERROR => AicError::Internal,
72            AIC_ERROR_CODE_PARAMETER_FIXED => AicError::ParameterFixed,
73            AIC_ERROR_CODE_LICENSE_FORMAT_INVALID => AicError::LicenseFormatInvalid,
74            AIC_ERROR_CODE_LICENSE_VERSION_UNSUPPORTED => AicError::LicenseVersionUnsupported,
75            AIC_ERROR_CODE_LICENSE_EXPIRED => AicError::LicenseExpired,
76            AIC_ERROR_CODE_MODEL_INVALID => AicError::ModelInvalid,
77            AIC_ERROR_CODE_MODEL_VERSION_UNSUPPORTED => AicError::ModelVersionUnsupported,
78            AIC_ERROR_CODE_MODEL_FILE_PATH_INVALID => AicError::ModelFilePathInvalid,
79            AIC_ERROR_CODE_FILE_SYSTEM_ERROR => AicError::FileSystemError,
80            AIC_ERROR_CODE_MODEL_DATA_UNALIGNED => AicError::ModelDataUnaligned,
81            code => AicError::Unknown(code),
82        }
83    }
84}
85
86/// Helper function to convert C error codes into Result.
87pub(crate) fn handle_error(error_code: AicErrorCode::Type) -> Result<(), AicError> {
88    match error_code {
89        AIC_ERROR_CODE_SUCCESS => Ok(()),
90        code => Err(AicError::from(code)),
91    }
92}
93
94pub(crate) fn assert_success(error_code: AicErrorCode::Type, message: &str) {
95    assert_eq!(error_code, AIC_ERROR_CODE_SUCCESS, "{}", message);
96}