use std::fmt;
use crate::TransportError;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Reason {
WrongCode,
Refused,
Busy,
Failed,
Declined,
HoldsSpace,
}
impl Reason {
pub(crate) const fn number(self) -> u8 {
match self {
Self::WrongCode => 1,
Self::Refused => 2,
Self::Busy => 3,
Self::Failed => 4,
Self::Declined => 5,
Self::HoldsSpace => 6,
}
}
pub(crate) const fn of_number(number: u8) -> Option<Self> {
match number {
1 => Some(Self::WrongCode),
2 => Some(Self::Refused),
3 => Some(Self::Busy),
4 => Some(Self::Failed),
5 => Some(Self::Declined),
6 => Some(Self::HoldsSpace),
_ => None,
}
}
}
impl fmt::Display for Reason {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::WrongCode => "the pairing code did not match",
Self::Refused => {
"it does not sync with this device, which it never paired with or revoked"
}
Self::Busy => "it is syncing with another replica",
Self::Failed => "something went wrong on its side",
Self::Declined => "the invitation was declined",
Self::HoldsSpace => "the library that was invited holds the space already",
})
}
}
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum SyncError {
#[error("the connection failed: {0}")]
Connection(#[from] TransportError),
#[error("the other device broke the protocol: {0}")]
Protocol(String),
#[error("the other device ended the connection: {0}")]
Ended(Reason),
#[error("the pairing code did not match")]
WrongCode,
#[error("the other device is no replica of this library")]
NotAReplica,
#[error("the invitation was declined")]
Declined,
#[error("this library holds the space already")]
HoldsSpace,
#[error(transparent)]
Holder(Box<dyn std::error::Error + Send + Sync>),
}
impl SyncError {
pub(crate) fn protocol(what: impl Into<String>) -> Self {
Self::Protocol(what.into())
}
pub(crate) fn holder(error: impl std::error::Error + Send + Sync + 'static) -> Self {
Self::Holder(Box::new(error))
}
pub(crate) fn either_side(self) -> Self {
match self {
Self::Ended(Reason::WrongCode) => Self::WrongCode,
error => error,
}
}
pub(crate) const fn reason(&self) -> Option<Reason> {
match self {
Self::Ended(_) | Self::Connection(_) => None,
Self::NotAReplica => Some(Reason::Refused),
Self::WrongCode => Some(Reason::WrongCode),
Self::Declined => Some(Reason::Declined),
Self::HoldsSpace => Some(Reason::HoldsSpace),
Self::Protocol(_) | Self::Holder(_) => Some(Reason::Failed),
}
}
}