π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
doc
Manav Rathib6e6944d1964
diff --git a/rust/README.md b/rust/README.mdnew file mode 100644index 0000000000..af589b21c5--- /dev/null+++ b/rust/README.md@@ -0,0 +1,193 @@+# Rust in Ente++```+ βββββββββββββββββββββββββββββββββββββββββ+ β rust/core/ β+ β (ente-core crate) β+ β β+ β Pure Rust - NO FFI annotations β+ β Stateless business logic β+ β Fully testable with cargo test β+ βββββββββββββββββββββββββββββββββββββββββ+ β+ ββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββ+ β β β+ βΌ βΌ βΌ+βββββββββββββββββββββββββββββ βββββββββββββββββββββββββββββ ββββββββββββββββββββββββββ+β mobile/apps/photos/rust/ β β web/packages/ente-wasm/ β β rust/cli/ β+β β β (ente-wasm crate) β β β+β Depends on ente-core β β β β (future: when CLI β+β #[frb] wrappers inline β β Rust crate + wasm-pack β β uses ente-core) β+β + app-specific (usearch) β β #[wasm_bindgen] wrappers β β β+βββββββββββββββββββββββββββββ βββββββββββββββββββββββββββββ ββββββββββββββββββββββββββ+ β β+ βΌ βΌ+βββββββββββββββββββββββββββββ βββββββββββββββββββββββββββββ+β Flutter Photos App β β Web Apps β+β β β (Photos, Auth, etc.) β+βββββββββββββββββββββββββββββ βββββββββββββββββββββββββββββ+```++## Directory Structure++```+rust/+βββ src/ # Existing CLI code (untouched until later)+β βββ main.rs+βββ Cargo.toml # Existing CLI Cargo.toml+β+βββ core/ # Pure Rust business logic+ βββ src/+ β βββ lib.rs+ β βββ urls.rs+ βββ Cargo.toml # crate name: ente-core++web/packages/ente-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/apps/photos/rust/ # FRB bindings (lives in mobile app)+βββ src/+β βββ lib.rs+β βββ api/ # #[frb] wrappers around ente-core+β βββ *.rs+βββ Cargo.toml # crate name: rust_lib_photos+```++**Crates:**++- `ente-core` - shared business logic (pure Rust, no FFI)+- `ente-wasm` - wasm-bindgen wrappers for web+- `rust_lib_photos` - FRB wrappers for mobile++## Tooling++### [wasm-bindgen](https://github.com/wasm-bindgen/wasm-bindgen)++A Rust library that provides the `#[wasm_bindgen]` attribute macro. When you annotate a function with `#[wasm_bindgen]`, it:++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++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 wrappers (annotated with `#[frb]`) live in `mobile/apps/photos/rust/` and depend on `ente-core`.++Currently, App-specific functionality (usearch, ML) stays alongside in the same crate. In the future, if another one of Ente's Flutter apps needs the same wrappers, we can extract a shared crate.++## Development++### Commands++**ente-core (rust/core/):**++```sh+cargo fmt # format+cargo clippy # lint+cargo build # build+cargo test # test+```++**ente-wasm (web/packages/ente-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"+> ```++**rust_lib_photos (mobile/apps/photos/rust/):**++One time:++```sh+cargo install flutter_rust_bridge_codegen+```++From `mobile/apps/photos`:++```sh+flutter pub get+flutter_rust_bridge_codegen generate+```++Then `mobile/apps/photos/rust/`++```sh+cargo build+```++## Future++### CLI Migration++When CLI should use ente-core:++1. Move `rust/src/` β `rust/cli/`+2. Create workspace `Cargo.toml` at `rust/`+3. CLI depends on ente-core+4. Gradually replace CLI's own implementations with ente-core calls++### 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"]+```++### Other notes++- 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++## Tests++- **ente-core:** Standard `cargo test` - comprehensive unit tests+- **rust_lib_photos:** 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