use doublets::data::LinkReference;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum LinkError {
#[error("Link not found: {0}")]
NotFound(u128),
#[error("Invalid link format: {0}")]
InvalidFormat(String),
#[error("Storage error: {0}")]
StorageError(String),
#[error("Query error: {0}")]
QueryError(String),
#[error("Parse error: {0}")]
ParseError(String),
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("Doublets store error: {0}")]
Doublets(String),
#[error("Lock error: {0}")]
Lock(String),
#[error("Transaction error: {0}")]
Transaction(String),
#[error("Address {0} does not fit into the configured link address type")]
AddressOutOfRange(u128),
}
impl LinkError {
pub fn not_found<T: LinkReference>(index: T) -> Self {
Self::NotFound(to_u128(index))
}
}
impl<T: LinkReference> From<doublets::Error<T>> for LinkError {
fn from(error: doublets::Error<T>) -> Self {
match error {
doublets::Error::NotExists(index) => Self::NotFound(to_u128(index)),
other => Self::Doublets(other.to_string()),
}
}
}
fn to_u128<T: LinkReference>(index: T) -> u128 {
TryInto::<u128>::try_into(index).unwrap_or(u128::MAX)
}