// Copyright 2025 Tree xie.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use snafu::Snafu;
use tibba_error::Error as BaseError;

#[derive(Debug, Snafu)]
pub enum Error {
    /// 封装 hmac crate 的 `InvalidLength`，使调用方可直接使用 `.context(HmacSha256Snafu)`。
    #[snafu(display("hmac sha256 error: {source}"))]
    HmacSha256 { source: hmac::digest::InvalidLength },

    /// 密钥列表为空，无法执行签名或验签操作。
    #[snafu(display("key grip empty"))]
    KeyGripEmpty,
}

impl From<Error> for BaseError {
    fn from(val: Error) -> Self {
        let err = match val {
            Error::HmacSha256 { source } => BaseError::new(source).with_sub_category("hmac_sha256"),
            Error::KeyGripEmpty => BaseError::new("key grip empty")
                .with_sub_category("key_grip")
                .with_status(500)
                .with_exception(true),
        };
        err.with_category("crypto")
    }
}

mod key_grip;

pub use key_grip::*;
