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
57
58
59
60
61
62
63
64
65
66
//! JSON conversion helpers used by declarative merging.
use Value;
use crate::;
/// Deserialise a JSON [`Value`] into `T`.
///
/// # Errors
///
/// Returns an [`crate::OrthoError`] when deserialisation fails.
///
/// # Examples
///
/// ```rust
/// use ortho_config::declarative::from_value;
/// use serde::Deserialize;
/// use serde_json::json;
///
/// #[derive(Debug, Deserialize, PartialEq)]
/// struct App { port: u16 }
///
/// let v = json!({"port": 8080});
/// let app: App = from_value(v).expect("value deserialises");
/// assert_eq!(app.port, 8080);
/// ```
/// Deserialise a JSON [`Value`] into `T`, routing errors to [`crate::OrthoError::Merge`].
///
/// Use this function when deserialising in a merge context where failures should
/// be attributed to the merge phase rather than the gathering phase. This
/// semantic distinction clarifies that failures at the merge phase (combining
/// and deserialising) are separate from failures during the gathering phase
/// (reading sources).
///
/// # Errors
///
/// Returns an [`crate::OrthoError::Merge`] when deserialisation fails.
///
/// # Examples
///
/// ```rust
/// use ortho_config::declarative::from_value_merge;
/// use ortho_config::OrthoError;
/// use serde::Deserialize;
/// use serde_json::json;
///
/// #[derive(Debug, Deserialize, PartialEq)]
/// struct App { port: u16 }
///
/// // Valid input deserialises successfully.
/// let v = json!({"port": 8080});
/// let app: App = from_value_merge(v).expect("value deserialises successfully");
/// assert_eq!(app.port, 8080);
///
/// // Invalid input produces Merge error.
/// let invalid = json!({"port": "not_a_number"});
/// let err = from_value_merge::<App>(invalid).unwrap_err();
/// assert!(matches!(&*err, OrthoError::Merge { .. }));
/// ```