knowledge-search

git clone git://archive.git.mtrnord.blog/MTRNord/knowledge-search.git
Log | Files | Refs | README | LICENSE

commit 38026299528dd7c9fa15cef029675a08b58a8c06
parent 089a5b99a29d7aeca51ebba9cc8a484a6d021afb
Author: MTRNord <mtrnord1@gmail.com>
Date:   Fri, 24 Mar 2023 01:13:55 +0100

feat(matrix-indexer): Add config parsing using kdl-rs

Diffstat:
M.gitignore | 5+++--
MCargo.lock | 262+++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------
Mcrates/matrix-indexer/Cargo.toml | 4+++-
Acrates/matrix-indexer/config.example.kdl | 13+++++++++++++
Acrates/matrix-indexer/src/config.rs | 197+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mcrates/matrix-indexer/src/main.rs | 35++++++++++++++++++++++++++---------
Mcrates/matrix-indexer/src/matrix.rs | 18+++++++++++++-----
Mcrates/utils/src/lib.rs | 12++++++++----
8 files changed, 447 insertions(+), 99 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,3 +1,4 @@ /target /data -/matrix_data -\ No newline at end of file +matrix_data +config.kdl +\ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock @@ -134,7 +134,7 @@ version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e4655ae1a7b0cdf149156f780c5bf3f1352bc53cbd9e0a361a7ef7b22947e965" dependencies = [ - "proc-macro2 1.0.52", + "proc-macro2 1.0.53", "quote 1.0.26", "syn 1.0.109", ] @@ -145,9 +145,9 @@ version = "0.1.67" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "86ea188f25f0255d8f92797797c97ebf5631fa88178beb1a46fdf5622c9a00e4" dependencies = [ - "proc-macro2 1.0.52", + "proc-macro2 1.0.53", "quote 1.0.26", - "syn 2.0.5", + "syn 2.0.8", ] [[package]] @@ -182,7 +182,7 @@ checksum = "349f8ccfd9221ee7d1f3d4b33e1f8319b3a81ed8f61f2ea40b37b859794b4491" dependencies = [ "async-trait", "axum-core", - "bitflags 1.3.2", + "bitflags", "bytes", "futures-util", "http", @@ -273,12 +273,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] -name = "bitflags" -version = "2.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "487f1e0fcbe47deb8b0574e646def1c903389d95241dd1bbcc6ce4a715dfc0c1" - -[[package]] name = "blake3" version = "1.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -404,11 +398,11 @@ dependencies = [ [[package]] name = "clap" -version = "4.1.11" +version = "4.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42dfd32784433290c51d92c438bb72ea5063797fc3cc9a21a8c4346bebbb2098" +checksum = "3c911b090850d79fc64fe9ea01e28e465f65e821e08813ced95bced72f7a8a9b" dependencies = [ - "bitflags 2.0.2", + "bitflags", "clap_derive", "clap_lex", "is-terminal", @@ -419,15 +413,14 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.1.9" +version = "4.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fddf67631444a3a3e3e5ac51c36a5e01335302de677bd78759eaa90ab1f46644" +checksum = "9a932373bab67b984c790ddf2c9ca295d8e3af3b7ef92de5a5bacdccdee4b09b" dependencies = [ "heck", - "proc-macro-error", - "proc-macro2 1.0.52", + "proc-macro2 1.0.53", "quote 1.0.26", - "syn 1.0.109", + "syn 2.0.8", ] [[package]] @@ -445,7 +438,7 @@ version = "0.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" dependencies = [ - "bitflags 1.3.2", + "bitflags", ] [[package]] @@ -587,7 +580,7 @@ checksum = "109c1ca6e6b7f82cc233a97004ea8ed7ca123a9af07a8230878fcfda9b158bf0" dependencies = [ "fnv", "ident_case", - "proc-macro2 1.0.52", + "proc-macro2 1.0.53", "quote 1.0.26", "strsim", "syn 1.0.109", @@ -642,7 +635,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1f91d4cfa921f1c05904dc3c57b4a32c38aed3340cce209f3a6fd1478babafc4" dependencies = [ "darling", - "proc-macro2 1.0.52", + "proc-macro2 1.0.53", "quote 1.0.26", "syn 1.0.109", ] @@ -689,7 +682,7 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3bf95dc3f046b9da4f2d51833c0d3547d8564ef6910f5c1ed130306a75b92886" dependencies = [ - "proc-macro2 1.0.52", + "proc-macro2 1.0.53", "quote 1.0.26", "syn 1.0.109", ] @@ -877,7 +870,7 @@ version = "0.3.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3eb14ed937631bd8b8b8977f2c198443447a8355b6e3ca599f38c975e5a963b6" dependencies = [ - "proc-macro2 1.0.52", + "proc-macro2 1.0.53", "quote 1.0.26", "syn 1.0.109", ] @@ -1282,6 +1275,12 @@ dependencies = [ ] [[package]] +name = "is_ci" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "616cde7c720bb2bb5824a224687d8f77bfd38922027f01d825cd7453be5099fb" + +[[package]] name = "itertools" version = "0.10.5" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1324,6 +1323,17 @@ dependencies = [ ] [[package]] +name = "kdl" +version = "4.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "062c875482ccb676fd40c804a40e3824d4464c18c364547456d1c8e8e951ae47" +dependencies = [ + "miette", + "nom", + "thiserror", +] + +[[package]] name = "knowledge-search" version = "0.0.0" dependencies = [ @@ -1396,13 +1406,15 @@ version = "0.0.0" dependencies = [ "async-channel", "cfg-if", - "clap", "color-eyre", "futures", + "kdl", "lazy_static", "matrix-sdk", + "miette", "serde", "serde_json", + "thiserror", "tokio", "tracing", "tracing-subscriber", @@ -1604,12 +1616,49 @@ dependencies = [ ] [[package]] +name = "miette" +version = "5.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07749fb52853e739208049fb513287c6f448de9103dfa78b05ae01f2fc5809bb" +dependencies = [ + "backtrace", + "is-terminal", + "miette-derive", + "once_cell", + "owo-colors", + "supports-color", + "supports-hyperlinks", + "supports-unicode", + "terminal_size", + "textwrap", + "thiserror", + "unicode-width", +] + +[[package]] +name = "miette-derive" +version = "5.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a07ad93a80d1b92bb44cb42d7c49b49c9aab1778befefad49cceb5e4c5bf460" +dependencies = [ + "proc-macro2 1.0.53", + "quote 1.0.26", + "syn 1.0.109", +] + +[[package]] name = "mime" version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" [[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + +[[package]] name = "miniz_oxide" version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1637,6 +1686,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5ce46fe64a9d73be07dcbe690a38ce1b293be448fd8ce1e6c1b8062c9f72c6a" [[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + +[[package]] name = "nu-ansi-term" version = "0.46.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1812,7 +1871,7 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "069bdb1e05adc7a8990dce9cc75370895fbe4e3d58b9b73bf1aee56359344a55" dependencies = [ - "proc-macro2 1.0.52", + "proc-macro2 1.0.53", "quote 1.0.26", "syn 1.0.109", ] @@ -1862,7 +1921,7 @@ version = "0.1.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c8646e95016a7a6c4adea95bafa8a16baab64b583356217f2c85db4a39d9a86" dependencies = [ - "proc-macro2 1.0.52", + "proc-macro2 1.0.53", "syn 1.0.109", ] @@ -1877,30 +1936,6 @@ dependencies = [ ] [[package]] -name = "proc-macro-error" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" -dependencies = [ - "proc-macro-error-attr", - "proc-macro2 1.0.52", - "quote 1.0.26", - "syn 1.0.109", - "version_check", -] - -[[package]] -name = "proc-macro-error-attr" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" -dependencies = [ - "proc-macro2 1.0.52", - "quote 1.0.26", - "version_check", -] - -[[package]] name = "proc-macro2" version = "0.4.30" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1911,9 +1946,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.52" +version = "1.0.53" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d0e1ae9e836cc3beddd63db0df682593d7e2d3d891ae8c9083d2113e1744224" +checksum = "ba466839c78239c09faf015484e5cc04860f88242cff4d03eb038f04b4699b73" dependencies = [ "unicode-ident", ] @@ -1958,7 +1993,7 @@ checksum = "4ea9b0f8cbe5e15a8a042d030bd96668db28ecb567ec37d691971ff5731d2b1b" dependencies = [ "anyhow", "itertools", - "proc-macro2 1.0.52", + "proc-macro2 1.0.53", "quote 1.0.26", "syn 1.0.109", ] @@ -1987,7 +2022,7 @@ version = "1.0.26" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc" dependencies = [ - "proc-macro2 1.0.52", + "proc-macro2 1.0.53", ] [[package]] @@ -2182,7 +2217,7 @@ version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" dependencies = [ - "bitflags 1.3.2", + "bitflags", ] [[package]] @@ -2369,7 +2404,7 @@ checksum = "0f82e91eb61cd86d9287303133ee55b54618eccb75a522cc22a42c15f5bda340" dependencies = [ "once_cell", "proc-macro-crate", - "proc-macro2 1.0.52", + "proc-macro2 1.0.53", "quote 1.0.26", "ruma-identifiers-validation", "serde", @@ -2379,9 +2414,9 @@ dependencies = [ [[package]] name = "rustc-demangle" -version = "0.1.21" +version = "0.1.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342" +checksum = "d4a36c42d1873f9a77c53bde094f9664d9891bc604a45b4798fd2c389ed12e5b" [[package]] name = "rustix" @@ -2389,7 +2424,7 @@ version = "0.36.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "db4165c9963ab29e422d6c26fbc1d37f15bace6b2810221f9d925023480fcf0e" dependencies = [ - "bitflags 1.3.2", + "bitflags", "errno", "io-lifetimes", "libc", @@ -2470,9 +2505,9 @@ version = "1.0.158" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e801c1712f48475582b7696ac71e0ca34ebb30e09338425384269d9717c62cad" dependencies = [ - "proc-macro2 1.0.52", + "proc-macro2 1.0.53", "quote 1.0.26", - "syn 2.0.5", + "syn 2.0.8", ] [[package]] @@ -2578,6 +2613,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" [[package]] +name = "smawk" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f67ad224767faa3c7d8b6d91985b78e70a1324408abcb1cfcc2be4c06bc06043" + +[[package]] name = "socket2" version = "0.4.9" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2615,6 +2656,34 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601" [[package]] +name = "supports-color" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4950e7174bffabe99455511c39707310e7e9b440364a2fcb1cc21521be57b354" +dependencies = [ + "is-terminal", + "is_ci", +] + +[[package]] +name = "supports-hyperlinks" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b4806e0b03b9906e76b018a5d821ebf198c8e9dc0829ed3328eeeb5094aed60" +dependencies = [ + "is-terminal", +] + +[[package]] +name = "supports-unicode" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b6c2cb240ab5dd21ed4906895ee23fe5a48acdbd15a3ce388e7b62a9b66baf7" +dependencies = [ + "is-terminal", +] + +[[package]] name = "syn" version = "0.15.44" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2631,18 +2700,18 @@ version = "1.0.109" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" dependencies = [ - "proc-macro2 1.0.52", + "proc-macro2 1.0.53", "quote 1.0.26", "unicode-ident", ] [[package]] name = "syn" -version = "2.0.5" +version = "2.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89c2d1c76a26822187a1fbb5964e3fff108bc208f02e820ab9dac1234f6b388a" +checksum = "bcc02725fd69ab9f26eab07fad303e2497fad6fb9eba4f96c4d1687bdf704ad9" dependencies = [ - "proc-macro2 1.0.52", + "proc-macro2 1.0.53", "quote 1.0.26", "unicode-ident", ] @@ -2659,7 +2728,7 @@ version = "0.12.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f36bdaa60a83aca3921b5259d5400cbf5e90fc51931376a9bd4a0eb79aa7210f" dependencies = [ - "proc-macro2 1.0.52", + "proc-macro2 1.0.53", "quote 1.0.26", "syn 1.0.109", "unicode-xid 0.2.4", @@ -2688,6 +2757,27 @@ dependencies = [ ] [[package]] +name = "terminal_size" +version = "0.1.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "633c1a546cee861a1a6d0dc69ebeca693bf4296661ba7852b9d21d159e0506df" +dependencies = [ + "libc", + "winapi", +] + +[[package]] +name = "textwrap" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7b3e525a49ec206798b40326a44121291b530c963cfb01018f63e135bac543d" +dependencies = [ + "smawk", + "unicode-linebreak", + "unicode-width", +] + +[[package]] name = "thiserror" version = "1.0.40" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2702,9 +2792,9 @@ version = "1.0.40" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" dependencies = [ - "proc-macro2 1.0.52", + "proc-macro2 1.0.53", "quote 1.0.26", - "syn 2.0.5", + "syn 2.0.8", ] [[package]] @@ -2768,7 +2858,7 @@ version = "1.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d266c00fde287f55d3f1c3e96c500c362a2b8c695076ec180f27918820bc6df8" dependencies = [ - "proc-macro2 1.0.52", + "proc-macro2 1.0.53", "quote 1.0.26", "syn 1.0.109", ] @@ -2826,9 +2916,9 @@ checksum = "3ab8ed2edee10b50132aed5f331333428b011c99402b5a534154ed15746f9622" [[package]] name = "toml_edit" -version = "0.19.7" +version = "0.19.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc18466501acd8ac6a3f615dd29a3438f8ca6bb3b19537138b3106e575621274" +checksum = "239410c8609e8125456927e6707163a3b1fdb40561e4b803bc041f466ccfdc13" dependencies = [ "indexmap", "toml_datetime", @@ -2874,7 +2964,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5bf5e9b9c0f7e0a7c027dcfaba7b2c60816c7049171f679d99ee2ff65d0de8c4" dependencies = [ "prettyplease", - "proc-macro2 1.0.52", + "proc-macro2 1.0.53", "prost-build", "quote 1.0.26", "syn 1.0.109", @@ -2930,7 +3020,7 @@ version = "0.1.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4017f8f45139870ca7e672686113917c71c7a6e02d4924eda67186083c03081a" dependencies = [ - "proc-macro2 1.0.52", + "proc-macro2 1.0.53", "quote 1.0.26", "syn 1.0.109", ] @@ -3015,6 +3105,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" [[package]] +name = "unicode-linebreak" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c5faade31a542b8b35855fff6e8def199853b2da8da256da52f52f1316ee3137" +dependencies = [ + "hashbrown", + "regex", +] + +[[package]] name = "unicode-normalization" version = "0.1.22" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -3024,6 +3124,12 @@ dependencies = [ ] [[package]] +name = "unicode-width" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" + +[[package]] name = "unicode-xid" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -3181,7 +3287,7 @@ dependencies = [ "bumpalo", "log", "once_cell", - "proc-macro2 1.0.52", + "proc-macro2 1.0.53", "quote 1.0.26", "syn 1.0.109", "wasm-bindgen-shared", @@ -3215,7 +3321,7 @@ version = "0.2.84" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2aff81306fcac3c7515ad4e177f521b5c9a15f2b08f4e32d823066102f35a5f6" dependencies = [ - "proc-macro2 1.0.52", + "proc-macro2 1.0.53", "quote 1.0.26", "syn 1.0.109", "wasm-bindgen-backend", @@ -3403,9 +3509,9 @@ checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" [[package]] name = "winnow" -version = "0.3.6" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23d020b441f92996c80d94ae9166e8501e59c7bb56121189dc9eab3bd8216966" +checksum = "deac0939bd6e4f24ab5919fbf751c97a8cfc8543bb083a305ed5c0c10bb241d1" dependencies = [ "memchr", ] @@ -3446,7 +3552,7 @@ version = "1.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "44bf07cb3e50ea2003396695d58bf46bc9887a1f362260446fad6bc4e79bd36c" dependencies = [ - "proc-macro2 1.0.52", + "proc-macro2 1.0.53", "quote 1.0.26", "syn 1.0.109", "synstructure", diff --git a/crates/matrix-indexer/Cargo.toml b/crates/matrix-indexer/Cargo.toml @@ -8,13 +8,15 @@ edition = "2021" [dependencies] async-channel = "1.8.0" cfg-if = "1.0.0" -clap = { version = "4.1.11", features = ["derive"] } color-eyre = "0.6.2" futures = "0.3.27" +kdl = "4.6.0" lazy_static = "1.4.0" matrix-sdk = { version = "0.6.2", features = ["experimental-timeline", "eyre", "rustls-tls", "sled"], default-features = false } +miette = { version = "5.6.0", features = ["fancy"] } serde = { version = "1.0.158", features = ["derive"] } serde_json = "1.0.94" +thiserror = "1.0.40" tokio = { version = "1.26.0", features = ["full"] } tracing = "0.1.37" tracing-subscriber = "0.3.16" diff --git a/crates/matrix-indexer/config.example.kdl b/crates/matrix-indexer/config.example.kdl @@ -0,0 +1,12 @@ +// Settings related to the matrix bot +matrix { + // The URL of the homeserver. + homeserver_url "https://matrixsomewhere.com" + // You can either use username and password or a token. + // The config will be automatically rewritten after first login. + username "@mxid:matrix.example" + password "abcdef" +} + +// Usually you can just keep this as is +indradb_address "grpc://127.0.0.1:27615" +\ No newline at end of file diff --git a/crates/matrix-indexer/src/config.rs b/crates/matrix-indexer/src/config.rs @@ -0,0 +1,197 @@ +use kdl::KdlDocument; +use miette::{Diagnostic, NamedSource, SourceSpan}; +use thiserror::Error; + +pub struct Config { + pub homeserver_url: String, + pub indradb_endpoint: String, + pub auth_data: AuthData, +} + +pub enum AuthData { + UsernamePassword(String, String), + AccessToken(String, String, String), +} + +#[derive(Debug, Error, Diagnostic)] +#[error("Incorrect Config")] +#[diagnostic()] +pub struct MissingFieldError { + #[source_code] + src: NamedSource, + + #[help] + advice: String, + + #[label] + snippet: SourceSpan, +} + +fn parse(input: &str) -> miette::Result<KdlDocument> { + Ok(input.parse::<KdlDocument>()?) +} + +fn missing_field(source: String, snippet: &SourceSpan, help: &str) { + let error: miette::Result<()> = Err(MissingFieldError { + src: NamedSource::new("config.kdl", source), + snippet: *snippet, + advice: help.to_string(), + } + .into()); + + // This is stupid. But oh well. + if let Err(error) = error { + eprintln!("{error:?}"); + std::process::exit(1); + } +} + +#[allow(clippy::unwrap_used)] +#[allow(clippy::too_many_lines)] +pub fn load() -> Config { + let source = std::fs::read_to_string("config.kdl") + .expect("Unable to open config.kdl file. Is it present?"); + match parse(&source) { + Ok(config) => { + let matrix = config.get("matrix"); + if let Some(matrix) = matrix { + if let Some(matrix_children) = matrix.children() { + let homeserver_url_entry = matrix_children.get("homeserver_url"); + let homeserver_url = if let Some(homeserver_url_entry) = homeserver_url_entry { + if let Some(homeserver_url) = homeserver_url_entry.entries().first() { + if let Some(homeserver_url) = homeserver_url.value().as_string() { + homeserver_url.to_string() + } else { + missing_field(source, homeserver_url.span(),"\"homeserver_url\" field has incorrect type. Make sure it is actually a string. Try adding quotes."); + unreachable!(); + } + } else { + missing_field(source, homeserver_url_entry.span(),"\"homeserver_url\" field has no value. Make sure it is actually a string. Try adding quotes."); + unreachable!(); + } + } else { + missing_field(source,matrix_children.span(),"Missing \"homeserver_url\" field. See the example config for how to define it"); + unreachable!(); + }; + let username_entry = matrix_children.get("username"); + let username = if let Some(username_entry) = username_entry { + if let Some(username) = username_entry.entries().first() { + if let Some(username) = username.value().as_string() { + username.to_string() + } else { + missing_field(source, username.span(),"\"username\" field has incorrect type. Make sure it is actually a string. Try adding quotes."); + unreachable!(); + } + } else { + missing_field(source, username_entry.span(),"\"username\" field has no value. Make sure it is actually a string. Try adding quotes."); + unreachable!(); + } + } else { + missing_field(source,matrix_children.span(),"Missing \"username\" field. See the example config for how to define it"); + unreachable!(); + }; + let password_entry = matrix_children.get("password"); + let password = if let Some(password_entry) = password_entry { + if let Some(password) = password_entry.entries().first() { + if let Some(password) = password.value().as_string() { + Some(password.to_string()) + } else { + missing_field(source, password.span(),"\"password\" field has incorrect type. Make sure it is actually a string. Try adding quotes."); + unreachable!(); + } + } else { + missing_field(source, password_entry.span(),"\"password\" field has no value. Make sure it is actually a string. Try adding quotes."); + unreachable!(); + } + } else { + None + }; + + let indradb_address_entry = config.get("indradb_address"); + let indradb_endpoint = if let Some(indradb_address_entry) = + indradb_address_entry + { + if let Some(indradb_address) = indradb_address_entry.entries().first() { + if let Some(indradb_address) = indradb_address.value().as_string() { + indradb_address.to_string() + } else { + missing_field(source, indradb_address.span(),"\"indradb_address\" field has incorrect type. Make sure it is actually a string. Try adding quotes."); + unreachable!(); + } + } else { + missing_field(source, indradb_address_entry.span(),"\"indradb_address\" field has no value. Make sure it is actually a string. Try adding quotes."); + unreachable!(); + } + } else { + missing_field(source,config.span(),"Missing \"indradb_address\" field. See the example config for how to define it"); + unreachable!(); + }; + + let access_token_entry = matrix_children.get("access_token"); + let access_token = if let Some(access_token_entry) = access_token_entry { + if let Some(access_token) = access_token_entry.entries().first() { + if let Some(access_token) = access_token.value().as_string() { + Some(access_token.to_string()) + } else { + missing_field(source, access_token.span(),"\"access_token\" field has incorrect type. Please reset it to be username and password."); + unreachable!(); + } + } else { + missing_field(source, access_token_entry.span(),"\"access_token\" field has no value. Please reset it to be username and password."); + unreachable!(); + } + } else { + None + }; + let device_id_entry = matrix_children.get("device_id"); + let device_id = if let Some(device_id_entry) = device_id_entry { + if let Some(device_id) = device_id_entry.entries().first() { + if let Some(device_id) = device_id.value().as_string() { + Some(device_id.to_string()) + } else { + missing_field(source, device_id.span(),"\"device_id\" field has incorrect type. Please reset it to be username and password."); + unreachable!(); + } + } else { + missing_field(source, device_id_entry.span(),"\"device_id\" field has no value. Please reset it to be username and password."); + unreachable!(); + } + } else { + None + }; + + let auth_data = if let Some(password) = password { + AuthData::UsernamePassword(username, password) + } else { + // In theory this is safe :D + AuthData::AccessToken(username, access_token.unwrap(), device_id.unwrap()) + }; + + Config { + homeserver_url, + indradb_endpoint, + auth_data, + } + } else { + missing_field( + source, + matrix.span(), + "Missing \"matrix\" choldren. See the example config for how to define it", + ); + unreachable!(); + } + } else { + missing_field( + source, + config.span(), + "Missing \"matrix\" section. See the example config for how to define it", + ); + unreachable!(); + } + } + Err(error) => { + eprintln!("{error:?}"); + std::process::exit(1); + } + } +} diff --git a/crates/matrix-indexer/src/main.rs b/crates/matrix-indexer/src/main.rs @@ -26,25 +26,42 @@ use color_eyre::Result; -use clap::Parser; +use config::load; use matrix::IndexerBot; +mod config; mod indradb_utils; mod matrix; -/// An indexer for the knowledge search that indexes matrix -#[derive(Parser, Debug)] -#[command(author, version, about, long_about = None)] -struct Args {} - #[tokio::main] async fn main() -> Result<()> { - color_eyre::install()?; tracing_subscriber::fmt::init(); - let _args = Args::parse(); + + // Errors in the config will crash directly. + let config = load(); // TODO: config which rewrites itself to have the data after login - let mut bot = IndexerBot::new(String::new(), String::new(), String::new()).await?; + let mut bot = match config.auth_data { + config::AuthData::UsernamePassword(mxid, password) => { + IndexerBot::new( + config.homeserver_url, + mxid, + password, + config.indradb_endpoint, + ) + .await? + } + config::AuthData::AccessToken(mxid, access_token, device_id) => { + IndexerBot::relogin( + config.homeserver_url, + mxid, + access_token, + device_id, + config.indradb_endpoint, + ) + .await? + } + }; bot.start_processing().await?; Ok(()) diff --git a/crates/matrix-indexer/src/matrix.rs b/crates/matrix-indexer/src/matrix.rs @@ -42,9 +42,11 @@ impl IndexerBot { Ok(client_builder.build().await?) } - async fn get_indexer_client() -> Result<(utils::indradb_proto::Client, Identifiers)> { + async fn get_indexer_client( + endpoint: String, + ) -> Result<(utils::indradb_proto::Client, Identifiers)> { info!("Trying to connect to indradb"); - let mut indexer_client = utils::get_client_retrying().await?; + let mut indexer_client = utils::get_client_retrying(endpoint).await?; indexer_client.ping().await?; let room_type = utils::indradb::Identifier::new("matrix_room")?; let room_id_type = utils::indradb::Identifier::new("room_id")?; @@ -85,7 +87,12 @@ impl IndexerBot { )) } - pub async fn new(homeserver_url: String, user_id: String, password: String) -> Result<Self> { + pub async fn new( + homeserver_url: String, + user_id: String, + password: String, + indra_endpoint: String, + ) -> Result<Self> { let client = IndexerBot::get_client(homeserver_url).await?; client .login_username(&user_id, &password) @@ -93,7 +100,7 @@ impl IndexerBot { .send() .await?; - let (indexer_client, identifiers) = IndexerBot::get_indexer_client().await?; + let (indexer_client, identifiers) = IndexerBot::get_indexer_client(indra_endpoint).await?; let client_clone = client.clone(); tokio::spawn(async move { @@ -117,6 +124,7 @@ impl IndexerBot { user_id: String, access_token: String, device_id: String, + indra_endpoint: String, ) -> Result<Self> { let client = IndexerBot::get_client(homeserver_url).await?; client @@ -128,7 +136,7 @@ impl IndexerBot { }) .await?; - let (indexer_client, identifiers) = IndexerBot::get_indexer_client().await?; + let (indexer_client, identifiers) = IndexerBot::get_indexer_client(indra_endpoint).await?; let client_clone = client.clone(); tokio::spawn(async move { diff --git a/crates/utils/src/lib.rs b/crates/utils/src/lib.rs @@ -28,18 +28,22 @@ pub use indradb; pub use indradb_proto; use tokio::time::{sleep, Duration}; -pub async fn get_client() -> Result<indradb_proto::Client, indradb_proto::ClientError> { - let mut client = indradb_proto::Client::new("grpc://127.0.0.1:27615".try_into()?).await?; +pub async fn get_client( + endpoint: String, +) -> Result<indradb_proto::Client, indradb_proto::ClientError> { + let mut client = indradb_proto::Client::new(endpoint.try_into()?).await?; client.ping().await?; Ok(client) } -pub async fn get_client_retrying() -> Result<indradb_proto::Client, indradb_proto::ClientError> { +pub async fn get_client_retrying( + endpoint: String, +) -> Result<indradb_proto::Client, indradb_proto::ClientError> { let mut retry_count = 10u8; let mut last_err = Option::<indradb_proto::ClientError>::None; while retry_count > 0 { - match get_client().await { + match get_client(endpoint.clone()).await { Ok(client) => return Ok(client), Err(err) => { last_err = Some(err);