|
4 | 4 | use std::{fmt::Display, str::FromStr}; |
5 | 5 |
|
6 | 6 | use secrecy::SecretString; |
| 7 | +use serde::{de::Error as deError, Deserialize, Deserializer}; |
7 | 8 | use serde_aux::field_attributes::deserialize_number_from_string; |
8 | 9 |
|
9 | 10 | #[derive(serde::Deserialize, Clone, Debug)] |
@@ -55,6 +56,67 @@ impl Display for LogLevel { |
55 | 56 | } |
56 | 57 | } |
57 | 58 |
|
| 59 | +/// when deserializing, strip out any trailing slashes from the end (useful for URLs) |
| 60 | +pub fn deserialize_trim_trailing_slash<'de, D>(deserializer: D) -> Result<String, D::Error> |
| 61 | +where |
| 62 | + D: Deserializer<'de>, |
| 63 | +{ |
| 64 | + let mut base: String = Deserialize::deserialize(deserializer)?; |
| 65 | + if base.ends_with("/") { |
| 66 | + base.pop(); |
| 67 | + } |
| 68 | + Ok(base) |
| 69 | +} |
| 70 | + |
| 71 | +/// custom deserializer which enforces and normalizes system names |
| 72 | +pub fn deserialize_enforce_topic_prefixes<'de, D>(deserializer: D) -> Result<String, D::Error> |
| 73 | +where |
| 74 | + D: Deserializer<'de>, |
| 75 | +{ |
| 76 | + let mut base: String = Deserialize::deserialize(deserializer)?; |
| 77 | + |
| 78 | + // TODO will need to update this logic later, when we reduce the namespacing as part of a new INTERSECT release |
| 79 | + let mut period_count = 0_u16; |
| 80 | + for character in base.chars() { |
| 81 | + if character == '-' || character.is_ascii_digit() || character.is_ascii_lowercase() { |
| 82 | + continue; |
| 83 | + } else if character == '.' { |
| 84 | + period_count += 1; |
| 85 | + } else { |
| 86 | + return Err(deError::custom(format!( |
| 87 | + "topic_prefix: Invalid character detected: {}", |
| 88 | + character |
| 89 | + ))); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + match period_count { |
| 94 | + 2 => { |
| 95 | + if base.ends_with(".") { |
| 96 | + Err(deError::custom( |
| 97 | + "topic_prefix: 3 levels of namespacing expected but only 2 provided", |
| 98 | + )) |
| 99 | + } else { |
| 100 | + base += "."; |
| 101 | + Ok(base) |
| 102 | + } |
| 103 | + } |
| 104 | + 3 => { |
| 105 | + if base.ends_with(".") { |
| 106 | + Ok(base) |
| 107 | + } else { |
| 108 | + Err(deError::custom( |
| 109 | + "topic_prefix: 3 levels of namespacing expected but 4 provided", |
| 110 | + )) |
| 111 | + } |
| 112 | + } |
| 113 | + c => Err(deError::custom(format!( |
| 114 | + "topic_prefix: Too many periods ({})", |
| 115 | + c |
| 116 | + ))), |
| 117 | + } |
| 118 | +} |
| 119 | + |
58 | 120 | /// Common logic for obtaining a configuration of type T |
59 | 121 | /// |
60 | 122 | /// Rules: |
|
0 commit comments