Skip to content

Commit 46d8ce0

Browse files
feat: utils
Signed-off-by: David Dal Busco <david.dalbusco@outlook.com>
1 parent b0a8ead commit 46d8ce0

2 files changed

Lines changed: 84 additions & 1 deletion

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Utils
2+
3+
The following utilities are provided to help you work with document and asset data inside your Satellite. They simplify tasks such as decoding and encoding data, serializing custom types, and interacting with Juno’s core features in a consistent way.
4+
5+
:::note[📦 Crate]
6+
7+
All utilities on this page are provided by the [junobuild-utils](https://docs.rs/junobuild-utils/latest/junobuild_utils/index.html) crate.
8+
9+
To use them, add this to your Cargo.toml:
10+
11+
```toml
12+
[dependencies]
13+
junobuild-utils = "*"
14+
```
15+
16+
Replace `*` with the specific version you want to use, or omit the version to always use the latest version.
17+
18+
:::
19+
20+
---
21+
22+
## decode_doc_data
23+
24+
Deserializes raw document data (`&[u8]`) into a typed Rust struct. Use this inside hooks or assertions to get the document contents in a strongly typed way.
25+
26+
```rust
27+
pub fn decode_doc_data<T: for<'a> Deserialize<'a>>(
28+
data: &[u8],
29+
) -> Result<T, String>
30+
```
31+
32+
📦 See full definition on [docs.rs](https://docs.rs/junobuild-utils/latest/junobuild_utils/fn.decode_doc_data.html)
33+
34+
---
35+
36+
## encode_doc_data
37+
38+
Serializes a Rust struct into a `Vec<u8>` for storing a document into the datastore. Use this when modifying or creating document data inside a hook or assertion.
39+
40+
```rust
41+
pub fn encode_doc_data<T: Serialize>(data: &T) -> Result<Vec<u8>, String>
42+
```
43+
44+
📦 See full definition on [docs.rs](https://docs.rs/junobuild-utils/latest/junobuild_utils/fn.encode_doc_data.html)
45+
46+
---
47+
48+
## encode_doc_data_to_string
49+
50+
Serializes a Rust struct into a JSON String. Use this if you want to store or inspect document data in a readable format. Commonly used when exposing JSON data on the web, for example by reproducing documents from the datastore into the storage.
51+
52+
```rust
53+
pub fn encode_doc_data_to_string<T: Serialize>(
54+
data: &T,
55+
) -> Result<String, String>
56+
```
57+
58+
📦 See full definition on [docs.rs](https://docs.rs/junobuild-utils/latest/junobuild_utils/fn.encode_doc_data_to_string.html)

sidebars.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,32 @@ const sidebars: SidebarsConfig = {
196196
"reference/cli",
197197
"reference/configuration",
198198
"reference/plugins",
199-
"reference/settings"
199+
"reference/settings",
200+
{
201+
type: "category",
202+
label: "Functions",
203+
link: {
204+
type: "generated-index",
205+
slug: "/reference/functions",
206+
description:
207+
"API reference for writing serverless functions in Rust or TypeScript."
208+
},
209+
items: [
210+
{
211+
type: "category",
212+
label: "Rust",
213+
link: {
214+
type: "generated-index",
215+
slug: "/reference/functions/rust",
216+
description:
217+
"API reference for writing serverless functions in Rust."
218+
},
219+
items: [
220+
"reference/functions/rust/utils"
221+
]
222+
},
223+
]
224+
},
200225
]
201226
};
202227

0 commit comments

Comments
 (0)