Initial commit
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum ErrorKind {
|
||||
InvalidJson,
|
||||
InvalidRequest,
|
||||
UnknownMethod,
|
||||
Unauthorized,
|
||||
Forbidden,
|
||||
NotFound,
|
||||
RateLimited,
|
||||
NetworkError,
|
||||
TlsError,
|
||||
ServerError,
|
||||
WebsocketDisconnected,
|
||||
InternalError,
|
||||
}
|
||||
|
||||
impl ErrorKind {
|
||||
pub fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
Self::InvalidJson => "invalid_json",
|
||||
Self::InvalidRequest => "invalid_request",
|
||||
Self::UnknownMethod => "unknown_method",
|
||||
Self::Unauthorized => "unauthorized",
|
||||
Self::Forbidden => "forbidden",
|
||||
Self::NotFound => "not_found",
|
||||
Self::RateLimited => "rate_limited",
|
||||
Self::NetworkError => "network_error",
|
||||
Self::TlsError => "tls_error",
|
||||
Self::ServerError => "server_error",
|
||||
Self::WebsocketDisconnected => "websocket_disconnected",
|
||||
Self::InternalError => "internal_error",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn retryable(self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
Self::RateLimited
|
||||
| Self::NetworkError
|
||||
| Self::ServerError
|
||||
| Self::WebsocketDisconnected
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum BridgeError {
|
||||
#[error("{kind}: {message}", kind = kind.as_str())]
|
||||
Protocol { kind: ErrorKind, message: String },
|
||||
}
|
||||
|
||||
impl BridgeError {
|
||||
pub fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
|
||||
Self::Protocol {
|
||||
kind,
|
||||
message: message.into(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn kind(&self) -> ErrorKind {
|
||||
match self {
|
||||
Self::Protocol { kind, .. } => *kind,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn message(&self) -> &str {
|
||||
match self {
|
||||
Self::Protocol { message, .. } => message,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
||||
macro_rules! string_id {
|
||||
($name:ident) => {
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
#[serde(transparent)]
|
||||
pub struct $name(pub String);
|
||||
|
||||
impl $name {
|
||||
pub fn new(value: impl Into<String>) -> Self {
|
||||
Self(value.into())
|
||||
}
|
||||
|
||||
pub fn as_str(&self) -> &str {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for $name {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str(self.as_str())
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
string_id!(ServerId);
|
||||
string_id!(TeamId);
|
||||
string_id!(ChannelId);
|
||||
string_id!(UserId);
|
||||
string_id!(PostId);
|
||||
|
||||
impl Default for ServerId {
|
||||
fn default() -> Self {
|
||||
Self::new("default")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
//! Shared domain types for emacs-mattermost.
|
||||
//!
|
||||
//! This crate contains no network code.
|
||||
|
||||
pub mod error;
|
||||
pub mod ids;
|
||||
pub mod model;
|
||||
|
||||
pub use error::{BridgeError, ErrorKind};
|
||||
pub use ids::{ChannelId, PostId, ServerId, TeamId, UserId};
|
||||
pub use model::{BridgeState, ServerSummary};
|
||||
@@ -0,0 +1,14 @@
|
||||
use crate::ids::ServerId;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum BridgeState {
|
||||
Running,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct ServerSummary {
|
||||
pub server_id: ServerId,
|
||||
pub state: String,
|
||||
}
|
||||
Reference in New Issue
Block a user