From c0f9a1a7715529493fee72675a9f1bc84e28ee68 Mon Sep 17 00:00:00 2001 From: David Rousselie Date: Sun, 9 Jan 2022 14:11:36 +0100 Subject: [PATCH] Move API Task type to shared crate contextswitch-types --- Cargo.lock | 347 ++++++++++++------------------------------- Cargo.toml | 21 +-- src/contextswitch.rs | 45 ++++++ src/lib.rs | 56 ++----- src/main.rs | 2 +- src/taskwarrior.rs | 119 ++------------- tests/task.rs | 4 +- 7 files changed, 180 insertions(+), 414 deletions(-) create mode 100644 src/contextswitch.rs diff --git a/Cargo.lock b/Cargo.lock index 13019b4..5aa129a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,15 +4,16 @@ version = 3 [[package]] name = "actix-codec" -version = "0.4.0" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d5dbeb2d9e51344cb83ca7cc170f1217f9fe25bfc50160e6e200b5c31c1019a" +checksum = "a36c014a3e811624313b51a227b775ecba55d36ef9462bbaac7d4f13e54c9271" dependencies = [ "bitflags", "bytes", "futures-core", "futures-sink", "log", + "memchr", "pin-project-lite", "tokio", "tokio-util", @@ -20,14 +21,13 @@ dependencies = [ [[package]] name = "actix-http" -version = "3.0.0-beta.11" +version = "3.0.0-beta.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c9b510d35f13987537289f38bf136e7e702a5c87cc28760310cc459544f40afd" +checksum = "dac76407e2be239b5e544c9d83bdac4a0d68ae188031ac0424aa3e4868a669be" dependencies = [ "actix-codec", "actix-rt", "actix-service", - "actix-tls", "actix-utils", "ahash", "base64", @@ -39,24 +39,20 @@ dependencies = [ "encoding_rs", "flate2", "futures-core", - "futures-util", "h2", "http", "httparse", "httpdate", - "itoa", + "itoa 1.0.1", "language-tags", "local-channel", "log", "mime", - "once_cell", "percent-encoding", - "pin-project", "pin-project-lite", "rand", "sha-1", "smallvec", - "tokio", "zstd", ] @@ -72,9 +68,9 @@ dependencies = [ [[package]] name = "actix-router" -version = "0.5.0-beta.2" +version = "0.5.0-beta.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "36b95ce0d76d1aa2f98b681702807475ade0f99bd4552546a6843a966d42ea3d" +checksum = "ddd9f117b910fbcce6e9f45092ffd4ff017785a346d09e2d4fd049f4e20384f4" dependencies = [ "bytestring", "firestorm", @@ -86,9 +82,9 @@ dependencies = [ [[package]] name = "actix-rt" -version = "2.3.0" +version = "2.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea360596a50aa9af459850737f99293e5cb9114ae831118cb6026b3bbc7583ad" +checksum = "82cf33e04d9911b39bfb7be3c01309568b4315895d3358372dce64ed2c2bf32d" dependencies = [ "actix-macros", "futures-core", @@ -97,17 +93,19 @@ dependencies = [ [[package]] name = "actix-server" -version = "2.0.0-beta.6" +version = "2.0.0-rc.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7367665785765b066ad16e1086d26a087f696bc7c42b6f93004ced6cfcf1eeca" +checksum = "c9259b4f3cc9ca96d7d91a7da66b7b01c47653a0da5b0ba3f7f45a344480443b" dependencies = [ "actix-rt", "actix-service", "actix-utils", "futures-core", + "futures-util", "log", - "mio", + "mio 0.8.0", "num_cpus", + "socket2", "tokio", ] @@ -122,23 +120,6 @@ dependencies = [ "pin-project-lite", ] -[[package]] -name = "actix-tls" -version = "3.0.0-beta.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e4af84e13e4600829858a3e68079be710d1ada461431e1e4c5ae663479ea0a3c" -dependencies = [ - "actix-codec", - "actix-rt", - "actix-service", - "actix-utils", - "derive_more", - "futures-core", - "http", - "log", - "tokio-util", -] - [[package]] name = "actix-utils" version = "3.0.0" @@ -151,9 +132,9 @@ dependencies = [ [[package]] name = "actix-web" -version = "4.0.0-beta.10" +version = "4.0.0-beta.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8a4b9d00991d8da308070a5cea7f1bbaa153a91c3fb5567937d99b9f46d601e" +checksum = "a542a92e385844d988b34f1af289dbb0ca927c23740076a56fdf770b1af69fbc" dependencies = [ "actix-codec", "actix-http", @@ -169,17 +150,15 @@ dependencies = [ "cfg-if", "cookie", "derive_more", - "either", "encoding_rs", "futures-core", "futures-util", - "itoa", + "itoa 1.0.1", "language-tags", "log", "mime", "once_cell", - "paste", - "pin-project", + "pin-project-lite", "regex", "serde", "serde_json", @@ -192,9 +171,9 @@ dependencies = [ [[package]] name = "actix-web-codegen" -version = "0.5.0-beta.5" +version = "0.5.0-beta.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dfe80a8828fa88a0420dc8fdd4c16b8207326c917f17701881b063eadc2a8d3b" +checksum = "30a90b7f6c2fde9a1fe3df4da758c2c3c9d620dfa3eae4da0b6925dc0a13444a" dependencies = [ "actix-router", "proc-macro2", @@ -245,12 +224,6 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" -[[package]] -name = "base-x" -version = "0.2.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4521f3e3d031370679b3b140beb36dfe4801b09ac77e30c61941f97df3ef28b" - [[package]] name = "base64" version = "0.13.0" @@ -265,9 +238,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "block-buffer" -version = "0.9.0" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" +checksum = "f1d36a02058e76b040de25a4464ba1c80935655595b661505c8b39b664828b95" dependencies = [ "generic-array", ] @@ -349,13 +322,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06821ea598337a8412cf47c5b71c3bc694a7f0aed188ac28b836fab164a2c202" [[package]] -name = "const_fn" -version = "0.4.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f92cfa0fd5690b3cf8c1ef2cabbd9b7ef22fa53cf5e1f92b05103f6d5d1cf6e7" - -[[package]] -name = "contextswitch" +name = "contextswitch-api" version = "0.1.0" dependencies = [ "actix-http", @@ -363,6 +330,7 @@ dependencies = [ "actix-web", "chrono", "configparser", + "contextswitch-types", "dotenv", "env_logger", "listenfd", @@ -374,6 +342,16 @@ dependencies = [ "uuid", ] +[[package]] +name = "contextswitch-types" +version = "0.1.0" +dependencies = [ + "chrono", + "serde", + "serde_json", + "uuid", +] + [[package]] name = "convert_case" version = "0.4.0" @@ -382,12 +360,12 @@ checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" [[package]] name = "cookie" -version = "0.15.1" +version = "0.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5f1c7727e460397e56abc4bddc1d49e07a1ad78fc98eb2e1c8f032a58a2f80d" +checksum = "94d4706de1b0fa5b132270cddffa8585166037822e260a944fe161acd137ca05" dependencies = [ "percent-encoding", - "time 0.2.27", + "time 0.3.4", "version_check", ] @@ -425,6 +403,15 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "crypto-common" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "683d6b536309245c849479fba3da410962a43ed8e51c26b729208ec0ac2798d0" +dependencies = [ + "generic-array", +] + [[package]] name = "derive_more" version = "0.99.16" @@ -434,37 +421,27 @@ dependencies = [ "convert_case", "proc-macro2", "quote", - "rustc_version 0.3.3", + "rustc_version", "syn", ] [[package]] name = "digest" -version = "0.9.0" +version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" +checksum = "b697d66081d42af4fba142d56918a3cb21dc8eb63372c6b85d14f44fb9c5979b" dependencies = [ + "block-buffer", + "crypto-common", "generic-array", ] -[[package]] -name = "discard" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "212d0f5754cb6769937f4501cc0e67f4f4483c8d2c3e1e922ee9edbe4ab4c7c0" - [[package]] name = "dotenv" version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" -[[package]] -name = "either" -version = "1.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" - [[package]] name = "encoding_rs" version = "0.8.29" @@ -571,7 +548,6 @@ checksum = "36568465210a3a6ee45e1f165136d68671471a501e632e9a98d96872222b5481" dependencies = [ "autocfg", "futures-core", - "futures-sink", "futures-task", "pin-project-lite", "pin-utils", @@ -600,9 +576,9 @@ dependencies = [ [[package]] name = "h2" -version = "0.3.7" +version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fd819562fcebdac5afc5c113c3ec36f902840b70fd4fc458799c8ce4607ae55" +checksum = "8f072413d126e57991455e0a922b31e4c8ba7c2ffbebf6b78b4f8521397d65cd" dependencies = [ "bytes", "fnv", @@ -640,7 +616,7 @@ checksum = "1323096b05d41827dadeaee54c9981958c0f94e670bc94ed80037d1a7b8b186b" dependencies = [ "bytes", "fnv", - "itoa", + "itoa 0.4.8", ] [[package]] @@ -687,7 +663,7 @@ dependencies = [ "http-body", "httparse", "httpdate", - "itoa", + "itoa 0.4.8", "pin-project-lite", "socket2", "tokio", @@ -751,6 +727,12 @@ version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4" +[[package]] +name = "itoa" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" + [[package]] name = "jobserver" version = "0.1.24" @@ -875,6 +857,19 @@ dependencies = [ "winapi", ] +[[package]] +name = "mio" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba272f85fa0b41fc91872be579b3bbe0f56b792aa361a380eb669469f68dafb2" +dependencies = [ + "libc", + "log", + "miow", + "ntapi", + "winapi", +] + [[package]] name = "miow" version = "0.3.7" @@ -955,12 +950,6 @@ version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "692fcb63b64b1758029e0a96ee63e049ce8c5948587f2f7208df04625e5f6b56" -[[package]] -name = "opaque-debug" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" - [[package]] name = "openssl" version = "0.10.37" @@ -1040,26 +1029,6 @@ dependencies = [ "ucd-trie", ] -[[package]] -name = "pin-project" -version = "1.0.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "576bc800220cc65dac09e99e97b08b358cfab6e17078de8dc5fee223bd2d0c08" -dependencies = [ - "pin-project-internal", -] - -[[package]] -name = "pin-project-internal" -version = "1.0.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e8fe8163d14ce7f0cdac2e040116f22eac817edabff0be91e8aff7e9accf389" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "pin-project-lite" version = "0.2.7" @@ -1084,12 +1053,6 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed0cfbc8191465bed66e1718596ee0b0b35d5ee1f41c5df2189d0fe8bde535ba" -[[package]] -name = "proc-macro-hack" -version = "0.5.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" - [[package]] name = "proc-macro2" version = "1.0.32" @@ -1218,22 +1181,13 @@ dependencies = [ "winreg", ] -[[package]] -name = "rustc_version" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" -dependencies = [ - "semver 0.9.0", -] - [[package]] name = "rustc_version" version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0dfe2087c51c460008730de8b57e6a320782fbfb312e1f4d520e6c6fae155ee" dependencies = [ - "semver 0.11.0", + "semver", ] [[package]] @@ -1281,30 +1235,15 @@ dependencies = [ "libc", ] -[[package]] -name = "semver" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" -dependencies = [ - "semver-parser 0.7.0", -] - [[package]] name = "semver" version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f301af10236f6df4160f7c3f04eec6dbc70ace82d23326abad5edee88801c6b6" dependencies = [ - "semver-parser 0.10.2", + "semver-parser", ] -[[package]] -name = "semver-parser" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" - [[package]] name = "semver-parser" version = "0.10.2" @@ -1340,7 +1279,7 @@ version = "1.0.71" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "063bf466a64011ac24040a49009724ee60a57da1b437617ceb32e53ad61bfb19" dependencies = [ - "itoa", + "itoa 0.4.8", "ryu", "serde", ] @@ -1352,30 +1291,22 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "edfa57a7f8d9c1d260a549e7224100f6c43d43f9103e06dd8b4095a9b2b43ce9" dependencies = [ "form_urlencoded", - "itoa", + "itoa 0.4.8", "ryu", "serde", ] [[package]] name = "sha-1" -version = "0.9.8" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "99cd6713db3cf16b6c84e06321e049a9b9f699826e16096d23bbcc44d15d51a6" +checksum = "028f48d513f9678cda28f6e4064755b3fbb2af6acd672f2c209b62323f7aea0f" dependencies = [ - "block-buffer", "cfg-if", "cpufeatures", "digest", - "opaque-debug", ] -[[package]] -name = "sha1" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2579985fda508104f7587689507983eadd6a6e84dd35d6d115361f530916fa0d" - [[package]] name = "signal-hook-registry" version = "1.4.0" @@ -1407,64 +1338,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "standback" -version = "0.2.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e113fb6f3de07a243d434a56ec6f186dfd51cb08448239fe7bcae73f87ff28ff" -dependencies = [ - "version_check", -] - -[[package]] -name = "stdweb" -version = "0.4.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d022496b16281348b52d0e30ae99e01a73d737b2f45d38fed4edf79f9325a1d5" -dependencies = [ - "discard", - "rustc_version 0.2.3", - "stdweb-derive", - "stdweb-internal-macros", - "stdweb-internal-runtime", - "wasm-bindgen", -] - -[[package]] -name = "stdweb-derive" -version = "0.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c87a60a40fccc84bef0652345bbbbbe20a605bf5d0ce81719fc476f5c03b50ef" -dependencies = [ - "proc-macro2", - "quote", - "serde", - "serde_derive", - "syn", -] - -[[package]] -name = "stdweb-internal-macros" -version = "0.2.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "58fa5ff6ad0d98d1ffa8cb115892b6e69d67799f6763e162a1c9db421dc22e11" -dependencies = [ - "base-x", - "proc-macro2", - "quote", - "serde", - "serde_derive", - "serde_json", - "sha1", - "syn", -] - -[[package]] -name = "stdweb-internal-runtime" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "213701ba3370744dcd1a12960caa4843b3d68b4d1c0a5d575e0d65b2ee9d16c0" - [[package]] name = "syn" version = "1.0.81" @@ -1510,53 +1383,22 @@ dependencies = [ "winapi", ] -[[package]] -name = "time" -version = "0.2.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4752a97f8eebd6854ff91f1c1824cd6160626ac4bd44287f7f4ea2035a02a242" -dependencies = [ - "const_fn", - "libc", - "standback", - "stdweb", - "time-macros", - "version_check", - "winapi", -] - [[package]] name = "time" version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "99beeb0daeac2bd1e86ac2c21caddecb244b39a093594da1a661ec2060c7aedd" dependencies = [ - "itoa", + "itoa 0.4.8", "libc", + "time-macros", ] [[package]] name = "time-macros" -version = "0.1.1" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "957e9c6e26f12cb6d0dd7fc776bb67a706312e7299aed74c8dd5b17ebb27e2f1" -dependencies = [ - "proc-macro-hack", - "time-macros-impl", -] - -[[package]] -name = "time-macros-impl" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd3c141a1b43194f3f56a1411225df8646c55781d5f26db825b3d98507eb482f" -dependencies = [ - "proc-macro-hack", - "proc-macro2", - "quote", - "standback", - "syn", -] +checksum = "25eb0ca3468fc0acc11828786797f6ef9aa1555e4a211a60d64cc8e4d1be47d6" [[package]] name = "tinyvec" @@ -1575,15 +1417,14 @@ checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" [[package]] name = "tokio" -version = "1.13.0" +version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "588b2d10a336da58d877567cd8fb8a14b463e2104910f8132cd054b4b96e29ee" +checksum = "fbbf1c778ec206785635ce8ad57fe52b3009ae9e0c9f574a728f3049d3e55838" dependencies = [ - "autocfg", "bytes", "libc", "memchr", - "mio", + "mio 0.7.14", "once_cell", "parking_lot", "pin-project-lite", @@ -1848,18 +1689,18 @@ dependencies = [ [[package]] name = "zstd" -version = "0.7.0+zstd.1.4.9" +version = "0.9.1+zstd.1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9428752481d8372e15b1bf779ea518a179ad6c771cca2d2c60e4fbff3cc2cd52" +checksum = "538b8347df9257b7fbce37677ef7535c00a3c7bf1f81023cc328ed7fe4b41de8" dependencies = [ "zstd-safe", ] [[package]] name = "zstd-safe" -version = "3.1.0+zstd.1.4.9" +version = "4.1.2+zstd.1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5aa1926623ad7fe406e090555387daf73db555b948134b4d73eac5eb08fb666d" +checksum = "9fb4cfe2f6e6d35c5d27ecd9d256c4b6f7933c4895654917460ec56c29336cc1" dependencies = [ "libc", "zstd-sys", @@ -1867,9 +1708,9 @@ dependencies = [ [[package]] name = "zstd-sys" -version = "1.5.0+zstd.1.4.9" +version = "1.6.2+zstd.1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e6c094340240369025fc6b731b054ee2a834328fa584310ac96aa4baebdc465" +checksum = "2daf2f248d9ea44454bfcb2516534e8b8ad2fc91bf818a1885495fc42bc8ac9f" dependencies = [ "cc", "libc", diff --git a/Cargo.toml b/Cargo.toml index 8db8dbe..1d79099 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "contextswitch" +name = "contextswitch-api" version = "0.1.0" edition = "2021" authors = ["David Rousselie "] @@ -9,20 +9,21 @@ path = "src/lib.rs" [[bin]] path = "src/main.rs" -name = "contextswitch" +name = "contextswitch-api" [dependencies] -actix-web = "=4.0.0-beta.10" -actix-http = "=3.0.0-beta.11" -serde = "1.0.130" -serde_json = "1.0.71" -uuid = { version = "0.8.2", features = ["serde"] } -chrono = { version = "0.4.19", features = ["serde"] } -mktemp = "0.4.1" +contextswitch-types = { path = "../types" } +actix-web = "=4.0.0-beta.18" +actix-http = "=3.0.0-beta.17" +serde = { version = "1.0.0", features = ["derive"] } +serde_json = "1.0" +uuid = { version = "0.8.0", features = ["serde"] } +chrono = { version = "0.4.0", features = ["serde"] } +mktemp = "0.4.0" configparser = "3.0.0" env_logger = "0.9.0" dotenv = "0.15.0" -listenfd = "0.3.5" +listenfd = "0.3.0" [dev-dependencies] actix-rt = "2" diff --git a/src/contextswitch.rs b/src/contextswitch.rs new file mode 100644 index 0000000..39bdf6e --- /dev/null +++ b/src/contextswitch.rs @@ -0,0 +1,45 @@ +use crate::taskwarrior; +use contextswitch_types::{ContextSwitchMetadata, Task}; +use std::io::Error; + +impl TryFrom<&taskwarrior::Task> for Task { + type Error = std::io::Error; + + fn try_from(task: &taskwarrior::Task) -> Result { + let cs_metadata = task.contextswitch.as_ref().map_or( + Ok(None), + |cs_string| -> Result, serde_json::Error> { + if cs_string.is_empty() || cs_string == "{}" { + Ok(None) + } else { + Some(serde_json::from_str(&cs_string)).transpose() + } + }, + )?; + + Ok(Task { + uuid: task.uuid, + id: task.id, + entry: task.entry, + modified: task.modified, + status: task.status, + description: task.description.clone(), + urgency: task.urgency, + due: task.due, + end: task.end, + parent: task.parent, + project: task.project.clone(), + recur: task.recur, + tags: task.tags.clone(), + contextswitch: cs_metadata, + }) + } +} + +pub fn export(filters: Vec<&str>) -> Result, Error> { + let tasks: Result, Error> = taskwarrior::export(filters)? + .iter() + .map(|task| Task::try_from(task)) + .collect(); + tasks +} diff --git a/src/lib.rs b/src/lib.rs index 19e1f4f..343e2b7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,11 +1,11 @@ -use actix_web::{dev::Server, middleware::Logger, web, App, HttpResponse, HttpServer}; -use chrono::{DateTime, Utc}; +use actix_web::{dev::Server, middleware, web, App, HttpResponse, HttpServer}; use listenfd::ListenFd; -use serde::{Deserialize, Serialize}; +use serde::Deserialize; +use std::env; use std::io::Error; use std::net::TcpListener; -use uuid::Uuid; +pub mod contextswitch; pub mod taskwarrior; #[derive(Deserialize)] @@ -13,43 +13,8 @@ struct TaskQuery { filter: String, } -#[derive(Debug, Serialize, Deserialize, PartialEq)] -pub struct Task { - pub uuid: Uuid, - pub id: u32, - #[serde(with = "taskwarrior::tw_date_format")] - pub entry: DateTime, - #[serde(with = "taskwarrior::tw_date_format")] - pub modified: DateTime, - pub status: taskwarrior::Status, - pub description: String, - pub urgency: f64, - #[serde( - default, - skip_serializing_if = "Option::is_none", - with = "taskwarrior::opt_tw_date_format" - )] - pub due: Option>, - #[serde( - default, - skip_serializing_if = "Option::is_none", - with = "taskwarrior::opt_tw_date_format" - )] - pub end: Option>, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub parent: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub project: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub recur: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub tags: Option>, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub contextswitch: Option, -} - async fn list_tasks(task_query: web::Query) -> Result { - let tasks = taskwarrior::export(task_query.filter.split(' ').collect())?; + let tasks = contextswitch::export(task_query.filter.split(' ').collect())?; Ok(HttpResponse::Ok() .content_type("application/json") @@ -61,9 +26,16 @@ async fn health_check() -> HttpResponse { } pub fn run(listener: TcpListener) -> Result { - let mut server = HttpServer::new(|| { + let cs_front_base_url = + env::var("CS_FRONT_BASE_URL").unwrap_or("http://localhost:8080".to_string()); + let mut server = HttpServer::new(move || { App::new() - .wrap(Logger::default()) + .wrap(middleware::Logger::default()) + .wrap(middleware::Compress::default()) + .wrap( + middleware::DefaultHeaders::new() + .add(("Access-Control-Allow-Origin", cs_front_base_url.as_bytes())), + ) .route("/ping", web::get().to(health_check)) .route("/tasks", web::get().to(list_tasks)) }) diff --git a/src/main.rs b/src/main.rs index 7d00f37..58c6a6f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ extern crate dotenv; extern crate env_logger; extern crate listenfd; -use contextswitch::run; +use contextswitch_api::run; use dotenv::dotenv; use std::env; use std::net::TcpListener; diff --git a/src/taskwarrior.rs b/src/taskwarrior.rs index da21940..506656f 100644 --- a/src/taskwarrior.rs +++ b/src/taskwarrior.rs @@ -1,6 +1,6 @@ use chrono::{DateTime, Utc}; use configparser::ini::Ini; -use serde::{de, Deserialize, Deserializer, Serialize}; +use serde::{Deserialize, Serialize}; use serde_json; use std::env; use std::io::Error; @@ -9,50 +9,27 @@ use std::process::Command; use std::str; use uuid::Uuid; -#[derive(Debug, Serialize, Deserialize, PartialEq)] -#[serde(rename_all = "lowercase")] -pub enum Recurrence { - Daily, - Weekly, - Monthly, - Yearly, -} - -#[derive(Debug, Serialize, Deserialize, PartialEq)] -#[serde(rename_all = "lowercase")] -pub enum Status { - Pending, - Completed, - Recurring, - Deleted, -} - -#[derive(Debug, Serialize, Deserialize, PartialEq)] -pub struct ContextSwitchMetadata { - pub test: u32, -} - #[derive(Debug, Serialize, Deserialize, PartialEq)] pub struct Task { pub uuid: Uuid, pub id: u32, - #[serde(with = "tw_date_format")] + #[serde(with = "contextswitch_types::tw_date_format")] pub entry: DateTime, - #[serde(with = "tw_date_format")] + #[serde(with = "contextswitch_types::tw_date_format")] pub modified: DateTime, - pub status: Status, + pub status: contextswitch_types::Status, pub description: String, pub urgency: f64, #[serde( default, skip_serializing_if = "Option::is_none", - with = "opt_tw_date_format" + with = "contextswitch_types::opt_tw_date_format" )] pub due: Option>, #[serde( default, skip_serializing_if = "Option::is_none", - with = "opt_tw_date_format" + with = "contextswitch_types::opt_tw_date_format" )] pub end: Option>, #[serde(default, skip_serializing_if = "Option::is_none")] @@ -60,75 +37,11 @@ pub struct Task { #[serde(default, skip_serializing_if = "Option::is_none")] pub project: Option, #[serde(default, skip_serializing_if = "Option::is_none")] - pub recur: Option, + pub recur: Option, #[serde(default, skip_serializing_if = "Option::is_none")] pub tags: Option>, - #[serde( - default, - skip_serializing_if = "Option::is_none", - deserialize_with = "deserialize_from_json" - )] - pub contextswitch: Option, -} - -fn deserialize_from_json<'de, D>(deserializer: D) -> Result, D::Error> -where - D: Deserializer<'de>, -{ - let s: String = Deserialize::deserialize(deserializer)?; - serde_json::from_str(&s).map_err(de::Error::custom) -} - -pub mod tw_date_format { - use chrono::{DateTime, TimeZone, Utc}; - use serde::{self, Deserialize, Deserializer, Serializer}; - - const FORMAT: &'static str = "%Y%m%dT%H%M%SZ"; - - pub fn serialize(date: &DateTime, serializer: S) -> Result - where - S: Serializer, - { - let s = format!("{}", date.format(FORMAT)); - serializer.serialize_str(&s) - } - - pub fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> - where - D: Deserializer<'de>, - { - let s = String::deserialize(deserializer)?; - Utc.datetime_from_str(&s, FORMAT) - .map_err(serde::de::Error::custom) - } -} - -pub mod opt_tw_date_format { - use chrono::{DateTime, TimeZone, Utc}; - use serde::{self, Deserialize, Deserializer, Serializer}; - - const FORMAT: &'static str = "%Y%m%dT%H%M%SZ"; - - pub fn serialize(date: &Option>, serializer: S) -> Result - where - S: Serializer, - { - if let Some(ref d) = *date { - return serializer.serialize_str(&d.format(FORMAT).to_string()); - } - - serializer.serialize_none() - } - - pub fn deserialize<'de, D>(deserializer: D) -> Result>, D::Error> - where - D: Deserializer<'de>, - { - let s = String::deserialize(deserializer)?; - Utc.datetime_from_str(&s, FORMAT) - .map(Some) - .map_err(serde::de::Error::custom) - } + #[serde(default, skip_serializing_if = "Option::is_none")] + pub contextswitch: Option, } pub fn load_config(task_data_location: Option<&str>) -> String { @@ -151,13 +64,6 @@ pub fn load_config(task_data_location: Option<&str>) -> String { }); let mut taskrc = Ini::new(); - taskrc.setstr("default", "uda.contextswitch.type", Some("string")); - taskrc.setstr( - "default", - "uda.contextswitch.label", - Some("Context Switch metadata"), - ); - taskrc.setstr("default", "uda.contextswitch.default", Some("{}")); taskrc.setstr("default", "data.location", Some(&data_location)); taskrc.setstr("default", "uda.contextswitch.type", Some("string")); taskrc.setstr( @@ -173,7 +79,7 @@ pub fn load_config(task_data_location: Option<&str>) -> String { env::set_var("TASKRC", taskrc_location); - return data_location; + data_location } pub fn export(filters: Vec<&str>) -> Result, Error> { @@ -183,12 +89,13 @@ pub fn export(filters: Vec<&str>) -> Result, Error> { let tasks: Vec = serde_json::from_slice(&export_output.stdout)?; - return Ok(tasks); + Ok(tasks) } pub fn add(add_args: Vec<&str>) -> Result<(), Error> { let mut args = vec!["add"]; args.extend(add_args); Command::new("task").args(args).output()?; - return Ok(()); + + Ok(()) } diff --git a/tests/task.rs b/tests/task.rs index 1a68552..3ecfa32 100644 --- a/tests/task.rs +++ b/tests/task.rs @@ -1,7 +1,7 @@ pub mod test_helper; -use contextswitch::taskwarrior; -use contextswitch::Task; +use contextswitch_api::taskwarrior; +use contextswitch_types::Task; #[actix_rt::test] async fn list_tasks() {