|
| 1 | +--- |
| 2 | +title: Rust Assertions Example |
| 3 | +description: An example demonstrating how to write custom assertions in Rust for Juno serverless functions. |
| 4 | +keywords: [rust, assertion, serverless, juno, satellite, example] |
| 5 | +sidebar_label: Assertions |
| 6 | +--- |
| 7 | + |
| 8 | +# Rust Assertions Example |
| 9 | + |
| 10 | +This example demonstrates how to write a **custom assertion** in **Rust** for a Juno **serverless function**. It shows how to intercept and validate data operations—such as rejecting specific content—before it's written to the datastore. |
| 11 | + |
| 12 | +The project includes a minimal frontend to help trigger and test the logic, but the primary focus is the backend assertion. |
| 13 | + |
| 14 | +You can browse the source code here: [github.com/junobuild/examples/tree/main/rust/assertions](https://github.com/junobuild/examples/tree/main/rust/assertions) |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## Folder Structure |
| 19 | + |
| 20 | +``` |
| 21 | +rust/assertions/ |
| 22 | +├── src/ |
| 23 | +│ ├── satellite/ # Rust Satellite serverless function |
| 24 | +│ │ ├── src/ |
| 25 | +│ │ │ └── lib.rs # Main Rust logic for Satellite |
| 26 | +│ │ ├── satellite.did # Candid interface definition |
| 27 | +│ │ └── Cargo.toml # Rust package config |
| 28 | +├── src/components/ # Minimal frontend React components |
| 29 | +├── juno.config.ts # Juno Satellite configuration |
| 30 | +├── package.json # Frontend dependencies |
| 31 | +└── ... |
| 32 | +``` |
| 33 | + |
| 34 | +--- |
| 35 | + |
| 36 | +## Key Features |
| 37 | + |
| 38 | +- **Custom Assertions in Rust**: Demonstrates how to reject or validate data before it's saved, using Rust serverless functions. |
| 39 | +- **Serverless Integration**: Runs as a Satellite function and integrates with Juno's datastore and authentication system. |
| 40 | +- **Minimal UI for Testing**: A simple frontend is included to test and demonstrate the assertion logic in action. |
| 41 | + |
| 42 | +--- |
| 43 | + |
| 44 | +## Main Backend Components |
| 45 | + |
| 46 | +- **src/satellite/src/lib.rs**: The core Rust logic for the Satellite serverless function. Implements the custom assertions (e.g., only allow certain valid inputs, etc.). |
| 47 | +- **src/satellite/Cargo.toml**: Rust package configuration for the Satellite function. |
| 48 | + |
| 49 | +--- |
| 50 | + |
| 51 | +## Example: Custom Assertion in Rust |
| 52 | + |
| 53 | +Here’s the actual Rust logic from `lib.rs`: |
| 54 | + |
| 55 | +```rust |
| 56 | +// This example defines a custom assertion in a Juno Satellite using Rust. |
| 57 | +// It checks if a document being saved to the "notes" collection contains the word "hello". |
| 58 | +// If it does, the assertion rejects the operation and logs a message. |
| 59 | + |
| 60 | +use ic_cdk::print; |
| 61 | +use junobuild_macros::assert_set_doc; |
| 62 | +use junobuild_satellite::{include_satellite, AssertSetDocContext}; |
| 63 | +use junobuild_utils::decode_doc_data; |
| 64 | +use serde::{Deserialize, Serialize}; |
| 65 | + |
| 66 | +#[derive(Serialize, Deserialize)] |
| 67 | +struct Note { |
| 68 | + text: String, |
| 69 | + url: Option<String>, |
| 70 | +} |
| 71 | + |
| 72 | +#[assert_set_doc(collections = ["notes"])] |
| 73 | +fn assert_set_doc(context: AssertSetDocContext) -> Result<(), String> { |
| 74 | + let note = decode_doc_data::<Note>(&context.data.data.proposed.data)?; |
| 75 | + |
| 76 | + if note.text.to_lowercase().contains("hello") { |
| 77 | + print(format!("❌ Rejected note containing 'hello': {}", note.text)); |
| 78 | + return Err("The note should not contain the keyword 'hello'.".to_string()); |
| 79 | + } |
| 80 | + |
| 81 | + print(format!("✅ Note accepted: {}", note.text)); |
| 82 | + |
| 83 | + Ok(()) |
| 84 | +} |
| 85 | + |
| 86 | +include_satellite!(); |
| 87 | +``` |
| 88 | + |
| 89 | +**Explanation:** |
| 90 | + |
| 91 | +- Defines a `Note` struct with `text` and optional `url` fields. Similar as the fields used in the frontend. |
| 92 | +- Uses the `#[assert_set_doc]` macro to create a custom assertion for the `notes` collection. |
| 93 | +- When a note is created or updated, the assertion checks if the note's text contains the word "hello" (case-insensitive). |
| 94 | +- If it does, the note is rejected and an error message is returned; otherwise, the note is accepted. |
| 95 | +- Prints a message to the log for both accepted and rejected notes. |
| 96 | +- `include_satellite!();` brings in the necessary boilerplate for the Juno Satellite runtime. |
| 97 | + |
| 98 | +--- |
| 99 | + |
| 100 | +## How to Run |
| 101 | + |
| 102 | +1. **Clone the repo**: |
| 103 | + |
| 104 | +```bash |
| 105 | +git clone https://github.com/junobuild/examples |
| 106 | +cd rust/assertions |
| 107 | +``` |
| 108 | + |
| 109 | +2. **Install dependencies**: |
| 110 | + |
| 111 | +```bash |
| 112 | +npm install |
| 113 | +``` |
| 114 | + |
| 115 | +3. **Start Juno local emulator**: |
| 116 | + |
| 117 | +:::important |
| 118 | + |
| 119 | +Requires the Juno CLI to be available `npm i -g @junobuild/cli` |
| 120 | + |
| 121 | +::: |
| 122 | + |
| 123 | +```bash |
| 124 | +juno dev start |
| 125 | +``` |
| 126 | + |
| 127 | +4. **Create a Satellite** for local dev: |
| 128 | + |
| 129 | +- Visit [http://localhost:5866](http://localhost:5866) and follow the instructions. |
| 130 | +- Update `juno.config.ts` with your Satellite ID. |
| 131 | + |
| 132 | +5. **Create required collections**: |
| 133 | + |
| 134 | +- `notes` in Datastore: [http://localhost:5866/datastore](http://localhost:5866/datastore) |
| 135 | +- `images` in Storage: [http://localhost:5866/storage](http://localhost:5866/storage) |
| 136 | + |
| 137 | +6. **Start the frontend dev server** (in a separate terminal): |
| 138 | + |
| 139 | +```bash |
| 140 | +npm run dev |
| 141 | +``` |
| 142 | + |
| 143 | +6. **Build the serverless functions** (in a separate terminal): |
| 144 | + |
| 145 | +```bash |
| 146 | +juno functions build |
| 147 | +``` |
| 148 | + |
| 149 | +The emulator will automatically upgrade your Satellite and live reload the changes. |
| 150 | + |
| 151 | +--- |
| 152 | + |
| 153 | +## Juno-Specific Configuration |
| 154 | + |
| 155 | +- **juno.config.ts**: Defines Satellite IDs for development/production, build source, and predeploy steps. See the [Configuration reference](../../../reference/configuration.mdx) for details. |
| 156 | +- **vite.config.ts**: Registers the `juno` plugin to inject environment variables automatically. See the [Vite Plugin reference](../../../reference/plugins.mdx#vite-plugin) for more information. |
| 157 | + |
| 158 | +--- |
| 159 | + |
| 160 | +## Production Deployment |
| 161 | + |
| 162 | +- Create a Satellite on the [Juno Console](https://console.juno.build) for mainnet. |
| 163 | +- Update `juno.config.ts` with the production Satellite ID. |
| 164 | +- Build and deploy the frontend: |
| 165 | + |
| 166 | +```bash |
| 167 | +npm run build |
| 168 | +juno deploy |
| 169 | +``` |
| 170 | + |
| 171 | +- Build and upgrade the serverless functions: |
| 172 | + |
| 173 | +```bash |
| 174 | +juno functions build |
| 175 | +juno functions upgrade |
| 176 | +``` |
| 177 | + |
| 178 | +--- |
| 179 | + |
| 180 | +## Notes |
| 181 | + |
| 182 | +- This example focuses on the Rust serverless function; the frontend is intentionally minimal and only included for demonstration purposes. |
| 183 | +- Use this project as a starting point for writing custom assertions and backend logic in Rust with Juno. |
| 184 | + |
| 185 | +--- |
| 186 | + |
| 187 | +## Real-World Example |
| 188 | + |
| 189 | +Want to see how assertions and serverless logic are used in a live project? |
| 190 | + |
| 191 | +Check out [proposals.network](https://proposals.network), an open-source app built with Juno: |
| 192 | + |
| 193 | +- GitHub: [github.com/peterpeterparker/proposals.network](https://github.com/peterpeterparker/proposals.network) |
| 194 | +- Example logic: [src/satellite/src/lib.rs](https://github.com/peterpeterparker/proposals.network/blob/main/src/satellite/src/lib.rs) |
| 195 | + |
| 196 | +This app uses: |
| 197 | + |
| 198 | +- `#[on_delete_doc]` and `#[assert_delete_doc]` to validate and clean up related documents and assets |
| 199 | +- Shared helper modules like `assert`, `delete`, and `types` to keep logic organized |
| 200 | +- A real-world pattern of chaining asset/document deletions with assertions |
| 201 | + |
| 202 | +It’s a great reference for more advanced setups and multi-collection coordination. |
| 203 | + |
| 204 | +--- |
| 205 | + |
| 206 | +## References |
| 207 | + |
| 208 | +- [Serverless Functions Guide](../../../guides/rust.mdx) |
| 209 | +- [Rust Functions Development](../../../build/functions/index.md) |
| 210 | +- [Rust SDK Reference](../../../reference/functions/rust/sdk.mdx) |
| 211 | +- [Rust Utils Reference](../../../reference/functions/rust/utils.mdx) |
| 212 | +- [Run Local Development](../../../guides/local-development.mdx) |
| 213 | +- [CLI Reference](../../../reference/cli) |
| 214 | +- [Configuration Reference](../../../reference/configuration.mdx) |
| 215 | +- [Datastore Collections](../../../build/datastore/collections.md) |
0 commit comments