commit dd22dfacc2397b7e14bb503919be318186b6d33a Author: Aiden McClelland Date: Thu Apr 1 18:00:16 2021 -0600 create rpc toolkit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..bf1275f --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,1201 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +dependencies = [ + "winapi", +] + +[[package]] +name = "anyhow" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28b2cd92db5cbd74e8e5028f7e27dd7aa3090e89e4f2a197cc7c8dfb69c7063b" + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + +[[package]] +name = "autocfg" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" + +[[package]] +name = "base64" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" + +[[package]] +name = "bitflags" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" + +[[package]] +name = "bumpalo" +version = "3.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63396b8a4b9de3f4fdfb320ab6080762242f66a8ef174c49d8e19b674db4cdbe" + +[[package]] +name = "bytes" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b700ce4376041dcd0a327fd0097c41095743c4c8af8887265942faf1100bd040" + +[[package]] +name = "cc" +version = "1.0.67" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3c69b077ad434294d3ce9f1f6143a2a4b89a8a2d54ef813d85003a4fd1137fd" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "clap" +version = "2.33.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" +dependencies = [ + "ansi_term", + "atty", + "bitflags", + "strsim", + "textwrap", + "unicode-width", + "vec_map", +] + +[[package]] +name = "core-foundation" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a89e2ae426ea83155dccf10c0fa6b1463ef6d5fcb44cee0b224a408fa640a62" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea221b5284a47e40033bf9b66f35f984ec0ea2931eb03505246cd27a963f981b" + +[[package]] +name = "encoding_rs" +version = "0.8.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80df024fbc5ac80f87dfef0d9f5209a252f2a497f7f42944cff24d8253cac065" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + +[[package]] +name = "form_urlencoded" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fc25a87fa4fd2094bffb06925852034d90a17f0d1e05197d4956d3555752191" +dependencies = [ + "matches", + "percent-encoding", +] + +[[package]] +name = "futures-channel" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c2dd2df839b57db9ab69c2c9d8f3e8c81984781937fe2807dc6dcf3b2ad2939" +dependencies = [ + "futures-core", +] + +[[package]] +name = "futures-core" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15496a72fabf0e62bdc3df11a59a3787429221dd0710ba8ef163d6f7a9112c94" + +[[package]] +name = "futures-sink" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85754d98985841b7d4f5e8e6fbfa4a4ac847916893ec511a2917ccd8525b8bb3" + +[[package]] +name = "futures-task" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa189ef211c15ee602667a6fcfe1c1fd9e07d42250d2156382820fba33c9df80" + +[[package]] +name = "futures-util" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1812c7ab8aedf8d6f2701a43e1243acdbcc2b36ab26e2ad421eb99ac963d96d1" +dependencies = [ + "futures-core", + "futures-task", + "pin-project-lite", + "pin-utils", +] + +[[package]] +name = "getrandom" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "h2" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc018e188373e2777d0ef2467ebff62a08e66c3f5857b23c8fbec3018210dc00" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "half" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62aca2aba2d62b4a7f5b33f3712cb1b0692779a56fb510499d5c0aa594daeaf3" + +[[package]] +name = "hashbrown" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04" + +[[package]] +name = "hermit-abi" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c" +dependencies = [ + "libc", +] + +[[package]] +name = "http" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7245cd7449cc792608c3c8a9eaf69bd4eabbabf802713748fd739c98b82f0747" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "http-body" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5dfb77c123b4e2f72a2069aeae0b4b4949cc7e966df277813fc16347e7549737" +dependencies = [ + "bytes", + "http", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "615caabe2c3160b313d52ccc905335f4ed5f10881dd63dc5699d47e90be85691" + +[[package]] +name = "httpdate" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "494b4d60369511e7dea41cf646832512a94e542f68bb9c49e54518e0f468eb47" + +[[package]] +name = "hyper" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8bf09f61b52cfcf4c00de50df88ae423d6c02354e385a86341133b5338630ad1" +dependencies = [ + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "h2", + "http", + "http-body", + "httparse", + "httpdate", + "itoa", + "pin-project", + "socket2", + "tokio", + "tower-service", + "tracing", + "want", +] + +[[package]] +name = "hyper-tls" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905" +dependencies = [ + "bytes", + "hyper", + "native-tls", + "tokio", + "tokio-native-tls", +] + +[[package]] +name = "idna" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89829a5d69c23d348314a7ac337fe39173b61149a9864deabd260983aed48c21" +dependencies = [ + "matches", + "unicode-bidi", + "unicode-normalization", +] + +[[package]] +name = "indexmap" +version = "1.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "824845a0bf897a9042383849b02c1bc219c2383772efcd5c6f9766fa4b81aef3" +dependencies = [ + "autocfg", + "hashbrown", +] + +[[package]] +name = "instant" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61124eeebbd69b8190558df225adf7e4caafce0d743919e5d6b19652314ec5ec" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "ipnet" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47be2f14c678be2fdcab04ab1171db51b2762ce6f0a8ee87c8dd4a04ed216135" + +[[package]] +name = "itoa" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" + +[[package]] +name = "js-sys" +version = "0.3.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d99f9e3e84b8f67f846ef5b4cbbc3b1c29f6c759fcbce6f01aa0e73d932a24c" +dependencies = [ + "wasm-bindgen", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56d855069fafbb9b344c0f962150cd2c1187975cb1c22c1522c240d8c4986714" + +[[package]] +name = "lock_api" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd96ffd135b2fd7b973ac026d28085defbe8983df057ced3eb4f2130b0831312" +dependencies = [ + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "matches" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" + +[[package]] +name = "memchr" +version = "2.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" + +[[package]] +name = "mime" +version = "0.3.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" + +[[package]] +name = "mio" +version = "0.7.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf80d3e903b34e0bd7282b218398aec54e082c840d9baf8339e0080a0c542956" +dependencies = [ + "libc", + "log", + "miow", + "ntapi", + "winapi", +] + +[[package]] +name = "miow" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9f1c5b025cda876f66ef43a113f91ebc9f4ccef34843000e0adf6ebbab84e21" +dependencies = [ + "winapi", +] + +[[package]] +name = "native-tls" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8d96b2e1c8da3957d58100b09f102c6d9cfdfced01b7ec5a8974044bb09dbd4" +dependencies = [ + "lazy_static", + "libc", + "log", + "openssl", + "openssl-probe", + "openssl-sys", + "schannel", + "security-framework", + "security-framework-sys", + "tempfile", +] + +[[package]] +name = "ntapi" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f6bb902e437b6d86e03cce10a7e2af662292c5dfef23b65899ea3ac9354ad44" +dependencies = [ + "winapi", +] + +[[package]] +name = "num_cpus" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3" +dependencies = [ + "hermit-abi", + "libc", +] + +[[package]] +name = "once_cell" +version = "1.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af8b08b04175473088b46763e51ee54da5f9a164bc162f615b91bc179dbf15a3" + +[[package]] +name = "openssl" +version = "0.10.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a61075b62a23fef5a29815de7536d940aa35ce96d18ce0cc5076272db678a577" +dependencies = [ + "bitflags", + "cfg-if", + "foreign-types", + "libc", + "once_cell", + "openssl-sys", +] + +[[package]] +name = "openssl-probe" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77af24da69f9d9341038eba93a073b1fdaaa1b788221b00a69bce9e762cb32de" + +[[package]] +name = "openssl-sys" +version = "0.9.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "313752393519e876837e09e1fa183ddef0be7735868dced3196f4472d536277f" +dependencies = [ + "autocfg", + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "parking_lot" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d7744ac029df22dca6284efe4e898991d28e3085c706c972bcd7da4a27a15eb" +dependencies = [ + "instant", + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa7a782938e745763fe6907fc6ba86946d72f49fe7e21de074e08128a99fb018" +dependencies = [ + "cfg-if", + "instant", + "libc", + "redox_syscall", + "smallvec", + "winapi", +] + +[[package]] +name = "percent-encoding" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" + +[[package]] +name = "pin-project" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc174859768806e91ae575187ada95c91a29e96a98dc5d2cd9a1fed039501ba6" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a490329918e856ed1b083f244e3bfe2d8c4f336407e4ea9e1a9f479ff09049e5" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc0e1f259c92177c30a4c9d177246edd0a3568b25756a977d0632cf8fa37e905" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "pkg-config" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c" + +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + +[[package]] +name = "proc-macro2" +version = "1.0.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a152013215dca273577e18d2bf00fa862b89b24169fb78c4c95aeb07992c9cec" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" +dependencies = [ + "rand_core", +] + +[[package]] +name = "redox_syscall" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94341e4e44e24f6b591b59e47a8a027df12e008d73fd5672dbea9cc22f4507d9" +dependencies = [ + "bitflags", +] + +[[package]] +name = "remove_dir_all" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" +dependencies = [ + "winapi", +] + +[[package]] +name = "reqwest" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf12057f289428dbf5c591c74bf10392e4a8003f993405a902f20117019022d4" +dependencies = [ + "base64", + "bytes", + "encoding_rs", + "futures-core", + "futures-util", + "http", + "http-body", + "hyper", + "hyper-tls", + "ipnet", + "js-sys", + "lazy_static", + "log", + "mime", + "native-tls", + "percent-encoding", + "pin-project-lite", + "serde", + "serde_urlencoded", + "tokio", + "tokio-native-tls", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "winreg", +] + +[[package]] +name = "rpc-toolkit" +version = "0.1.0" +dependencies = [ + "clap", + "hyper", + "lazy_static", + "reqwest", + "rpc-toolkit-macro", + "serde", + "serde_cbor", + "serde_json", + "thiserror", + "tokio", + "url", + "yajrc", +] + +[[package]] +name = "rpc-toolkit-macro" +version = "0.1.0" +dependencies = [ + "proc-macro2", + "rpc-toolkit-macro-internals", + "syn", +] + +[[package]] +name = "rpc-toolkit-macro-internals" +version = "0.1.0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "ryu" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" + +[[package]] +name = "schannel" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f05ba609c234e60bee0d547fe94a4c7e9da733d1c962cf6e59efa4cd9c8bc75" +dependencies = [ + "lazy_static", + "winapi", +] + +[[package]] +name = "scopeguard" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" + +[[package]] +name = "security-framework" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3670b1d2fdf6084d192bc71ead7aabe6c06aa2ea3fbd9cc3ac111fa5c2b1bd84" +dependencies = [ + "bitflags", + "core-foundation", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3676258fd3cfe2c9a0ec99ce3038798d847ce3e4bb17746373eb9f0f1ac16339" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "serde" +version = "1.0.125" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "558dc50e1a5a5fa7112ca2ce4effcb321b0300c0d4ccf0776a9f60cd89031171" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_cbor" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e18acfa2f90e8b735b2836ab8d538de304cbb6729a7360729ea5a895d15a622" +dependencies = [ + "half", + "serde", +] + +[[package]] +name = "serde_derive" +version = "1.0.125" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b093b7a2bb58203b5da3056c05b4ec1fed827dcfdb37347a8841695263b3d06d" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "799e97dc9fdae36a5c8b8f2cae9ce2ee9fdce2058c57a93e6099d919fd982f79" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "serde_urlencoded" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edfa57a7f8d9c1d260a549e7224100f6c43d43f9103e06dd8b4095a9b2b43ce9" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "signal-hook-registry" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16f1d0fef1604ba8f7a073c7e701f213e056707210e9020af4528e0101ce11a6" +dependencies = [ + "libc", +] + +[[package]] +name = "slab" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" + +[[package]] +name = "smallvec" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" + +[[package]] +name = "socket2" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e3dfc207c526015c632472a77be09cf1b6e46866581aecae5cc38fb4235dea2" +dependencies = [ + "libc", + "winapi", +] + +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" + +[[package]] +name = "syn" +version = "1.0.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ce15dd3ed8aa2f8eeac4716d6ef5ab58b6b9256db41d7e1a0224c2788e8fd87" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "tempfile" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22" +dependencies = [ + "cfg-if", + "libc", + "rand", + "redox_syscall", + "remove_dir_all", + "winapi", +] + +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + +[[package]] +name = "thiserror" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0f4a65597094d4483ddaed134f409b2cb7c1beccf25201a9f73c719254fa98e" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7765189610d8241a44529806d6fd1f2e0a08734313a35d5b3a556f92b381f3c0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tinyvec" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "317cca572a0e89c3ce0ca1f1bdc9369547fe318a683418e42ac8f59d14701023" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" + +[[package]] +name = "tokio" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "134af885d758d645f0f0505c9a8b3f9bf8a348fd822e112ab5248138348f1722" +dependencies = [ + "autocfg", + "bytes", + "libc", + "memchr", + "mio", + "num_cpus", + "once_cell", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "tokio-macros", + "winapi", +] + +[[package]] +name = "tokio-macros" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "caf7b11a536f46a809a8a9f0bb4237020f70ecbf115b842360afb127ea2fda57" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tokio-native-tls" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7d995660bd2b7f8c1568414c1126076c13fbb725c40112dc0120b78eb9b717b" +dependencies = [ + "native-tls", + "tokio", +] + +[[package]] +name = "tokio-util" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5143d049e85af7fbc36f5454d990e62c2df705b3589f123b71f441b6b59f443f" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "log", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tower-service" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "360dfd1d6d30e05fda32ace2c8c70e9c0a9da713275777f5a4dbb8a1893930c6" + +[[package]] +name = "tracing" +version = "0.1.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01ebdc2bb4498ab1ab5f5b73c5803825e60199229ccba0698170e3be0e7f959f" +dependencies = [ + "cfg-if", + "pin-project-lite", + "tracing-core", +] + +[[package]] +name = "tracing-core" +version = "0.1.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f50de3927f93d202783f4513cda820ab47ef17f624b03c096e86ef00c67e6b5f" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "try-lock" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" + +[[package]] +name = "unicode-bidi" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" +dependencies = [ + "matches", +] + +[[package]] +name = "unicode-normalization" +version = "0.1.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07fbfce1c8a97d547e8b5334978438d9d6ec8c20e38f56d4a4374d181493eaef" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "unicode-width" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" + +[[package]] +name = "unicode-xid" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" + +[[package]] +name = "url" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ccd964113622c8e9322cfac19eb1004a07e636c545f325da085d5cdde6f1f8b" +dependencies = [ + "form_urlencoded", + "idna", + "matches", + "percent-encoding", +] + +[[package]] +name = "vcpkg" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b00bca6106a5e23f3eee943593759b7fcddb00554332e856d990c893966879fb" + +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + +[[package]] +name = "want" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ce8a968cb1cd110d136ff8b819a556d6fb6d919363c61534f6860c7eb172ba0" +dependencies = [ + "log", + "try-lock", +] + +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" + +[[package]] +name = "wasm-bindgen" +version = "0.2.73" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83240549659d187488f91f33c0f8547cbfef0b2088bc470c116d1d260ef623d9" +dependencies = [ + "cfg-if", + "serde", + "serde_json", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.73" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae70622411ca953215ca6d06d3ebeb1e915f0f6613e3b495122878d7ebec7dae" +dependencies = [ + "bumpalo", + "lazy_static", + "log", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81b8b767af23de6ac18bf2168b690bed2902743ddf0fb39252e36f9e2bfc63ea" +dependencies = [ + "cfg-if", + "js-sys", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.73" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e734d91443f177bfdb41969de821e15c516931c3c3db3d318fa1b68975d0f6f" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.73" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d53739ff08c8a68b0fdbcd54c372b8ab800b1449ab3c9d706503bc7dd1621b2c" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.73" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9a543ae66aa233d14bb765ed9af4a33e81b8b58d1584cf1b47ff8cd0b9e4489" + +[[package]] +name = "web-sys" +version = "0.3.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a905d57e488fec8861446d3393670fb50d27a262344013181c2cdf9fff5481be" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "winreg" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0120db82e8a1e0b9fb3345a539c478767c0048d842860994d96113d5b667bd69" +dependencies = [ + "winapi", +] + +[[package]] +name = "yajrc" +version = "0.1.0" +dependencies = [ + "anyhow", + "serde", + "serde_json", + "thiserror", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..333618a --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,2 @@ +[workspace] +members = ["rpc-toolkit", "rpc-toolkit-macro", "rpc-toolkit-macro-internals"] diff --git a/rpc-toolkit-macro-internals/.gitignore b/rpc-toolkit-macro-internals/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/rpc-toolkit-macro-internals/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/rpc-toolkit-macro-internals/Cargo.toml b/rpc-toolkit-macro-internals/Cargo.toml new file mode 100644 index 0000000..977fd8c --- /dev/null +++ b/rpc-toolkit-macro-internals/Cargo.toml @@ -0,0 +1,12 @@ +[package] +authors = ["Aiden McClelland "] +edition = "2018" +name = "rpc-toolkit-macro-internals" +version = "0.1.0" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +proc-macro2 = "1.0.26" +quote = "1.0.9" +syn = { version = "1.0.68", features = ["fold"] } diff --git a/rpc-toolkit-macro-internals/src/command/build.rs b/rpc-toolkit-macro-internals/src/command/build.rs new file mode 100644 index 0000000..1b40b47 --- /dev/null +++ b/rpc-toolkit-macro-internals/src/command/build.rs @@ -0,0 +1,824 @@ +use super::parse::*; +use super::*; +use proc_macro2::*; +use quote::*; +use std::collections::HashSet; +use syn::{ + fold::Fold, + punctuated::Punctuated, + spanned::Spanned, + token::{Comma, Where}, +}; + +fn build_app(name: LitStr, opt: &mut Options, params: &mut [ParamType]) -> TokenStream { + let about = opt.common().about.clone().into_iter(); + let (subcommand, subcommand_required) = if let Options::Parent(opt) = opt { + ( + opt.subcommands + .iter() + .map(|subcmd| { + let mut path = subcmd.clone(); + path.segments.last_mut().unwrap().arguments = PathArguments::None; + path + }) + .collect(), + opt.self_impl.is_none(), + ) + } else { + (Vec::new(), false) + }; + let arg = params + .iter_mut() + .filter_map(|param| { + if let ParamType::Arg(arg) = param { + if arg.stdin { + return None; + } + let name = arg.name.clone().unwrap(); + let name_str = LitStr::new(&name.to_string(), name.span()); + let help = arg.help.clone().into_iter(); + let short = arg.short.clone().into_iter(); + let long = arg.long.clone().into_iter(); + let mut modifications = TokenStream::default(); + let ty_span = arg.ty.span(); + if let Type::Path(p) = &mut arg.ty { + if p.path.is_ident("bool") + && arg.parse.is_none() + && (arg.short.is_some() || arg.long.is_some()) + { + arg.check_is_present = true; + modifications.extend(quote_spanned! { ty_span => + arg = arg.takes_value(false); + }); + } else { + modifications.extend(quote_spanned! { ty_span => + arg = arg.takes_value(true); + }); + if p.path.segments.last().unwrap().ident != "Option" { + modifications.extend(quote_spanned! { ty_span => + arg = arg.required(true); + }); + } else { + arg.optional = true; + modifications.extend(quote_spanned! { ty_span => + arg = arg.required(false); + }); + } + } + }; + Some(quote! { + { + let mut arg = rpc_toolkit_prelude::Arg::with_name(#name_str); + #( + arg = arg.help(#help); + )* + #( + arg = arg.short(#short); + )* + #( + arg = arg.long(#long); + )* + #modifications + + arg + } + }) + } else { + None + } + }) + .collect::>(); + let required = LitBool::new(subcommand_required, Span::call_site()); + quote! { + pub fn build_app() -> rpc_toolkit_prelude::App<'static, 'static> { + let mut app = rpc_toolkit_prelude::App::new(#name); + #( + app = app.about(#about); + )* + #( + app = app.arg(#arg); + )* + #( + app = app.subcommand(#subcommand::build_app()); + )* + if #required { + app = app.setting(rpc_toolkit_prelude::AppSettings::SubcommandRequired); + } + app + } + } +} + +struct GenericFilter<'a> { + src: &'a Generics, + lifetimes: HashSet, + types: HashSet, +} +impl<'a> GenericFilter<'a> { + fn new(src: &'a Generics) -> Self { + GenericFilter { + src, + lifetimes: HashSet::new(), + types: HashSet::new(), + } + } + fn finish(self) -> Generics { + let mut params: Punctuated = Default::default(); + let mut where_clause = self + .src + .where_clause + .as_ref() + .map(|wc| WhereClause { + where_token: wc.where_token, + predicates: Default::default(), + }) + .unwrap_or_else(|| WhereClause { + where_token: Where(Span::call_site()), + predicates: Default::default(), + }); + for src_param in &self.src.params { + match src_param { + GenericParam::Lifetime(l) if self.lifetimes.contains(&l.lifetime) => { + params.push(src_param.clone()) + } + GenericParam::Type(t) if self.types.contains(&t.ident) => { + params.push(src_param.clone()) + } + _ => (), + } + } + for src_predicate in self.src.where_clause.iter().flat_map(|wc| &wc.predicates) { + match src_predicate { + WherePredicate::Lifetime(l) if self.lifetimes.contains(&l.lifetime) => { + where_clause.predicates.push(src_predicate.clone()) + } + WherePredicate::Type(PredicateType { + bounded_ty: Type::Path(t), + .. + }) if self.types.contains(&t.path.segments.last().unwrap().ident) => { + where_clause.predicates.push(src_predicate.clone()) + } + _ => (), + } + } + Generics { + lt_token: if params.is_empty() { + None + } else { + self.src.lt_token.clone() + }, + gt_token: if params.is_empty() { + None + } else { + self.src.gt_token.clone() + }, + params, + where_clause: if where_clause.predicates.is_empty() { + None + } else { + Some(where_clause) + }, + } + } +} +impl<'a> Fold for GenericFilter<'a> { + fn fold_lifetime(&mut self, i: Lifetime) -> Lifetime { + self.lifetimes + .extend(self.src.params.iter().filter_map(|param| match param { + GenericParam::Lifetime(l) if l.lifetime == i => Some(l.lifetime.clone()), + _ => None, + })); + i + } + fn fold_type(&mut self, i: Type) -> Type { + self.types.extend( + self.src + .params + .iter() + .filter_map(|param| match (param, &i) { + (GenericParam::Type(t), Type::Path(i)) if i.path.is_ident(&t.ident) => { + Some(t.ident.clone()) + } + _ => None, + }), + ); + i + } +} + +fn rpc_handler( + fn_name: &Ident, + fn_generics: &Generics, + opt: &Options, + params: &[ParamType], +) -> TokenStream { + let mut param_def = Vec::new(); + let mut ctx_ty = quote! { () }; + for param in params { + match param { + ParamType::Arg(arg) => { + let name = arg.name.clone().unwrap(); + let rename = LitStr::new(&name.to_string(), name.span()); + let field_name = Ident::new(&format!("arg_{}", name), name.span()); + let ty = arg.ty.clone(); + param_def.push(quote! { + #[serde(rename = #rename)] + #field_name: #ty, + }) + } + ParamType::Context(ctx) => { + ctx_ty = quote! { #ctx }; + } + _ => (), + } + } + let (_, fn_type_generics, _) = fn_generics.split_for_impl(); + let fn_turbofish = fn_type_generics.as_turbofish(); + let fn_path: Path = macro_try!(syn::parse2(quote! { super::#fn_name#fn_turbofish })); + let mut param_generics_filter = GenericFilter::new(fn_generics); + for param in params { + if let ParamType::Arg(a) = param { + param_generics_filter.fold_type(a.ty.clone()); + } + } + let param_generics = param_generics_filter.finish(); + let (_, param_ty_generics, _) = param_generics.split_for_impl(); + let param_struct_def = quote! { + #[derive(rpc_toolkit_prelude::Deserialize)] + pub struct Params#param_ty_generics { + #( + #param_def + )* + #[serde(flatten)] + #[serde(default)] + rest: rpc_toolkit_prelude::Value, + } + }; + let param = params.iter().map(|param| match param { + ParamType::Arg(arg) => { + let name = arg.name.clone().unwrap(); + let field_name = Ident::new(&format!("arg_{}", name), name.span()); + quote! { args.#field_name } + } + ParamType::Context(_) => quote! { ctx }, + _ => unreachable!(), + }); + match opt { + Options::Leaf(opt) if matches!(opt.exec_ctx, ExecutionContext::LocalOnly(_)) => quote! { + #param_struct_def + + pub async fn rpc_handler#fn_generics( + _ctx: #ctx_ty, + method: &str, + _args: Params#param_ty_generics, + ) -> Result { + Err(RpcError { + data: Some(method.into()), + ..rpc_toolkit_prelude::yajrc::METHOD_NOT_FOUND_ERROR + }) + } + }, + Options::Leaf(opt) => { + let invocation = if opt.is_async { + quote! { + #fn_path(#(#param),*).await? + } + } else if opt.blocking.is_some() { + quote! { + rpc_toolkit_prelude::spawn_blocking(move || #fn_path(#(#param),*)).await? + } + } else { + quote! { + #fn_path(#(#param),*)? + } + }; + quote! { + #param_struct_def + + pub async fn rpc_handler#fn_generics( + ctx: #ctx_ty, + method: &str, + args: Params#param_ty_generics, + ) -> Result { + Ok(rpc_toolkit_prelude::to_value(#invocation)?) + } + } + } + Options::Parent(ParentOptions { + common, + subcommands, + self_impl, + }) => { + let cmd_preprocess = if common.is_async { + quote! { + let ctx = #fn_path(#(#param),*).await?; + } + } else if common.blocking.is_some() { + quote! { + let ctx = rpc_toolkit_prelude::spawn_blocking(move || #fn_path(#(#param),*)).await?; + } + } else { + quote! { + let ctx = #fn_path(#(#param),*)?; + } + }; + let subcmd_impl = subcommands.iter().map(|subcommand| { + let mut subcommand = subcommand.clone(); + let rpc_handler = PathSegment { + ident: Ident::new("rpc_handler", Span::call_site()), + arguments: std::mem::replace( + &mut subcommand.segments.last_mut().unwrap().arguments, + PathArguments::None, + ), + }; + quote_spanned!{ subcommand.span() => + [#subcommand::NAME, rest] => #subcommand::#rpc_handler(ctx, rest, rpc_toolkit_prelude::from_value(args.rest)?).await + } + }); + let subcmd_impl = quote! { + match method.splitn(2, ".").chain(std::iter::repeat("")).take(2).collect::>().as_slice() { + #( + #subcmd_impl, + )* + _ => Err(RpcError { + data: Some(method.into()), + ..rpc_toolkit_prelude::yajrc::METHOD_NOT_FOUND_ERROR + }) + } + }; + match self_impl { + Some(self_impl) if !matches!(common.exec_ctx, ExecutionContext::LocalOnly(_)) => { + let self_impl_fn = &self_impl.path; + let self_impl = if self_impl.is_async { + quote_spanned! { self_impl_fn.span() => + #self_impl_fn(ctx).await? + } + } else if self_impl.blocking { + quote_spanned! { self_impl_fn.span() => + rpc_toolkit_prelude::spawn_blocking(move || #self_impl_fn(ctx)).await? + } + } else { + quote_spanned! { self_impl_fn.span() => + #self_impl_fn(ctx)? + } + }; + quote! { + #param_struct_def + + pub async fn rpc_handler#fn_generics( + ctx: #ctx_ty, + method: &str, + args: Params#param_ty_generics, + ) -> Result { + #cmd_preprocess + + if method.is_empty() { + Ok(rpc_toolkit_prelude::to_value(&#self_impl)?) + } else { + #subcmd_impl + } + } + } + } + _ => { + quote! { + #param_struct_def + + pub async fn rpc_handler#fn_generics( + ctx: #ctx_ty, + method: &str, + args: Params#param_ty_generics, + ) -> Result { + #cmd_preprocess + + #subcmd_impl + } + } + } + } + } + } +} + +fn cli_handler( + fn_name: &Ident, + fn_generics: &Generics, + opt: &mut Options, + params: &[ParamType], +) -> TokenStream { + let mut ctx_ty = quote! { () }; + for param in params { + match param { + ParamType::Context(ctx) => { + ctx_ty = quote! { #ctx }; + } + _ => (), + } + } + let mut generics = fn_generics.clone(); + generics.params.push(macro_try!(syn::parse2( + quote! { ParentParams: rpc_toolkit_prelude::Serialize } + ))); + if generics.lt_token.is_none() { + generics.lt_token = Some(Default::default()); + } + if generics.gt_token.is_none() { + generics.gt_token = Some(Default::default()); + } + let (_, fn_type_generics, _) = fn_generics.split_for_impl(); + let fn_turbofish = fn_type_generics.as_turbofish(); + let fn_path: Path = macro_try!(syn::parse2(quote! { super::#fn_name#fn_turbofish })); + let param = params.iter().map(|param| match param { + ParamType::Arg(arg) => { + let name = arg.name.clone().unwrap(); + let field_name = Ident::new(&format!("arg_{}", name), name.span()); + quote! { params.#field_name.clone() } + } + ParamType::Context(_) => quote! { ctx }, + _ => unreachable!(), + }); + let mut param_generics_filter = GenericFilter::new(fn_generics); + for param in params { + if let ParamType::Arg(a) = param { + param_generics_filter.fold_type(a.ty.clone()); + } + } + let mut param_generics = param_generics_filter.finish(); + param_generics.params.push(macro_try!(syn::parse2(quote! { + ParentParams: rpc_toolkit_prelude::Serialize + }))); + if param_generics.lt_token.is_none() { + generics.lt_token = Some(Default::default()); + } + if param_generics.gt_token.is_none() { + generics.gt_token = Some(Default::default()); + } + let (_, param_ty_generics, _) = param_generics.split_for_impl(); + let mut arg_def = Vec::new(); + for param in params { + match param { + ParamType::Arg(arg) => { + let name = arg.name.clone().unwrap(); + let rename = LitStr::new(&name.to_string(), name.span()); + let field_name = Ident::new(&format!("arg_{}", name), name.span()); + let ty = arg.ty.clone(); + arg_def.push(quote! { + #[serde(rename = #rename)] + #field_name: #ty, + }) + } + _ => (), + } + } + let arg = params + .iter() + .filter_map(|param| { + if let ParamType::Arg(a) = param { + Some(a) + } else { + None + } + }) + .map(|arg| { + let name = arg.name.clone().unwrap(); + let arg_name = LitStr::new(&name.to_string(), name.span()); + let field_name = Ident::new(&format!("arg_{}", name), name.span()); + if arg.stdin { + quote! { + #field_name: rpc_toolkit_prelude::default_stdin_parser(&mut std::io::stdin(), matches)?, + } + } else if arg.check_is_present { + quote! { + #field_name: matches.is_present(#arg_name), + } + } else { + let parse_val = if let Some(parse) = &arg.parse { + quote! { + #parse(arg_val, matches)? + } + } else { + quote! { + rpc_toolkit_prelude::default_arg_parser(arg_val, matches)? + } + }; + if arg.optional { + quote! { + #field_name: if let Some(arg_val) = matches.value_of(#arg_name) { + Some(#parse_val) + } else { + None + }, + } + } else { + quote! { + #field_name: { + let arg_val = matches.value_of(#arg_name).unwrap(); + #parse_val + }, + } + } + } + }); + let param_struct_def = quote! { + #[derive(rpc_toolkit_prelude::Serialize)] + struct Params#param_ty_generics { + #( + #arg_def + )* + #[serde(flatten)] + rest: ParentParams, + } + let params: Params#param_ty_generics = Params { + #( + #arg + )* + rest: parent_params, + }; + }; + let create_rt = quote! { + let rt_ref = if let Some(rt) = rt.as_mut() { + &*rt + } else { + rt = Some(rpc_toolkit_prelude::Runtime::new().map_err(|e| RpcError { + data: Some(format!("{}", e).into()), + ..rpc_toolkit_prelude::yajrc::INTERNAL_ERROR + })?); + rt.as_ref().unwrap() + }; + }; + let display = if let Some(display) = &opt.common().display { + quote! { #display } + } else { + quote! { rpc_toolkit_prelude::default_display } + }; + match opt { + Options::Leaf(opt) if matches!(opt.exec_ctx, ExecutionContext::RemoteOnly(_)) => quote! { + pub fn cli_handler#generics( + _ctx: #ctx_ty, + _rt: Option, + _matches: &rpc_toolkit_prelude::ArgMatches<'_>, + method: rpc_toolkit_prelude::Cow<'_, str>, + _parent_params: ParentParams, + ) -> Result<(), rpc_toolkit_prelude::RpcError> { + Err(RpcError { + data: Some(method.into()), + ..rpc_toolkit_prelude::yajrc::METHOD_NOT_FOUND_ERROR + }) + } + }, + Options::Leaf(opt) if matches!(opt.exec_ctx, ExecutionContext::LocalOnly(_)) => { + let invocation = if opt.is_async { + quote! { + rt_ref.block_on(#fn_path(#(#param),*))? + } + } else { + quote! { + #fn_path(#(#param),*)? + } + }; + let display_res = if let Some(display_fn) = &opt.display { + quote! { + #display_fn(#invocation) + } + } else { + quote! { + rpc_toolkit_prelude::default_display(#invocation) + } + }; + let rt_action = if opt.is_async { + create_rt + } else { + quote! { + drop(rt); + } + }; + quote! { + pub fn cli_handler#generics( + ctx: #ctx_ty, + mut rt: Option, + matches: &rpc_toolkit_prelude::ArgMatches<'_>, + _method: rpc_toolkit_prelude::Cow<'_, str>, + _parent_params: ParentParams + ) -> Result<(), rpc_toolkit_prelude::RpcError> { + #rt_action + Ok(#display_res) + } + } + } + Options::Leaf(opt) => { + let param = param.map(|_| quote! { unreachable!() }); + let invocation = if opt.is_async { + quote! { + rt_ref.block_on(#fn_path(#(#param),*))? + } + } else { + quote! { + #fn_path(#(#param),*)? + } + }; + quote! { + pub fn cli_handler#generics( + ctx: #ctx_ty, + mut rt: Option, + matches: &rpc_toolkit_prelude::ArgMatches<'_>, + method: rpc_toolkit_prelude::Cow<'_, str>, + parent_params: ParentParams, + ) -> Result<(), rpc_toolkit_prelude::RpcError> { + #param_struct_def + + #create_rt + + #[allow(unreachable_code)] + let return_ty = if true { + rpc_toolkit_prelude::PhantomData + } else { + rpc_toolkit_prelude::make_phantom(#invocation) + }; + + let res = rt_ref.block_on(rpc_toolkit_prelude::call_remote(ctx, method.as_ref(), params, return_ty))?; + Ok(#display(res.result?)) + } + } + } + Options::Parent(ParentOptions { + common, + subcommands, + self_impl, + }) => { + let cmd_preprocess = if common.is_async { + quote! { + #create_rt + let ctx = rt_ref.block_on(#fn_path(#(#param),*))?; + } + } else { + quote! { + let ctx = #fn_path(#(#param),*)?; + } + }; + let subcmd_impl = subcommands.iter().map(|subcommand| { + let mut subcommand = subcommand.clone(); + let mut cli_handler = PathSegment { + ident: Ident::new("cli_handler", Span::call_site()), + arguments: std::mem::replace( + &mut subcommand.segments.last_mut().unwrap().arguments, + PathArguments::None, + ), + }; + cli_handler.arguments = match cli_handler.arguments { + PathArguments::None => PathArguments::AngleBracketed( + syn::parse2(quote! { :: }).unwrap(), + ), + PathArguments::AngleBracketed(mut a) => { + a.args + .push(syn::parse2(quote! { Params#param_ty_generics }).unwrap()); + PathArguments::AngleBracketed(a) + } + _ => unreachable!(), + }; + quote_spanned! { subcommand.span() => + (#subcommand::NAME, Some(sub_m)) => { + let method = if method.is_empty() { + #subcommand::NAME.into() + } else { + method + "." + #subcommand::NAME + }; + #subcommand::#cli_handler(ctx, rt, sub_m, method, params) + }, + } + }); + let self_impl = match (self_impl, &common.exec_ctx) { + (Some(self_impl), ExecutionContext::LocalOnly(_)) => { + let self_impl_fn = &self_impl.path; + let create_rt = if common.is_async { + None + } else { + Some(create_rt) + }; + let self_impl = if self_impl.is_async { + quote_spanned! { self_impl_fn.span() => + #create_rt + rt_ref.block_on(#self_impl_fn(ctx))? + } + } else { + quote_spanned! { self_impl_fn.span() => + #self_impl_fn(ctx)? + } + }; + quote! { + Ok(#display(#self_impl)), + } + } + (Some(self_impl), ExecutionContext::Standard) => { + let self_impl_fn = &self_impl.path; + let self_impl = if self_impl.is_async { + quote! { + rt_ref.block_on(#self_impl_fn(ctx)) + } + } else { + quote! { + #self_impl_fn(ctx) + } + }; + let create_rt = if common.is_async { + None + } else { + Some(create_rt) + }; + quote! { + { + #create_rt + + #[allow(unreachable_code)] + let return_ty = if true { + rpc_toolkit_prelude::PhantomData + } else { + let ctx_new = unreachable!(); + rpc_toolkit_prelude::match_types(&ctx, &ctx_new); + let ctx = ctx_new; + rpc_toolkit_prelude::make_phantom(#self_impl?) + }; + + let res = rt_ref.block_on(rpc_toolkit_prelude::call_remote(ctx, method.as_ref(), params, return_ty))?; + Ok(#display(res.result?)) + } + } + } + _ => quote! { + Err(RpcError { + data: Some(method.into()), + ..rpc_toolkit_prelude::yajrc::METHOD_NOT_FOUND_ERROR + }), + }, + }; + quote! { + pub fn cli_handler#generics( + ctx: #ctx_ty, + mut rt: Option, + matches: &rpc_toolkit_prelude::ArgMatches<'_>, + method: rpc_toolkit_prelude::Cow<'_, str>, + parent_params: ParentParams, + ) -> Result<(), rpc_toolkit_prelude::RpcError> { + #param_struct_def + + #cmd_preprocess + + match matches.subcommand() { + #( + #subcmd_impl + )* + _ => #self_impl + } + } + } + } + } +} + +pub fn build(args: AttributeArgs, mut item: ItemFn) -> TokenStream { + let mut params = macro_try!(parse_param_attrs(&mut item)); + let mut opt = macro_try!(parse_command_attr(args)); + if let Some(a) = &opt.common().blocking { + if item.sig.asyncness.is_some() { + return Error::new(a.span(), "cannot use `blocking` on an async fn").to_compile_error(); + } + } + opt.common().is_async = item.sig.asyncness.is_some(); + let fn_vis = &item.vis; + let fn_name = &item.sig.ident; + let fn_generics = &item.sig.generics; + let command_name = opt + .common() + .rename + .clone() + .unwrap_or_else(|| fn_name.clone()); + let command_name_str = LitStr::new(&command_name.to_string(), command_name.span()); + let is_async = LitBool::new( + opt.common().is_async, + item.sig + .asyncness + .map(|a| a.span()) + .unwrap_or_else(Span::call_site), + ); + let build_app = build_app(command_name_str.clone(), &mut opt, &mut params); + let rpc_handler = rpc_handler(fn_name, fn_generics, &opt, ¶ms); + let cli_handler = cli_handler(fn_name, fn_generics, &mut opt, ¶ms); + + let res = quote! { + #item + #fn_vis mod #fn_name { + use super::*; + use rpc_toolkit::command_helpers::prelude as rpc_toolkit_prelude; + + pub const NAME: &'static str = #command_name_str; + pub const ASYNC: bool = #is_async; + + #build_app + + #rpc_handler + + #cli_handler + } + }; + // panic!("{}", res); + res +} diff --git a/rpc-toolkit-macro-internals/src/command/mod.rs b/rpc-toolkit-macro-internals/src/command/mod.rs new file mode 100644 index 0000000..6fab29e --- /dev/null +++ b/rpc-toolkit-macro-internals/src/command/mod.rs @@ -0,0 +1,85 @@ +use syn::*; + +pub mod build; +mod parse; + +pub enum ExecutionContext { + Standard, + LocalOnly(Path), + RemoteOnly(Path), +} +impl Default for ExecutionContext { + fn default() -> Self { + ExecutionContext::Standard + } +} + +#[derive(Default)] +pub struct LeafOptions { + blocking: Option, + is_async: bool, + about: Option, + rename: Option, + exec_ctx: ExecutionContext, + display: Option, +} + +pub struct SelfImplInfo { + path: Path, + is_async: bool, + blocking: bool, +} +pub struct ParentOptions { + common: LeafOptions, + subcommands: Vec, + self_impl: Option, +} +impl From for ParentOptions { + fn from(opt: LeafOptions) -> Self { + ParentOptions { + common: opt, + subcommands: Default::default(), + self_impl: Default::default(), + } + } +} + +pub enum Options { + Leaf(LeafOptions), + Parent(ParentOptions), +} +impl Options { + fn to_parent(&mut self) -> Result<&mut ParentOptions> { + if let Options::Leaf(opt) = self { + *self = Options::Parent(std::mem::replace(opt, Default::default()).into()); + } + Ok(match self { + Options::Parent(a) => a, + _ => unreachable!(), + }) + } + fn common(&mut self) -> &mut LeafOptions { + match self { + Options::Leaf(ref mut opt) => opt, + Options::Parent(opt) => &mut opt.common, + } + } +} + +pub struct ArgOptions { + ty: Type, + optional: bool, + check_is_present: bool, + help: Option, + name: Option, + short: Option, + long: Option, + parse: Option, + stdin: bool, +} + +pub enum ParamType { + None, + Arg(ArgOptions), + Context(Type), +} diff --git a/rpc-toolkit-macro-internals/src/command/parse.rs b/rpc-toolkit-macro-internals/src/command/parse.rs new file mode 100644 index 0000000..ff0300a --- /dev/null +++ b/rpc-toolkit-macro-internals/src/command/parse.rs @@ -0,0 +1,537 @@ +use super::*; +use syn::spanned::Spanned; + +pub fn parse_command_attr(args: AttributeArgs) -> Result { + let mut opt = Options::Leaf(Default::default()); + for arg in args { + match arg { + NestedMeta::Meta(Meta::List(list)) if list.path.is_ident("subcommands") => { + let inner = opt.to_parent()?; + if !inner.subcommands.is_empty() { + return Err(Error::new(list.span(), "duplicate argument `subcommands`")); + } + for subcmd in list.nested { + match subcmd { + NestedMeta::Meta(Meta::Path(subcmd)) => inner.subcommands.push(subcmd), + NestedMeta::Lit(Lit::Str(s)) => { + inner.subcommands.push(syn::parse_str(&s.value())?) + } + NestedMeta::Meta(Meta::List(mut self_impl)) + if self_impl.path.is_ident("self") => + { + if self_impl.nested.len() == 1 { + match self_impl.nested.pop().unwrap().into_value() { + NestedMeta::Meta(Meta::Path(self_impl)) => { + if inner.self_impl.is_some() { + return Err(Error::new( + self_impl.span(), + "duplicate argument `self`", + )); + } + inner.self_impl = Some(SelfImplInfo { + path: self_impl, + is_async: false, + blocking: false, + }) + } + NestedMeta::Meta(Meta::List(l)) if l.nested.len() == 1 => { + if inner.self_impl.is_some() { + return Err(Error::new( + self_impl.span(), + "duplicate argument `self`", + )); + } + let blocking = match l.nested.first().unwrap() { + NestedMeta::Meta(Meta::Path(p)) if p.is_ident("blocking") => true, + NestedMeta::Meta(Meta::Path(p)) if p.is_ident("async") => false, + arg => return Err(Error::new(arg.span(), "unknown argument")), + }; + inner.self_impl = Some(SelfImplInfo { + path: l.path, + is_async: !blocking, + blocking, + }) + } + a => { + return Err(Error::new( + a.span(), + "`self` implementation must be a path, or a list with 1 argument", + )) + } + } + } else { + return Err(Error::new( + self_impl.nested.span(), + "`self` can only have one implementation", + )); + } + } + arg => { + return Err(Error::new(arg.span(), "unknown argument to `subcommands`")) + } + } + } + if inner.subcommands.is_empty() { + return Err(Error::new( + list.path.span(), + "`subcommands` requires at least 1 argument", + )); + } + } + NestedMeta::Meta(Meta::Path(p)) if p.is_ident("subcommands") => { + return Err(Error::new( + p.span(), + "`subcommands` requires at least 1 argument", + )); + } + NestedMeta::Meta(Meta::NameValue(nv)) if nv.path.is_ident("subcommands") => { + return Err(Error::new( + nv.path.span(), + "`subcommands` cannot be assigned to", + )); + } + NestedMeta::Meta(Meta::List(list)) if list.path.is_ident("display") => { + if list.nested.len() == 1 { + match &list.nested[0] { + NestedMeta::Meta(Meta::Path(display_impl)) => { + if opt.common().display.is_some() { + return Err(Error::new( + display_impl.span(), + "duplicate argument `display`", + )); + } + opt.common().display = Some(display_impl.clone()) + } + a => { + return Err(Error::new( + a.span(), + "`display` implementation must be a path", + )) + } + } + } else { + return Err(Error::new( + list.nested.span(), + "`display` can only have one implementation", + )); + } + } + NestedMeta::Meta(Meta::Path(p)) if p.is_ident("display") => { + return Err(Error::new(p.span(), "`display` requires an argument")); + } + NestedMeta::Meta(Meta::NameValue(nv)) if nv.path.is_ident("display") => { + return Err(Error::new( + nv.path.span(), + "`display` cannot be assigned to", + )); + } + NestedMeta::Meta(Meta::Path(p)) if p.is_ident("local") => { + match &opt.common().exec_ctx { + ExecutionContext::Standard => { + opt.common().exec_ctx = ExecutionContext::LocalOnly(p) + } + ExecutionContext::LocalOnly(_) => { + return Err(Error::new(p.span(), "duplicate argument: `local`")) + } + ExecutionContext::RemoteOnly(_) => { + return Err(Error::new( + p.span(), + "`local` and `remote` are mutually exclusive", + )) + } + } + } + NestedMeta::Meta(Meta::List(list)) if list.path.is_ident("local") => { + return Err(Error::new( + list.path.span(), + "`local` does not take any arguments", + )); + } + NestedMeta::Meta(Meta::NameValue(nv)) if nv.path.is_ident("local") => { + return Err(Error::new(nv.path.span(), "`local` cannot be assigned to")); + } + NestedMeta::Meta(Meta::Path(p)) if p.is_ident("remote") => { + match &opt.common().exec_ctx { + ExecutionContext::Standard => { + opt.common().exec_ctx = ExecutionContext::RemoteOnly(p) + } + ExecutionContext::LocalOnly(_) => { + return Err(Error::new(p.span(), "duplicate argument: `remote`")) + } + ExecutionContext::RemoteOnly(_) => { + return Err(Error::new( + p.span(), + "`local` and `remote` are mutually exclusive", + )) + } + } + } + NestedMeta::Meta(Meta::List(list)) if list.path.is_ident("remote") => { + return Err(Error::new( + list.path.span(), + "`remote` does not take any arguments", + )); + } + NestedMeta::Meta(Meta::NameValue(nv)) if nv.path.is_ident("remote") => { + return Err(Error::new(nv.path.span(), "`remote` cannot be assigned to")); + } + NestedMeta::Meta(Meta::Path(p)) if p.is_ident("blocking") => { + if opt.common().blocking.is_some() { + return Err(Error::new(p.span(), "duplicate argument `blocking`")); + } + opt.common().blocking = Some(p); + } + NestedMeta::Meta(Meta::List(list)) if list.path.is_ident("blocking") => { + return Err(Error::new( + list.path.span(), + "`blocking` does not take any arguments", + )); + } + NestedMeta::Meta(Meta::NameValue(nv)) if nv.path.is_ident("blocking") => { + return Err(Error::new( + nv.path.span(), + "`blocking` cannot be assigned to", + )); + } + NestedMeta::Meta(Meta::NameValue(nv)) if nv.path.is_ident("about") => { + if let Lit::Str(about) = nv.lit { + if opt.common().about.is_some() { + return Err(Error::new(about.span(), "duplicate argument `about`")); + } + opt.common().about = Some(about); + } else { + return Err(Error::new(nv.lit.span(), "about message must be a string")); + } + } + NestedMeta::Meta(Meta::List(list)) if list.path.is_ident("about") => { + return Err(Error::new( + list.path.span(), + "`about` does not take any arguments", + )); + } + NestedMeta::Meta(Meta::Path(p)) if p.is_ident("about") => { + return Err(Error::new(p.span(), "`about` must be assigned to")); + } + NestedMeta::Meta(Meta::NameValue(nv)) if nv.path.is_ident("rename") => { + if let Lit::Str(rename) = nv.lit { + if opt.common().rename.is_some() { + return Err(Error::new(rename.span(), "duplicate argument `rename`")); + } + opt.common().rename = Some( + syn::parse_str(&rename.value()) + .map_err(|e| Error::new(rename.span(), format!("{}", e)))?, + ); + } else { + return Err(Error::new(nv.lit.span(), "`rename` must be a string")); + } + } + NestedMeta::Meta(Meta::List(list)) if list.path.is_ident("rename") => { + return Err(Error::new( + list.path.span(), + "`rename` does not take any arguments", + )); + } + NestedMeta::Meta(Meta::Path(p)) if p.is_ident("rename") => { + return Err(Error::new(p.span(), "`rename` must be assigned to")); + } + _ => { + return Err(Error::new(arg.span(), "unknown argument")); + } + } + } + if let Options::Parent(opt) = &opt { + if opt.self_impl.is_none() { + if let Some(display) = &opt.common.display { + return Err(Error::new( + display.span(), + "cannot define `display` for a command without an implementation", + )); + } + match &opt.common.exec_ctx { + ExecutionContext::LocalOnly(local) => { + return Err(Error::new( + local.span(), + "cannot define `local` for a command without an implementation", + )) + } + ExecutionContext::RemoteOnly(remote) => { + return Err(Error::new( + remote.span(), + "cannot define `remote` for a command without an implementation", + )) + } + _ => (), + } + } + } + Ok(opt) +} + +pub fn parse_arg_attr(attr: Attribute, arg: PatType, has_stdin: &mut bool) -> Result { + let arg_span = arg.span(); + let mut opt = ArgOptions { + ty: *arg.ty, + optional: false, + check_is_present: false, + help: None, + name: match *arg.pat { + Pat::Ident(i) => Some(i.ident), + _ => None, + }, + short: None, + long: None, + parse: None, + stdin: false, + }; + match attr.parse_meta()? { + Meta::List(list) => { + for arg in list.nested { + match arg { + NestedMeta::Meta(Meta::List(mut list)) if list.path.is_ident("parse") => { + if list.nested.len() == 1 { + match list.nested.pop().unwrap().into_value() { + NestedMeta::Meta(Meta::Path(parse_impl)) => { + if opt.parse.is_some() { + return Err(Error::new( + list.span(), + "duplicate argument `parse`", + )); + } + opt.parse = Some(parse_impl) + } + a => { + return Err(Error::new( + a.span(), + "`parse` implementation must be a path", + )) + } + } + } else { + return Err(Error::new( + list.nested.span(), + "`parse` can only have one implementation", + )); + } + } + NestedMeta::Meta(Meta::Path(p)) if p.is_ident("parse") => { + return Err(Error::new(p.span(), "`parse` requires an argument")); + } + NestedMeta::Meta(Meta::NameValue(nv)) if nv.path.is_ident("parse") => { + return Err(Error::new(nv.path.span(), "`parse` cannot be assigned to")); + } + NestedMeta::Meta(Meta::Path(p)) if p.is_ident("stdin") => { + if *has_stdin { + return Err(Error::new(p.span(), "duplicate argument `stdin`")); + } + if opt.short.is_some() { + return Err(Error::new( + p.span(), + "`stdin` and `short` are mutually exclusive", + )); + } + if opt.long.is_some() { + return Err(Error::new( + p.span(), + "`stdin` and `long` are mutually exclusive", + )); + } + if opt.help.is_some() { + return Err(Error::new( + p.span(), + "`stdin` and `help` are mutually exclusive", + )); + } + opt.stdin = true; + *has_stdin = true; + } + NestedMeta::Meta(Meta::List(list)) if list.path.is_ident("stdin") => { + return Err(Error::new( + list.path.span(), + "`stdin` does not take any arguments", + )); + } + NestedMeta::Meta(Meta::NameValue(nv)) if nv.path.is_ident("stdin") => { + return Err(Error::new(nv.path.span(), "`stdin` cannot be assigned to")); + } + NestedMeta::Meta(Meta::NameValue(nv)) if nv.path.is_ident("help") => { + if let Lit::Str(help) = nv.lit { + if opt.help.is_some() { + return Err(Error::new(help.span(), "duplicate argument `help`")); + } + if opt.stdin { + return Err(Error::new( + help.span(), + "`stdin` and `help` are mutually exclusive", + )); + } + opt.help = Some(help); + } else { + return Err(Error::new(nv.lit.span(), "help message must be a string")); + } + } + NestedMeta::Meta(Meta::List(list)) if list.path.is_ident("help") => { + return Err(Error::new( + list.path.span(), + "`help` does not take any arguments", + )); + } + NestedMeta::Meta(Meta::Path(p)) if p.is_ident("help") => { + return Err(Error::new(p.span(), "`help` must be assigned to")); + } + NestedMeta::Meta(Meta::NameValue(nv)) if nv.path.is_ident("rename") => { + if let Lit::Str(rename) = nv.lit { + if opt.name.is_some() { + return Err(Error::new( + rename.span(), + "duplicate argument `rename`", + )); + } + opt.name = Some( + syn::parse_str(&rename.value()) + .map_err(|e| Error::new(rename.span(), format!("{}", e)))?, + ); + } else { + return Err(Error::new(nv.lit.span(), "`rename` must be a string")); + } + } + NestedMeta::Meta(Meta::List(list)) if list.path.is_ident("rename") => { + return Err(Error::new( + list.path.span(), + "`rename` does not take any arguments", + )); + } + NestedMeta::Meta(Meta::Path(p)) if p.is_ident("rename") => { + return Err(Error::new(p.span(), "`rename` must be assigned to")); + } + NestedMeta::Meta(Meta::NameValue(nv)) if nv.path.is_ident("short") => { + if let Lit::Str(short) = nv.lit { + if short.value().len() != 1 { + return Err(Error::new( + short.span(), + "`short` value must be 1 character", + )); + } + if opt.short.is_some() { + return Err(Error::new(short.span(), "duplicate argument `short`")); + } + if opt.stdin { + return Err(Error::new( + short.span(), + "`stdin` and `short` are mutually exclusive", + )); + } + opt.short = Some(short); + } else { + return Err(Error::new(nv.lit.span(), "`short` must be a string")); + } + } + NestedMeta::Meta(Meta::List(list)) if list.path.is_ident("short") => { + return Err(Error::new( + list.path.span(), + "`short` does not take any arguments", + )); + } + NestedMeta::Meta(Meta::Path(p)) if p.is_ident("short") => { + return Err(Error::new(p.span(), "`short` must be assigned to")); + } + NestedMeta::Meta(Meta::NameValue(nv)) if nv.path.is_ident("long") => { + if let Lit::Str(long) = nv.lit { + if opt.long.is_some() { + return Err(Error::new(long.span(), "duplicate argument `long`")); + } + if opt.stdin { + return Err(Error::new( + long.span(), + "`stdin` and `long` are mutually exclusive", + )); + } + opt.long = Some(long); + } else { + return Err(Error::new(nv.lit.span(), "`long` must be a string")); + } + } + NestedMeta::Meta(Meta::List(list)) if list.path.is_ident("long") => { + return Err(Error::new( + list.path.span(), + "`long` does not take any arguments", + )); + } + NestedMeta::Meta(Meta::Path(p)) if p.is_ident("long") => { + return Err(Error::new(p.span(), "`long` must be assigned to")); + } + _ => { + return Err(Error::new(arg.span(), "unknown argument")); + } + } + } + } + Meta::Path(_) => (), + Meta::NameValue(nv) => return Err(Error::new(nv.span(), "`arg` cannot be assigned to")), + } + if opt.name.is_none() { + return Err(Error::new( + arg_span, + "cannot infer name for pattern argument", + )); + } + Ok(opt) +} + +pub fn parse_param_attrs(item: &mut ItemFn) -> Result> { + let mut params = Vec::new(); + let mut has_stdin = false; + for param in item.sig.inputs.iter_mut() { + if let FnArg::Typed(param) = param { + let mut ty = ParamType::None; + let mut i = 0; + while i != param.attrs.len() { + if param.attrs[i].path.is_ident("arg") { + let attr = param.attrs.remove(i); + if matches!(ty, ParamType::None) { + ty = ParamType::Arg(parse_arg_attr(attr, param.clone(), &mut has_stdin)?); + } else if matches!(ty, ParamType::Arg(_)) { + return Err(Error::new( + attr.span(), + "`arg` attribute may only be specified once", + )); + } else if matches!(ty, ParamType::Context(_)) { + return Err(Error::new( + attr.span(), + "`arg` and `context` are mutually exclusive", + )); + } + } else if param.attrs[i].path.is_ident("context") { + let attr = param.attrs.remove(i); + if matches!(ty, ParamType::None) { + ty = ParamType::Context(*param.ty.clone()); + } else if matches!(ty, ParamType::Context(_)) { + return Err(Error::new( + attr.span(), + "`context` attribute may only be specified once", + )); + } else if matches!(ty, ParamType::Arg(_)) { + return Err(Error::new( + attr.span(), + "`arg` and `context` are mutually exclusive", + )); + } + } else { + i += 1; + } + } + if matches!(ty, ParamType::None) { + return Err(Error::new( + param.span(), + "must specify either `arg` or `context` attributes", + )); + } + params.push(ty) + } else { + return Err(Error::new( + param.span(), + "commands may not take `self` as an argument", + )); + } + } + Ok(params) +} diff --git a/rpc-toolkit-macro-internals/src/lib.rs b/rpc-toolkit-macro-internals/src/lib.rs new file mode 100644 index 0000000..71facb6 --- /dev/null +++ b/rpc-toolkit-macro-internals/src/lib.rs @@ -0,0 +1,18 @@ +macro_rules! macro_try { + ($x:expr) => { + match $x { + Ok(a) => a, + Err(e) => return e.to_compile_error(), + } + }; +} + +mod command; +mod rpc_server; +mod run_cli; + +pub use command::build::build as build_command; +pub use rpc_server::build::build as build_rpc_server; +pub use rpc_server::RpcServerArgs; +pub use run_cli::build::build as build_run_cli; +pub use run_cli::RunCliArgs; \ No newline at end of file diff --git a/rpc-toolkit-macro-internals/src/rpc_server/build.rs b/rpc-toolkit-macro-internals/src/rpc_server/build.rs new file mode 100644 index 0000000..aa66403 --- /dev/null +++ b/rpc-toolkit-macro-internals/src/rpc_server/build.rs @@ -0,0 +1,55 @@ +use super::*; +use proc_macro2::TokenStream; +use quote::quote; +use syn::spanned::Spanned; + +pub fn build(args: RpcServerArgs) -> TokenStream { + let mut command = args.command; + let arguments = std::mem::replace( + &mut command.segments.last_mut().unwrap().arguments, + PathArguments::None, + ); + command.segments.push(PathSegment { + ident: Ident::new("rpc_handler", command.span()), + arguments, + }); + let seed = args.seed; + let status_fn = args + .status_fn + .unwrap_or_else(|| syn::parse2(quote! { |_| rpc_toolkit::hyper::StatusCode::OK }).unwrap()); + quote! { + { + let seed = #seed; + let status_fn = #status_fn; + let (builder, ctx_phantom) = rpc_toolkit::rpc_server_helpers::make_builder(seed.clone()); + let make_svc = rpc_toolkit::hyper::service::make_service_fn(move |_| { + let seed = seed.clone(); + async move { + Ok::<_, hyper::Error>(rpc_toolkit::hyper::service::service_fn(move |mut req| { + let seed = seed.clone(); + async move { + let rpc_req = rpc_toolkit::rpc_server_helpers::make_request(&mut req).await; + rpc_toolkit::rpc_server_helpers::to_response( + &req, + match rpc_req { + Ok(rpc_req) => Ok(( + rpc_req.id, + #command( + rpc_toolkit::rpc_server_helpers::bind_type(ctx_phantom, rpc_toolkit::SeedableContext::new(seed)), + rpc_toolkit::yajrc::RpcMethod::as_str(&rpc_req.method), + rpc_req.params, + ) + .await, + )), + Err(e) => Err(e), + }, + status_fn, + ) + } + })) + } + }); + builder.serve(make_svc) + } + } +} diff --git a/rpc-toolkit-macro-internals/src/rpc_server/mod.rs b/rpc-toolkit-macro-internals/src/rpc_server/mod.rs new file mode 100644 index 0000000..9377905 --- /dev/null +++ b/rpc-toolkit-macro-internals/src/rpc_server/mod.rs @@ -0,0 +1,10 @@ +use syn::*; + +pub struct RpcServerArgs { + command: Path, + seed: Expr, + status_fn: Option, +} + +pub mod build; +mod parse; diff --git a/rpc-toolkit-macro-internals/src/rpc_server/parse.rs b/rpc-toolkit-macro-internals/src/rpc_server/parse.rs new file mode 100644 index 0000000..7947997 --- /dev/null +++ b/rpc-toolkit-macro-internals/src/rpc_server/parse.rs @@ -0,0 +1,23 @@ +use super::*; +use syn::parse::{Parse, ParseStream}; + +impl Parse for RpcServerArgs { + fn parse(input: ParseStream) -> Result { + let command = input.parse()?; + let _: token::Comma = input.parse()?; + let seed = input.parse()?; + if !input.is_empty() { + let _: token::Comma = input.parse()?; + } + let status_fn = if !input.is_empty() { + Some(input.parse()?) + } else { + None + }; + Ok(RpcServerArgs { + command, + seed, + status_fn, + }) + } +} diff --git a/rpc-toolkit-macro-internals/src/run_cli/build.rs b/rpc-toolkit-macro-internals/src/run_cli/build.rs new file mode 100644 index 0000000..c50eabd --- /dev/null +++ b/rpc-toolkit-macro-internals/src/run_cli/build.rs @@ -0,0 +1,71 @@ +use super::*; +use proc_macro2::TokenStream; +use quote::quote; +use syn::spanned::Spanned; + +pub fn build(args: RunCliArgs) -> TokenStream { + let mut command_handler = args.command.clone(); + let mut arguments = std::mem::replace( + &mut command_handler.segments.last_mut().unwrap().arguments, + PathArguments::None, + ); + let command = command_handler.clone(); + if let PathArguments::AngleBracketed(a) = &mut arguments { + a.args.push(syn::parse2(quote! { () }).unwrap()); + } + command_handler.segments.push(PathSegment { + ident: Ident::new("cli_handler", command.span()), + arguments, + }); + let app = if let Some(mut_app) = args.mut_app { + let ident = mut_app.app_ident; + let body = mut_app.body; + quote! { + { + let #ident = #command::build_app(); + #body + } + } + } else { + quote! { #command::build_app() } + }; + let make_ctx = if let Some(make_seed) = args.make_seed { + let ident = make_seed.matches_ident; + let body = make_seed.body; + quote! { + { + let #ident = &rpc_toolkit_matches; + rpc_toolkit::SeedableContext::new(#body) + } + } + } else { + quote! { rpc_toolkit::SeedableContext::new(&rpc_toolkit_matches) } + }; + let exit_fn = args + .exit_fn + .unwrap_or_else(|| syn::parse2(quote! { |code| code }).unwrap()); + quote! { + { + let rpc_toolkit_matches = #app.get_matches(); + let rpc_toolkit_ctx = #make_ctx; + if let Err(e) = #command_handler( + rpc_toolkit_ctx, + None, + &rpc_toolkit_matches, + "".into(), + (), + ) { + eprintln!("{}", e.message); + if let Some(data) = e.data { + eprintln!("{:?}", data); + } + let exit_fn = #exit_fn; + drop(rpc_toolkit_matches); + std::process::exit(exit_fn(e.code)) + } else { + drop(rpc_toolkit_matches); + std::process::exit(0) + } + } + } +} diff --git a/rpc-toolkit-macro-internals/src/run_cli/mod.rs b/rpc-toolkit-macro-internals/src/run_cli/mod.rs new file mode 100644 index 0000000..21edfc7 --- /dev/null +++ b/rpc-toolkit-macro-internals/src/run_cli/mod.rs @@ -0,0 +1,21 @@ +use syn::*; + +pub struct MakeSeed { + matches_ident: Ident, + body: Expr, +} + +pub struct MutApp { + app_ident: Ident, + body: Expr, +} + +pub struct RunCliArgs { + command: Path, + mut_app: Option, + make_seed: Option, + exit_fn: Option, +} + +pub mod build; +mod parse; diff --git a/rpc-toolkit-macro-internals/src/run_cli/parse.rs b/rpc-toolkit-macro-internals/src/run_cli/parse.rs new file mode 100644 index 0000000..23de053 --- /dev/null +++ b/rpc-toolkit-macro-internals/src/run_cli/parse.rs @@ -0,0 +1,59 @@ +use super::*; +use syn::parse::{Parse, ParseStream}; + +impl Parse for MakeSeed { + fn parse(input: ParseStream) -> Result { + let matches_ident = input.parse()?; + let _: token::FatArrow = input.parse()?; + let body = input.parse()?; + Ok(MakeSeed { + matches_ident, + body, + }) + } +} + +impl Parse for MutApp { + fn parse(input: ParseStream) -> Result { + let app_ident = input.parse()?; + let _: token::FatArrow = input.parse()?; + let body = input.parse()?; + Ok(MutApp { app_ident, body }) + } +} + +impl Parse for RunCliArgs { + fn parse(input: ParseStream) -> Result { + let command = input.parse()?; + if !input.is_empty() { + let _: token::Comma = input.parse()?; + } + let mut_app = if !input.is_empty() { + Some(input.parse()?) + } else { + None + }; + if !input.is_empty() { + let _: token::Comma = input.parse()?; + } + let make_seed = if !input.is_empty() { + Some(input.parse()?) + } else { + None + }; + if !input.is_empty() { + let _: token::Comma = input.parse()?; + } + let exit_fn = if !input.is_empty() { + Some(input.parse()?) + } else { + None + }; + Ok(RunCliArgs { + command, + mut_app, + make_seed, + exit_fn, + }) + } +} diff --git a/rpc-toolkit-macro/.gitignore b/rpc-toolkit-macro/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/rpc-toolkit-macro/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/rpc-toolkit-macro/Cargo.toml b/rpc-toolkit-macro/Cargo.toml new file mode 100644 index 0000000..45acc21 --- /dev/null +++ b/rpc-toolkit-macro/Cargo.toml @@ -0,0 +1,15 @@ +[package] +authors = ["Aiden McClelland "] +edition = "2018" +name = "rpc-toolkit-macro" +version = "0.1.0" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[lib] +proc-macro = true + +[dependencies] +proc-macro2 = "1.0.1" +rpc-toolkit-macro-internals = { path = "../rpc-toolkit-macro-internals" } +syn = "1.0.62" diff --git a/rpc-toolkit-macro/src/lib.rs b/rpc-toolkit-macro/src/lib.rs new file mode 100644 index 0000000..803260c --- /dev/null +++ b/rpc-toolkit-macro/src/lib.rs @@ -0,0 +1,52 @@ +use proc_macro::{Span, TokenStream}; +use rpc_toolkit_macro_internals::*; + +#[proc_macro_attribute] +pub fn command(args: TokenStream, item: TokenStream) -> TokenStream { + let args = syn::parse_macro_input!(args as syn::AttributeArgs); + let item = syn::parse_macro_input!(item as syn::ItemFn); + build_command(args, item).into() +} + +/// `#[arg(...)]` -> Take this argument as a parameter +/// - `#[arg(help = "Help text")]` -> Set help text for the arg +/// - `#[arg(rename = "new_name")]` -> Set the name of the arg to `new_name` in the RPC and CLI +/// - `#[arg(short = "a")]` -> Set the "short" representation of the arg to `-a` on the CLI +/// - `#[arg(long = "arg")]` -> Set the "long" representation of the arg to `--arg` on the CLI +/// - `#[arg(parse(custom_parse_fn))]` -> Use the function `custom_parse_fn` to parse the arg from the CLI +/// - `custom_parse_fn :: Into err => (&str, &ArgMatches<'_>) -> Result` +/// - note: `arg` is the type of the argument +/// - `#[arg(stdin)]` -> Parse the argument from stdin when using the CLI +/// - `custom_parse_fn :: Into err => (&[u8], &ArgMatches<'_>) -> Result` +#[proc_macro_attribute] +pub fn arg(_: TokenStream, _: TokenStream) -> TokenStream { + syn::Error::new( + Span::call_site().into(), + "`arg` is only allowed on arguments of a function with the `command` attribute", + ) + .to_compile_error() + .into() +} + +/// - `#[context]` -> Passes the application context into this parameter +#[proc_macro_attribute] +pub fn context(_: TokenStream, _: TokenStream) -> TokenStream { + syn::Error::new( + Span::call_site().into(), + "`context` is only allowed on arguments of a function with the `command` attribute", + ) + .to_compile_error() + .into() +} + +#[proc_macro] +pub fn rpc_server(item: TokenStream) -> TokenStream { + let item = syn::parse_macro_input!(item as RpcServerArgs); + build_rpc_server(item).into() +} + +#[proc_macro] +pub fn run_cli(item: TokenStream) -> TokenStream { + let item = syn::parse_macro_input!(item as RunCliArgs); + build_run_cli(item).into() +} diff --git a/rpc-toolkit/.gitignore b/rpc-toolkit/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/rpc-toolkit/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/rpc-toolkit/Cargo.toml b/rpc-toolkit/Cargo.toml new file mode 100644 index 0000000..cf36365 --- /dev/null +++ b/rpc-toolkit/Cargo.toml @@ -0,0 +1,24 @@ +[package] +authors = ["Aiden McClelland "] +edition = "2018" +name = "rpc-toolkit" +version = "0.1.0" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[features] +cbor = ["serde_cbor"] +default = ["cbor"] + +[dependencies] +clap = "2.33.3" +hyper = { version = "0.14.5", features = ["server", "http1", "http2", "tcp", "stream", "client"] } +lazy_static = "1.4.0" +reqwest = { version = "0.11.2" } +rpc-toolkit-macro = { path = "../rpc-toolkit-macro" } +serde = { version = "1.0.125", features = ["derive"] } +serde_cbor = { version = "0.11.1", optional = true } +serde_json = "1.0.64" +thiserror = "1.0.24" +tokio = { version = "1.4.0", features = ["full"] } +url = "2.2.1" +yajrc = { path = "../../yajrc" } diff --git a/rpc-toolkit/src/command_helpers.rs b/rpc-toolkit/src/command_helpers.rs new file mode 100644 index 0000000..002579c --- /dev/null +++ b/rpc-toolkit/src/command_helpers.rs @@ -0,0 +1,123 @@ +use std::str::FromStr; +use std::{fmt::Display, io::Stdin, marker::PhantomData}; + +use clap::ArgMatches; +use hyper::Method; +use serde::{Deserialize, Serialize}; +use thiserror::Error; +use yajrc::{GenericRpcMethod, Id, RpcError, RpcRequest, RpcResponse}; + +use crate::Context; + +pub mod prelude { + pub use super::{ + call_remote, default_arg_parser, default_display, default_stdin_parser, make_phantom, + match_types, + }; + pub use crate::Context; + pub use clap::{App, AppSettings, Arg, ArgMatches}; + pub use serde::{Deserialize, Serialize}; + pub use serde_json::{from_value, to_value, Value}; + pub use std::borrow::Cow; + pub use std::marker::PhantomData; + pub use tokio::{runtime::Runtime, task::spawn_blocking}; + pub use yajrc::{self, RpcError}; +} + +#[derive(Debug, Error)] +pub enum RequestError { + #[error("JSON Error: {0}")] + JSON(#[from] serde_json::Error), + #[cfg(feature = "cbor")] + #[error("CBOR Error: {0}")] + CBOR(#[from] serde_cbor::Error), + #[error("HTTP Error: {0}")] + HTTP(#[from] reqwest::Error), + #[error("Missing Content-Type")] + MissingContentType, +} + +pub fn make_phantom(_actual: T) -> PhantomData { + PhantomData +} + +pub fn match_types(_: &T, _: &T) {} + +pub async fn call_remote Deserialize<'de>>( + ctx: Ctx, + method: &str, + params: Params, + _return_ty: PhantomData, +) -> Result>, RequestError> { + let rpc_req: RpcRequest> = RpcRequest { + id: Some(Id::Number(0.into())), + method: GenericRpcMethod::new(method), + params, + }; + let mut req = ctx.client().request(Method::POST, ctx.url()); + let body; + #[cfg(feature = "cbor")] + { + req = req.header("content-type", "application/cbor"); + req = req.header("accept", "application/cbor, application/json"); + body = serde_cbor::to_vec(&rpc_req)?; + } + #[cfg(not(feature = "cbor"))] + { + req = req.header("content-type", "application/json"); + req = req.header("accept", "application/json"); + body = serde_json::to_vec(&req)?; + } + let res = req + .header("content-length", body.len()) + .body(body) + .send() + .await?; + Ok( + match res + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + { + Some("application/json") => serde_json::from_slice(&*res.bytes().await?)?, + #[cfg(feature = "cbor")] + Some("application/cbor") => serde_cbor::from_slice(&*res.bytes().await?)?, + _ => return Err(RequestError::MissingContentType), + }, + ) +} + +pub fn default_arg_parser, E: Display>( + arg: &str, + _: &ArgMatches<'_>, +) -> Result { + arg.parse().map_err(|e| RpcError { + data: Some(format!("{}", e).into()), + ..yajrc::INVALID_PARAMS_ERROR + }) +} + +pub fn default_stdin_parser, E: Display>( + stdin: &mut Stdin, + _: &ArgMatches<'_>, +) -> Result { + let mut s = String::new(); + stdin.read_line(&mut s).map_err(|e| RpcError { + data: Some(format!("{}", e).into()), + ..yajrc::INVALID_PARAMS_ERROR + })?; + if let Some(s) = s.strip_suffix("\n") { + s + } else { + &s + } + .parse() + .map_err(|e| RpcError { + data: Some(format!("{}", e).into()), + ..yajrc::INVALID_PARAMS_ERROR + }) +} + +pub fn default_display(t: T) { + println!("{}", t) +} diff --git a/rpc-toolkit/src/context.rs b/rpc-toolkit/src/context.rs new file mode 100644 index 0000000..eadc21a --- /dev/null +++ b/rpc-toolkit/src/context.rs @@ -0,0 +1,39 @@ +use lazy_static::lazy_static; +use reqwest::Client; +use url::{Host, Url}; + +lazy_static! { + static ref DEFAULT_CLIENT: Client = Client::new(); +} + +pub trait Context { + fn host(&self) -> Host<&str> { + Host::Ipv4([127, 0, 0, 1].into()) + } + fn port(&self) -> u16 { + 8080 + } + fn protocol(&self) -> &str { + "http" + } + fn url(&self) -> Url { + format!("{}://{}:{}", self.protocol(), self.host(), self.port()) + .parse() + .unwrap() + } + fn client(&self) -> &Client { + &*DEFAULT_CLIENT + } +} + +pub trait SeedableContext: Context { + fn new(seed: T) -> Self; +} + +impl Context for () {} + +impl SeedableContext for () { + fn new(_: T) -> Self { + () + } +} diff --git a/rpc-toolkit/src/lib.rs b/rpc-toolkit/src/lib.rs new file mode 100644 index 0000000..a1e3c92 --- /dev/null +++ b/rpc-toolkit/src/lib.rs @@ -0,0 +1,57 @@ +pub use crate::context::{Context, SeedableContext}; +pub use clap; +pub use hyper; +pub use serde; +pub use serde_json; +pub use tokio; +pub use yajrc; + +/// `#[command(...)]` +/// - `#[command(local)]` -> executed by CLI instead of RPC server (leaf commands only) +/// - `#[command(remote)]` -> no CLI bindings (leaf commands only) +/// - `#[command(blocking)]` -> run with [spawn_blocking](tokio::task::spawn_blocking) if in an async context +/// - `#[command(about = "About text")]` -> Set about text for the command +/// - `#[command(rename = "new_name")]` -> Set the name of the command to `new_name` in the RPC and CLI +/// - `#[command(subcommands(...))]` -> Set this as the parent command for the listed subcommands +/// - note: the return type of the decorated function must be the [Context] required by its subcommands, and all args must implement [Clone](std::clone::Clone). +/// - `#[command(subcommands(self(self_command_impl)))]` -> If no subcommand is provided, call this function +/// - `self_command_impl :: Context ctx, Display res, Into err => ctx -> Result` +/// - note: [Display](std::fmt::Display) is not required for `res` if it has a custom display function that will take it as input +/// - if `self_command_impl` is async, write `self(self_command_impl(async))` +/// - if `self_command_impl` is blocking, write `self(self_command_impl(blocking))` +/// - default: require a subcommand if subcommands are specified +/// - `#[command(display(custom_display_fn))]` -> Use the function `custom_display_fn` to display the command's result (leaf commands only) +/// - `custom_display_fn :: res -> ()` +/// - note: `res` is the type of the decorated command's output +/// - default: `default_display` +/// +/// See also: [arg](rpc_toolkit_macro::arg), [context](rpc_toolkit_macro::context) +pub use rpc_toolkit_macro::command; + +/// `rpc_server!(command, seed, status_fn)` +/// - returns: [Server](hyper::Server) +/// - `command`: path to an rpc command (with the `#[command]` attribute) +/// - `seed`: A seed for the [SeedableContext] of the rpc command. +/// - `status_fn` (optional): a function that takes a JSON RPC error code (`i32`) and returns a [StatusCode](hyper::StatusCode) +/// - default: `|_| StatusCode::OK` +pub use rpc_toolkit_macro::rpc_server; + +/// `run_cli!(command, app_mutator, make_seed, exit_fn)` +/// - this function does not return +/// - `command`: path to an rpc command (with the `#[command]` attribute) +/// - `app_mutator` (optional): an expression that returns a mutated app. +/// - example: `app => app.arg(Arg::with_name("port").long("port"))` +/// - default: `app => app` +/// - `make_seed` (optional): an expression that takes [&ArgMatches](clap::ArgMatches) and returns a seed. +/// - example: `matches => matches.value_of("port")` +/// - default: `matches => matches` +/// - `exit_fn` (optional): a function that takes a JSON RPC error code (`i32`) and returns an Exit code (`i32`) +/// - default: `|code| code` +pub use rpc_toolkit_macro::run_cli; + +pub mod command_helpers; +mod context; +pub mod rpc_server_helpers; + +#[cfg(test)] +mod test; diff --git a/rpc-toolkit/src/rpc_server_helpers.rs b/rpc-toolkit/src/rpc_server_helpers.rs new file mode 100644 index 0000000..7453136 --- /dev/null +++ b/rpc-toolkit/src/rpc_server_helpers.rs @@ -0,0 +1,105 @@ +use std::marker::PhantomData; + +use crate::SeedableContext; +use hyper::{ + body::Buf, + server::{conn::AddrIncoming, Builder, Server}, + Body, Request, Response, StatusCode, +}; +use lazy_static::lazy_static; +use serde::Deserialize; +use serde_json::Value; +use url::Host; +use yajrc::{AnyRpcMethod, GenericRpcMethod, Id, RpcError, RpcRequest, RpcResponse}; + +lazy_static! { + #[cfg(feature = "cbor")] + static ref CBOR_INTERNAL_ERROR: Vec = + serde_cbor::to_vec(&RpcResponse::>::from(yajrc::INTERNAL_ERROR)).unwrap(); + static ref JSON_INTERNAL_ERROR: Vec = + serde_json::to_vec(&RpcResponse::>::from(yajrc::INTERNAL_ERROR)).unwrap(); +} + +pub fn make_builder, Seed: Clone>( + seed: Seed, +) -> (Builder, PhantomData) { + let ctx = Ctx::new(seed); + let addr = match ctx.host() { + Host::Ipv4(ip) => (ip, ctx.port()).into(), + Host::Ipv6(ip) => (ip, ctx.port()).into(), + Host::Domain(localhost) if localhost == "localhost" => ([127, 0, 0, 1], ctx.port()).into(), + _ => ([0, 0, 0, 0], ctx.port()).into(), + }; + (Server::bind(&addr), PhantomData) +} + +pub fn bind_type(_phantom: PhantomData, actual: T) -> T { + actual +} + +pub async fn make_request Deserialize<'de> + 'static>( + req: &mut Request, +) -> Result>, RpcError> { + let body = hyper::body::aggregate(std::mem::replace(req.body_mut(), Body::empty())) + .await? + .reader(); + let rpc_req: RpcRequest>; + #[cfg(feature = "cbor")] + if req + .headers() + .get("content-type") + .and_then(|h| h.to_str().ok()) + == Some("application/cbor") + { + rpc_req = serde_cbor::from_reader(body)?; + } else { + rpc_req = serde_json::from_reader(body)?; + } + #[cfg(not(feature = "cbor"))] + { + rpc_req = serde_json::from_reader(body)?; + } + + Ok(rpc_req) +} + +pub fn to_response StatusCode>( + req: &Request, + res: Result<(Option, Result), RpcError>, + status_code_fn: F, +) -> Result, hyper::http::Error> { + let rpc_res: RpcResponse = match res { + Ok((id, result)) => RpcResponse { id, result }, + Err(e) => e.into(), + }; + let body; + let mut res = Response::builder(); + #[cfg(feature = "cbor")] + if req + .headers() + .get("accept") + .and_then(|h| h.to_str().ok()) + .iter() + .flat_map(|s| s.split(",")) + .map(|s| s.trim()) + .any(|s| s == "application/cbor") + // prefer cbor if accepted + { + res = res.header("content-type", "application/cbor"); + body = serde_cbor::to_vec(&rpc_res).unwrap_or_else(|_| CBOR_INTERNAL_ERROR.clone()); + } else { + res = res.header("content-type", "application/json"); + body = serde_json::to_vec(&rpc_res).unwrap_or_else(|_| JSON_INTERNAL_ERROR.clone()); + } + #[cfg(not(feature = "cbor"))] + { + res.header("content-type", "application/json"); + body = serde_json::to_vec(&rpc_res).unwrap_or_else(|_| JSON_INTERNAL_ERROR.clone()); + } + res = res.header("content-length", body.len()); + res = res.status(match &rpc_res.result { + Ok(_) => StatusCode::OK, + Err(e) => status_code_fn(e.code), + }); + res.body(Body::from(body)) +} diff --git a/rpc-toolkit/src/test.rs b/rpc-toolkit/src/test.rs new file mode 100644 index 0000000..e92da66 --- /dev/null +++ b/rpc-toolkit/src/test.rs @@ -0,0 +1,182 @@ +pub use crate as rpc_toolkit; +use crate::{command, rpc_server, Context, SeedableContext}; +use clap::Arg; +use rpc_toolkit_macro::run_cli; +use serde::{Deserialize, Serialize}; +use std::str::FromStr; +use std::{fmt::Display, sync::Arc}; +use url::Host; +use yajrc::RpcError; + +pub struct AppState { + seed: T, + data: U, +} +impl AppState { + pub fn map V, V>(self, f: F) -> AppState { + AppState { + seed: self.seed, + data: f(self.data), + } + } +} + +pub struct ConfigSeed { + host: Host, + port: u16, +} + +impl SeedableContext> for AppState, ()> { + fn new(seed: Arc) -> Self { + AppState { + seed: seed.clone(), + data: (), + } + } +} +impl Context for AppState, T> { + fn host(&self) -> Host<&str> { + match &self.seed.host { + Host::Domain(s) => Host::Domain(s.as_str()), + Host::Ipv4(i) => Host::Ipv4(*i), + Host::Ipv6(i) => Host::Ipv6(*i), + } + } + fn port(&self) -> u16 { + self.seed.port + } +} + +#[command( + about = "Does the thing", + subcommands("dothething2::", self(dothething_impl(async))) +)] +async fn dothething< + U: Serialize + for<'a> Deserialize<'a> + FromStr + Clone, + E: Display, +>( + #[context] ctx: AppState, ()>, + #[arg(short = "a")] arg1: Option, + #[arg(short = "b")] val: String, + #[arg(short = "c", help = "I am the flag `c`!")] arg3: bool, + #[arg(stdin)] structured: U, +) -> Result, (Option, String, bool, U)>, RpcError> { + Ok(ctx.map(|_| (arg1, val, arg3, structured))) +} + +async fn dothething_impl( + ctx: AppState, (Option, String, bool, U)>, +) -> Result { + Ok(format!( + "{:?}, {}, {}, {}", + ctx.data.0, + ctx.data.1, + ctx.data.2, + serde_json::to_string_pretty(&ctx.data.3)? + )) +} + +#[command(about = "Does the thing")] +fn dothething2 Deserialize<'a> + FromStr, E: Display>( + #[context] ctx: AppState, (Option, String, bool, U)>, + #[arg(stdin)] structured2: U, +) -> Result { + Ok(format!( + "{:?}, {}, {}, {}, {}", + ctx.data.0, + ctx.data.1, + ctx.data.2, + serde_json::to_string_pretty(&ctx.data.3)?, + serde_json::to_string_pretty(&structured2)?, + )) +} + +#[tokio::test] +async fn test() { + use crate as rpc_toolkit; + use tokio::io::AsyncWriteExt; + + let seed = Arc::new(ConfigSeed { + host: Host::parse("localhost").unwrap(), + port: 8000, + }); + let server = rpc_server!(dothething::, seed); + let handle = tokio::spawn(server); + let mut cmd = tokio::process::Command::new("cargo") + .arg("test") + .arg("--package") + .arg("rpc-toolkit") + .arg("--lib") + .arg("--") + .arg("test::cli_test") + .arg("--exact") + .arg("--nocapture") + .arg("--ignored") + .arg("--") + .arg("-b") + .arg("test") + .arg("dothething2") + .stdin(std::process::Stdio::piped()) + .stdout(std::process::Stdio::piped()) + .spawn() + .unwrap(); + cmd.stdin + .take() + .unwrap() + .write_all(b"TEST\nHAHA") + .await + .unwrap(); + let out = cmd.wait_with_output().await.unwrap(); + assert!(out.status.success()); + assert!(std::str::from_utf8(&out.stdout) + .unwrap() + .contains("\nNone, test, false, \"TEST\", \"HAHA\"\n")); + handle.abort(); +} + +#[test] +fn cli_test() { + let app = dothething::build_app(); + let mut skip = true; + let args = std::iter::once(std::ffi::OsString::from("cli_test")) + .chain(std::env::args_os().into_iter().skip_while(|a| { + if a == "--" { + skip = false; + return true; + } + skip + })) + .collect::>(); + if skip { + return; + } + let matches = app.get_matches_from(args); + let seed = Arc::new(ConfigSeed { + host: Host::parse("localhost").unwrap(), + port: 8000, + }); + dothething::cli_handler::( + SeedableContext::new(seed), + None, + &matches, + "".into(), + (), + ) + .unwrap(); +} + +#[test] +#[ignore] +fn cli_example() { + run_cli!( + dothething::, + app => app + .arg(Arg::with_name("host").long("host").short("h").takes_value(true)) + .arg(Arg::with_name("port").long("port").short("p").takes_value(true)), + matches => Arc::new(ConfigSeed { + host: Host::parse(matches.value_of("host").unwrap_or("localhost")).unwrap(), + port: matches.value_of("port").unwrap_or("8000").parse().unwrap(), + }), + |code| if code < 0 { 1 } else { code } + ) +} diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..64d94de --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,2 @@ +group_imports = "StdExternalCrate" +imports_granularity = "Module"