// Copyright lowRISC contributors.
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0

// This file was autogenerated by `//sw/host/hsmtool/scripts/pkcs11_consts.py`.
// Do not edit.'

use cryptoki_sys::*;
use num_enum::{FromPrimitive, IntoPrimitive};
use std::convert::TryFrom;

use crate::util::attribute::{AttrData, AttributeError};

#[derive(
    Clone,
    Copy,
    Debug,
    PartialEq,
    Eq,
    Hash,
    IntoPrimitive,
    FromPrimitive,
    serde::Serialize,
    serde::Deserialize,
)]
#[repr(u64)]
pub enum ObjectClass {
    #[serde(rename = "CKO_DATA")]
    Data = CKO_DATA,
    #[serde(rename = "CKO_CERTIFICATE")]
    Certificate = CKO_CERTIFICATE,
    #[serde(rename = "CKO_PUBLIC_KEY")]
    PublicKey = CKO_PUBLIC_KEY,
    #[serde(rename = "CKO_PRIVATE_KEY")]
    PrivateKey = CKO_PRIVATE_KEY,
    #[serde(rename = "CKO_SECRET_KEY")]
    SecretKey = CKO_SECRET_KEY,
    #[serde(rename = "CKO_HW_FEATURE")]
    HwFeature = CKO_HW_FEATURE,
    #[serde(rename = "CKO_DOMAIN_PARAMETERS")]
    DomainParameters = CKO_DOMAIN_PARAMETERS,
    #[serde(rename = "CKO_MECHANISM")]
    Mechanism = CKO_MECHANISM,
    #[serde(rename = "CKO_OTP_KEY")]
    OtpKey = CKO_OTP_KEY,
    #[serde(rename = "CKO_VENDOR_DEFINED")]
    VendorDefined = CKO_VENDOR_DEFINED,
    #[num_enum(catch_all)]
    UnknownObjectClass(u64) = u64::MAX,
}

impl From<cryptoki::object::ObjectClass> for ObjectClass {
    fn from(val: cryptoki::object::ObjectClass) -> Self {
        let val = CK_OBJECT_CLASS::from(val);
        Self::from(val)
    }
}

impl TryFrom<ObjectClass> for cryptoki::object::ObjectClass {
    type Error = cryptoki::error::Error;
    fn try_from(val: ObjectClass) -> Result<Self, Self::Error> {
        let val = CK_OBJECT_CLASS::from(val);
        cryptoki::object::ObjectClass::try_from(val)
    }
}

impl TryFrom<&AttrData> for ObjectClass {
    type Error = AttributeError;
    fn try_from(val: &AttrData) -> Result<Self, Self::Error> {
        match val {
            AttrData::ObjectClass(x) => Ok(*x),
            _ => Err(AttributeError::EncodingError),
        }
    }
}

impl From<ObjectClass> for AttrData {
    fn from(val: ObjectClass) -> Self {
        AttrData::ObjectClass(val)
    }
}
