worm_hole 1.1.0

CLI tool to easily jump between directories
Documentation
use std::fmt::Display;

/// List of errors that the program can return.
#[derive(Debug)]
pub enum WHError {
	DatabaseConnectionError(String),
	AliasNotFound(String),
	AliasAlreadyExists(String),
	PathOfAliasNotExist(String, String),
	PatternNotMatch(String),
}

impl Display for WHError {
	#[rustfmt::skip]
	/// Display the error message.
	/// ```
	/// use worm_hole::error::WHError;
	/// let error = WHError::DatabaseConnectionError("path".to_string());
	/// assert_eq!(format!("{}", error), "Error connecting to database at path");
	/// ```
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match self {
			WHError::DatabaseConnectionError(path) => write!(f, "Error connecting to database at {}", path),
			WHError::AliasNotFound(alias) => write!(f, "Alias {} does not exist", alias),
			WHError::AliasAlreadyExists(alias) => write!(f, "Cannot create alias {} because it already exists", alias),
			WHError::PathOfAliasNotExist(alias, path) => write!(f, "The path {} does no longer exist\nRun `wormhole rm {}` to remove the alias or `wormhole edit {} <new_path>` to update the path", path, alias, alias),
			WHError::PatternNotMatch(pattern) => write!(f, "The pattern {} does not match anything", pattern),
		}
	}
}

/// Result type which wither take a type T or a WHError.
pub type WHResult<T> = std::result::Result<T, WHError>;

/// Unwrap the WHResult and print the error message if it is an error.
/// ```
/// use worm_hole::error::{WHResult, unwrap_worm_hole_error};
/// let result: WHResult<i32> = Ok(1);
/// assert_eq!(unwrap_worm_hole_error(result), 1);
/// ```
/// ```should_panic
/// use worm_hole::error::{WHError, WHResult, unwrap_worm_hole_error};
/// let result: WHResult<i32> = Err(WHError::DatabaseConnectionError("path".to_string()));
/// unwrap_worm_hole_error(result);
/// ```
pub fn unwrap_worm_hole_error<T>(result: WHResult<T>) -> T {
	match result {
		Ok(value) => value,
		Err(error) => {
			eprintln!("{}", error);
			std::process::exit(1);
		}
	}
}