/*
 * SPDX-FileCopyrightText: Copyright (c) 2024, NVIDIA CORPORATION.
 * SPDX-License-Identifier: Apache-2.0
 */

use std::fmt;

#[derive(Debug, Clone)]
pub struct CuvsError {
    code: ffi::cuvsError_t,
    text: String,
}

#[derive(Debug, Clone)]
pub enum Error {
    CudaError(ffi::cudaError_t),
    CuvsError(CuvsError),
}

impl std::error::Error for Error {}
impl std::error::Error for CuvsError {}

pub type Result<T> = std::result::Result<T, Error>;

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Error::CudaError(cuda_error) => write!(f, "cudaError={:?}", cuda_error),
            Error::CuvsError(cuvs_error) => write!(f, "cuvsError={:?}", cuvs_error),
        }
    }
}

impl fmt::Display for CuvsError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}:{}", self.code, self.text)
    }
}

/// Simple wrapper to convert a cuvsError_t into a Result
pub fn check_cuvs(err: ffi::cuvsError_t) -> Result<()> {
    match err {
        ffi::cuvsError_t::CUVS_SUCCESS => Ok(()),
        _ => {
            // get a description of the error from cuvs
            let cstr = unsafe {
                let text_ptr = ffi::cuvsGetLastErrorText();
                std::ffi::CStr::from_ptr(text_ptr)
            };
            let text = std::string::String::from_utf8_lossy(cstr.to_bytes()).to_string();

            Err(Error::CuvsError(CuvsError { code: err, text }))
        }
    }
}

pub fn check_cuda(err: ffi::cudaError_t) -> Result<()> {
    match err {
        ffi::cudaError::cudaSuccess => Ok(()),
        _ => Err(Error::CudaError(err)),
    }
}
