use iggy_binary_protocol::consensus::EvictionReason;
use iggy_binary_protocol::version::IGGY_PROTOCOL_VERSION;
use super::iggy_error::IggyError;
#[must_use]
pub fn eviction_reason_to_error(
reason: EvictionReason,
server_protocol_version: u32,
server_protocol_version_min: u32,
) -> IggyError {
match reason {
EvictionReason::InvalidCredentials => IggyError::InvalidCredentials,
EvictionReason::InvalidToken => IggyError::InvalidPersonalAccessToken,
EvictionReason::UserInactive
| EvictionReason::SessionError
| EvictionReason::NoSession
| EvictionReason::SessionTooLow
| EvictionReason::SessionReleaseMismatch => IggyError::Unauthenticated,
EvictionReason::StaleClient => IggyError::StaleClient,
EvictionReason::IncompatibleProtocol => {
if server_protocol_version_min == 0
|| server_protocol_version < server_protocol_version_min
{
IggyError::Unauthenticated
} else {
IggyError::IncompatibleProtocolVersion(
IGGY_PROTOCOL_VERSION,
server_protocol_version_min,
server_protocol_version,
)
}
}
EvictionReason::MalformedLogin => IggyError::InvalidFormat,
_ => IggyError::InvalidCommand,
}
}