vld/combinators/
catch_val.rs1use serde_json::Value;
2
3use crate::error::VldError;
4use crate::schema::VldSchema;
5
6pub struct ZCatch<T: VldSchema>
13where
14 T::Output: Clone,
15{
16 inner: T,
17 fallback: T::Output,
18}
19
20impl<T: VldSchema> ZCatch<T>
21where
22 T::Output: Clone,
23{
24 pub fn new(inner: T, fallback: T::Output) -> Self {
25 Self { inner, fallback }
26 }
27
28 pub fn inner_schema(&self) -> &T {
30 &self.inner
31 }
32}
33
34impl<T: VldSchema> VldSchema for ZCatch<T>
35where
36 T::Output: Clone,
37{
38 type Output = T::Output;
39
40 fn parse_value(&self, value: &Value) -> Result<T::Output, VldError> {
41 match self.inner.parse_value(value) {
42 Ok(v) => Ok(v),
43 Err(_) => Ok(self.fallback.clone()),
44 }
45 }
46 fn output_json(&self, o: &Self::Output) -> Option<Value> {
47 self.inner.output_json(o)
48 }
49}