pub mod corruptabledb;
pub mod manager;
pub mod memdb;
pub mod rpcdb;

use std::io::Result;

use num_derive::{FromPrimitive, ToPrimitive};

use crate::rpcchainvm::health::Checkable;

#[tonic::async_trait]
pub trait Closer {
    async fn close(&self) -> Result<()>;
}

#[tonic::async_trait]
pub trait Database: KeyValueReaderWriterDeleter + Closer + Checkable {}

/// ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/database#KeyValueReaderWriterDeleter
#[tonic::async_trait]
pub trait KeyValueReaderWriterDeleter {
    async fn has(&self, key: &[u8]) -> Result<bool>;
    async fn get(&self, key: &[u8]) -> Result<Vec<u8>>;
    async fn put(&mut self, key: &[u8], value: &[u8]) -> Result<()>;
    async fn delete(&mut self, key: &[u8]) -> Result<()>;
}

#[tonic::async_trait]
pub trait VersionedDatabase {
    async fn close(&self) -> Result<()>;
}

/// ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/database#ErrClosed
#[derive(Copy, Clone, Debug, FromPrimitive, ToPrimitive)]
pub enum DatabaseError {
    None = 0,
    Closed,
    NotFound,
}
