rabbitmq_stream_protocol/response/
shims.rs

1use std::convert::TryFrom;
2
3use crate::protocol::responses::*;
4use crate::{error::DecodeError, ResponseCode};
5
6impl TryFrom<u16> for ResponseCode {
7    type Error = DecodeError;
8
9    fn try_from(value: u16) -> Result<Self, Self::Error> {
10        match value {
11            RESPONSE_CODE_OK => Ok(ResponseCode::Ok),
12            RESPONSE_CODE_STREAM_DOES_NOT_EXIST => Ok(ResponseCode::StreamDoesNotExist),
13            RESPONSE_CODE_SUBSCRIPTION_ID_ALREADY_EXISTS => {
14                Ok(ResponseCode::SubscriptionIdAlreadyExists)
15            }
16            RESPONSE_CODE_SUBSCRIPTION_ID_DOES_NOT_EXIST => {
17                Ok(ResponseCode::SubscriptionIdDoesNotExist)
18            }
19            RESPONSE_CODE_STREAM_ALREADY_EXISTS => Ok(ResponseCode::StreamAlreadyExists),
20            RESPONSE_CODE_STREAM_NOT_AVAILABLE => Ok(ResponseCode::StreamNotAvailable),
21            RESPONSE_CODE_SASL_MECHANISM_NOT_SUPPORTED => {
22                Ok(ResponseCode::SaslMechanismNotSupported)
23            }
24            RESPONSE_CODE_AUTHENTICATION_FAILURE => Ok(ResponseCode::AuthenticationFailure),
25            RESPONSE_CODE_SASL_ERROR => Ok(ResponseCode::SaslError),
26            RESPONSE_CODE_SASL_CHALLENGE => Ok(ResponseCode::SaslChallange),
27            RESPONSE_CODE_AUTHENTICATION_FAILURE_LOOPBACK => {
28                Ok(ResponseCode::AuthenticationFailureLoopback)
29            }
30            RESPONSE_CODE_VIRTUAL_HOST_ACCESS_FAILURE => Ok(ResponseCode::VirtualHostAccessFailure),
31            RESPONSE_CODE_UNKNOWN_FRAME => Ok(ResponseCode::UnknownFrame),
32            RESPONSE_CODE_FRAME_TOO_LARGE => Ok(ResponseCode::FrameTooLarge),
33            RESPONSE_CODE_INTERNAL_ERROR => Ok(ResponseCode::InternalError),
34            RESPONSE_CODE_ACCESS_REFUSED => Ok(ResponseCode::AccessRefused),
35            RESPONSE_CODE_PRECONDITION_FAILED => Ok(ResponseCode::PrecoditionFailed),
36            RESPONSE_CODE_PUBLISHER_DOES_NOT_EXIST => Ok(ResponseCode::PublisherDoesNotExist),
37            RESPONSE_CODE_OFFSET_NOT_FOUND => Ok(ResponseCode::OffsetNotFound),
38            _ => Err(DecodeError::UnknownResponseCode(value)),
39        }
40    }
41}
42
43impl From<&ResponseCode> for u16 {
44    fn from(code: &ResponseCode) -> Self {
45        match code {
46            ResponseCode::Ok => RESPONSE_CODE_OK,
47            ResponseCode::StreamDoesNotExist => RESPONSE_CODE_STREAM_DOES_NOT_EXIST,
48            ResponseCode::SubscriptionIdAlreadyExists => {
49                RESPONSE_CODE_SUBSCRIPTION_ID_ALREADY_EXISTS
50            }
51            ResponseCode::SubscriptionIdDoesNotExist => {
52                RESPONSE_CODE_SUBSCRIPTION_ID_DOES_NOT_EXIST
53            }
54            ResponseCode::StreamAlreadyExists => RESPONSE_CODE_STREAM_ALREADY_EXISTS,
55            ResponseCode::StreamNotAvailable => RESPONSE_CODE_STREAM_NOT_AVAILABLE,
56            ResponseCode::SaslMechanismNotSupported => RESPONSE_CODE_SASL_MECHANISM_NOT_SUPPORTED,
57            ResponseCode::AuthenticationFailure => RESPONSE_CODE_AUTHENTICATION_FAILURE,
58            ResponseCode::SaslError => RESPONSE_CODE_SASL_ERROR,
59            ResponseCode::SaslChallange => RESPONSE_CODE_SASL_CHALLENGE,
60            ResponseCode::AuthenticationFailureLoopback => {
61                RESPONSE_CODE_AUTHENTICATION_FAILURE_LOOPBACK
62            }
63            ResponseCode::VirtualHostAccessFailure => RESPONSE_CODE_VIRTUAL_HOST_ACCESS_FAILURE,
64            ResponseCode::UnknownFrame => RESPONSE_CODE_UNKNOWN_FRAME,
65            ResponseCode::FrameTooLarge => RESPONSE_CODE_FRAME_TOO_LARGE,
66            ResponseCode::InternalError => RESPONSE_CODE_INTERNAL_ERROR,
67            ResponseCode::AccessRefused => RESPONSE_CODE_ACCESS_REFUSED,
68            ResponseCode::PrecoditionFailed => RESPONSE_CODE_PRECONDITION_FAILED,
69            ResponseCode::PublisherDoesNotExist => RESPONSE_CODE_PUBLISHER_DOES_NOT_EXIST,
70            ResponseCode::OffsetNotFound => RESPONSE_CODE_OFFSET_NOT_FOUND,
71        }
72    }
73}