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