|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! Helpers for decoding driver-owned config carried as protobuf Structs. |
| 5 | +
|
| 6 | +use serde::{Deserialize, Deserializer, de::Error as _}; |
| 7 | + |
| 8 | +/// Convert a protobuf Struct into a JSON object for typed serde decoding. |
| 9 | +#[must_use] |
| 10 | +pub fn struct_to_json_object( |
| 11 | + config: &prost_types::Struct, |
| 12 | +) -> serde_json::Map<String, serde_json::Value> { |
| 13 | + config |
| 14 | + .fields |
| 15 | + .iter() |
| 16 | + .map(|(key, value)| (key.clone(), value_to_json(value))) |
| 17 | + .collect() |
| 18 | +} |
| 19 | + |
| 20 | +/// Convert a protobuf Struct into a JSON value for typed serde decoding. |
| 21 | +#[must_use] |
| 22 | +pub fn struct_to_json_value(config: &prost_types::Struct) -> serde_json::Value { |
| 23 | + serde_json::Value::Object(struct_to_json_object(config)) |
| 24 | +} |
| 25 | + |
| 26 | +/// Convert a protobuf Value into a JSON value for typed serde decoding. |
| 27 | +#[must_use] |
| 28 | +pub fn value_to_json(value: &prost_types::Value) -> serde_json::Value { |
| 29 | + match value.kind.as_ref() { |
| 30 | + Some(prost_types::value::Kind::NumberValue(num)) => serde_json::Number::from_f64(*num) |
| 31 | + .map_or(serde_json::Value::Null, serde_json::Value::Number), |
| 32 | + Some(prost_types::value::Kind::StringValue(val)) => serde_json::Value::String(val.clone()), |
| 33 | + Some(prost_types::value::Kind::BoolValue(val)) => serde_json::Value::Bool(*val), |
| 34 | + Some(prost_types::value::Kind::StructValue(val)) => { |
| 35 | + let mut map = serde_json::Map::new(); |
| 36 | + for (key, value) in &val.fields { |
| 37 | + map.insert(key.clone(), value_to_json(value)); |
| 38 | + } |
| 39 | + serde_json::Value::Object(map) |
| 40 | + } |
| 41 | + Some(prost_types::value::Kind::ListValue(list)) => { |
| 42 | + let values = list.values.iter().map(value_to_json).collect(); |
| 43 | + serde_json::Value::Array(values) |
| 44 | + } |
| 45 | + Some(prost_types::value::Kind::NullValue(_)) | None => serde_json::Value::Null, |
| 46 | + } |
| 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