| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
use std::time::Duration; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 15 |
pub enum LimitScope { |
| 16 |
|
| 17 |
User, |
| 18 |
|
| 19 |
Global, |
| 20 |
} |
| 21 |
|
| 22 |
impl std::fmt::Display for LimitScope { |
| 23 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 24 |
match self { |
| 25 |
Self::User => f.write_str("per-user"), |
| 26 |
Self::Global => f.write_str("global"), |
| 27 |
} |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
#[derive(Debug, thiserror::Error)] |
| 33 |
pub enum ChatError { |
| 34 |
#[error( |
| 35 |
"retention exceeds the crate ceiling (asked for {max_age:?} / {max_messages} messages)" |
| 36 |
)] |
| 37 |
RetentionTooLong { |
| 38 |
max_age: Duration, |
| 39 |
max_messages: usize, |
| 40 |
}, |
| 41 |
|
| 42 |
#[error("retention must be non-zero in both bounds")] |
| 43 |
RetentionTooShort, |
| 44 |
|
| 45 |
#[error("message is {len} characters, over the {max} character limit")] |
| 46 |
MessageTooLong { len: usize, max: usize }, |
| 47 |
|
| 48 |
#[error("message is empty")] |
| 49 |
MessageEmpty, |
| 50 |
|
| 51 |
#[error("invalid send nonce")] |
| 52 |
InvalidNonce, |
| 53 |
|
| 54 |
#[error("room is closed")] |
| 55 |
RoomClosed, |
| 56 |
|
| 57 |
#[error("room is read-only")] |
| 58 |
RoomReadOnly, |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
#[error("connection limit reached ({0})")] |
| 63 |
ConnectionLimit(LimitScope), |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
#[error("send rejected: {0:?}")] |
| 68 |
Rejected(crate::send::SendRejection), |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
#[error("host error: {0}")] |
| 73 |
Host(#[source] Box<dyn std::error::Error + Send + Sync>), |
| 74 |
} |
| 75 |
|
| 76 |
impl ChatError { |
| 77 |
|
| 78 |
pub fn host<E: std::error::Error + Send + Sync + 'static>(err: E) -> Self { |
| 79 |
Self::Host(Box::new(err)) |
| 80 |
} |
| 81 |
} |
| 82 |
|