Skip to content

Commit eb9df74

Browse files
Implement AsRef<str> for a few enums that represent string values
1 parent eca1070 commit eb9df74

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed

src/commons.rs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,39 @@ impl From<String> for SupportedProtocol {
153153
}
154154
}
155155

156+
impl AsRef<str> for SupportedProtocol {
157+
fn as_ref(&self) -> &str {
158+
match self {
159+
SupportedProtocol::Clustering => SUPPORTED_PROTOCOL_CLUSTERING,
160+
SupportedProtocol::AMQP => SUPPORTED_PROTOCOL_AMQP,
161+
SupportedProtocol::AMQPWithTLS => SUPPORTED_PROTOCOL_AMQP_WITH_TLS,
162+
SupportedProtocol::Stream => SUPPORTED_PROTOCOL_STREAM,
163+
SupportedProtocol::StreamWithTLS => SUPPORTED_PROTOCOL_STREAM_WITH_TLS,
164+
SupportedProtocol::MQTT => SUPPORTED_PROTOCOL_MQTT,
165+
SupportedProtocol::MQTTWithTLS => SUPPORTED_PROTOCOL_MQTT_WITH_TLS,
166+
SupportedProtocol::STOMP => SUPPORTED_PROTOCOL_STOMP,
167+
SupportedProtocol::STOMPWithTLS => SUPPORTED_PROTOCOL_STOMP_WITH_TLS,
168+
SupportedProtocol::AMQPOverWebSockets => SUPPORTED_PROTOCOL_AMQP_OVER_WEBSOCKETS,
169+
SupportedProtocol::AMQPOverWebSocketsWithTLS => {
170+
SUPPORTED_PROTOCOL_AMQP_OVER_WEBSOCKETS_WITH_TLS
171+
}
172+
SupportedProtocol::MQTTOverWebSockets => SUPPORTED_PROTOCOL_MQTT_OVER_WEBSOCKETS,
173+
SupportedProtocol::MQTTOverWebSocketsWithTLS => {
174+
SUPPORTED_PROTOCOL_MQTT_OVER_WEBSOCKETS_WITH_TLS
175+
}
176+
SupportedProtocol::STOMPOverWebsockets => SUPPORTED_PROTOCOL_STOMP_OVER_WEBSOCKETS,
177+
SupportedProtocol::STOMPOverWebsocketsWithTLS => {
178+
SUPPORTED_PROTOCOL_STOMP_OVER_WEBSOCKETS_WITH_TLS
179+
}
180+
SupportedProtocol::Prometheus => SUPPORTED_PROTOCOL_PROMETHEUS,
181+
SupportedProtocol::PrometheusWithTLS => SUPPORTED_PROTOCOL_PROMETHEUS_WITH_TLS,
182+
SupportedProtocol::HTTP => SUPPORTED_PROTOCOL_HTTP,
183+
SupportedProtocol::HTTPWithTLS => SUPPORTED_PROTOCOL_HTTP_WITH_TLS,
184+
SupportedProtocol::Other(s) => s.as_str(),
185+
}
186+
}
187+
}
188+
156189
impl From<SupportedProtocol> for String {
157190
fn from(value: SupportedProtocol) -> String {
158191
match value {
@@ -290,6 +323,26 @@ impl From<String> for ExchangeType {
290323
}
291324
}
292325

326+
impl AsRef<str> for ExchangeType {
327+
fn as_ref(&self) -> &str {
328+
match self {
329+
ExchangeType::Fanout => EXCHANGE_TYPE_FANOUT,
330+
ExchangeType::Topic => EXCHANGE_TYPE_TOPIC,
331+
ExchangeType::Direct => EXCHANGE_TYPE_DIRECT,
332+
ExchangeType::Headers => EXCHANGE_TYPE_HEADERS,
333+
ExchangeType::ConsistentHashing => EXCHANGE_TYPE_CONSISTENT_HASHING,
334+
ExchangeType::ModulusHash => EXCHANGE_TYPE_MODULUS_HASH,
335+
ExchangeType::Random => EXCHANGE_TYPE_RANDOM,
336+
ExchangeType::LocalRandom => EXCHANGE_TYPE_LOCAL_RANDOM,
337+
ExchangeType::JmsTopic => EXCHANGE_TYPE_JMS_TOPIC,
338+
ExchangeType::RecentHistory => EXCHANGE_TYPE_RECENT_HISTORY,
339+
ExchangeType::DelayedMessage => EXCHANGE_TYPE_DELAYED_MESSAGE,
340+
ExchangeType::MessageDeduplication => EXCHANGE_TYPE_MESSAGE_DEDUPLICATION,
341+
ExchangeType::Plugin(s) => s,
342+
}
343+
}
344+
}
345+
293346
impl From<ExchangeType> for String {
294347
fn from(value: ExchangeType) -> String {
295348
match value {
@@ -355,6 +408,18 @@ impl From<String> for QueueType {
355408
}
356409
}
357410

411+
impl AsRef<str> for QueueType {
412+
fn as_ref(&self) -> &str {
413+
match self {
414+
QueueType::Classic => "classic",
415+
QueueType::Quorum => "quorum",
416+
QueueType::Stream => "stream",
417+
QueueType::Delayed => "delayed",
418+
QueueType::Unsupported(s) => s.as_str(),
419+
}
420+
}
421+
}
422+
358423
impl From<QueueType> for String {
359424
fn from(value: QueueType) -> Self {
360425
match value {
@@ -412,6 +477,15 @@ impl From<String> for BindingDestinationType {
412477
}
413478
}
414479

480+
impl AsRef<str> for BindingDestinationType {
481+
fn as_ref(&self) -> &str {
482+
match self {
483+
BindingDestinationType::Queue => "queue",
484+
BindingDestinationType::Exchange => "exchange",
485+
}
486+
}
487+
}
488+
415489
impl From<BindingDestinationType> for String {
416490
fn from(value: BindingDestinationType) -> Self {
417491
match value {
@@ -532,6 +606,19 @@ impl From<String> for PolicyTarget {
532606
}
533607
}
534608

609+
impl AsRef<str> for PolicyTarget {
610+
fn as_ref(&self) -> &str {
611+
match self {
612+
PolicyTarget::Queues => "queues",
613+
PolicyTarget::ClassicQueues => "classic_queues",
614+
PolicyTarget::QuorumQueues => "quorum_queues",
615+
PolicyTarget::Streams => "streams",
616+
PolicyTarget::Exchanges => "exchanges",
617+
PolicyTarget::All => "all",
618+
}
619+
}
620+
}
621+
535622
impl From<PolicyTarget> for String {
536623
fn from(value: PolicyTarget) -> Self {
537624
match value {
@@ -649,6 +736,16 @@ impl From<String> for MessageTransferAcknowledgementMode {
649736
}
650737
}
651738

739+
impl AsRef<str> for MessageTransferAcknowledgementMode {
740+
fn as_ref(&self) -> &str {
741+
match self {
742+
MessageTransferAcknowledgementMode::Immediate => "no-ack",
743+
MessageTransferAcknowledgementMode::WhenPublished => "on-publish",
744+
MessageTransferAcknowledgementMode::WhenConfirmed => "on-confirm",
745+
}
746+
}
747+
}
748+
652749
impl Display for MessageTransferAcknowledgementMode {
653750
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
654751
match self {

src/responses.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,17 @@ impl From<String> for ChannelState {
774774
}
775775
}
776776

777+
impl AsRef<str> for ChannelState {
778+
fn as_ref(&self) -> &str {
779+
match self {
780+
ChannelState::Starting => "starting",
781+
ChannelState::Running => "running",
782+
ChannelState::Closing => "closing",
783+
ChannelState::Unknown(s) => s.as_str(),
784+
}
785+
}
786+
}
787+
777788
impl fmt::Display for ChannelState {
778789
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
779790
match self {
@@ -1572,6 +1583,12 @@ impl<'a> From<&'a str> for ClusterIdentity {
15721583
}
15731584
}
15741585

1586+
impl AsRef<str> for ClusterIdentity {
1587+
fn as_ref(&self) -> &str {
1588+
&self.name
1589+
}
1590+
}
1591+
15751592
#[derive(Debug, Deserialize, Clone)]
15761593
#[allow(dead_code)]
15771594
pub struct ClusterTags(pub Map<String, serde_json::Value>);
@@ -2447,6 +2464,17 @@ impl From<String> for FeatureFlagState {
24472464
}
24482465
}
24492466

2467+
impl AsRef<str> for FeatureFlagState {
2468+
fn as_ref(&self) -> &str {
2469+
match self {
2470+
FeatureFlagState::Enabled => "enabled",
2471+
FeatureFlagState::Disabled => "disabled",
2472+
FeatureFlagState::StateChanging => "state_changing",
2473+
FeatureFlagState::Unavailable => "unavailable",
2474+
}
2475+
}
2476+
}
2477+
24502478
impl From<FeatureFlagState> for String {
24512479
fn from(value: FeatureFlagState) -> Self {
24522480
match value {
@@ -2501,6 +2529,16 @@ impl From<String> for FeatureFlagStability {
25012529
}
25022530
}
25032531

2532+
impl AsRef<str> for FeatureFlagStability {
2533+
fn as_ref(&self) -> &str {
2534+
match self {
2535+
FeatureFlagStability::Required => "required",
2536+
FeatureFlagStability::Stable => "stable",
2537+
FeatureFlagStability::Experimental => "experimental",
2538+
}
2539+
}
2540+
}
2541+
25042542
impl From<FeatureFlagStability> for String {
25052543
fn from(value: FeatureFlagStability) -> Self {
25062544
match value {
@@ -2597,6 +2635,18 @@ impl From<String> for DeprecationPhase {
25972635
}
25982636
}
25992637

2638+
impl AsRef<str> for DeprecationPhase {
2639+
fn as_ref(&self) -> &str {
2640+
match self {
2641+
DeprecationPhase::PermittedByDefault => "permitted_by_default",
2642+
DeprecationPhase::DeniedByDefault => "denied_by_default",
2643+
DeprecationPhase::Disconnected => "disconnected",
2644+
DeprecationPhase::Removed => "removed",
2645+
DeprecationPhase::Undefined => "undefined",
2646+
}
2647+
}
2648+
}
2649+
26002650
impl From<DeprecationPhase> for String {
26012651
fn from(value: DeprecationPhase) -> Self {
26022652
match value {

0 commit comments

Comments
 (0)