|
3 | 3 |
|
4 | 4 | //! Helpers for decoding `google.protobuf.Struct` values. |
5 | 5 |
|
| 6 | +use serde::{Deserialize, Deserializer, de::Error as _}; |
| 7 | + |
6 | 8 | /// Convert a protobuf Struct into a JSON object for typed serde decoding. |
7 | 9 | #[must_use] |
8 | 10 | pub fn struct_to_json_object( |
@@ -43,3 +45,76 @@ pub fn value_to_json(value: &prost_types::Value) -> serde_json::Value { |
43 | 45 | Some(prost_types::value::Kind::NullValue(_)) | None => serde_json::Value::Null, |
44 | 46 | } |
45 | 47 | } |
| 48 | + |
| 49 | +/// Deserialize a present field as a non-empty list of non-empty strings. |
| 50 | +/// |
| 51 | +/// Use with `#[serde(default, deserialize_with = "...")]` on |
| 52 | +/// `Option<Vec<String>>` fields. Missing fields use the option default; present |
| 53 | +/// fields must be arrays and cannot be empty. |
| 54 | +pub fn deserialize_optional_non_empty_string_list<'de, D>( |
| 55 | + deserializer: D, |
| 56 | +) -> Result<Option<Vec<String>>, D::Error> |
| 57 | +where |
| 58 | + D: Deserializer<'de>, |
| 59 | +{ |
| 60 | + let values = Vec::<String>::deserialize(deserializer)?; |
| 61 | + if values.is_empty() { |
| 62 | + return Err(D::Error::custom("must be a non-empty list of strings")); |
| 63 | + } |
| 64 | + |
| 65 | + for (idx, value) in values.iter().enumerate() { |
| 66 | + if value.trim().is_empty() { |
| 67 | + return Err(D::Error::custom(format!( |
| 68 | + "[{idx}] must be a non-empty string" |
| 69 | + ))); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + Ok(Some(values)) |
| 74 | +} |
| 75 | + |
| 76 | +#[cfg(test)] |
| 77 | +mod tests { |
| 78 | + use super::*; |
| 79 | + |
| 80 | + #[derive(Debug, Default, Deserialize)] |
| 81 | + #[serde(default)] |
| 82 | + struct TestConfig { |
| 83 | + #[serde( |
| 84 | + default, |
| 85 | + deserialize_with = "deserialize_optional_non_empty_string_list" |
| 86 | + )] |
| 87 | + devices: Option<Vec<String>>, |
| 88 | + } |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn optional_non_empty_string_list_defaults_when_absent() { |
| 92 | + let config: TestConfig = serde_json::from_value(serde_json::json!({})).unwrap(); |
| 93 | + |
| 94 | + assert_eq!(config.devices, None); |
| 95 | + } |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn optional_non_empty_string_list_parses_present_list() { |
| 99 | + let config: TestConfig = |
| 100 | + serde_json::from_value(serde_json::json!({"devices": ["nvidia.com/gpu=0"]})).unwrap(); |
| 101 | + |
| 102 | + assert_eq!(config.devices, Some(vec!["nvidia.com/gpu=0".to_string()])); |
| 103 | + } |
| 104 | + |
| 105 | + #[test] |
| 106 | + fn optional_non_empty_string_list_rejects_empty_list() { |
| 107 | + let err = |
| 108 | + serde_json::from_value::<TestConfig>(serde_json::json!({"devices": []})).unwrap_err(); |
| 109 | + |
| 110 | + assert!(err.to_string().contains("non-empty list")); |
| 111 | + } |
| 112 | + |
| 113 | + #[test] |
| 114 | + fn optional_non_empty_string_list_rejects_empty_string() { |
| 115 | + let err = |
| 116 | + serde_json::from_value::<TestConfig>(serde_json::json!({"devices": [""]})).unwrap_err(); |
| 117 | + |
| 118 | + assert!(err.to_string().contains("non-empty string")); |
| 119 | + } |
| 120 | +} |
0 commit comments