From 79bc90057f5083ed90a9cd71c5c25b596d729861 Mon Sep 17 00:00:00 2001 From: astar76 Date: Tue, 24 Nov 2026 23:29:30 -0600 Subject: [PATCH] S001: workspace, store, settings and strings Co-Authored-By: Claude Opus 5.5 --- CLAUDE.md | 2 +- Cargo.lock | 981 ++++++++++++++++++ Cargo.toml | 25 + .../S001-workspace-store-settings.md | 16 +- Plans/INDEX.md | 2 +- crates/app/Cargo.toml | 9 + crates/app/src/main.rs | 5 + crates/cli/Cargo.toml | 9 + crates/cli/src/lib.rs | 17 + crates/core/Cargo.toml | 19 + crates/core/lang/en.json | 36 + crates/core/src/lib.rs | 11 + crates/core/src/logging.rs | 111 ++ crates/core/src/model.rs | 284 +++++ crates/core/src/paths.rs | 127 +++ crates/core/src/redact.rs | 92 ++ crates/core/src/settings.rs | 235 +++++ crates/core/src/store/001.sql | 77 ++ crates/core/src/store/mod.rs | 271 +++++ crates/core/src/store/rows.rs | 230 ++++ crates/core/src/store/tests.rs | 141 +++ crates/core/src/strings.rs | 293 ++++++ crates/core/tests/store_kill.rs | 64 ++ crates/testserver/Cargo.toml | 8 + crates/testserver/src/lib.rs | 1 + docs/DECISIONS.md | 1 + docs/PROGRESS.md | 11 + 27 files changed, 3068 insertions(+), 10 deletions(-) create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 crates/app/Cargo.toml create mode 100644 crates/app/src/main.rs create mode 100644 crates/cli/Cargo.toml create mode 100644 crates/cli/src/lib.rs create mode 100644 crates/core/Cargo.toml create mode 100644 crates/core/lang/en.json create mode 100644 crates/core/src/lib.rs create mode 100644 crates/core/src/logging.rs create mode 100644 crates/core/src/model.rs create mode 100644 crates/core/src/paths.rs create mode 100644 crates/core/src/redact.rs create mode 100644 crates/core/src/settings.rs create mode 100644 crates/core/src/store/001.sql create mode 100644 crates/core/src/store/mod.rs create mode 100644 crates/core/src/store/rows.rs create mode 100644 crates/core/src/store/tests.rs create mode 100644 crates/core/src/strings.rs create mode 100644 crates/core/tests/store_kill.rs create mode 100644 crates/testserver/Cargo.toml create mode 100644 crates/testserver/src/lib.rs diff --git a/CLAUDE.md b/CLAUDE.md index 98bbcaf..f026376 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -22,7 +22,7 @@ After ticking a box: `ArtifactData` `update` with `url` = the tracker, `collecti Root: the folder holding this file ``` -Planned (the Foundation plans create it; keep this list true): +Keep this list true (tools/, assets/ and docs/CONNECTOR come with their plans): crates/core/ engine, limits, queues, store, connector server; no UI code crates/app/ egui app: theme, motion, widgets, views, dialogs, tray crates/cli/ command line and headless mode, a library used by the `medlar` binary diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..104b286 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,981 @@ +# 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 = "android_system_properties" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae221649c9976a6f6c56ae1facf410f3ddb33cc661c4b7b61020a912d4237fbc" +dependencies = [ + "libc", +] + +[[package]] +name = "autocfg" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" + +[[package]] +name = "bitflags" +version = "2.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" + +[[package]] +name = "bumpalo" +version = "3.20.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87a0cf66fd1eb2fb7b822cb78370d82303750809bc6ba855f3f58e517d3238db" + +[[package]] +name = "cc" +version = "1.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02f0e023adb368345ce0ca6cdfc0f24a06554468d0e8503e14a0841de79bfa69" +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 = "chrono" +version = "0.4.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aa79e62e7697b8e29b513a68abacf485adcd1fe8284a4316c5ae868e6633327" +dependencies = [ + "iana-time-zone", + "num-traits", + "windows-link", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "dirs" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3e8aa94d75141228480295a7d0e7feb620b1a5ad9f12bc40be62411e38cce4e" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e01a3366d27ee9890022452ee61b2b63a67e6f13f58900b651ff5665f0bb1fab" +dependencies = [ + "libc", + "option-ext", + "redox_users", + "windows-sys", +] + +[[package]] +name = "displaydoc" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6232dd377dcc64799954cbd3a9bb882e9cdc1308ccd87b1c098f1fb2eaf82a8" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", +] + +[[package]] +name = "errno" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e35d6e78f4cebb90e51d543db8fe41528851d6deeef6ef21ebd4bfdc1e784776" +dependencies = [ + "libc", + "windows-sys", +] + +[[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 = "fastrand" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223" + +[[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 = "form_urlencoded" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afa489199eaffb1211795df1c6236ce1ad0d41a6fa45250add73e3b02420f06e" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "futures-core" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92d699e522242e69e3003b94ecc1f960f3a5e015aa7c5d7486e65ad01dd94f5e" + +[[package]] +name = "futures-task" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd417de3d1d015fc3bfd2b1ea46dfc7bab72ef86f1cc7cc9c78e728b34a6d1fd" + +[[package]] +name = "futures-util" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d50a92467f8ba5dd6e3ee5d4bd04d73ab2e4e1c44474a0674821dfce14b79bc" +dependencies = [ + "futures-core", + "futures-task", + "pin-project-lite", + "slab", +] + +[[package]] +name = "getrandom" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099" +dependencies = [ + "cfg-if", + "libc", + "r-efi", +] + +[[package]] +name = "hashbrown" +version = "0.17.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "735eaa21a6a5ffb2e84362faaeb90c05ba8e19797ca30428478cb6ea3a4921bc" +dependencies = [ + "foldhash", +] + +[[package]] +name = "hashlink" +version = "0.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a596f1b20ed2cc5ecac41a164aaebc7258057060f06c0cf7a2ba3991ee7990fb" +dependencies = [ + "hashbrown", +] + +[[package]] +name = "iana-time-zone" +version = "0.1.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "log", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "icu_collections" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe090a42a0d3140fed8c63263ed1fecc02422cb2f4bfe55cd8de2de1b8e105dd" +dependencies = [ + "displaydoc", + "potential_utf", + "utf8_iter", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_locale_core" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25d29b57c17c6528d0dc4c2141cfa2d08200465acb537a3909c8b9b8a4a984c0" +dependencies = [ + "displaydoc", + "litemap", + "tinystr", + "writeable", + "zerovec", +] + +[[package]] +name = "icu_normalizer" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "404a3b8c486f205c87fe7f23ab6894d84a4ad76d6e4671ae1981ba7def87c599" +dependencies = [ + "icu_collections", + "icu_normalizer_data", + "icu_properties", + "icu_provider", + "smallvec", + "zerovec", +] + +[[package]] +name = "icu_normalizer_data" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20f0088bfb7441f7a37fa0c9e20cb1e9cba1cac6b9ce8cd2a6dc44294f8e8cbc" + +[[package]] +name = "icu_properties" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ea83d991c56158f7cf898bf86436c1dfe7c07ee82fe88951f2695c9f37a379f" +dependencies = [ + "displaydoc", + "icu_collections", + "icu_locale_core", + "icu_properties_data", + "icu_provider", + "zerotrie", + "zerovec", +] + +[[package]] +name = "icu_properties_data" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b98f29af82d02b163fe50161388350e743bb3107cc8e8c2f7314cb505faddfcf" + +[[package]] +name = "icu_provider" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "413099315636c588ee8f011190b329b971a09a8fae9d157e01dcae8379468514" +dependencies = [ + "displaydoc", + "icu_locale_core", + "writeable", + "yoke", + "zerofrom", + "zerotrie", + "zerovec", +] + +[[package]] +name = "idna" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b0875f23caa03898994f6ddc501886a45c7d3d62d04d2d90788d47be1b1e4de" +dependencies = [ + "idna_adapter", + "smallvec", + "utf8_iter", +] + +[[package]] +name = "idna_adapter" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb68373c0d6620ef8105e855e7745e18b0d00d3bdb07fb532e434244cdb9a714" +dependencies = [ + "icu_normalizer", + "icu_properties", +] + +[[package]] +name = "itoa" +version = "1.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3bd1574e617a9a6c1f8f8181bd7e841e1f20e4277607ef28945765188d6284d4" + +[[package]] +name = "js-sys" +version = "0.3.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8dc09d2154098bbf4ef54eab6b117a3c24cbbffe94046b16ee6d6d4510584d3a" +dependencies = [ + "cfg-if", + "futures-util", + "wasm-bindgen", +] + +[[package]] +name = "libc" +version = "0.2.191" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a" + +[[package]] +name = "libredox" +version = "0.1.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61ff90caf6077a803a240f62fdbe88645a890bbca49ef8174c3cb0404362171d" +dependencies = [ + "libc", +] + +[[package]] +name = "libsqlite3-sys" +version = "0.40.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b06e69070b7e12262b0cd90832f0a7396f142a2b330181255562decf9f0c6215" +dependencies = [ + "cc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "linux-raw-sys" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" + +[[package]] +name = "litemap" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d9d19d1d6efa0109d2f65ff4c85cddd50bd572e5a00127ab10987290bcefae" + +[[package]] +name = "log" +version = "0.4.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d15f86d12da44cd8d4ec3c73984537eb7f4cadcb9776a575a0a8a05ca11005c" + +[[package]] +name = "medlar" +version = "0.1.0" +dependencies = [ + "medlar-cli", +] + +[[package]] +name = "medlar-cli" +version = "0.1.0" +dependencies = [ + "medlar-core", +] + +[[package]] +name = "medlar-core" +version = "0.1.0" +dependencies = [ + "chrono", + "dirs", + "log", + "regex", + "rusqlite", + "serde", + "serde_json", + "tempfile", + "url", +] + +[[package]] +name = "medlar-testserver" +version = "0.1.0" + +[[package]] +name = "memchr" +version = "2.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + +[[package]] +name = "option-ext" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" + +[[package]] +name = "percent-encoding" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" + +[[package]] +name = "pin-project-lite" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c938015719552d665fd0553b90d96cd7ff58e83e4c061ca99e6bb78865a72752" + +[[package]] +name = "pkg-config" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548" + +[[package]] +name = "potential_utf" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d83eb9bc6d8e5cf568e7a1101d60ee05e81ed50ea106026f3d18deeb046d7661" +dependencies = [ + "zerovec", +] + +[[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 = "r-efi" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" + +[[package]] +name = "redox_users" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60dc65c0ff1a7ae1294b0c67b9f14baf70b644404010370171787bfac1038fc0" +dependencies = [ + "libredox", + "thiserror", +] + +[[package]] +name = "regex" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9807af0e638c12e753aa1b4e601791bf2a8f71274023c8bbeec12fc149950857" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d60ef07df457506a0a1a2073112eb4aae5733dbd2813c3da77d1b0fd441158" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d902046e6a28a1e4c3cab97258a0ce11e994903f01c2d97a9447a0b3df85f4f6" + +[[package]] +name = "rsqlite-vfs" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "353b74f9810d5b085b6c42c9d86a5f0fdb795553235cdd48753b32e3809d18a7" +dependencies = [ + "thiserror", +] + +[[package]] +name = "rusqlite" +version = "0.43.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f172c4812a19b26d1c23cdb0070e69db335503dfa378b910eb857386e5b8beba" +dependencies = [ + "bitflags", + "fallible-iterator", + "fallible-streaming-iterator", + "hashlink", + "libsqlite3-sys", + "smallvec", + "sqlite-wasm-rs", +] + +[[package]] +name = "rustix" +version = "1.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5e76742e5294153d4dde1207dbec5bdd81eef6fabdc0d2118babadabfd858aa" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + +[[package]] +name = "rustversion" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" + +[[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 3.0.10", +] + +[[package]] +name = "serde_json" +version = "1.0.154" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6" +dependencies = [ + "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 = "slab" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" + +[[package]] +name = "smallvec" +version = "1.16.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3244542535d5de959d90ec55d1b5ff0fd2bb7b1310223907d5f11932847f7b59" + +[[package]] +name = "sqlite-wasm-rs" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cd504cecbfe7eef649988840f9a6cb768d11085efec138f8e99f160632d867" +dependencies = [ + "cc", + "rsqlite-vfs", +] + +[[package]] +name = "stable_deref_trait" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" + +[[package]] +name = "syn" +version = "2.0.119" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "3.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ae9ccbea3a7b0b09443036490c8f34a8b1b760fd345e5c0dae0aac718faea79" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "synstructure" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "901704edd0dfe137f1987838ee4f259e4e063c31371bdb423f7ae38ec6f77f02" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", +] + +[[package]] +name = "tempfile" +version = "3.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" +dependencies = [ + "fastrand", + "getrandom", + "once_cell", + "rustix", + "windows-sys", +] + +[[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 3.0.10", +] + +[[package]] +name = "tinystr" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abc840c238154d58f9e86b2792261edfde565a17696e1cda1a449de27979801a" +dependencies = [ + "displaydoc", + "zerovec", +] + +[[package]] +name = "unicode-ident" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" + +[[package]] +name = "url" +version = "2.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f00cd2407bb42c0ef17be88d4cf020f5cceffd121665bac9b8b9493a369b3b1a" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", + "serde", +] + +[[package]] +name = "utf8_iter" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af3bd9d3e571c26fcce9d033b87ea5ab061b8d2ffe9b7a5675d810d68f03edf0" +dependencies = [ + "utf_types", +] + +[[package]] +name = "utf_types" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a3b425286980a367278453f3b6d28cdcb134b574e5a35824a6ff5eebad50bb6" + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + +[[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 3.0.10", + "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 = "windows-core" +version = "0.62.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-link", + "windows-result", + "windows-strings", +] + +[[package]] +name = "windows-implement" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "windows-interface" +version = "0.59.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-result" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-strings" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091" +dependencies = [ + "windows-link", +] + +[[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 = "writeable" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ad82d2a33cdc9674dc7465672f271e096168fcdbe0f799d9e6db8c5892679dc" + +[[package]] +name = "yoke" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "417ef3fcf6c300b7dbd25c85531a746931c02ad536f3cf4786ed1e88223340b3" +dependencies = [ + "stable_deref_trait", + "yoke-derive", + "zerofrom", +] + +[[package]] +name = "yoke-derive" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec8ebde2db3681e8c9980cc27822030e68752690ddfa9473e739aeb4dbde6d71" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", + "synstructure", +] + +[[package]] +name = "zerofrom" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ec05a11813ea801ff6d75110ad09cd0824ddba17dfe17128ea0d5f68e6c5272" +dependencies = [ + "zerofrom-derive", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f75b4683f6c7f45248d4d64056a24298c6281e0993356d7d1b4a1a962ef10d4a" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", + "synstructure", +] + +[[package]] +name = "zerotrie" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ea269c3bd32f0a32c321907a2ae912ba6f4649bb0fc764a15627e99a7095a3f" +dependencies = [ + "displaydoc", + "yoke", + "zerofrom", +] + +[[package]] +name = "zerovec" +version = "0.11.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f58a2799cfe9a1670c7ea8edbd52ef8edb93cb37a7f83bf31ba9e35d9dae6a40" +dependencies = [ + "yoke", + "zerofrom", + "zerovec-derive", +] + +[[package]] +name = "zerovec-derive" +version = "0.11.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3743ddbf47738d929cd1ae2e199d40d0162979dfe31aef8ae646d2bf00bf97a" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", +] + +[[package]] +name = "zmij" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..bc91c09 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,25 @@ +[workspace] +members = ["crates/*"] +resolver = "3" + +[workspace.package] +version = "0.1.0" +edition = "2024" +license = "MIT" + +[workspace.dependencies] +medlar-core = { path = "crates/core" } +medlar-cli = { path = "crates/cli" } +medlar-testserver = { path = "crates/testserver" } +log = "0.4" +serde = { version = "1", features = ["derive"] } +serde_json = "1" + +[profile.release] +opt-level = 3 +lto = "thin" +codegen-units = 4 +strip = true + +[profile.dev.package."*"] +opt-level = 2 diff --git a/Plans/01 Foundation/S001-workspace-store-settings.md b/Plans/01 Foundation/S001-workspace-store-settings.md index 312d446..50c65e9 100644 --- a/Plans/01 Foundation/S001-workspace-store-settings.md +++ b/Plans/01 Foundation/S001-workspace-store-settings.md @@ -12,14 +12,14 @@ The Cargo workspace with four crates, and in `core`: task model, SQLite store, s - ~/Projects/Damson/src/config.rs ## Steps -- [ ] Workspace: `medlar-core`, `medlar` (app), `medlar-cli` (library), `medlar-testserver`; release profile like Damson's; version 0.1.0, MIT -- [ ] Paths: XDG dirs, plus portable mode when a `portable` file sits beside the binary; a `MEDLAR_HOME` override for tests -- [ ] Task model: task, part, queue, tag, host profile, error kind (empty enum list to fill in S007), states and the legal moves between them, with a test that refuses illegal moves -- [ ] Store: rusqlite (bundled), numbered migrations, WAL, one writer thread; survives a kill mid-write (test) -- [ ] Settings: serde with defaults, unknown keys kept, a damaged file is moved aside and reported, never fatal -- [ ] String catalogue: key to text with plurals and arguments, English built in, user files dropped in a folder; a test fails on a missing key -- [ ] Logging to a rotating file; a redaction helper for cookies, tokens, passwords and URL query strings, with tests -- [ ] `docs/PROGRESS.md` entry, tick `Plans/INDEX.md`, commit `S001: …`, push +- [x] Workspace: `medlar-core`, `medlar` (app), `medlar-cli` (library), `medlar-testserver`; release profile like Damson's; version 0.1.0, MIT +- [x] Paths: XDG dirs, plus portable mode when a `portable` file sits beside the binary; a `MEDLAR_HOME` override for tests +- [x] Task model: task, part, queue, tag, host profile, error kind (empty enum list to fill in S007), states and the legal moves between them, with a test that refuses illegal moves +- [x] Store: rusqlite (bundled), numbered migrations, WAL, one writer thread; survives a kill mid-write (test) +- [x] Settings: serde with defaults, unknown keys kept, a damaged file is moved aside and reported, never fatal +- [x] String catalogue: key to text with plurals and arguments, English built in, user files dropped in a folder; a test fails on a missing key +- [x] Logging to a rotating file; a redaction helper for cookies, tokens, passwords and URL query strings, with tests +- [x] `docs/PROGRESS.md` entry, tick `Plans/INDEX.md`, commit `S001: …`, push ## End criteria - `nice cargo test -j 2` green diff --git a/Plans/INDEX.md b/Plans/INDEX.md index d4c2afb..2c81e50 100644 --- a/Plans/INDEX.md +++ b/Plans/INDEX.md @@ -6,7 +6,7 @@ Tracker: https://claude.ai/artifact/XouimfVJS6FYVKskGJ8s7H ## 01 Foundation -- [ ] [S001](01%20Foundation/S001-workspace-store-settings.md) Workspace, store, settings and strings +- [x] [S001](01%20Foundation/S001-workspace-store-settings.md) Workspace, store, settings and strings - [ ] [S002](01%20Foundation/S002-test-server.md) Test server with faults - [ ] [S003](01%20Foundation/S003-shell-motion-tools.md) App shell, theme, motion and screenshot tools diff --git a/crates/app/Cargo.toml b/crates/app/Cargo.toml new file mode 100644 index 0000000..ab21319 --- /dev/null +++ b/crates/app/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "medlar" +version.workspace = true +edition.workspace = true +license.workspace = true +description = "A download manager for Linux" + +[dependencies] +medlar-cli.workspace = true diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs new file mode 100644 index 0000000..c6755f3 --- /dev/null +++ b/crates/app/src/main.rs @@ -0,0 +1,5 @@ +use std::process::ExitCode; + +fn main() -> ExitCode { + medlar_cli::run(std::env::args().skip(1)) +} diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml new file mode 100644 index 0000000..b38b8bf --- /dev/null +++ b/crates/cli/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "medlar-cli" +version.workspace = true +edition.workspace = true +license.workspace = true +description = "Medlar's command line" + +[dependencies] +medlar-core.workspace = true diff --git a/crates/cli/src/lib.rs b/crates/cli/src/lib.rs new file mode 100644 index 0000000..c60658f --- /dev/null +++ b/crates/cli/src/lib.rs @@ -0,0 +1,17 @@ +//! Command line of the `medlar` binary. + +use medlar_core::tr; +use std::process::ExitCode; + +/// Runs the command line and returns the exit code of the process. +pub fn run(mut args: impl Iterator) -> ExitCode { + match args.next().as_deref() { + Some("--version" | "-V") => println!("medlar {}", medlar_core::VERSION), + None | Some("--help" | "-h") => println!("{}", tr!("cli.usage")), + Some(other) => { + eprintln!("{}", tr!("cli.unknown", arg = other)); + return ExitCode::from(2); + } + } + ExitCode::SUCCESS +} diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml new file mode 100644 index 0000000..82bde0a --- /dev/null +++ b/crates/core/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "medlar-core" +version.workspace = true +edition.workspace = true +license.workspace = true +description = "Medlar's engine, store and settings" + +[dependencies] +chrono = { version = "0.4", default-features = false, features = ["clock", "std"] } +dirs = "6" +log = { workspace = true, features = ["std"] } +regex = { version = "1", default-features = false, features = ["std", "unicode-case", "unicode-perl"] } +rusqlite = { version = "0.43", features = ["bundled", "fallible_uint"] } +serde.workspace = true +serde_json.workspace = true +url = "2" + +[dev-dependencies] +tempfile = "3" diff --git a/crates/core/lang/en.json b/crates/core/lang/en.json new file mode 100644 index 0000000..7e8cf48 --- /dev/null +++ b/crates/core/lang/en.json @@ -0,0 +1,36 @@ +{ + "cli.usage": "Usage: medlar [--version | --help]", + "cli.unknown": "Unknown argument: {arg}. Try medlar --help.", + + "queue.main": "Main", + "tasks.count": {"one": "{n} download", "other": "{n} downloads"}, + + "state.queued": "Queued", + "state.waiting": "Waiting", + "state.running": "Downloading", + "state.paused": "Paused", + "state.checking": "Checking", + "state.finishing": "Finishing", + "state.done": "Done", + "state.failed": "Failed", + "state.gone": "File is gone", + + "priority.lowest": "Lowest", + "priority.low": "Low", + "priority.normal": "Normal", + "priority.high": "High", + "priority.highest": "Highest", + "priority.only": "Only this one", + + "settings.damaged": "Some settings could not be read and are back to their defaults: {keys}. The old file is kept.", + "settings.damaged_all": "The settings file could not be read, so every setting is back to its default. The old file is kept.", + + "store.db": "The download list could not be read or saved: {why}", + "store.newer": "The download list was made by a newer Medlar (format {found}, this one knows {known}). Update Medlar to open it.", + "store.illegal_move": "A download can't go from “{from}” to “{to}”.", + "store.not_found": "That download, queue or tag is no longer in the list.", + "store.secret": "A login or cookie would have been saved outside the keyring, so nothing was saved.", + "store.main_queue": "The main queue can't be removed.", + "store.bad_value": "The download list holds a value this version doesn't know: {value}", + "store.closed": "The download list stopped working. Start Medlar again." +} diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs new file mode 100644 index 0000000..cb3e8ec --- /dev/null +++ b/crates/core/src/lib.rs @@ -0,0 +1,11 @@ +//! Medlar's engine, limits, queues, store and settings. No UI code. + +pub mod logging; +pub mod model; +pub mod paths; +pub mod redact; +pub mod settings; +pub mod store; +pub mod strings; + +pub const VERSION: &str = env!("CARGO_PKG_VERSION"); diff --git a/crates/core/src/logging.rs b/crates/core/src/logging.rs new file mode 100644 index 0000000..c0fb942 --- /dev/null +++ b/crates/core/src/logging.rs @@ -0,0 +1,111 @@ +//! Log file: rotated by size, every line redacted. + +use crate::redact; +use log::{Level, LevelFilter, Log, Metadata, Record}; +use std::fs::{File, OpenOptions}; +use std::io::{self, Write}; +use std::os::unix::fs::OpenOptionsExt; +use std::path::{Path, PathBuf}; +use std::sync::Mutex; + +const MAX_BYTES: u64 = 1024 * 1024; +/// Old files kept beside the current one: `medlar.log.1`, `.2`, `.3`. +const KEEP: u32 = 3; + +struct Rotating { + path: PathBuf, + max: u64, + file: File, + size: u64, +} + +impl Rotating { + fn open(path: &Path, max: u64) -> io::Result { + let file = OpenOptions::new().create(true).append(true).mode(0o600).open(path)?; + let size = file.metadata()?.len(); + Ok(Rotating { path: path.into(), max, file, size }) + } + + fn write(&mut self, line: &str) -> io::Result<()> { + if self.size > 0 && self.size + line.len() as u64 > self.max { + let old = |n: u32| PathBuf::from(format!("{}.{n}", self.path.display())); + for n in (1..KEEP).rev() { + let _ = std::fs::rename(old(n), old(n + 1)); + } + std::fs::rename(&self.path, old(1))?; + *self = Rotating::open(&self.path, self.max)?; + } + self.size += line.len() as u64; + self.file.write_all(line.as_bytes()) + } +} + +struct Logger(Mutex); + +impl Log for Logger { + fn enabled(&self, m: &Metadata) -> bool { + // Libraries speak up only when something is wrong. + m.level() <= Level::Warn || m.target().starts_with("medlar") + } + + fn log(&self, r: &Record) { + if self.enabled(r.metadata()) { + let line = line(r.level(), r.target(), &r.args().to_string()); + // A log that can't be written must not stop the app. + let _ = self.0.lock().unwrap_or_else(|e| e.into_inner()).write(&line); + } + } + + fn flush(&self) {} +} + +/// One entry is one line, whatever a server put into the message. +fn line(level: Level, target: &str, message: &str) -> String { + let message = redact::text(message).replace(|c: char| c.is_control(), " "); + format!("{} {level:5} {target}: {message}\n", chrono::Local::now().format("%Y-%m-%d %H:%M:%S%.3f")) +} + +/// Starts logging to `path`. Call once; a second call changes nothing. +pub fn init(path: &Path, level: LevelFilter) -> io::Result<()> { + let logger = Logger(Mutex::new(Rotating::open(path, MAX_BYTES)?)); + if log::set_boxed_logger(Box::new(logger)).is_ok() { + log::set_max_level(level); + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn rotates_and_keeps_three() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("medlar.log"); + let mut log = Rotating::open(&path, 25).unwrap(); + for n in 0..12 { + log.write(&format!("line {n:04}\n")).unwrap(); + } + assert_eq!(std::fs::read_to_string(&path).unwrap(), "line 0010\nline 0011\n"); + assert_eq!(std::fs::read_to_string(dir.path().join("medlar.log.1")).unwrap(), "line 0008\nline 0009\n"); + assert!(dir.path().join("medlar.log.3").exists()); + assert!(!dir.path().join("medlar.log.4").exists()); + assert!(std::fs::metadata(&path).unwrap().len() <= 25); + } + + #[test] + fn continues_an_existing_file() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("medlar.log"); + Rotating::open(&path, 25).unwrap().write("first\n").unwrap(); + Rotating::open(&path, 25).unwrap().write("second\n").unwrap(); + assert_eq!(std::fs::read_to_string(&path).unwrap(), "first\nsecond\n"); + } + + #[test] + fn entries_are_one_redacted_line() { + let l = line(Level::Info, "medlar_core", "name evil\n2026 ERROR fake https://h/f?key=abc"); + assert_eq!(l.matches('\n').count(), 1); + assert!(l.ends_with("name evil 2026 ERROR fake https://h/f?…\n"), "{l}"); + } +} diff --git a/crates/core/src/model.rs b/crates/core/src/model.rs new file mode 100644 index 0000000..3068b95 --- /dev/null +++ b/crates/core/src/model.rs @@ -0,0 +1,284 @@ +//! What Medlar remembers: tasks, parts, queues, tags and hosts. +//! +//! Secrets (cookies, logins, tokens) never appear here. `secret` fields hold the name of a keyring item. + +use serde::{Deserialize, Serialize}; +use std::path::PathBuf; + +pub type TaskId = i64; +pub type QueueId = i64; +pub type TagId = i64; +/// Milliseconds since 1970. +pub type Time = i64; + +/// The queue that always exists. +pub const MAIN_QUEUE: QueueId = 1; + +/// An enum stored by name. +macro_rules! named_enum { + ($(#[$meta:meta])* $name:ident { $($(#[$vmeta:meta])* $variant:ident = $text:literal),* $(,)? }) => { + $(#[$meta])* + #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] + pub enum $name { $($(#[$vmeta])* #[serde(rename = $text)] $variant),* } + + impl $name { + pub const ALL: &'static [$name] = &[$($name::$variant),*]; + + pub fn name(self) -> &'static str { + match self { $($name::$variant => $text),* } + } + + pub fn from_name(name: &str) -> Option<$name> { + $name::ALL.iter().copied().find(|v| v.name() == name) + } + } + }; +} + +named_enum!( + /// What a task is doing. The catalogue holds its label under `state.`. + State { + /// Ready, until its queue has room. + Queued = "queued", + /// Held back by a time, another task, a retry or the network. + Waiting = "waiting", + Running = "running", + Paused = "paused", + /// Comparing the checksum. + Checking = "checking", + /// Naming, unpacking or converting the finished file. + Finishing = "finishing", + Done = "done", + Failed = "failed", + /// Was done, but the file is no longer there. + Gone = "gone", + } +); + +impl State { + /// The states this one may move to. + pub fn moves(self) -> &'static [State] { + use State::*; + match self { + Queued => &[Running, Waiting, Paused, Failed], + Waiting => &[Queued, Running, Paused, Failed], + // Queued again: the app was killed, or the task gave up its place. + Running => &[Queued, Waiting, Paused, Checking, Finishing, Failed], + Paused => &[Queued, Waiting, Failed], + Checking => &[Queued, Paused, Finishing, Failed], + Finishing => &[Queued, Done, Failed], + // Queued: download again. Checking: check the file again. + Done => &[Queued, Checking, Gone], + Failed => &[Queued, Waiting, Paused], + Gone => &[Queued, Done], + } + } + + pub fn can_become(self, to: State) -> bool { + self.moves().contains(&to) + } +} + +named_enum!( + /// Share of bandwidth and connections. + Priority { + Lowest = "lowest", + Low = "low", + Normal = "normal", + High = "high", + Highest = "highest", + /// Everything else waits. + Only = "only", + } +); + +named_enum!( + /// Why a task failed. Filled by S007; each kind has a label under `error.`. + ErrorKind {} +); + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct TaskError { + /// None for a kind this version doesn't know. + pub kind: Option, + /// Plain words, already free of secrets. + pub detail: String, +} + +/// A file to download. `id` is 0 until the store adds it. +#[derive(Debug, Clone, PartialEq)] +pub struct Task { + pub id: TaskId, + pub url: String, + pub mirrors: Vec, + /// The page the link came from, opened to get a new link. + pub source_page: Option, + pub name: String, + pub folder: PathBuf, + /// Changed through `Store::set_state` only. + pub state: State, + /// Changed through `Store::set_state` only. + pub error: Option, + pub queue: QueueId, + /// Order inside the queue. + pub position: f64, + pub priority: Priority, + pub size: Option, + pub downloaded: u64, + pub mime: Option, + pub etag: Option, + pub last_modified: Option, + /// `sha256:…`. + pub checksum: Option, + pub max_parts: Option, + /// Bytes per second. + pub speed_limit: Option, + /// Cookies and authorization are refused here; they go to the keyring. + pub headers: Vec<(String, String)>, + pub proxy: Option, + /// Keyring item with the cookies and login. + pub secret: Option, + pub after: Option, + pub start_at: Option