Skip to content

Commit 76adc84

Browse files
Add more context to QueueType::Unsupported
Implementing only TryFrom<T> would entail a lot more changes that are difficult to justify. This is a minimalis alternative to using TryFrom<T> and Result<T> all over the place.
1 parent 76f45c2 commit 76adc84

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

Diff for: src/commons.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ impl From<ExchangeType> for String {
338338
}
339339
}
340340

341-
#[derive(Eq, PartialEq, Debug, Serialize, Deserialize, Clone, Copy, Default)]
341+
#[derive(Eq, PartialEq, Debug, Serialize, Deserialize, Clone, Default)]
342342
#[serde(rename_all(serialize = "lowercase", deserialize = "PascalCase"))]
343343
pub enum QueueType {
344344
#[default]
@@ -348,28 +348,30 @@ pub enum QueueType {
348348
// Tanzu RabbitMQ-specific
349349
Delayed,
350350
// A type this client is not aware of
351-
Unsupported
351+
Unsupported(String),
352352
}
353353

354354
impl From<&str> for QueueType {
355355
fn from(value: &str) -> Self {
356-
match value.to_ascii_lowercase().as_str() {
356+
let val = value.to_ascii_lowercase();
357+
match val.as_str() {
357358
"classic" => QueueType::Classic,
358359
"quorum" => QueueType::Quorum,
359360
"stream" => QueueType::Stream,
360361
"delayed" => QueueType::Delayed,
361-
_ => QueueType::Unsupported,
362+
_ => QueueType::Unsupported(value.to_owned()),
362363
}
363364
}
364365
}
365366

366367
impl From<String> for QueueType {
367368
fn from(value: String) -> Self {
368-
match value.to_ascii_lowercase().as_str() {
369+
let val = value.to_ascii_lowercase();
370+
match val.as_str() {
369371
"classic" => QueueType::Classic,
370372
"quorum" => QueueType::Quorum,
371373
"stream" => QueueType::Stream,
372-
_ => QueueType::Unsupported,
374+
_ => QueueType::Unsupported(value),
373375
}
374376
}
375377
}
@@ -381,7 +383,7 @@ impl From<QueueType> for String {
381383
QueueType::Quorum => "quorum".to_owned(),
382384
QueueType::Stream => "stream".to_owned(),
383385
QueueType::Delayed => "delayed".to_owned(),
384-
QueueType::Unsupported => "unsupported".to_owned(),
386+
QueueType::Unsupported(val) => val.to_owned(),
385387
}
386388
}
387389
}
@@ -459,10 +461,10 @@ impl From<QueueType> for PolicyTarget {
459461
fn from(value: QueueType) -> Self {
460462
match value {
461463
QueueType::Classic => PolicyTarget::ClassicQueues,
462-
QueueType::Quorum => PolicyTarget::QuorumQueues,
463-
QueueType::Stream => PolicyTarget::Streams,
464+
QueueType::Quorum => PolicyTarget::QuorumQueues,
465+
QueueType::Stream => PolicyTarget::Streams,
464466
QueueType::Delayed => PolicyTarget::Queues,
465-
QueueType::Unsupported => PolicyTarget::Queues,
467+
QueueType::Unsupported(_) => PolicyTarget::Queues,
466468
}
467469
}
468470
}

Diff for: src/error.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ pub enum Error<U, S, E, BT> {
5353
#[error("could not convert provided value into an HTTP header value")]
5454
InvalidHeaderValue { error: InvalidHeaderValue },
5555
#[error("Unsupported argument value for property (field) {property}")]
56-
UnsupportedArgumentValue {
57-
property: String
58-
},
56+
UnsupportedArgumentValue { property: String },
5957
#[error("encountered an error when performing an HTTP request")]
6058
RequestError { error: E, backtrace: BT },
6159
#[error("an unspecified error")]

Diff for: tests/unit_queue_tests.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,25 @@ fn test_unit_queue_type_from_str() {
3333
assert_eq!(QueueType::Delayed, QueueType::from("delayed"));
3434
assert_eq!(QueueType::Delayed, QueueType::from("Delayed"));
3535

36-
assert_eq!(QueueType::Unsupported, QueueType::from("%%a-non-existent-type"));
36+
let unsupported_val1 = "%%a-non-existent-type".to_owned();
37+
assert_eq!(
38+
QueueType::Unsupported(unsupported_val1.clone()),
39+
QueueType::from(unsupported_val1.clone())
40+
);
41+
assert_eq!(
42+
QueueType::Unsupported(unsupported_val1.clone()),
43+
QueueType::from(unsupported_val1.as_str())
44+
);
45+
46+
let unsupported_val2 = "123123123".to_owned();
47+
assert_eq!(
48+
QueueType::Unsupported(unsupported_val2.clone()),
49+
QueueType::from(unsupported_val2.clone())
50+
);
51+
assert_eq!(
52+
QueueType::Unsupported(unsupported_val2.clone()),
53+
QueueType::from(unsupported_val2.as_str())
54+
);
3755
}
3856

3957
#[test]

0 commit comments

Comments
 (0)