feat: with crunch file support

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2023-09-24 12:18:29 +02:00
parent 4e70adfb38
commit 38f36f1aa5
Signed by: kjuulh
GPG Key ID: 9AA7BC13CE474394
7 changed files with 621 additions and 45 deletions

317
Cargo.lock generated
View File

@ -19,9 +19,9 @@ checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "aho-corasick"
version = "1.0.5"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c378d78423fdad8089616f827526ee33c19f2fddbd5de1629152c9593ba4783"
checksum = "ea5d730647d4fadd988536d06fecce94b7b4f2a7efdae548f1cf4b63205518ab"
dependencies = [
"memchr",
]
@ -32,6 +32,54 @@ version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299"
[[package]]
name = "anstream"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1f58811cfac344940f1a400b6e6231ce35171f614f26439e80f8c1465c5cc0c"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b84bf0a05bbb2a83e5eb6fa36bb6e87baa08193c35ff52bbf6b38d8af2890e46"
[[package]]
name = "anstyle-parse"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "938874ff5980b03a87c5524b3ae5b59cf99b1d6bc836848df7bc5ada9643c333"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58f54d10c6dfa51283a066ceab3ec1ab78d13fae00aa49243a45e4571fb79dfd"
dependencies = [
"anstyle",
"windows-sys",
]
[[package]]
name = "anyhow"
version = "1.0.75"
@ -72,7 +120,7 @@ checksum = "bc00ceb34980c03614e35a3a4e218276a0a824e911d07651cd0d858a51e8c0f0"
dependencies = [
"proc-macro2 1.0.67",
"quote 1.0.33",
"syn 2.0.35",
"syn 2.0.37",
]
[[package]]
@ -285,11 +333,45 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123"
dependencies = [
"bitflags 1.3.2",
"clap_lex",
"indexmap",
"clap_lex 0.2.4",
"indexmap 1.9.3",
"textwrap",
]
[[package]]
name = "clap"
version = "4.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1d7b8d5ec32af0fadc644bf1fd509a688c2103b185644bb1e29d164e0703136"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5179bb514e4d7c2051749d8fcefa2ed6d06a9f4e6d69faf3805f5d80b8cf8d56"
dependencies = [
"anstream",
"anstyle",
"clap_lex 0.5.1",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0862016ff20d69b84ef8247369fabf5c008a7417002411897d40ee1f4532b873"
dependencies = [
"heck 0.4.1",
"proc-macro2 1.0.67",
"quote 1.0.33",
"syn 2.0.37",
]
[[package]]
name = "clap_lex"
version = "0.2.4"
@ -299,6 +381,18 @@ dependencies = [
"os_str_bytes",
]
[[package]]
name = "clap_lex"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd7cc57abe963c6d3b9d8be5b06ba7c8957a930305ca90304f24ef040aa6f961"
[[package]]
name = "colorchoice"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
[[package]]
name = "concurrent-queue"
version = "2.2.0"
@ -349,7 +443,7 @@ dependencies = [
"atty",
"cast",
"ciborium",
"clap",
"clap 3.2.25",
"criterion-plot",
"itertools 0.10.5",
"lazy_static",
@ -437,6 +531,19 @@ dependencies = [
"uuid",
]
[[package]]
name = "crunch-cli"
version = "0.1.0"
dependencies = [
"anyhow",
"async-trait",
"clap 4.4.4",
"thiserror",
"tokio",
"tracing",
"tracing-subscriber",
]
[[package]]
name = "crunch-envelope"
version = "0.1.0"
@ -454,6 +561,19 @@ dependencies = [
"thiserror",
]
[[package]]
name = "crunch-file"
version = "0.1.0"
dependencies = [
"anyhow",
"async-trait",
"crunch-traits",
"pretty_assertions",
"serde",
"tokio",
"toml_edit",
]
[[package]]
name = "crunch-in-memory"
version = "0.1.0"
@ -532,6 +652,12 @@ dependencies = [
"serde",
]
[[package]]
name = "diff"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8"
[[package]]
name = "digest"
version = "0.9.0"
@ -568,6 +694,12 @@ version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
[[package]]
name = "equivalent"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "errno"
version = "0.3.3"
@ -705,7 +837,7 @@ checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72"
dependencies = [
"proc-macro2 1.0.67",
"quote 1.0.33",
"syn 2.0.35",
"syn 2.0.37",
]
[[package]]
@ -777,6 +909,12 @@ version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
[[package]]
name = "hashbrown"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c6201b9ff9fd90a5a3bac2e56a830d0caa509576f0e503818ee82c181b3437a"
[[package]]
name = "heck"
version = "0.3.3"
@ -786,6 +924,12 @@ dependencies = [
"unicode-segmentation",
]
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]]
name = "hermit-abi"
version = "0.1.19"
@ -797,9 +941,9 @@ dependencies = [
[[package]]
name = "hermit-abi"
version = "0.3.2"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "443144c8cdadd93ebf52ddb4056d257f5b52c04d3c804e657d19eb73fc33668b"
checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7"
[[package]]
name = "idna"
@ -818,7 +962,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99"
dependencies = [
"autocfg",
"hashbrown",
"hashbrown 0.12.3",
]
[[package]]
name = "indexmap"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d5477fe2230a79769d8dc68e0eabf5437907c0457a5614a9e8dddb67f65eb65d"
dependencies = [
"equivalent",
"hashbrown 0.14.0",
]
[[package]]
@ -857,6 +1011,15 @@ dependencies = [
"either",
]
[[package]]
name = "itertools"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57"
dependencies = [
"either",
]
[[package]]
name = "itoa"
version = "1.0.9"
@ -1039,7 +1202,7 @@ version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43"
dependencies = [
"hermit-abi 0.3.2",
"hermit-abi 0.3.3",
"libc",
]
@ -1199,6 +1362,16 @@ version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "pretty_assertions"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af7cee1a6c8a5b9208b3cb1061f10c0cb689087b3d8ce85fb9d2dd7a29b6ba66"
dependencies = [
"diff",
"yansi",
]
[[package]]
name = "proc-macro2"
version = "0.4.30"
@ -1245,7 +1418,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb788126ea840817128183f8f603dce02cb7aea25c2a0b764359d8e20010702e"
dependencies = [
"bytes 0.4.12",
"heck",
"heck 0.3.3",
"itertools 0.8.2",
"log",
"multimap",
@ -1276,10 +1449,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "265baba7fabd416cf5078179f7d2cbeca4ce7a9041111900675ea7c4cb8a4c32"
dependencies = [
"anyhow",
"itertools 0.10.5",
"itertools 0.11.0",
"proc-macro2 1.0.67",
"quote 1.0.33",
"syn 2.0.35",
"syn 2.0.37",
]
[[package]]
@ -1357,9 +1530,9 @@ dependencies = [
[[package]]
name = "rayon"
version = "1.7.0"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d2df5196e37bcc87abebc0053e20787d73847bb33134a69841207dd0a47f03b"
checksum = "9c27db03db7734835b3f53954b534c91069375ce6ccaa2e065441e07d9b6cdb1"
dependencies = [
"either",
"rayon-core",
@ -1367,14 +1540,12 @@ dependencies = [
[[package]]
name = "rayon-core"
version = "1.11.0"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b8f95bd6966f5c87776639160a66bd8ab9895d9d4ab01ddba9fc60661aebe8d"
checksum = "5ce3fb6ad83f861aac485e76e1985cd109d9a3713802152be56c3b1f0e0658ed"
dependencies = [
"crossbeam-channel",
"crossbeam-deque",
"crossbeam-utils",
"num_cpus",
]
[[package]]
@ -1438,9 +1609,9 @@ checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76"
[[package]]
name = "rustix"
version = "0.38.13"
version = "0.38.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7db8590df6dfcd144d22afd1b83b36c21a18d7cbc1dc4bb5295a8712e9eb662"
checksum = "747c788e9ce8e92b12cd485c49ddf90723550b654b32508f979b71a7b1ecda4f"
dependencies = [
"bitflags 2.4.0",
"errno",
@ -1563,7 +1734,7 @@ checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2"
dependencies = [
"proc-macro2 1.0.67",
"quote 1.0.33",
"syn 2.0.35",
"syn 2.0.37",
]
[[package]]
@ -1594,7 +1765,16 @@ checksum = "8725e1dfadb3a50f7e5ce0b1a540466f6ed3fe7a0fca2ac2b8b831d31316bd00"
dependencies = [
"proc-macro2 1.0.67",
"quote 1.0.33",
"syn 2.0.35",
"syn 2.0.37",
]
[[package]]
name = "serde_spanned"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96426c9936fd7a0124915f9185ea1d20aa9445cc9821142f0a73bc9207a2e186"
dependencies = [
"serde",
]
[[package]]
@ -1657,9 +1837,9 @@ dependencies = [
[[package]]
name = "smallvec"
version = "1.11.0"
version = "1.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62bb4feee49fdd9f707ef802e22365a35de4b7b299de4763d44bfea899442ff9"
checksum = "942b4a808e05215192e39f4ab80813e599068285906cc91aa64f923db842bd5a"
[[package]]
name = "socket2"
@ -1686,6 +1866,12 @@ dependencies = [
"der",
]
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "subtle"
version = "2.5.0"
@ -1705,9 +1891,9 @@ dependencies = [
[[package]]
name = "syn"
version = "2.0.35"
version = "2.0.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59bf04c28bee9043ed9ea1e41afc0552288d3aba9c6efdd78903b802926f4879"
checksum = "7303ef2c05cd654186cb250d29049a24840ca25d2747c25c0381c8d9e2f582e8"
dependencies = [
"proc-macro2 1.0.67",
"quote 1.0.33",
@ -1757,7 +1943,7 @@ checksum = "49922ecae66cc8a249b77e68d1d0623c1b2c514f0060c27cdc68bd62a1219d35"
dependencies = [
"proc-macro2 1.0.67",
"quote 1.0.33",
"syn 2.0.35",
"syn 2.0.37",
]
[[package]]
@ -1772,9 +1958,9 @@ dependencies = [
[[package]]
name = "time"
version = "0.3.28"
version = "0.3.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17f6bb557fd245c28e6411aa56b6403c689ad95061f50e4be16c274e70a17e48"
checksum = "426f806f4089c493dcac0d24c29c01e2c38baf8e30f1b716ee37e83d200b18fe"
dependencies = [
"deranged",
"itoa",
@ -1785,15 +1971,15 @@ dependencies = [
[[package]]
name = "time-core"
version = "0.1.1"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7300fbefb4dadc1af235a9cef3737cea692a9d97e1b9cbcd4ebdae6f8868e6fb"
checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3"
[[package]]
name = "time-macros"
version = "0.2.14"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a942f44339478ef67935ab2bbaec2fb0322496cf3cbe84b261e06ac3814c572"
checksum = "4ad70d68dba9e1f8aceda7aa6711965dfec1cac869f311a51bd08b3a2ccbce20"
dependencies = [
"time-core",
]
@ -1850,7 +2036,7 @@ checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e"
dependencies = [
"proc-macro2 1.0.67",
"quote 1.0.33",
"syn 2.0.35",
"syn 2.0.37",
]
[[package]]
@ -1867,9 +2053,9 @@ dependencies = [
[[package]]
name = "tokio-util"
version = "0.7.8"
version = "0.7.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "806fe8c2c87eccc8b3267cbae29ed3ab2d0bd37fca70ab622e46aaa9375ddb7d"
checksum = "1d68074620f57a0b21594d9735eb2e98ab38b17f80d3fcb189fca266771ca60d"
dependencies = [
"bytes 1.5.0",
"futures-core",
@ -1878,6 +2064,28 @@ dependencies = [
"tokio",
]
[[package]]
name = "toml_datetime"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7cda73e2f1397b1262d6dfdcef8aafae14d1de7748d66822d3bfeeb6d03e5e4b"
dependencies = [
"serde",
]
[[package]]
name = "toml_edit"
version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ff63e60a958cefbb518ae1fd6566af80d9d4be430a33f3723dfc47d1d411d95"
dependencies = [
"indexmap 2.0.0",
"serde",
"serde_spanned",
"toml_datetime",
"winnow",
]
[[package]]
name = "tracing"
version = "0.1.37"
@ -1899,7 +2107,7 @@ checksum = "5f4f31f56159e98206da9efd823404b79b6ef3143b4a7ab76e67b1751b25a4ab"
dependencies = [
"proc-macro2 1.0.67",
"quote 1.0.33",
"syn 2.0.35",
"syn 2.0.37",
]
[[package]]
@ -1993,6 +2201,12 @@ dependencies = [
"percent-encoding",
]
[[package]]
name = "utf8parse"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "uuid"
version = "1.4.1"
@ -2057,7 +2271,7 @@ dependencies = [
"once_cell",
"proc-macro2 1.0.67",
"quote 1.0.33",
"syn 2.0.35",
"syn 2.0.37",
"wasm-bindgen-shared",
]
@ -2079,7 +2293,7 @@ checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b"
dependencies = [
"proc-macro2 1.0.67",
"quote 1.0.33",
"syn 2.0.35",
"syn 2.0.37",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
@ -2138,9 +2352,9 @@ checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596"
dependencies = [
"winapi",
]
@ -2217,6 +2431,21 @@ version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
[[package]]
name = "winnow"
version = "0.5.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c2e3184b9c4e92ad5167ca73039d0c42476302ab603e2fec4487511f38ccefc"
dependencies = [
"memchr",
]
[[package]]
name = "yansi"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec"
[[package]]
name = "zeroize"
version = "1.6.0"
@ -2234,5 +2463,5 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69"
dependencies = [
"proc-macro2 1.0.67",
"quote 1.0.33",
"syn 2.0.35",
"syn 2.0.37",
]

View File

@ -18,4 +18,9 @@ thiserror = {version = "1.0.48"}
async-trait = "0.1.73"
uuid = { version = "1.4.1", features = ["v4"]}
futures = "0.3.28"
nats = "0.24.0"
nats = "0.24.0"
clap = {version = "4.4.4", features = ["derive"]}
toml_edit = {version = "0.20.0",features = ["serde"]}
serde = {version = "1.0.88", features = ["derive"]}
pretty_assertions = "1.4.0"

View File

@ -0,0 +1,15 @@
[package]
name = "crunch-cli"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow.workspace = true
tracing.workspace = true
tokio.workspace = true
thiserror.workspace = true
async-trait.workspace = true
tracing-subscriber.workspace = true
clap.workspace = true

View File

@ -0,0 +1,78 @@
use clap::{Args, Parser, Subcommand, ValueEnum};
use tracing::Level;
#[derive(Parser, Clone)]
#[command(author, version, about, long_about = None, subcommand_required = true)]
struct Cli {
#[command(subcommand)]
commands: Commands,
#[command(flatten)]
global_args: GlobalArgs,
}
#[derive(Subcommand, Clone)]
enum Commands {
Generate {},
}
#[derive(Args, Clone)]
struct GlobalArgs {
#[arg(long, default_value = "none", global = true, help_heading = "Global")]
log: LogArg,
#[arg(
long,
default_value = ".crunch.toml",
global = true,
help_heading = "Global"
)]
crunch_file: String,
}
#[derive(Clone, ValueEnum)]
enum LogArg {
None,
Trace,
Debug,
Info,
Warn,
Error,
}
#[tokio::main]
async fn main() {
let cli = Cli::parse();
init_logging(&cli.global_args.log);
match &cli.commands {
Commands::Generate {} => {}
}
}
fn init_logging(log: &LogArg) {
match log {
LogArg::None => {}
LogArg::Trace => {
tracing_subscriber::fmt()
.with_max_level(Level::TRACE)
.init();
}
LogArg::Debug => {
tracing_subscriber::fmt()
.with_max_level(Level::DEBUG)
.init();
}
LogArg::Info => {
tracing_subscriber::fmt().with_max_level(Level::INFO).init();
}
LogArg::Warn => {
tracing_subscriber::fmt().with_max_level(Level::WARN).init();
}
LogArg::Error => {
tracing_subscriber::fmt()
.with_max_level(Level::ERROR)
.init();
}
}
}

View File

@ -0,0 +1,18 @@
[package]
name = "crunch-file"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
crunch-traits.workspace = true
anyhow.workspace = true
tokio.workspace = true
async-trait.workspace = true
toml_edit.workspace = true
serde.workspace = true
[dev-dependencies]
pretty_assertions.workspace = true

View File

@ -0,0 +1,223 @@
use serde::{Deserialize, Serialize};
use tokio::io::AsyncWriteExt;
use toml_edit::{value, Document};
#[derive(Debug)]
pub struct File {
doc: Document,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Config {
pub service: Service,
pub publish: Option<Vec<Publish>>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Service {
pub service: String,
pub domain: String,
pub codegen: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Publish {
#[serde(alias = "schema-path")]
pub schema_path: String,
#[serde(alias = "output-path")]
pub output_path: String,
}
#[allow(dead_code)]
impl File {
pub async fn parse_file(path: &std::path::Path) -> anyhow::Result<File> {
let file = tokio::fs::read_to_string(path).await?;
Self::parse(&file).await
}
pub async fn parse(content: &str) -> anyhow::Result<File> {
let config: Document = content.parse::<Document>()?;
Ok(File { doc: config })
}
pub async fn write_file(&self, path: &std::path::Path) -> anyhow::Result<()> {
let content = self.write().await?;
let mut file = tokio::fs::File::create(path).await?;
file.write_all(content.as_bytes()).await?;
file.sync_all().await?;
Ok(())
}
pub async fn write(&self) -> anyhow::Result<String> {
let content = self.doc.to_string();
Ok(content)
}
pub fn add_publish(&mut self, schema_path: &str, output_path: &str) -> &mut Self {
let mut publish = toml_edit::Table::new();
publish["schema-path"] = value(schema_path);
publish["output-path"] = value(output_path);
if !self.doc.contains_key("publish") {
self.doc["publish"] = toml_edit::array()
}
self.doc["publish"]
.as_array_of_tables_mut()
.expect("publish to be present and be array of tables [[publish]]")
.push(publish);
self
}
pub fn get_config(&self) -> anyhow::Result<Config> {
let content = self.doc.to_string();
let config: Config = toml_edit::de::from_str(&content)?;
Ok(config)
}
}
#[cfg(test)]
mod test {
use super::*;
#[tokio::test]
async fn test_can_write() -> anyhow::Result<()> {
let raw = r#"[service]
service = "my-service"
domain = "my-domain"
codegen = ["rust"]
[[publish]]
schema-path = "schemas/crunch"
output-path = "src/crunch"
"#;
let output = File::parse(raw).await?.write().await?;
pretty_assertions::assert_eq!(output, raw);
Ok(())
}
#[tokio::test]
async fn test_can_add_publish() -> anyhow::Result<()> {
let raw = r#"[service]
service = "my-service"
domain = "my-domain"
codegen = ["rust"]
[[publish]]
schema-path = "schemas/crunch"
output-path = "src/crunch"
"#;
let expected = r#"[service]
service = "my-service"
domain = "my-domain"
codegen = ["rust"]
[[publish]]
schema-path = "schemas/crunch"
output-path = "src/crunch"
[[publish]]
schema-path = "some-schema"
output-path = "some-output"
"#;
let mut config = File::parse(raw).await?;
let config = config.add_publish("some-schema", "some-output");
let output = config.write().await?;
pretty_assertions::assert_eq!(output, expected);
Ok(())
}
#[tokio::test]
async fn test_can_add_publish_if_none() -> anyhow::Result<()> {
let raw = r#"[service]
service = "my-service"
domain = "my-domain"
codegen = ["rust"]
"#;
let expected = r#"[service]
service = "my-service"
domain = "my-domain"
codegen = ["rust"]
[[publish]]
schema-path = "some-schema"
output-path = "some-output"
"#;
let mut config = File::parse(raw).await?;
let config = config.add_publish("some-schema", "some-output");
let output = config.write().await?;
pretty_assertions::assert_eq!(output, expected);
Ok(())
}
#[tokio::test]
async fn test_can_get_config() -> anyhow::Result<()> {
let raw = r#"[service]
service = "my-service"
domain = "my-domain"
codegen = ["rust"]
"#;
let config = File::parse(raw).await?.get_config()?;
pretty_assertions::assert_eq!(
config,
Config {
service: Service {
service: "my-service".into(),
domain: "my-domain".into(),
codegen: vec!["rust".into()]
},
publish: None
}
);
Ok(())
}
#[tokio::test]
async fn test_can_get_config_publish() -> anyhow::Result<()> {
let raw = r#"[service]
service = "my-service"
domain = "my-domain"
codegen = ["rust"]
[[publish]]
schema-path = "some-schema"
output-path = "some-output"
"#;
let config = File::parse(raw).await?.get_config()?;
pretty_assertions::assert_eq!(
config,
Config {
service: Service {
service: "my-service".into(),
domain: "my-domain".into(),
codegen: vec!["rust".into()]
},
publish: Some(vec![Publish {
schema_path: "some-schema".into(),
output_path: "some-output".into()
}])
}
);
Ok(())
}
}

View File

@ -0,0 +1,8 @@
[service]
service = "my-service"
domain = "my-domain"
codegen = ["rust"]
[[publish]]
schema-path = "schemas/crunch"
output-path = "src/crunch"