clickhouse_cloud_api/serde_helpers.rs
1//! Serde helpers used by generated models.
2
3/// Deserialize a buffered payload into `T`, handing the payload back unchanged
4/// if it does not fit `T`.
5///
6/// Used by the `discriminated_union!` macro in `models.rs` so a union whose
7/// discriminator selects a variant the rest of the payload no longer fits
8/// degrades to that union's `Unknown(serde_json::Value)` catch-all instead of
9/// failing the whole response. Field-level tolerance covers a field the API
10/// stops sending; this covers a field whose *shape* the API changes.
11pub fn deserialize_or_raw<T>(value: serde_json::Value) -> Result<T, serde_json::Value>
12where
13 T: serde::de::DeserializeOwned,
14{
15 match serde_json::from_value(value.clone()) {
16 Ok(parsed) => Ok(parsed),
17 Err(_) => Err(value),
18 }
19}