diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c5e7111..872c576 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,6 +18,11 @@ jobs: run: cargo check --workspace --all-targets - name: cargo test run: cargo test --workspace + # app/ is its own workspace, so the steps above never build it. + - name: cargo check (app) + run: cargo check --manifest-path app/Cargo.toml --all-targets --locked + - name: cargo test (app) + run: cargo test --manifest-path app/Cargo.toml --locked banned-names: runs-on: ubuntu-latest diff --git a/app/Cargo.lock b/app/Cargo.lock new file mode 100644 index 0000000..78d9f3b --- /dev/null +++ b/app/Cargo.lock @@ -0,0 +1,593 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "aho-corasick" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c982642fa9e8606056828ee9a8505737230110bb1099153c79efe865c59d12ba" +dependencies = [ + "memchr", +] + +[[package]] +name = "bitflags" +version = "2.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" + +[[package]] +name = "bstr" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6bb31b46c14244e20ee9984b11bf5c992b91fb6939fea616e3512c8baecdbe5f" +dependencies = [ + "memchr", + "serde_core", +] + +[[package]] +name = "bumpalo" +version = "3.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" + +[[package]] +name = "cc" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86e055fa6e3e40ad6187ba25b6855eeb635d5271f8e0f0e1e9a5826b8d75bea0" +dependencies = [ + "find-msvc-tools", + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" + +[[package]] +name = "crossbeam-deque" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "622f3fc73690be383c7214310406f28a90e6edeadc3cea882f9d71e495b9711a" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc74980687109a3b14c72fd458107bf0baa1da1a1a805e178d15501ba9b86d9d" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31eee39dddec8330830986fcd7625edb5a24ec90ea038215273bbc3adb08ac6" + +[[package]] +name = "equivalent" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00d174d5400e5e8fd687ad1049e2f578285fa914201b1af7e8b112a4546bd826" + +[[package]] +name = "fallible-iterator" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649" + +[[package]] +name = "fallible-streaming-iterator" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a" + +[[package]] +name = "find-msvc-tools" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c66915cc5391097de7a2b0b972e8505aa33c0df401fd3a6811dbdb32881ba48" + +[[package]] +name = "foldhash" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb" + +[[package]] +name = "globset" +version = "0.4.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f98b0d85522dc0a824cfebbc9594ff4a9b289c568bea0370319dfbdf20ccdd2d" +dependencies = [ + "aho-corasick", + "bstr", + "log", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "hashbrown" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" +dependencies = [ + "foldhash", +] + +[[package]] +name = "hashbrown" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" +dependencies = [ + "foldhash", +] + +[[package]] +name = "hashlink" +version = "0.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a596f1b20ed2cc5ecac41a164aaebc7258057060f06c0cf7a2ba3991ee7990fb" +dependencies = [ + "hashbrown 0.17.1", +] + +[[package]] +name = "ignore" +version = "0.4.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "484be5657551d7868d67587df967fb4fcbe9d266a761cc855aaa148fda0b3cc7" +dependencies = [ + "crossbeam-deque", + "globset", + "log", + "memchr", + "regex-automata", + "same-file", + "walkdir", + "winapi-util", +] + +[[package]] +name = "indexmap" +version = "2.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc4e190f5d26ca7051642629da2c52fc03bde85a03197c99408dcd291734c855" +dependencies = [ + "equivalent", + "hashbrown 0.17.1", +] + +[[package]] +name = "itoa" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" + +[[package]] +name = "js-sys" +version = "0.3.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8dc09d2154098bbf4ef54eab6b117a3c24cbbffe94046b16ee6d6d4510584d3a" +dependencies = [ + "cfg-if", + "wasm-bindgen", +] + +[[package]] +name = "lapis-lattice" +version = "0.4.2" +source = "git+https://github.com/Hedronite/lapis-lattice?rev=da4d1ce815d55f89db5cbf8ec2879e14f8957add#da4d1ce815d55f89db5cbf8ec2879e14f8957add" +dependencies = [ + "ignore", + "regex", + "rusqlite", + "serde", + "serde_json", + "sqlite-vec", + "thiserror", +] + +[[package]] +name = "lapis-notes" +version = "0.4.2" +source = "git+https://github.com/Hedronite/lapis-lattice?rev=da4d1ce815d55f89db5cbf8ec2879e14f8957add#da4d1ce815d55f89db5cbf8ec2879e14f8957add" +dependencies = [ + "serde", + "serde_json", + "thiserror", + "yaml_serde", +] + +[[package]] +name = "libsqlite3-sys" +version = "0.38.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1d20bef17f513b9b3004532233187769cd072d790971f4e4da0e346eb6401e8" +dependencies = [ + "cc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "libyaml-rs" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e126dda6f34391ab7b444f9922055facc83c07a910da3eb16f1e4d9c45dc777" + +[[package]] +name = "log" +version = "0.4.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6" + +[[package]] +name = "memchr" +version = "2.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" + +[[package]] +name = "objc2" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08849bbd4767dfae9457696856ae1c84fe4e0281bbe4a7abff2d0e06fb7981f8" +dependencies = [ + "objc2-encode", +] + +[[package]] +name = "objc2-encode" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef25abbcd74fb2609453eb695bd2f860d389e457f67dc17cafc8b8cbc89d0c33" + +[[package]] +name = "objc2-foundation" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3e0adef53c21f888deb4fa59fc59f7eb17404926ee8a6f59f5df0fd7f9f3272" +dependencies = [ + "bitflags", + "objc2", +] + +[[package]] +name = "pendant-app" +version = "0.0.1" +dependencies = [ + "lapis-lattice", + "lapis-notes", + "objc2-foundation", + "rusqlite", +] + +[[package]] +name = "pkg-config" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548" + +[[package]] +name = "proc-macro2" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "regex" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f020237b6c8eed93db2e2cb53c00c60a8e1bc73da7d073199a1180401450218d" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad8553b9b26413251cbf30e620595c7a41b3887f03da04579c0e6b0d6a06b4b2" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4" + +[[package]] +name = "rsqlite-vfs" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c51c9ae4df8a7fba42103df5c621fa3c37eccf3a3c650879e90fc48b11cc192c" +dependencies = [ + "hashbrown 0.16.1", + "thiserror", +] + +[[package]] +name = "rusqlite" +version = "0.40.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23f2a97da3e3873c73cb2a2e71b35c40ff95e0b1eefa8d72d8499a6928c3b5b3" +dependencies = [ + "bitflags", + "fallible-iterator", + "fallible-streaming-iterator", + "hashlink", + "libsqlite3-sys", + "smallvec", + "sqlite-wasm-rs", +] + +[[package]] +name = "rustversion" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" + +[[package]] +name = "ryu" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f" + +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "serde" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.154" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6" +dependencies = [ + "foldhash", + "indexmap", + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "shlex" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" + +[[package]] +name = "smallvec" +version = "1.16.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0d5d4b1981b8bbbb7aed204cae5726d683d5d6ac7e74ec54122ea28f315e7c8" + +[[package]] +name = "sqlite-vec" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0ba424237a9a5db2f6071f193319e2b6a32f7f3961debb2fbbfe67067abce3f" +dependencies = [ + "cc", +] + +[[package]] +name = "sqlite-wasm-rs" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc3efc0da82635d7e1ced0053bbbfa8c7ab9645d0bf36ceb4f7127bb85315d75" +dependencies = [ + "cc", + "js-sys", + "rsqlite-vfs", + "wasm-bindgen", +] + +[[package]] +name = "syn" +version = "3.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d78c8dee4c7bf0e14673097256fed6142ce9d3b85a408189d07482442145823b" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thiserror" +version = "2.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09e52cb86a36cede5cb101bf8908837b3e4c6e5e59fe7fd85c23fb56200d189e" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe5197923287db20a58125f0bc85c062f7f2c892de97b18c356f9efb14b28524" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "unicode-ident" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + +[[package]] +name = "wasm-bindgen" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0181c14ddd6c7b8b0b0132a5f1ea2f32be0d893cd3ea97b6e3b28886b8a3d1c" +dependencies = [ + "cfg-if", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e931a02a39abdbf66966fc35902b5ed58344750a70885bed4ebb3d8eaf28ed9" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ca4b54280e455bd831e33a6b8e734baf432fe63fbc35c42ac14ce67a043a53a" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b1f9c3798725016c6ee5f2f57869ec0831e8bbc0ab0310a86e787e97f41389e" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "winapi-util" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + +[[package]] +name = "yaml_serde" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33b729a08a9a6be689bbad3e2bf8015926db54b6622cc89c3a5f7dc174b9e918" +dependencies = [ + "indexmap", + "itoa", + "libyaml-rs", + "ryu", + "serde", +] + +[[package]] +name = "zmij" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" diff --git a/app/Cargo.toml b/app/Cargo.toml new file mode 100644 index 0000000..4ad5c49 --- /dev/null +++ b/app/Cargo.toml @@ -0,0 +1,30 @@ +[package] +name = "pendant-app" +version = "0.0.1" +edition = "2021" +rust-version = "1.85" +license = "MIT" +repository = "https://github.com/Hedronite/pendant" +description = "Pendant's phone app" +publish = false + +# Until app/src/lib.rs lands, the notes store is the library root. +[lib] +path = "src/notes/store.rs" + +[dependencies] +# Pinned to the Lapis engine merge that carries GOAL-mobile G0-G3. lapis-notes +# is publish = false, so both stay git deps at this exact rev, never a version. +lapis-lattice = { git = "https://github.com/Hedronite/lapis-lattice", rev = "da4d1ce815d55f89db5cbf8ec2879e14f8957add" } +lapis-notes = { git = "https://github.com/Hedronite/lapis-lattice", rev = "da4d1ce815d55f89db5cbf8ec2879e14f8957add" } + +# NSURLIsExcludedFromBackupKey on the index directory (iOS, and macOS dev hosts). +[target.'cfg(target_vendor = "apple")'.dependencies] +objc2-foundation = { version = "0.3", default-features = false, features = ["std", "NSError", "NSString", "NSURL", "NSValue"] } + +[dev-dependencies] +# Same major as lapis-lattice's, so it unifies: the tests read the index file. +rusqlite = "0.40" + +# Its own workspace, apart from the bridge crates at the repo root. +[workspace] diff --git a/app/src/notes/store.rs b/app/src/notes/store.rs new file mode 100644 index 0000000..6bf89a1 --- /dev/null +++ b/app/src/notes/store.rs @@ -0,0 +1,733 @@ +//! Pendant's notes store: the Lapis vault on the phone, offline. +//! +//! One worker thread owns the [`Engine`]. The UI thread only hands it jobs, so +//! opening the index, refreshing it, searching and capturing never run on the +//! UI thread. The index lives in a caller-chosen directory in app-private +//! storage, never in the vault, and is excluded from backups. Search is +//! keyword (FTS5); the embedder is `None`. Capture goes through +//! [`lapis_notes::create_new`], which writes a new file and never overwrites. +//! Nothing here spawns a process or opens a network connection. + +use std::fmt; +use std::path::{Path, PathBuf}; +use std::sync::mpsc::{self, Receiver, Sender}; +use std::thread::{self, JoinHandle}; + +use lapis_lattice::{Engine, Hit, RefreshReport}; +use lapis_notes::{CreateRequest, WrittenNote}; + +/// Name of the store's worker thread. +pub const WORKER: &str = "pendant-notes"; + +/// Where the store reads the vault and keeps its index. +#[derive(Debug, Clone)] +pub struct Config { + /// The vault root. The store writes nothing in it except a new capture. + pub vault: PathBuf, + /// The index directory, in app-private storage. On iOS, a directory under + /// Application Support; the store marks it excluded from backups. On + /// Android, a directory under `Context.getNoBackupFilesDir()`, which the + /// OS already leaves out of Auto Backup; anything else is refused. + pub index_dir: PathBuf, + /// The inbox bucket captures land in. Always passed to `create_new`, so + /// the vault's `.lapis/vault.json` is never consulted. + pub inbox: String, +} + +/// Why a refresh ran. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Trigger { + Launch, + Foreground, +} + +/// Called on the worker thread when a refresh ends. A UI must hop to its own +/// thread before touching views. +pub type OnRefresh = Box) + Send>; + +/// A capture written to the vault. +#[derive(Debug, Clone)] +pub struct Captured { + pub note: WrittenNote, + /// Why the new note is not searchable yet. The file is on disk either + /// way, and the next refresh indexes it. + pub index_error: Option, +} + +#[derive(Debug)] +pub enum StoreError { + /// The engine refused or failed: confinement, SQLite, I/O. + Index(lapis_lattice::Error), + /// `create_new` refused or failed, an existing note included. + Capture(lapis_notes::Error), + /// The index directory could not be kept out of backups. + Backup(String), + /// `created` is not a `YYYY-MM-DD` date. + Date(String), + /// The worker thread could not start or has stopped. + Worker(String), +} + +impl fmt::Display for StoreError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + StoreError::Index(e) => write!(f, "index: {e}"), + StoreError::Capture(e) => write!(f, "capture: {e}"), + StoreError::Backup(m) => write!(f, "backup exclusion: {m}"), + StoreError::Date(m) => write!(f, "date: {m}"), + StoreError::Worker(m) => write!(f, "notes worker: {m}"), + } + } +} + +impl std::error::Error for StoreError {} + +enum Job { + Refresh(Trigger), + Search { + query: String, + limit: u32, + reply: Sender, StoreError>>, + }, + Capture { + title: String, + body: Option, + created: String, + reply: Sender>, + }, +} + +/// The notes store. Dropping it stops the worker after its current job. +pub struct Store { + jobs: Option>, + worker: Option>, + index_dir: PathBuf, +} + +impl Store { + /// Start the worker and open the index there. Returns once the index is + /// open, or with the refusal: an index directory that is the vault, inside + /// it, reached through `..`, or through a symlink into it is refused + /// before anything is written. + pub fn open(config: Config, on_refresh: OnRefresh) -> Result { + let (jobs, queue) = mpsc::channel(); + let (opened, open_result) = mpsc::channel(); + let worker = thread::Builder::new() + .name(WORKER.into()) + .spawn(move || match open_engine(&config) { + Ok(engine) => { + let _ = opened.send(Ok(engine.db_path())); + run(engine, &config, queue, on_refresh); + } + Err(err) => { + let _ = opened.send(Err(err)); + } + }) + .map_err(|e| StoreError::Worker(e.to_string()))?; + let db = open_result + .recv() + .map_err(|_| StoreError::Worker("stopped while opening".into()))??; + let index_dir = db.parent().map(Path::to_path_buf).unwrap_or_default(); + Ok(Store { + jobs: Some(jobs), + worker: Some(worker), + index_dir, + }) + } + + /// The resolved index directory. + pub fn index_dir(&self) -> &Path { + &self.index_dir + } + + /// Queue a refresh for app launch. Returns at once; the result goes to + /// the `on_refresh` callback. + pub fn launched(&self) -> Result<(), StoreError> { + self.send(Job::Refresh(Trigger::Launch)) + } + + /// Queue a refresh for the app coming to the foreground. + pub fn foregrounded(&self) -> Result<(), StoreError> { + self.send(Job::Refresh(Trigger::Foreground)) + } + + /// Keyword search. Waits for the worker, so call it off the UI thread. + pub fn search(&self, query: &str, limit: u32) -> Result, StoreError> { + let (reply, answer) = mpsc::channel(); + self.send(Job::Search { + query: query.to_string(), + limit, + reply, + })?; + answer + .recv() + .map_err(|_| StoreError::Worker("stopped".into()))? + } + + /// Write a new note into the inbox bucket, stamped `created` (`YYYY-MM-DD`, + /// the caller's local date). A title whose note already exists is refused + /// and the existing file is left as it is. Waits for the worker, so call + /// it off the UI thread. + pub fn capture( + &self, + title: &str, + body: Option<&str>, + created: &str, + ) -> Result { + check_date(created)?; + let (reply, answer) = mpsc::channel(); + self.send(Job::Capture { + title: title.to_string(), + body: body.map(str::to_string), + created: created.to_string(), + reply, + })?; + answer + .recv() + .map_err(|_| StoreError::Worker("stopped".into()))? + } + + fn send(&self, job: Job) -> Result<(), StoreError> { + self.jobs + .as_ref() + .and_then(|jobs| jobs.send(job).ok()) + .ok_or_else(|| StoreError::Worker("stopped".into())) + } +} + +impl Drop for Store { + fn drop(&mut self) { + self.jobs.take(); + if let Some(worker) = self.worker.take() { + let _ = worker.join(); + } + } +} + +fn open_engine(config: &Config) -> Result { + check_backup_location(&config.index_dir)?; + // Confinement is the engine's: it resolves the directory, refuses `..`, + // the vault and anything inside it, and writes nothing when it refuses. + let engine = + Engine::open_in(&config.vault, &config.index_dir, None).map_err(StoreError::Index)?; + if let Some(dir) = engine.db_path().parent() { + exclude_from_backup(dir)?; + } + Ok(engine) +} + +fn run(mut engine: Engine, config: &Config, queue: Receiver, on_refresh: OnRefresh) { + for job in queue { + match job { + Job::Refresh(trigger) => { + on_refresh(trigger, engine.refresh().map_err(StoreError::Index)) + } + Job::Search { + query, + limit, + reply, + } => { + let _ = reply.send(engine.search(&query, limit).map_err(StoreError::Index)); + } + Job::Capture { + title, + body, + created, + reply, + } => { + let _ = reply.send(capture(&mut engine, config, title, body, created)); + } + } + } +} + +fn capture( + engine: &mut Engine, + config: &Config, + title: String, + body: Option, + created: String, +) -> Result { + let req = CreateRequest { + title, + body, + created, + inbox: Some(config.inbox.clone()), + ..Default::default() + }; + let note = lapis_notes::create_new(&config.vault, &req, None).map_err(StoreError::Capture)?; + let index_error = engine.reindex_path(¬e.path).err().map(|e| e.to_string()); + Ok(Captured { note, index_error }) +} + +fn check_date(created: &str) -> Result<(), StoreError> { + let b = created.as_bytes(); + let digits = |r: std::ops::Range| b[r].iter().all(u8::is_ascii_digit); + let ok = b.len() == 10 + && b[4] == b'-' + && b[7] == b'-' + && digits(0..4) + && digits(5..7) + && digits(8..10) + && (1..=12).contains(&created[5..7].parse::().unwrap_or(0)) + && (1..=31).contains(&created[8..10].parse::().unwrap_or(0)); + if ok { + Ok(()) + } else { + Err(StoreError::Date(format!("{created:?} is not YYYY-MM-DD"))) + } +} + +/// Android leaves `getNoBackupFilesDir()` out of Auto Backup and device +/// transfer. The index must sit under it. +#[cfg_attr(not(any(target_os = "android", test)), allow(dead_code))] +fn under_no_backup(index_dir: &Path) -> bool { + index_dir.components().any(|c| c.as_os_str() == "no_backup") +} + +fn check_backup_location(index_dir: &Path) -> Result<(), StoreError> { + #[cfg(target_os = "android")] + if !under_no_backup(index_dir) { + return Err(StoreError::Backup(format!( + "{} is not under the app's no_backup directory", + index_dir.display() + ))); + } + let _ = index_dir; + Ok(()) +} + +#[cfg(target_vendor = "apple")] +fn exclude_from_backup(dir: &Path) -> Result<(), StoreError> { + use objc2_foundation::{NSNumber, NSString, NSURLIsExcludedFromBackupKey, NSURL}; + let path = dir + .to_str() + .ok_or_else(|| StoreError::Backup(format!("{} is not UTF-8", dir.display())))?; + let url = NSURL::fileURLWithPath_isDirectory(&NSString::from_str(path), true); + let yes = NSNumber::new_bool(true); + unsafe { url.setResourceValue_forKey_error(Some(&yes), NSURLIsExcludedFromBackupKey) } + .map_err(|e| StoreError::Backup(e.localizedDescription().to_string())) +} + +/// Android: the location is the exclusion, checked before the index opened. +/// Desktop dev hosts keep no phone backup. +#[cfg(not(target_vendor = "apple"))] +fn exclude_from_backup(_dir: &Path) -> Result<(), StoreError> { + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::sync::mpsc::Receiver; + use std::thread::ThreadId; + use std::time::{Duration, SystemTime, UNIX_EPOCH}; + + /// Unique per call: pid, nanos and a counter. + fn dir(name: &str) -> PathBuf { + static SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); + let n = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_nanos(); + let seq = SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + let d = std::env::temp_dir().join(format!( + "pendant-store-{name}-{}-{n}-{seq}", + std::process::id() + )); + std::fs::create_dir_all(&d).unwrap(); + d + } + + /// Two made-up notes. + fn vault() -> PathBuf { + let d = dir("vault"); + std::fs::create_dir_all(d.join("notes")).unwrap(); + std::fs::write( + d.join("notes/Welcome.md"), + "---\nname: Welcome\n---\n# Welcome\n\nSee [[Alpha]].\n", + ) + .unwrap(); + std::fs::write( + d.join("notes/Alpha.md"), + "---\nname: Alpha\n---\n# Alpha\n\nMarigold first, then back to [[Welcome]].\n", + ) + .unwrap(); + d + } + + /// Every file under `root` with its bytes, sorted. + fn tree(root: &Path) -> Vec<(PathBuf, Vec)> { + fn walk(dir: &Path, out: &mut Vec<(PathBuf, Vec)>) { + for entry in std::fs::read_dir(dir).unwrap() { + let entry = entry.unwrap(); + let ft = entry.file_type().unwrap(); + if ft.is_dir() { + walk(&entry.path(), out); + } else if ft.is_file() { + out.push((entry.path(), std::fs::read(entry.path()).unwrap())); + } + } + } + let mut out = Vec::new(); + walk(root, &mut out); + out.sort(); + out + } + + struct Refreshed { + trigger: Trigger, + result: Result, + thread: Option, + id: ThreadId, + } + + fn config(vault: &Path, index_dir: &Path) -> Config { + Config { + vault: vault.to_path_buf(), + index_dir: index_dir.to_path_buf(), + inbox: "inbox".into(), + } + } + + fn open(config: Config) -> (Store, Receiver) { + let (tx, rx) = mpsc::channel(); + let store = Store::open( + config, + Box::new(move |trigger, result| { + let me = thread::current(); + let _ = tx.send(Refreshed { + trigger, + result, + thread: me.name().map(str::to_string), + id: me.id(), + }); + }), + ) + .unwrap(); + (store, rx) + } + + fn next(done: &Receiver) -> Refreshed { + done.recv_timeout(Duration::from_secs(60)) + .expect("a refresh reported back") + } + + fn counts(r: &RefreshReport) -> (u64, u64, u64) { + (r.added, r.updated, r.removed) + } + + /// The `-shm` file is left out: SQLite updates its read marks on every read. + fn index_files(db: &Path) -> Vec>> { + let wal = PathBuf::from(format!("{}-wal", db.display())); + vec![std::fs::read(db).ok(), std::fs::read(wal).ok()] + } + + fn rows(db: &Path) -> Vec> { + let conn = rusqlite::Connection::open(db).unwrap(); + let mut stmt = conn + .prepare("SELECT path, mtime, size, hash FROM documents ORDER BY path") + .unwrap(); + stmt.query_map([], |r| (0..4).map(|i| r.get(i)).collect()) + .unwrap() + .collect::>() + .unwrap() + } + + fn paths(store: &Store, query: &str) -> Vec { + store + .search(query, 10) + .unwrap() + .into_iter() + .map(|h| h.path) + .collect() + } + + #[cfg(target_vendor = "apple")] + fn excluded_from_backup(dir: &Path) -> bool { + use objc2_foundation::{NSNumber, NSString, NSURLIsExcludedFromBackupKey, NSURL}; + let url = + NSURL::fileURLWithPath_isDirectory(&NSString::from_str(dir.to_str().unwrap()), true); + let mut value = None; + unsafe { url.getResourceValue_forKey_error(&mut value, NSURLIsExcludedFromBackupKey) } + .unwrap(); + value + .and_then(|v| v.downcast::().ok()) + .is_some_and(|n| n.as_bool()) + } + + /// G1a: the index builds in app storage outside the vault, the vault gains + /// no file, the directory is excluded from backups, and search reads it. + #[test] + fn the_index_lives_outside_the_vault_and_out_of_backups() { + let v = vault(); + let before = tree(&v); + let app = dir("app"); + let index = app.join("Library/Application Support/notes-index"); + let (store, done) = open(config(&v, &index)); + + assert_eq!(store.index_dir(), index.canonicalize().unwrap()); + assert!(store.index_dir().join("lattice.sqlite").is_file()); + store.launched().unwrap(); + let r = next(&done); + assert_eq!(counts(&r.result.unwrap()), (2, 0, 0)); + assert_eq!(paths(&store, "marigold"), ["notes/Alpha.md"]); + assert_eq!(tree(&v), before, "the vault gains no file"); + #[cfg(target_vendor = "apple")] + assert!( + excluded_from_backup(store.index_dir()), + "the index directory is excluded from backups" + ); + drop(store); + let _ = std::fs::remove_dir_all(&v); + let _ = std::fs::remove_dir_all(&app); + } + + /// G1b: the vault, a directory inside it, a `..` walk back into it, and a + /// symlink into it are each refused, and nothing appears anywhere. + #[cfg(unix)] + #[test] + fn an_index_in_the_vault_is_refused_before_a_write() { + let v = vault(); + let before = tree(&v); + let outside = dir("outside"); + std::os::unix::fs::symlink(v.join("notes"), outside.join("in-vault")).unwrap(); + let cases = [ + v.clone(), + v.join("idx"), + outside.join("..").join(v.file_name().unwrap()).join("idx"), + outside.join("in-vault"), + outside.join("in-vault/deeper"), + ]; + for index in cases { + let err = Store::open(config(&v, &index), Box::new(|_, _| {})) + .err() + .expect("refused"); + assert!( + matches!(err, StoreError::Index(lapis_lattice::Error::Usage(_))), + "{}: {err}", + index.display() + ); + assert!( + !index.join("lattice.sqlite").exists(), + "{}", + index.display() + ); + } + assert_eq!(tree(&v), before, "a refusal writes nothing in the vault"); + let left: Vec<_> = std::fs::read_dir(&outside) + .unwrap() + .map(|e| e.unwrap().file_name()) + .collect(); + assert_eq!( + left, + ["in-vault"], + "a refusal creates nothing outside it either" + ); + let _ = std::fs::remove_dir_all(&outside); + let _ = std::fs::remove_dir_all(&v); + } + + /// G2a: launch and foreground refreshes run on the store's worker, not on + /// the thread that asked for them. + #[test] + fn refresh_runs_off_the_calling_thread() { + let v = vault(); + let app = dir("app"); + let (store, done) = open(config(&v, &app.join("index"))); + let ui = thread::current().id(); + + store.launched().unwrap(); + store.foregrounded().unwrap(); + for want in [Trigger::Launch, Trigger::Foreground] { + let r = next(&done); + assert_eq!(r.trigger, want); + assert!(r.result.is_ok()); + assert_eq!(r.thread.as_deref(), Some(WORKER)); + assert_ne!(r.id, ui, "refresh ran on the caller's thread"); + } + drop(store); + let _ = std::fs::remove_dir_all(&v); + let _ = std::fs::remove_dir_all(&app); + } + + /// G2b: a refresh with nothing changed commits nothing and leaves the + /// index files' bytes as they were. + #[test] + fn a_no_change_refresh_writes_nothing() { + let v = vault(); + let app = dir("app"); + let (store, done) = open(config(&v, &app.join("index"))); + store.launched().unwrap(); + assert_eq!(counts(&next(&done).result.unwrap()), (2, 0, 0)); + + let db = store.index_dir().join("lattice.sqlite"); + // data_version moves when another connection commits. + let probe = rusqlite::Connection::open(&db).unwrap(); + let version = || { + probe + .query_row("PRAGMA data_version", [], |r| r.get::<_, i64>(0)) + .unwrap() + }; + let v0 = version(); + let files = index_files(&db); + assert!(files[0].is_some(), "the index file exists"); + + store.foregrounded().unwrap(); + assert_eq!(counts(&next(&done).result.unwrap()), (0, 0, 0)); + assert_eq!(version(), v0, "the no-change refresh committed"); + assert_eq!(index_files(&db), files, "the no-change refresh wrote bytes"); + drop(probe); + drop(store); + let _ = std::fs::remove_dir_all(&v); + let _ = std::fs::remove_dir_all(&app); + } + + /// G2b: a refresh that fails after it already rewrote a changed note rolls + /// back whole: the rows and the index file's bytes are the prior ones, and + /// the next refresh still applies the change. + #[test] + fn a_failed_refresh_rolls_back() { + let v = vault(); + let app = dir("app"); + let (store, done) = open(config(&v, &app.join("index"))); + store.launched().unwrap(); + assert_eq!(counts(&next(&done).result.unwrap()), (2, 0, 0)); + + let db = store.index_dir().join("lattice.sqlite"); + let probe = rusqlite::Connection::open(&db).unwrap(); + // Changed rows are rewritten before new files are added, so aborting + // the add fails the refresh after a write in the same transaction. + probe + .execute_batch( + "CREATE TRIGGER boom BEFORE INSERT ON documents WHEN NEW.path = 'notes/Boom.md' \ + BEGIN SELECT RAISE(ABORT, 'forced failure'); END;", + ) + .unwrap(); + probe + .query_row("PRAGMA wal_checkpoint(TRUNCATE)", [], |_| Ok(())) + .unwrap(); + let prior_rows = rows(&db); + let prior_bytes = std::fs::read(&db).unwrap(); + + std::fs::write( + v.join("notes/Alpha.md"), + "---\nname: Alpha\n---\n# Alpha\n\nZinnia now, a longer line.\n", + ) + .unwrap(); + std::fs::write(v.join("notes/Boom.md"), "# Boom\n").unwrap(); + store.foregrounded().unwrap(); + let err = next(&done).result.expect_err("the forced failure surfaces"); + assert!(err.to_string().contains("forced failure"), "{err}"); + assert_eq!(rows(&db), prior_rows, "the rewritten row rolled back"); + assert_eq!( + std::fs::read(&db).unwrap(), + prior_bytes, + "the index file kept its bytes" + ); + assert!( + paths(&store, "zinnia").is_empty(), + "the rolled-back text is not searchable" + ); + assert_eq!( + paths(&store, "marigold"), + ["notes/Alpha.md"], + "the prior text still is" + ); + + probe.execute_batch("DROP TRIGGER boom;").unwrap(); + store.foregrounded().unwrap(); + assert_eq!(counts(&next(&done).result.unwrap()), (1, 1, 0)); + assert_eq!(paths(&store, "zinnia"), ["notes/Alpha.md"]); + drop(probe); + drop(store); + let _ = std::fs::remove_dir_all(&v); + let _ = std::fs::remove_dir_all(&app); + } + + /// G3a, G3b: capture writes into the configured inbox with `created` set, + /// ignores the vault's own inbox setting, is searchable at once, and a + /// second capture of the same title is refused with the first file's + /// bytes unchanged. + #[test] + fn capture_writes_a_new_note_and_never_overwrites() { + let v = vault(); + std::fs::create_dir_all(v.join(".lapis")).unwrap(); + std::fs::write( + v.join(".lapis/vault.json"), + r#"{"buckets":{"inbox":"elsewhere"}}"#, + ) + .unwrap(); + let app = dir("app"); + let (store, done) = open(config(&v, &app.join("index"))); + store.launched().unwrap(); + next(&done).result.unwrap(); + + let first = store + .capture("Seed order", Some("Cosmos and larkspur.\n"), "2026-10-30") + .unwrap(); + assert!(first.note.path.starts_with("inbox/"), "{}", first.note.path); + assert!( + !v.join("elsewhere").exists(), + "the vault's own inbox setting was read" + ); + assert_eq!(first.index_error, None); + let written = std::fs::read(v.join(&first.note.path)).unwrap(); + let text = String::from_utf8(written.clone()).unwrap(); + assert!( + text.lines() + .any(|l| l.starts_with("created:") && l.contains("2026-10-30")), + "{text}" + ); + assert_eq!(paths(&store, "larkspur"), [first.note.path.as_str()]); + + let second = store.capture("Seed order", Some("Different bytes.\n"), "2026-10-31"); + assert!(matches!(second, Err(StoreError::Capture(_))), "{second:?}"); + assert_eq!( + std::fs::read(v.join(&first.note.path)).unwrap(), + written, + "the first note was overwritten" + ); + assert_eq!( + std::fs::read_dir(v.join("inbox")).unwrap().count(), + 1, + "no second file appeared" + ); + drop(store); + let _ = std::fs::remove_dir_all(&v); + let _ = std::fs::remove_dir_all(&app); + } + + /// A malformed `created` is refused before the worker writes anything. + #[test] + fn capture_refuses_a_date_that_is_not_iso() { + let v = vault(); + let app = dir("app"); + let (store, _done) = open(config(&v, &app.join("index"))); + for bad in [ + "30/10/2026", + "2026-13-01", + "2026-10-00", + "2026-1-5", + "2026-10-30T12:00", + "", + ] { + let r = store.capture("Dated", None, bad); + assert!(matches!(r, Err(StoreError::Date(_))), "{bad:?}: {r:?}"); + } + assert!(!v.join("inbox").exists()); + drop(store); + let _ = std::fs::remove_dir_all(&v); + let _ = std::fs::remove_dir_all(&app); + } + + #[test] + fn android_index_must_sit_under_no_backup() { + assert!(under_no_backup(Path::new( + "/data/user/0/com.hedronite.pendant/no_backup/notes-index" + ))); + assert!(!under_no_backup(Path::new( + "/data/user/0/com.hedronite.pendant/files/notes-index" + ))); + } +}