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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//! Serde serialization helpers.
use crateRedacted;
use crateRedactionStrategy;
/// A helper for serializing the [`Redacted`] representation of a value with [serde].
///
/// [`Redacted`] types themselves are not serializable. To serialize a struct containing
/// [`Redacted`] field types, the fields must be annotated with `#[serde(serialize_with)]`.
///
/// Use this one to use the _redacted_ representation for serialization.
///
/// ## Warning
///
/// By serializing the redacted representation, serialization and deserialization do **not**
/// round-trip. This can be unexpected depending on your use case. This is a soft recommendation to
/// only use this feature if you're not expecting to deserialize a struct that you previously
/// serialized.
///
/// ## Example
///
/// ```rust
/// use apollo_redaction::Redacted;
///
/// #[derive(serde::Serialize)]
/// struct DisplayRedacted {
/// #[serde(serialize_with = "apollo_redaction::ser::display_redacted")]
/// value: Redacted<String>,
/// }
///
/// let s = DisplayRedacted {
/// value: "top secret".to_string().into(),
/// };
/// assert_eq!(
/// serde_json::to_string(&s).unwrap(),
/// r#"{"value":"[REDACTED]"}"#,
/// );
/// ```
/// A helper for serializing the underlying value of a [`Redacted`] value with [serde].
///
/// [`Redacted`] types themselves are not serializable. To serialize a struct containing
/// [`Redacted`] field types, the fields must be annotated with `#[serde(serialize_with)]`.
///
/// Use this one to use the _underlying_ representation for serialization.
///
/// ## Example
///
/// ```rust
/// use apollo_redaction::Redacted;
///
/// #[derive(serde::Serialize)]
/// struct DisplayRedacted {
/// #[serde(serialize_with = "apollo_redaction::ser::unredacted")]
/// value: Redacted<String>,
/// }
///
/// let s = DisplayRedacted {
/// value: "top secret".to_string().into(),
/// };
/// assert_eq!(
/// serde_json::to_string(&s).unwrap(),
/// r#"{"value":"top secret"}"#,
/// );
/// ```