Skip to content

armgit / ente

Public Imported from GitHub
0

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

doc

Manav Rathib6e6944d1964
diff --git a/rust/README.md b/rust/README.md
new file mode 100644
index 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