databoard 0.3.1

Provides a hierarchical key-value-store
Documentation
// Copyright © 2025 Stephan Kunz
//! The [`Databoard`](crate::Databoard) error handling.

use crate::ConstString;

/// Shortcut for [`Databoard`](crate::Databoard)'s Result<T, E> type
pub type Result<T> = core::result::Result<T, Error>;

/// Things that may go wrong using the [`Databoard`](crate::Databoard).
#[non_exhaustive]
pub enum Error {
	/// Entry with `key` already exists.
	AlreadyExists {
		/// Key of the entry to create.
		key: ConstString,
	},
	/// Key is already remapped.
	AlreadyRemapped {
		/// Key to be remapped.
		key: ConstString,
		/// The already existing remapping.
		remapped: ConstString,
	},
	/// Remapping of `key` is a value assignment.
	Assignment {
		/// Key to be remapped.
		key: ConstString,
		/// Value to be asssigned.
		value: ConstString,
	},
	/// Value of `target` cannot be converted into a [`RemappingTarget`](crate::RemappingTarget).
	CreateRemapping {
		/// str to convert into target.
		source: ConstString,
	},
	/// Entry with `key` is locked.
	IsLocked {
		/// Key of the wanted entry.
		key: ConstString,
	},
	/// Defined a remapping without giving a parent.
	NoParent {
		/// Original key.
		key: ConstString,
		/// Defined remapping.
		remapped: ConstString,
	},
	/// Defined a remapping without valid target.
	NoTarget {
		/// Original key.
		key: ConstString,
	},
	/// Entry with `key` not stored.
	NotFound {
		/// Key of the wanted entry.
		key: ConstString,
	},
	/// Entry with `key` is stored with a different type.
	WrongType {
		/// Key of the wanted entry.
		key: ConstString,
	},
	/// Something impossible happened.
	Unreachable(ConstString, u32),
}

/// Currently the default implementation is sufficient.
impl core::error::Error for Error {
	// fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
	// 	None
	// }

	// fn cause(&self) -> Option<&dyn core::error::Error> {
	// 	self.source()
	// }

	// fn provide<'a>(&'a self, request: &mut core::error::Request<'a>) {}
}

impl core::fmt::Debug for Error {
	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
		match self {
			Self::AlreadyExists { key } => write!(f, "AlreadyExists(key: {key})"),
			Self::AlreadyRemapped { key, remapped } => {
				write!(f, "AlreadyRemapped(key: {key}, remapped: {remapped})")
			}
			Self::Assignment { key, value } => write!(f, "Assignment(key: {key}, value: {value})"),
			Self::CreateRemapping { source } => write!(f, "CreateRemapping(source: {source})"),
			Self::IsLocked { key } => write!(f, "Locked(key: {key}"),
			Self::NoParent { key, remapped } => write!(f, "NoParent(key: {key}, remapped: {remapped})"),
			Self::NoTarget { key } => write!(f, "NoTarget(key: {key})"),
			Self::NotFound { key } => write!(f, "NotFound(key: {key})"),
			Self::WrongType { key } => write!(f, "WrongType(key: {key})"),
			Self::Unreachable(file, line) => write!(f, "Unreachable(file: {file}, line: {line})"),
		}
	}
}

impl core::fmt::Display for Error {
	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
		match self {
			Self::AlreadyExists { key } => write!(f, "cannot create data with key {key} as they already exist"),
			Self::AlreadyRemapped { key, remapped } => {
				write!(f, "key {key} is already remapped as {remapped}")
			}
			Self::Assignment { key, value } => write!(f, "remapping of {key} contains an assignment of {value}"),
			Self::CreateRemapping { source } => write!(f, "cannot create a remapping target from {source}"),
			Self::IsLocked { key } => write!(f, "the entry {key} is locked"),
			Self::NoParent { key, remapped } => write!(f, "remapping of {key} to {remapped} without a parent board"),
			Self::NoTarget { key } => write!(f, "no target for {key} defined"),
			Self::NotFound { key } => write!(f, "an entry for the key {key} is not existing"),
			Self::WrongType { key } => write!(f, "the entry for the key {key} is stored with a different type"),
			Self::Unreachable(file, line) => write!(f, "an unexpected error occured in {file} at line {line}"),
		}
	}
}

#[cfg(test)]
mod tests {
	use super::*;

	// check, that the auto traits are available
	const fn is_normal<T: Sized + Send + Sync>() {}

	#[test]
	const fn normal_types() {
		is_normal::<Error>();
	}
}