use std::fmt::Display;
use serde::{Deserialize, Serialize};
use crate::StdError;
#[derive(Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct ServerError {
pub message: String,
}
impl ServerError {
pub fn new<M: Into<String>>(message: M) -> ServerError {
ServerError {
message: message.into(),
}
}
}
impl Display for ServerError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.message)
}
}
impl From<String> for ServerError {
fn from(message: String) -> Self {
ServerError::new(message)
}
}
impl From<&str> for ServerError {
fn from(message: &str) -> Self {
ServerError::new(message.to_string())
}
}
impl From<StdError> for ServerError {
fn from(error: StdError) -> Self {
ServerError::new(format!("{error:?}"))
}
}
#[derive(Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct ClientError {
pub label: String,
pub message: String,
}
impl ClientError {
pub fn new<L: Into<String>, M: Into<String>>(label: L, message: M) -> ClientError {
ClientError {
label: label.into(),
message: message.into(),
}
}
}
impl Display for ClientError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {}", self.label, self.message)
}
}