Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

docs: keep rust/README stable for stacked PRs

Prateek Sunal7db58000dbd5
diff --git a/rust/README.md b/rust/README.md
index d669d026f9..df6e28ab72 100644
--- a/rust/README.md
+++ b/rust/README.md
@@ -1,40 +1,213 @@
# Rust in Ente
-This directory hosts Rust crates used across Ente clients.
+```
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
+ β”‚ rust/core/ β”‚
+ β”‚ (ente-core crate) β”‚
+ β”‚ β”‚
+ β”‚ Pure Rust - NO FFI annotations β”‚
+ β”‚ Stateless business logic β”‚
+ β”‚ Fully testable with cargo test β”‚
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
+ β”‚
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
+ β”‚ β”‚ β”‚
+ β–Ό β–Ό β–Ό
+β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
+β”‚ mobile/packages/rust/ β”‚ β”‚ web/packages/wasm/ β”‚ β”‚ rust/cli/ β”‚
+β”‚ (ente_rust crate) β”‚ β”‚ (ente-wasm crate) β”‚ β”‚ β”‚
+β”‚ β”‚ β”‚ β”‚ β”‚ CLI binary, depends β”‚
+β”‚ Shared #[frb] wrappers β”‚ β”‚ #[wasm_bindgen] wrappers β”‚ β”‚ on ente-core β”‚
+β”‚ for all mobile apps β”‚ β”‚ for all web apps β”‚ β”‚ β”‚
+β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
+ β”‚ β”‚ β”‚
+ β”‚ └──────────────┐ β”‚
+ β–Ό β–Ό β–Ό
+β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
+β”‚ Photos App β”‚ β”‚ Other apps β”‚ β”‚ Web Apps β”‚
+β”‚ β”‚ β”‚ (Auth ...) β”‚ β”‚ (Photos, Auth, etc.) β”‚
+β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
+ β–²
+ β”‚
+β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
+β”‚ mobile/apps/photos/rust/ β”‚
+β”‚ (ente_photos_rust crate) β”‚
+β”‚ β”‚
+β”‚ App-specific #[frb]: β”‚
+β”‚ usearch, ML, etc. β”‚
+β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
+```
+
+## Directory Structure
+
+```
+rust/
+β”œβ”€β”€ cli/ # CLI package
+β”‚ β”œβ”€β”€ src/
+β”‚ β”‚ └── main.rs
+β”‚ β”œβ”€β”€ Cargo.toml
+β”‚ └── Cargo.lock
+β”‚
+└── core/ # Pure Rust business logic
+ β”œβ”€β”€ src/
+ β”‚ β”œβ”€β”€ lib.rs
+ β”‚ └── urls.rs
+ └── Cargo.toml # crate name: ente-core
+
+web/packages/wasm/ # WASM bindings (lives in web workspace)
+β”œβ”€β”€ src/
+β”‚ └── lib.rs # #[wasm_bindgen] wrappers around ente-core
+β”œβ”€β”€ Cargo.toml # crate name: ente-wasm
+β”œβ”€β”€ package.json # includes wasm-pack as devDependency
+└── pkg/ # generated output (gitignored)
+
+mobile/packages/rust/ # Shared FRB bindings for all mobile apps
+β”œβ”€β”€ rust/
+β”‚ β”œβ”€β”€ src/
+β”‚ β”‚ └── api/ # #[frb] wrappers around ente-core
+β”‚ └── Cargo.toml # crate name: ente_rust
+β”œβ”€β”€ lib/ # Generated Dart bindings
+└── pubspec.yaml # Flutter plugin package
+
+mobile/apps/photos/rust/ # Photos app-specific FRB bindings
+β”œβ”€β”€ src/
+β”‚ β”œβ”€β”€ lib.rs
+β”‚ └── api/ # #[frb] app-specific code (usearch, ML)
+β”‚ └── *.rs
+└── Cargo.toml # crate name: ente_photos_rust
+```
+
+**Crates:**
+
+- `ente-core` - shared business logic (pure Rust, no FFI)
+- `ente-wasm` - wasm-bindgen wrappers for web
+- `ente_rust` - shared FRB wrappers for mobile (Dart class: `EnteRust`)
+- `ente_photos_rust` - Photos app-specific FRB (Dart class: `EntePhotosRust`)
+
+## Tooling
-## Crates
+### [wasm-bindgen](https://github.com/wasm-bindgen/wasm-bindgen)
-### `rust/core/` (ente-core)
+A Rust library that provides the `#[wasm_bindgen]` attribute macro. When you annotate a function with `#[wasm_bindgen]`, it:
-Pure Rust shared logic (primarily cryptography) that is wire-compatible with the existing
-JS/Dart implementations.
+1. Marks the function for export to JavaScript
+2. Handles type conversions between Rust and JS (e.g., `String` ↔ JS string, `i64` ↔ `BigInt`)
+3. Generates metadata that the wasm-bindgen CLI uses to create JS/TS glue code
-- Source: `rust/core/src/`
-- Docs:
- - `rust/core/docs/crypto.md`
+The library itself is lightweight - just macros and runtime types.
+
+### [wasm-pack](https://github.com/drager/wasm-pack)
+
+A CLI tool that orchestrates the WASM build process:
+
+1. Runs `cargo build --target wasm32-unknown-unknown`
+2. Invokes `wasm-bindgen` CLI to generate JS/TS bindings from the compiled WASM
+3. Outputs everything to a `pkg/` directory ready for npm/bundlers
+
+wasm-pack is installed via npm as a devDependency, so `yarn install` handles it.
+
+### [Flutter Rust Bridge (FRB)](https://github.com/fzyzcjy/flutter_rust_bridge)
+
+Used for Flutter integration. FRB is used on two crates:
+
+- **`ente_rust`** (`mobile/packages/rust/`) - Shared wrappers around `ente-core`, used by multiple mobile apps (Photos, Auth, etc.)
+- **`ente_photos_rust`** (`mobile/apps/photos/rust/`) - Photos app-specific functionality (usearch, ML)
+
+Both depend on `ente-core` and use `#[frb]` annotations to generate Dart bindings.
## Development
+### Commands
+
**ente-core (rust/core/):**
```sh
-cargo fmt
-cargo clippy
-cargo build
-cargo test
+cargo fmt # format
+cargo clippy # lint
+cargo build # build
+cargo test # test
+```
+
+**ente-wasm (web/packages/wasm/):**
+
+```sh
+yarn install # installs wasm-pack
+yarn build # runs wasm-pack build --target bundler
+```
+
+Or from web/ root:
+
+```sh
+yarn build:wasm # builds the WASM package
+```
+
+> [!TIP]
+>
+> For active Rust development, use watch mode in a separate terminal:
+>
+> ```sh
+> cargo install cargo-watch
+> cd web/
+> cargo watch -w ../rust/core -w packages/wasm/src -s "yarn build:wasm"
+> ```
+
+**ente_rust (mobile/packages/rust/):**
+
+```sh
+cargo install flutter_rust_bridge_codegen
+flutter_rust_bridge_codegen generate
+```
+
+`flutter_rust_bridge_codegen generate` needs to be run either manually (or in watch mode, `flutter_rust_bridge_codegen generate --watch`) whenever the Rust source changes to get the bindings to update.
+
+**ente_photos_rust (mobile/apps/photos/rust/):**
+
+```sh
+flutter_rust_bridge_codegen generate
+flutter test
```
-## Integrations
+## Future
+
+### CLI Migration
+
+A WIP CLI lives in `rust/cli/` and depends on ente-core via path dependency.
+
+Next steps:
-- Web (wasm-bindgen): `web/packages/wasm/` wraps `ente-core` for web apps.
-- Mobile (FRB): `mobile/packages/rust/` wraps `ente-core` for Flutter apps.
+1. Move code from CLI to ente-core, adding tests as we migrate.
+2. Update CLI to use ente-core implementations
+3. Prune CLI dependencies as code moves to core
+
+### WASM Compatibility
+
+Core crate must handle deps that don't compile to WASM:
+
+| Dependency | Issue | Solution |
+| ---------------------- | ---------------- | --------------------------------------------------------------------------------------- |
+| `libsodium-sys-stable` | Native C library | Feature-gate; use pure Rust crypto (e.g., `chacha20poly1305` crate) or WebCrypto via JS |
+| `rusqlite` | Native SQLite | Feature-gate; WASM uses IndexedDB via JS interop |
+| `tokio` (full) | Threading | Use WASM-compatible features only |
+| Filesystem ops | No FS in browser | Abstract behind traits |
+
+**Approach:** Cargo feature flags in ente-core?
+
+```toml
+[features]
+default = ["native"]
+native = ["libsodium-sys-stable", "rusqlite/bundled"]
+wasm = ["getrandom/js"]
+```
-## CLI (added in a follow-up PR)
+### Other notes
-A Rust CLI lives in `rust/cli/` and will depend on `ente-core`.
+- Never panic across FFI boundary - always return Result or map errors
+- Keep binding functions thin - logic belongs in ente-core
+- Mobile binary size: LTO + symbol stripping
-## Validation + Fuzzing (added in a follow-up PR)
+## Tests
-Validation suite + benchmarks (vs libsodium) and fuzz targets will live under:
-- `rust/validation/`
-- `rust/core/fuzz/`
+- **ente-core:** Standard `cargo test` - comprehensive unit tests
+- **ente_photos_rust:** Minimal FRB smoke test in Dart to catch binding drift
+- **ente-wasm:** Vitest tests in web package
+- **Golden fixtures:** Share test vectors across native/FRB/WASM for crypto parity