unobtanium 3.0.0

Opinioated Web search engine library with crawler and viewer companion.
Documentation
/*
 * Provides unified facilities for error handling.
 */

use thiserror::Error;

use crate::database::DatabaseError;

#[derive(Error, Debug)]
pub enum UnobtaniumError {
	#[error("{0}")]
	GenericError(String),
	#[error("DatabaseError: {source}")]
	DatabaseError {
		source: DatabaseError,
	}
}

impl UnobtaniumError {
	/// Wheter the error is about something not being found.
	pub fn is_not_found(&self) -> bool {
		match self {
			Self::GenericError(_) => false,
			Self::DatabaseError { source } => source.is_not_found(),
		}
	}
}

impl From<DatabaseError> for UnobtaniumError {
	fn from(error: DatabaseError) -> Self {
		Self::DatabaseError { source: error }
	}
}

impl From<String> for UnobtaniumError {
	fn from(message: String) -> Self {
		Self::GenericError(message)
	}
}

pub trait NotFoundToOpt<T> {
	fn err_to_opt(self) -> Result<Option<T>,UnobtaniumError>;
}

impl<T> NotFoundToOpt<T> for Result<T,UnobtaniumError> {
	fn err_to_opt(self) -> Result<Option<T>,UnobtaniumError> {
		match self {
			Ok(v) => Ok(Some(v)),
			Err(e) => if e.is_not_found() {
				Ok(None)
			} else {
				Err(e)
			}
		}
	}
}

impl<T> NotFoundToOpt<T> for Result<Option<T>,UnobtaniumError> {
	fn err_to_opt(self) -> Result<Option<T>,UnobtaniumError> {
		match self {
			Ok(v) => Ok(v),
			Err(e) => if e.is_not_found() {
				Ok(None)
			} else {
				Err(e)
			}
		}
	}
}