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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use crate::;
use ;
/// Decodes serialized document data into a Rust struct, tailored for use with Juno hooks.
///
/// This function is a utility for developers working with Juno hooks, particularly
/// when receiving document data that needs to be processed. It uses Serde's deserialization to
/// convert a byte slice into a specified Rust data structure, allowing for direct manipulation
/// of document data within hook functions.
///
/// # Parameters
/// - `data`: A byte slice (`&[u8]`) containing the serialized document data.
///
/// # Returns
/// - `Ok(T)`: Successfully deserialized data of type `T`.
/// - `Err(String)`: An error string if deserialization fails.
///
/// # Examples
/// Within a Juno hook, you might receive document data that needs to be decoded:
/// ```rust
/// #[derive(Serialize, Deserialize)]
/// struct Person {
/// name: String,
/// age: u32,
/// }
///
/// #[on_set_doc(collections = ["people"])]
/// async fn handle_set_person_doc(context: OnSetDocContext) -> Result<(), String> {
/// let person: Person = decode_doc_data(&context.data.data.after.data)
/// .expect("Failed to decode document data");
///
/// // Proceed with custom logic for the decoded `Person` instance
/// println!("Received document for person: {}", person.name);
///
/// Ok(())
/// }
/// ```
/// Encodes a Rust struct into serialized document data, designed for use with Juno hooks.
///
/// When preparing document data to be stored, this function facilitates the serialization
/// of a Rust data structure into a byte vector. It leverages Serde's serialization capabilities,
/// ensuring that any Rust type implementing the `Serialize` trait can be efficiently converted
/// into a format compatible with Juno's document requirements.
///
/// # Parameters
/// - `data`: A reference to the Rust data structure to be serialized.
///
/// # Returns
/// - `Ok(Vec<u8>)`: A byte vector containing the serialized data.
/// - `Err(String)`: An error string if serialization fails.
///
/// # Examples
/// In a Juno hook, you might want to modify and then store updated document data:
/// ```rust
/// #[derive(Serialize, Deserialize)]
/// struct Person {
/// name: String,
/// age: u32,
/// }
///
/// #[on_set_doc(collections = ["people"])]
/// async fn handle_set_person_doc(context: OnSetDocContext) -> Result<(), String> {
/// let mut person: Person = decode_doc_data(&context.data.data.after.data)?;
/// person.age += 1; // Increment the person's age
///
/// let updated_data = encode_doc_data(&person)
/// .expect("Failed to serialize updated document data");
///
/// // Use `updated_data` to store the modified document
///
/// Ok(())
/// }
/// ```
/// Encodes a Rust struct into a JSON string, designed for use with Juno hooks.
///
/// This function facilitates the serialization
/// of a Rust data structure representing a document data into a JSON string.
/// It leverages Serde's serialization capabilities,
/// ensuring that any Rust type implementing the `Serialize` trait can be efficiently converted
/// into a format compatible with Juno's document requirements.
///
/// # Parameters
/// - `data`: A reference to the Rust data structure to be serialized.
///
/// # Returns
/// - `Ok(String)`: A JSON string containing the serialized data.
/// - `Err(String)`: An error string if serialization fails.
///
/// # Examples
/// In a Juno hook, you might want to modify and then store updated document data:
/// ```rust
/// #[derive(Serialize, Deserialize)]
/// struct Person {
/// name: String,
/// age: u32,
/// }
///
/// #[on_set_doc(collections = ["people"])]
/// async fn handle_set_person_doc(context: OnSetDocContext) -> Result<(), String> {
/// let mut person: Person = decode_doc_data(&context.data.data.after.data)?;
/// person.age += 1; // Increment the person's age
///
/// let updated_data = encode_doc_data_to_string(&person)
/// .expect("Failed to serialize updated document data");
///
/// // Use `updated_data` to store the modified document
///
/// Ok(())
/// }
/// ```