junobuild_utils/doc.rs
1use crate::{decode_json_data, encode_json_data, encode_json_data_to_string};
2use serde::{Deserialize, Serialize};
3
4/// Decodes serialized document data into a Rust struct, tailored for use with Juno hooks.
5///
6/// This function is a utility for developers working with Juno hooks, particularly
7/// when receiving document data that needs to be processed. It uses Serde's deserialization to
8/// convert a byte slice into a specified Rust data structure, allowing for direct manipulation
9/// of document data within hook functions.
10///
11/// # Parameters
12/// - `data`: A byte slice (`&[u8]`) containing the serialized document data.
13///
14/// # Returns
15/// - `Ok(T)`: Successfully deserialized data of type `T`.
16/// - `Err(String)`: An error string if deserialization fails.
17///
18/// # Examples
19/// Within a Juno hook, you might receive document data that needs to be decoded:
20/// ```rust
21/// #[derive(Serialize, Deserialize)]
22/// struct Person {
23/// name: String,
24/// age: u32,
25/// }
26///
27/// #[on_set_doc(collections = ["people"])]
28/// async fn handle_set_person_doc(context: OnSetDocContext) -> Result<(), String> {
29/// let person: Person = decode_doc_data(&context.data.data.after.data)
30/// .expect("Failed to decode document data");
31///
32/// // Proceed with custom logic for the decoded `Person` instance
33/// println!("Received document for person: {}", person.name);
34///
35/// Ok(())
36/// }
37/// ```
38pub fn decode_doc_data<T: for<'a> Deserialize<'a>>(data: &[u8]) -> Result<T, String> {
39 decode_json_data::<T>(data)
40}
41
42/// Encodes a Rust struct into serialized document data, designed for use with Juno hooks.
43///
44/// When preparing document data to be stored, this function facilitates the serialization
45/// of a Rust data structure into a byte vector. It leverages Serde's serialization capabilities,
46/// ensuring that any Rust type implementing the `Serialize` trait can be efficiently converted
47/// into a format compatible with Juno's document requirements.
48///
49/// # Parameters
50/// - `data`: A reference to the Rust data structure to be serialized.
51///
52/// # Returns
53/// - `Ok(Vec<u8>)`: A byte vector containing the serialized data.
54/// - `Err(String)`: An error string if serialization fails.
55///
56/// # Examples
57/// In a Juno hook, you might want to modify and then store updated document data:
58/// ```rust
59/// #[derive(Serialize, Deserialize)]
60/// struct Person {
61/// name: String,
62/// age: u32,
63/// }
64///
65/// #[on_set_doc(collections = ["people"])]
66/// async fn handle_set_person_doc(context: OnSetDocContext) -> Result<(), String> {
67/// let mut person: Person = decode_doc_data(&context.data.data.after.data)?;
68/// person.age += 1; // Increment the person's age
69///
70/// let updated_data = encode_doc_data(&person)
71/// .expect("Failed to serialize updated document data");
72///
73/// // Use `updated_data` to store the modified document
74///
75/// Ok(())
76/// }
77/// ```
78pub fn encode_doc_data<T: Serialize>(data: &T) -> Result<Vec<u8>, String> {
79 encode_json_data::<T>(data)
80}
81
82/// Encodes a Rust struct into a JSON string, designed for use with Juno hooks.
83///
84/// This function facilitates the serialization
85/// of a Rust data structure representing a document data into a JSON string.
86/// It leverages Serde's serialization capabilities,
87/// ensuring that any Rust type implementing the `Serialize` trait can be efficiently converted
88/// into a format compatible with Juno's document requirements.
89///
90/// # Parameters
91/// - `data`: A reference to the Rust data structure to be serialized.
92///
93/// # Returns
94/// - `Ok(String)`: A JSON string containing the serialized data.
95/// - `Err(String)`: An error string if serialization fails.
96///
97/// # Examples
98/// In a Juno hook, you might want to modify and then store updated document data:
99/// ```rust
100/// #[derive(Serialize, Deserialize)]
101/// struct Person {
102/// name: String,
103/// age: u32,
104/// }
105///
106/// #[on_set_doc(collections = ["people"])]
107/// async fn handle_set_person_doc(context: OnSetDocContext) -> Result<(), String> {
108/// let mut person: Person = decode_doc_data(&context.data.data.after.data)?;
109/// person.age += 1; // Increment the person's age
110///
111/// let updated_data = encode_doc_data_to_string(&person)
112/// .expect("Failed to serialize updated document data");
113///
114/// // Use `updated_data` to store the modified document
115///
116/// Ok(())
117/// }
118/// ```
119pub fn encode_doc_data_to_string<T: Serialize>(data: &T) -> Result<String, String> {
120 encode_json_data_to_string::<T>(data)
121}