1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//! Bridge to serde: validate a field while serde deserializes the struct, so
//! one decoder does both.
//!
//! - [`parse`] — `#[serde(deserialize_with = "vld::de::parse")]` on a field
//! whose type implements [`VldParse`] (`schema!` / `#[derive(Validate)]`).
//! - [`deserialize_fn!`](crate::deserialize_fn) — a `deserialize_with`
//! function from any schema expression.
//! - [`with`] — the building block of both: parse with a schema you hold.
//!
//! ```
//! use serde::Deserialize;
//!
//! vld::deserialize_fn!(fn port -> u16 = vld::u16().min(1));
//! vld::deserialize_fn!(fn volume -> i64 = vld::number().int().min(0).max(100).clamp());
//!
//! #[derive(Deserialize)]
//! struct Settings {
//! #[serde(deserialize_with = "port")]
//! port: u16,
//! #[serde(deserialize_with = "volume")]
//! volume: i64,
//! }
//!
//! let s: Settings = serde_json::from_str(r#"{"port": 8080, "volume": 250}"#).unwrap();
//! assert_eq!((s.port, s.volume), (8080, 100));
//! let err = serde_json::from_str::<Settings>(r#"{"port": 0, "volume": 1}"#).err().unwrap();
//! assert!(err.to_string().contains("at least 1"), "{err}");
//! ```
//!
//! A `deserialize_with` field is required by serde; add `#[serde(default)]`
//! to allow a missing key. The vld error (all issues, as text) becomes the
//! serde error; for structured issues parse with vld itself.
use ;
use crate;
/// Parse the value with `schema`; a failure becomes `D::Error::custom`.
/// `deserialize_with` for a field whose type implements [`VldParse`].