morethantext-web/src/morethantext/error.rs

136 lines
3.2 KiB
Rust
Raw Permalink Normal View History

2022-09-27 07:31:59 -04:00
use std::{error::Error, fmt};
2022-07-22 20:34:50 -04:00
2023-07-01 15:56:12 -04:00
#[derive(Clone, Debug)]
2023-03-14 11:32:37 -04:00
pub enum ErrorCode {
2023-05-29 15:42:32 -04:00
// General
2023-03-14 11:32:37 -04:00
Undefined(String),
2023-06-17 12:01:58 -04:00
// Cache
IDNotFound(String),
2023-06-24 10:48:35 -04:00
// Store
DuplicateDatabase(String),
2023-07-21 18:10:38 -04:00
// Database
DuplicateTable(String),
2023-03-14 11:32:37 -04:00
}
impl fmt::Display for ErrorCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ErrorCode::Undefined(msg) => write!(f, "{}", msg),
2023-06-17 12:01:58 -04:00
ErrorCode::IDNotFound(id) => write!(f, "ID '{}' not found", id),
2023-06-24 10:48:35 -04:00
ErrorCode::DuplicateDatabase(name) => write!(f, "database '{}' already exists", name),
2023-07-21 18:10:38 -04:00
ErrorCode::DuplicateTable(name) => write!(f, "table '{}' already exists", name),
2023-05-29 15:42:32 -04:00
}
}
}
mod errorcodes {
use super::*;
const ITEMS: [&str; 2] = ["one", "two"];
#[test]
fn undefined_display() {
for item in ITEMS {
let err = ErrorCode::Undefined(item.to_string());
assert_eq!(err.to_string(), item);
2023-03-14 11:32:37 -04:00
}
}
2023-06-17 12:01:58 -04:00
#[test]
fn cache_missing() {
for item in ITEMS {
let err = ErrorCode::IDNotFound(item.to_string());
assert_eq!(err.to_string(), format!("ID '{}' not found", item));
}
}
2023-06-24 10:48:35 -04:00
#[test]
fn duplicate_database() {
for item in ITEMS {
let err = ErrorCode::DuplicateDatabase(item.to_string());
assert_eq!(
err.to_string(),
format!("database '{}' already exists", item)
);
}
}
2023-07-21 18:10:38 -04:00
#[test]
fn duplicate_table() {
for item in ITEMS {
let err = ErrorCode::DuplicateTable(item.to_string());
assert_eq!(err.to_string(), format!("table '{}' already exists", item));
}
}
2023-03-14 11:32:37 -04:00
}
2023-07-01 15:56:12 -04:00
#[derive(Clone, Debug)]
2023-05-29 15:42:32 -04:00
pub struct MTTError {
2023-06-17 12:01:58 -04:00
pub code: ErrorCode,
2022-07-23 21:28:34 -04:00
}
2023-05-29 15:42:32 -04:00
impl MTTError {
2023-06-17 12:01:58 -04:00
pub fn new<S>(msg: S) -> Self
2022-08-06 12:03:47 -04:00
where
S: Into<String>,
{
2023-05-29 15:42:32 -04:00
let text = msg.into();
2022-07-22 20:34:50 -04:00
Self {
2023-05-29 15:42:32 -04:00
code: ErrorCode::Undefined(text),
2022-07-22 20:34:50 -04:00
}
}
2022-07-23 21:28:34 -04:00
2023-06-17 12:01:58 -04:00
pub fn from_code(code: ErrorCode) -> Self {
2023-05-29 15:42:32 -04:00
Self { code: code }
2022-07-23 21:28:34 -04:00
}
2022-07-22 20:34:50 -04:00
}
2023-05-29 15:42:32 -04:00
impl Error for MTTError {}
2022-07-23 21:28:34 -04:00
2023-05-29 15:42:32 -04:00
impl fmt::Display for MTTError {
2022-07-22 20:34:50 -04:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2023-03-14 11:32:37 -04:00
write!(f, "{}", self.code)
2022-07-22 20:34:50 -04:00
}
}
#[cfg(test)]
2023-03-14 11:32:37 -04:00
mod errors {
2022-08-06 12:03:47 -04:00
use super::*;
#[test]
2023-05-29 15:42:32 -04:00
fn create_with_str() {
let msgs = ["one", "two"];
for msg in msgs {
let err = MTTError::new(msg);
assert_eq!(err.to_string(), msg);
2023-03-14 11:32:37 -04:00
}
}
#[test]
2023-05-29 15:42:32 -04:00
fn create_with_string() {
let msg = "three";
let err = MTTError::new(msg.to_string());
assert_eq!(err.to_string(), msg);
2023-03-14 11:32:37 -04:00
}
#[test]
2023-05-29 15:42:32 -04:00
fn create_from_code() {
let code = ErrorCode::Undefined("oops".to_string());
let err = MTTError::from_code(code);
match err.code {
ErrorCode::Undefined(_) => (),
_ => assert!(false, "{:?} is not undefined", err.code),
2023-03-14 11:32:37 -04:00
}
}
2023-06-17 12:01:58 -04:00
#[test]
fn create_missing_id_from_code() {
let code = ErrorCode::IDNotFound("123".to_string());
let err = MTTError::from_code(code);
match err.code {
ErrorCode::IDNotFound(_) => (),
_ => assert!(false, "{:?} is not undefined", err.code),
}
}
2023-03-14 11:32:37 -04:00
}