MCPcopy Create free account
hub / github.com/MaterializeInc/materialize / deserialize_optional

Function deserialize_optional

src/lowertest/src/lib.rs:103–121  ·  view source on GitHub ↗

If the `stream_iter` is not empty, deserialize the next `TokenTree` into a `D`. See [`to_json`] for the object spec syntax. `type_name` should be `D` in string form. `stream_iter` will advance by one `TokenTree` no matter the result.

(
    stream_iter: &mut I,
    type_name: &'static str,
    ctx: &mut C,
)

Source from the content-addressed store, hash-verified

101///
102/// `stream_iter` will advance by one `TokenTree` no matter the result.
103pub fn deserialize_optional<D, I, C>(
104 stream_iter: &mut I,
105 type_name: &'static str,
106 ctx: &mut C,
107) -> Result<Option<D>, String>
108where
109 C: TestDeserializeContext,
110 D: DeserializeOwned + MzReflect,
111 I: Iterator<Item = TokenTree>,
112{
113 let mut rti = ReflectedTypeInfo::default();
114 D::add_to_reflected_type_info(&mut rti);
115 match to_json(stream_iter, type_name, &rti, ctx)? {
116 Some(j) => Ok(Some(serde_json::from_str::<D>(&j).map_err(|e| {
117 format!("String while serializing: {}\nOriginal JSON: {}", e, j)
118 })?)),
119 None => Ok(None),
120 }
121}
122
123/// Simpler interface for [deserialize] when no syntax overrides or extensions are needed.
124pub fn deserialize_generic<D, I>(stream_iter: &mut I, type_name: &'static str) -> Result<D, String>

Callers 3

deserializeFunction · 0.85
create_test_enumFunction · 0.85

Calls 1

to_jsonFunction · 0.85

Tested by 1

create_test_enumFunction · 0.68