#![allow(unused_imports, dead_code, clippy::module_inception)]
use std::fmt;
pub type TabernaId = u64;
pub type MessageType = u32;
pub type PeerMessageId = u32;
pub const MSG_HELLO: MessageType = 1;
pub const MSG_HELLO_RESPONSE: MessageType = 2;
pub const MSG_KEEPALIVE: MessageType = 3;
pub const MSG_ACK: MessageType = 4;
pub const MSG_CLOSE: MessageType = 5;
pub const MSG_ERROR: MessageType = 6;
pub const MSG_BLOB_TRANSFER_START: MessageType = 7;
pub const MSG_BLOB_TRANSFER_CHUNK: MessageType = 8;
pub const MSG_BLOB_TRANSFER_COMPLETE: MessageType = 9;
pub type LogId = u32;
pub mod log_ids {
use super::LogId;
pub const HANDSHAKE_TOTAL_LIMIT: LogId = 1001;
pub const HANDSHAKE_PER_PEER_LIMIT: LogId = 1002;
pub const CALLIS_PER_PEER_LIMIT: LogId = 1003;
pub const LIMITED_LOG_IDS: &[LogId] = &[
HANDSHAKE_TOTAL_LIMIT,
HANDSHAKE_PER_PEER_LIMIT,
CALLIS_PER_PEER_LIMIT,
];
}
pub const ERROR_MESSAGE_MAX_LEN: usize = 1024;
#[repr(u32)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ErrorId {
UnknownTaberna = 1,
LocalQueueFull = 2,
PeerUnavailable = 3,
RemoteTabernaRejected = 4,
ConnectionLost = 5,
PeerRestarted = 6,
ProtocolViolation = 7,
UnsupportedVersion = 8,
EncodeFailure = 9,
DecodeFailure = 10,
TabernaBusy = 11,
SendTimeout = 12,
BlobCallisWithoutPrimary = 13,
BlobAckWindowExceeded = 14,
BlobStreamNotFound = 15,
BlobStreamOutOfOrder = 16,
BlobStreamIdleTimeout = 17,
BlobStreamMissingChunk = 18,
BlobBufferFull = 19,
AddressMismatch = 20,
TabernaAlreadyRegistered = 21,
InvalidConfig = 22,
DomusClosed = 23,
ReceiveTimeout = 24,
}
impl ErrorId {
pub fn as_u32(self) -> u32 {
self as u32
}
#[deprecated(note = "Use ErrorId::try_from or u32::try_into instead.")]
pub fn from_u32(value: u32) -> Option<Self> {
ErrorId::from_u32_inner(value)
}
fn from_u32_inner(value: u32) -> Option<Self> {
match value {
1 => Some(ErrorId::UnknownTaberna),
2 => Some(ErrorId::LocalQueueFull),
3 => Some(ErrorId::PeerUnavailable),
4 => Some(ErrorId::RemoteTabernaRejected),
5 => Some(ErrorId::ConnectionLost),
6 => Some(ErrorId::PeerRestarted),
7 => Some(ErrorId::ProtocolViolation),
8 => Some(ErrorId::UnsupportedVersion),
9 => Some(ErrorId::EncodeFailure),
10 => Some(ErrorId::DecodeFailure),
11 => Some(ErrorId::TabernaBusy),
12 => Some(ErrorId::SendTimeout),
13 => Some(ErrorId::BlobCallisWithoutPrimary),
14 => Some(ErrorId::BlobAckWindowExceeded),
15 => Some(ErrorId::BlobStreamNotFound),
16 => Some(ErrorId::BlobStreamOutOfOrder),
17 => Some(ErrorId::BlobStreamIdleTimeout),
18 => Some(ErrorId::BlobStreamMissingChunk),
19 => Some(ErrorId::BlobBufferFull),
20 => Some(ErrorId::AddressMismatch),
21 => Some(ErrorId::TabernaAlreadyRegistered),
22 => Some(ErrorId::InvalidConfig),
23 => Some(ErrorId::DomusClosed),
24 => Some(ErrorId::ReceiveTimeout),
_ => None,
}
}
}
impl TryFrom<u32> for ErrorId {
type Error = ();
fn try_from(value: u32) -> Result<Self, Self::Error> {
ErrorId::from_u32_inner(value).ok_or(())
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AureliaError {
pub kind: ErrorId,
pub message: Option<String>,
}
impl AureliaError {
pub fn new(kind: ErrorId) -> Self {
Self {
kind,
message: None,
}
}
pub fn with_message(kind: ErrorId, message: impl Into<String>) -> Self {
let mut message = message.into();
if message.len() > ERROR_MESSAGE_MAX_LEN {
let mut idx = ERROR_MESSAGE_MAX_LEN;
while idx > 0 && !message.is_char_boundary(idx) {
idx -= 1;
}
message.truncate(idx);
}
Self {
kind,
message: Some(message),
}
}
}
impl fmt::Display for AureliaError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.message {
Some(message) => write!(f, "{:?}: {}", self.kind, message),
None => write!(f, "{:?}", self.kind),
}
}
}
impl std::error::Error for AureliaError {}