From b6c6433aa371459e1fa98c44ab90de875613b6a8 Mon Sep 17 00:00:00 2001 From: Gregory Date: Wed, 4 Nov 2020 00:05:56 -0500 Subject: [PATCH] misc improvements --- Cargo.lock | 476 ++++++++++++++++++++---------------- Cargo.toml | 13 +- src/apps/system.rs | 2 +- src/apps/user.rs | 122 ++++++--- src/common/desktop_entry.rs | 38 ++- src/common/mime_types.rs | 53 ++-- src/config.rs | 34 +-- src/main.rs | 12 +- tests/p.html | 3 + 9 files changed, 432 insertions(+), 321 deletions(-) create mode 100644 tests/p.html diff --git a/Cargo.lock b/Cargo.lock index 6b5305c..407dfa3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,11 +1,5 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -[[package]] -name = "anyhow" -version = "1.0.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85bb70cc08ec97ca5450e6eba421deeea5f172c0fc61f78b5357b2a8e8be195f" - [[package]] name = "arrayref" version = "0.3.6" @@ -14,9 +8,9 @@ checksum = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544" [[package]] name = "arrayvec" -version = "0.5.1" +version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cff77d8686867eceff3105329d4698d96c2391c176d5d03adc90c7389162b5b8" +checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" [[package]] name = "ascii_table" @@ -32,26 +26,20 @@ checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" dependencies = [ "hermit-abi", "libc", - "winapi 0.3.8", + "winapi 0.3.9", ] [[package]] name = "autocfg" -version = "1.0.0" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" +checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" [[package]] name = "base64" -version = "0.11.0" +version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b41b7ea54a0c9d92199de89e20e58d49f02f8e699814ef3fdf266f6f748d15c7" - -[[package]] -name = "base64" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53d1ccbaf7d9ec9537465a97bf19edc1a4e158ecb49fc16178202238c569cc42" +checksum = "3441f0f7b02788e948e47f457ca01f1d7e6d92c693bc132c22b087d3141c03ff" [[package]] name = "bitflags" @@ -61,9 +49,9 @@ checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" [[package]] name = "blake2b_simd" -version = "0.5.10" +version = "0.5.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8fb2d74254a3a0b5cac33ac9f8ed0e44aa50378d9dbb2e5d83bd21ed1dc2c8a" +checksum = "afa748e348ad3be8263be728124b24a24f268266f6f5d58af9d75f6a40b5c587" dependencies = [ "arrayref", "arrayvec", @@ -111,27 +99,33 @@ checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" [[package]] name = "bytes" -version = "0.5.4" +version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "130aac562c0dd69c56b3b1cc8ffd2e17be31d0b6c25b61c96b76231aa23e39e1" +checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38" [[package]] name = "cc" -version = "1.0.54" +version = "1.0.61" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7bbb73db36c1246e9034e307d0fba23f9a2e251faa47ade70c1bd252220c8311" +checksum = "ed67cbde08356238e75fc4656be4749481eeffb09e19f320a25237d5221c985d" [[package]] name = "cfg-if" -version = "0.1.9" +version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b486ce3ccf7ffd79fdeb678eac06a9e6c09fc88d33836340becb8fffe87c5e33" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "3.0.0-beta.1" +version = "3.0.0-beta.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "860643c53f980f0d38a5e25dfab6c3c93b2cb3aa1fe192643d17a293c6c41936" +checksum = "4bd1061998a501ee7d4b6d449020df3266ca3124b941ec56cf2005c3779ca142" dependencies = [ "atty", "bitflags", @@ -148,9 +142,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "3.0.0-beta.1" +version = "3.0.0-beta.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb51c9e75b94452505acd21d929323f5a5c6c4735a852adbd39ef5fb1b014f30" +checksum = "370f715b81112975b1b69db93e0b56ea4cd4e5002ac43b2da8474106a54096a1" dependencies = [ "heck", "proc-macro-error", @@ -199,7 +193,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" dependencies = [ "autocfg", - "cfg-if", + "cfg-if 0.1.10", "lazy_static", ] @@ -218,7 +212,7 @@ version = "2.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "551a778172a450d7fc12e629ca3b0428d00f6afa9a43da1b630d54604e97371c" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "dirs-sys", ] @@ -228,7 +222,7 @@ version = "2.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13aea89a5c93364a98e9b37b2fa237effbb694d5cfe01c5b70941f7eb087d5e3" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "dirs-sys", ] @@ -240,28 +234,28 @@ checksum = "8e93d7f5705de3e49895a2b5e0b8855a1c27f080192ae9c32a6432d50741a57a" dependencies = [ "libc", "redox_users", - "winapi 0.3.8", + "winapi 0.3.9", ] [[package]] name = "dtoa" -version = "0.4.5" +version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4358a9e11b9a09cf52383b451b49a169e8d797b68aa02301ff586d70d9661ea3" +checksum = "134951f4028bdadb9b84baf4232681efbf277da25144b9b0ad65df75946c422b" [[package]] name = "either" -version = "1.5.3" +version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3" +checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" [[package]] name = "encoding_rs" -version = "0.8.23" +version = "0.8.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8ac63f94732332f44fe654443c46f6375d1939684c17b0afb6cb56b0456e171" +checksum = "801bbab217d7f79c0062f4f7205b5d4427c6d1a7bd7aafdd1475f7c59d62b283" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", ] [[package]] @@ -293,12 +287,12 @@ checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" [[package]] name = "freedesktop_entry_parser" -version = "0.2.2" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e71e40cde9e00afcb8c5f4e3339f72dae1fb0b30f299d54b690ceb6ce11f3e3" +checksum = "6f4120aac462b9a0bf55d898ad9cd845d0e067435842e00885ddd6b9e073d47b" dependencies = [ - "anyhow", "nom", + "thiserror", ] [[package]] @@ -319,40 +313,40 @@ checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" [[package]] name = "futures-channel" -version = "0.3.5" +version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f366ad74c28cca6ba456d95e6422883cfb4b252a83bed929c83abfdbbf2967d5" +checksum = "0448174b01148032eed37ac4aed28963aaaa8cfa93569a08e5b479bbc6c2c151" dependencies = [ "futures-core", ] [[package]] name = "futures-core" -version = "0.3.5" +version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59f5fff90fd5d971f936ad674802482ba441b6f09ba5e15fd8b39145582ca399" +checksum = "18eaa56102984bed2c88ea39026cff3ce3b4c7f508ca970cedf2450ea10d4e46" [[package]] name = "futures-sink" -version = "0.3.5" +version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f2032893cb734c7a05d85ce0cc8b8c4075278e93b24b66f9de99d6eb0fa8acc" +checksum = "0e3ca3f17d6e8804ae5d3df7a7d35b2b3a6fe89dac84b31872720fc3060a0b11" [[package]] name = "futures-task" -version = "0.3.5" +version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bdb66b5f09e22019b1ab0830f7785bcea8e7a42148683f99214f73f8ec21a626" +checksum = "96d502af37186c4fef99453df03e374683f8a1eec9dcc1e66b3b82dc8278ce3c" [[package]] name = "futures-util" -version = "0.3.5" +version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8764574ff08b701a084482c3c7031349104b07ac897393010494beaa18ce32c6" +checksum = "abcb44342f62e6f3e8ac427b8aa815f724fd705dfad060b18ac7866c15bb8e34" dependencies = [ "futures-core", "futures-task", - "pin-project", + "pin-project 1.0.1", "pin-utils", ] @@ -367,11 +361,11 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.1.14" +version = "0.1.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb" +checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "libc", "wasi", ] @@ -384,9 +378,9 @@ checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" [[package]] name = "h2" -version = "0.2.5" +version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79b7246d7e4b979c03fa093da39cfb3617a96bbeee6310af63991668d7e843ff" +checksum = "5e4728fd124914ad25e99e3d15a9361a879f6620f63cb56bbb08f95abb97a535" dependencies = [ "bytes", "fnv", @@ -395,10 +389,11 @@ dependencies = [ "futures-util", "http", "indexmap", - "log", "slab", "tokio", "tokio-util", + "tracing", + "tracing-futures", ] [[package]] @@ -414,6 +409,7 @@ dependencies = [ "json", "mime", "mime-db", + "once_cell", "pest", "pest_derive", "regex", @@ -425,6 +421,12 @@ dependencies = [ "xdg-mime", ] +[[package]] +name = "hashbrown" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04" + [[package]] name = "heck" version = "0.3.1" @@ -436,9 +438,9 @@ dependencies = [ [[package]] name = "hermit-abi" -version = "0.1.13" +version = "0.1.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91780f809e750b0a89f5544be56617ff6b1227ee485bcb06ebe10cdf89bd3b71" +checksum = "5aca5565f760fb5b220e499d72710ed156fdb74e631659e99377d9ebfbd13ae8" dependencies = [ "libc", ] @@ -471,10 +473,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9" [[package]] -name = "hyper" -version = "0.13.6" +name = "httpdate" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6e7655b9594024ad0ee439f3b5a7299369dc2a3f459b47c696f9ff676f9aa1f" +checksum = "494b4d60369511e7dea41cf646832512a94e542f68bb9c49e54518e0f468eb47" + +[[package]] +name = "hyper" +version = "0.13.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6ad767baac13b44d4529fcf58ba2cd0995e36e7b435bc5b039de6f47e880dbf" dependencies = [ "bytes", "futures-channel", @@ -484,21 +492,21 @@ dependencies = [ "http", "http-body", "httparse", + "httpdate", "itoa", - "log", - "pin-project", + "pin-project 1.0.1", "socket2", - "time", "tokio", "tower-service", + "tracing", "want", ] [[package]] name = "hyper-tls" -version = "0.4.1" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3adcd308402b9553630734e9c36b77a7e48b3821251ca2493e8cd596763aafaa" +checksum = "d979acc56dcb5b8dddba3917601745e877576475aa046df3226eabdecef78eed" dependencies = [ "bytes", "hyper", @@ -520,11 +528,12 @@ dependencies = [ [[package]] name = "indexmap" -version = "1.4.0" +version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c398b2b113b55809ceb9ee3e753fcbac793f1956663f3c36549c1346015c2afe" +checksum = "55e2e4c765aa53a0424761bf9f41aa7a6ac1efa87238f59560640e27fca028f2" dependencies = [ "autocfg", + "hashbrown", ] [[package]] @@ -536,6 +545,12 @@ dependencies = [ "libc", ] +[[package]] +name = "ipnet" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47be2f14c678be2fdcab04ab1171db51b2762ce6f0a8ee87c8dd4a04ed216135" + [[package]] name = "itertools" version = "0.9.0" @@ -547,15 +562,15 @@ dependencies = [ [[package]] name = "itoa" -version = "0.4.5" +version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8b7a7c0c47db5545ed3fef7468ee7bb5b74691498139e4b3f6a20685dc6dd8e" +checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" [[package]] name = "js-sys" -version = "0.3.40" +version = "0.3.45" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce10c23ad2ea25ceca0093bd3192229da4c5b3c0f2de499c1ecac0d98d452177" +checksum = "ca059e81d9486668f12d455a4ea6daa600bd408134cd17e3d3fb5a32d1f016f8" dependencies = [ "wasm-bindgen", ] @@ -590,24 +605,24 @@ checksum = "db65c6da02e61f55dae90a0ae427b2a5f6b3e8db09f58d10efab23af92592616" dependencies = [ "arrayvec", "bitflags", - "cfg-if", + "cfg-if 0.1.10", "ryu", "static_assertions", ] [[package]] name = "libc" -version = "0.2.71" +version = "0.2.80" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9457b06509d27052635f90d6466700c65095fdf75409b3fbdd903e988b886f49" +checksum = "4d58d1b70b004888f764dfbf6a26a3b0342a1632d33968e4a179d8011c760614" [[package]] name = "log" -version = "0.4.8" +version = "0.4.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7" +checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", ] [[package]] @@ -624,9 +639,9 @@ checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" [[package]] name = "memchr" -version = "2.3.3" +version = "2.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" +checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" [[package]] name = "mime" @@ -636,9 +651,9 @@ checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" [[package]] name = "mime-db" -version = "0.1.5" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fed5ecf1311eddf7b5de5f77c74a9eda2792e41cd22784736497a7ecd8840e96" +checksum = "76aa0dbe3627eba85b33e64310db99909007547454332ce5e867e4a20c9036bf" dependencies = [ "reqwest", "serde", @@ -662,7 +677,7 @@ version = "0.6.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fce347092656428bc8eaf6201042cb551b8d67855af7374542a92a0fbfcac430" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "fuchsia-zircon", "fuchsia-zircon-sys", "iovec", @@ -707,13 +722,13 @@ dependencies = [ [[package]] name = "net2" -version = "0.2.34" +version = "0.2.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2ba7c918ac76704fb42afcbbb43891e72731f3dcca3bef2a19786297baf14af7" +checksum = "3ebc3ec692ed7c9a255596c67808dee269f64655d8baf7b4f0638e51ba1d6853" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "libc", - "winapi 0.3.8", + "winapi 0.3.9", ] [[package]] @@ -737,6 +752,12 @@ dependencies = [ "libc", ] +[[package]] +name = "once_cell" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "260e51e7efe62b592207e9e13a68e43692a7a279171d6ba57abd208bf23645ad" + [[package]] name = "opaque-debug" version = "0.2.3" @@ -745,12 +766,12 @@ checksum = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c" [[package]] name = "openssl" -version = "0.10.29" +version = "0.10.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cee6d85f4cb4c4f59a6a85d5b68a233d280c82e29e822913b9c8b129fbf20bdd" +checksum = "8d575eff3665419f9b83678ff2815858ad9d11567e082f5ac1814baba4e2bcb4" dependencies = [ "bitflags", - "cfg-if", + "cfg-if 0.1.10", "foreign-types", "lazy_static", "libc", @@ -778,9 +799,9 @@ dependencies = [ [[package]] name = "os_str_bytes" -version = "2.3.1" +version = "2.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06de47b848347d8c4c94219ad8ecd35eb90231704b067e67e6ae2e36ee023510" +checksum = "2ac6fe3538f701e339953a3ebbe4f39941aababa8a3f6964635b24ab526daeac" [[package]] name = "percent-encoding" @@ -833,18 +854,38 @@ dependencies = [ [[package]] name = "pin-project" -version = "0.4.20" +version = "0.4.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e75373ff9037d112bb19bc61333a06a159eaeb217660dcfbea7d88e1db823919" +checksum = "2ffbc8e94b38ea3d2d8ba92aea2983b503cd75d0888d75b86bb37970b5698e15" dependencies = [ - "pin-project-internal", + "pin-project-internal 0.4.27", +] + +[[package]] +name = "pin-project" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee41d838744f60d959d7074e3afb6b35c7456d0f61cad38a24e35e6553f73841" +dependencies = [ + "pin-project-internal 1.0.1", ] [[package]] name = "pin-project-internal" -version = "0.4.20" +version = "0.4.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10b4b44893d3c370407a1d6a5cfde7c41ae0478e31c516c85f67eb3adc51be6d" +checksum = "65ad2ae56b6abe3a1ee25f15ee605bacadb9a764edaba9c2bf4103800d4a1895" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "pin-project-internal" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81a4ffa594b66bff340084d4081df649a7dc049ac8d7fc458d8e628bfbbb2f86" dependencies = [ "proc-macro2", "quote", @@ -853,9 +894,9 @@ dependencies = [ [[package]] name = "pin-project-lite" -version = "0.1.7" +version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "282adbf10f2698a7a77f8e983a74b2d18176c19a7fd32a45446139ae7b02b715" +checksum = "c917123afa01924fc84bb20c4c03f004d9c38e5127e3c039bbf7f4b9c76a2f6b" [[package]] name = "pin-utils" @@ -865,21 +906,21 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "pkg-config" -version = "0.3.17" +version = "0.3.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677" +checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c" [[package]] name = "ppv-lite86" -version = "0.2.8" +version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "237a5ed80e274dbc66f86bd59c1e25edc039660be53194b5fe0a482e0f2612ea" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" [[package]] name = "proc-macro-error" -version = "0.4.12" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18f33027081eba0a6d8aba6d1b1c3a3be58cbb12106341c2d5759fcd9b5277e7" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" dependencies = [ "proc-macro-error-attr", "proc-macro2", @@ -890,22 +931,20 @@ dependencies = [ [[package]] name = "proc-macro-error-attr" -version = "0.4.12" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a5b4b77fdb63c1eca72173d68d24501c54ab1269409f6b672c85deb18af69de" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" dependencies = [ "proc-macro2", "quote", - "syn", - "syn-mid", "version_check", ] [[package]] name = "proc-macro2" -version = "1.0.18" +version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "beae6331a816b1f65d04c45b078fd8e6c93e8071771f41b8163255bbd8d7c8fa" +checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" dependencies = [ "unicode-xid", ] @@ -962,15 +1001,15 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.1.56" +version = "0.1.57" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84" +checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" [[package]] name = "redox_users" -version = "0.3.4" +version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09b23093265f8d200fa7b4c2c76297f47e681c655f6f1285a8780d6a022f7431" +checksum = "de0737333e7a9502c789a36d7c7fa6092a49895d4faa31ca5df163857ded2e9d" dependencies = [ "getrandom", "redox_syscall", @@ -979,35 +1018,35 @@ dependencies = [ [[package]] name = "regex" -version = "1.3.9" +version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c3780fcf44b193bc4d09f36d2a3c87b251da4a046c87795a0d35f4f927ad8e6" +checksum = "38cf2c13ed4745de91a5eb834e11c00bcc3709e773173b2ce4c56c9fbde04b9c" dependencies = [ "regex-syntax", ] [[package]] name = "regex-syntax" -version = "0.6.18" +version = "0.6.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8" +checksum = "3b181ba2dcf07aaccad5448e8ead58db5b742cf85dfe035e2227f137a539a189" [[package]] name = "remove_dir_all" -version = "0.5.2" +version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a83fa3702a688b9359eccba92d153ac33fd2e8462f9e0e3fdf155239ea7792e" +checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" dependencies = [ - "winapi 0.3.8", + "winapi 0.3.9", ] [[package]] name = "reqwest" -version = "0.10.6" +version = "0.10.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b82c9238b305f26f53443e3a4bc8528d64b8d0bee408ec949eb7bf5635ec680" +checksum = "e9eaa17ac5d7b838b7503d118fa16ad88f440498bf9ffe5424e621f93190d61e" dependencies = [ - "base64 0.12.1", + "base64", "bytes", "encoding_rs", "futures-core", @@ -1016,6 +1055,7 @@ dependencies = [ "http-body", "hyper", "hyper-tls", + "ipnet", "js-sys", "lazy_static", "log", @@ -1038,11 +1078,11 @@ dependencies = [ [[package]] name = "rust-argon2" -version = "0.7.0" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2bc8af4bda8e1ff4932523b94d3dd20ee30a87232323eda55903ffd71d2fb017" +checksum = "9dab61250775933275e84053ac235621dfb739556d5c54a2f2e9313b7cf43a19" dependencies = [ - "base64 0.11.0", + "base64", "blake2b_simd", "constant_time_eq", "crossbeam-utils", @@ -1061,7 +1101,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f05ba609c234e60bee0d547fe94a4c7e9da733d1c962cf6e59efa4cd9c8bc75" dependencies = [ "lazy_static", - "winapi 0.3.8", + "winapi 0.3.9", ] [[package]] @@ -1089,18 +1129,18 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.111" +version = "1.0.117" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c9124df5b40cbd380080b2cc6ab894c040a3070d995f5c9dc77e18c34a8ae37d" +checksum = "b88fa983de7720629c9387e9f517353ed404164b1e482c970a90c1a4aaf7dc1a" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.111" +version = "1.0.117" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f2c3ac8e6ca1e9c80b8be1023940162bf81ae3cffbb1809474152f2ce1eb250" +checksum = "cbd1ae72adb44aab48f325a02444a5fc079349a8d804c1fc922aed3f7454c74e" dependencies = [ "proc-macro2", "quote", @@ -1109,9 +1149,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.55" +version = "1.0.59" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec2c5d7e739bc07a3e73381a39d61fdb5f671c60c1df26a130690665803d8226" +checksum = "dcac07dbffa1c65e7f816ab9eba78eb142c6d44410f4eeba1e26e4f5dfa56b95" dependencies = [ "itoa", "ryu", @@ -1154,22 +1194,16 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" -[[package]] -name = "smallvec" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7cb5678e1615754284ec264d9bb5b4c27d2018577fd90ac0ceb578591ed5ee4" - [[package]] name = "socket2" -version = "0.3.12" +version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03088793f677dce356f3ccc2edb1b314ad191ab702a5de3faf49304f7e104918" +checksum = "b1fa70dc5c8104ec096f4fe7ede7a221d35ae13dcd19ba1ad9a81d2cab9a1c44" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "libc", "redox_syscall", - "winapi 0.3.8", + "winapi 0.3.9", ] [[package]] @@ -1186,38 +1220,27 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "syn" -version = "1.0.31" +version = "1.0.48" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5304cfdf27365b7585c25d4af91b35016ed21ef88f17ced89c7093b43dba8b6" +checksum = "cc371affeffc477f42a221a1e4297aedcea33d47d19b61455588bd9d8f6b19ac" dependencies = [ "proc-macro2", "quote", "unicode-xid", ] -[[package]] -name = "syn-mid" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7be3539f6c128a931cf19dcee741c1af532c7fd387baa739c03dd2e96479338a" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "tempfile" version = "3.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "libc", "rand", "redox_syscall", "remove_dir_all", - "winapi 0.3.8", + "winapi 0.3.9", ] [[package]] @@ -1231,27 +1254,27 @@ dependencies = [ [[package]] name = "textwrap" -version = "0.11.0" +version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +checksum = "203008d98caf094106cfaba70acfed15e18ed3ddb7d94e49baec153a2b462789" dependencies = [ "unicode-width", ] [[package]] name = "thiserror" -version = "1.0.19" +version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b13f926965ad00595dd129fa12823b04bbf866e9085ab0a5f2b05b850fbfc344" +checksum = "0e9ae34b84616eedaaf1e9dd6026dbe00dcafa92aa0c8077cb69df1fcfe5e53e" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.19" +version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "893582086c2f98cde18f906265a65b5030a074b1046c674ae898be6519a7f479" +checksum = "9ba20f23e85b10754cd195504aebf6a27e2e6cbe28c17778a0c930724628dd56" dependencies = [ "proc-macro2", "quote", @@ -1259,20 +1282,16 @@ dependencies = [ ] [[package]] -name = "time" -version = "0.1.43" +name = "tinyvec" +version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438" -dependencies = [ - "libc", - "winapi 0.3.8", -] +checksum = "238ce071d267c5710f9d31451efec16c5ee22de34df17cc05e56cbc92e967117" [[package]] name = "tokio" -version = "0.2.21" +version = "0.2.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d099fa27b9702bed751524694adbe393e18b36b204da91eb1cbbbbb4a5ee2d58" +checksum = "5d34ca54d84bf2b5b4d7d31e901a8464f7b60ac145a284fba25ceb801f2ddccd" dependencies = [ "bytes", "fnv", @@ -1324,9 +1343,9 @@ dependencies = [ [[package]] name = "toml" -version = "0.5.6" +version = "0.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffc92d160b1eef40665be3a05630d003936a3bc7da7421277846c2613e92c71a" +checksum = "75cf45bb0bef80604d001caaec0d09da99611b3c0fd39d3080468875cdb65645" dependencies = [ "serde", ] @@ -1338,10 +1357,41 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e987b6bf443f4b5b3b6f38704195592cca41c5bb7aedd3c3693c7081f8289860" [[package]] -name = "try-lock" -version = "0.2.2" +name = "tracing" +version = "0.1.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e604eb7b43c06650e854be16a2a03155743d3752dd1c943f6829e26b7a36e382" +checksum = "b0987850db3733619253fe60e17cb59b82d37c7e6c0236bb81e4d6b87c879f27" +dependencies = [ + "cfg-if 0.1.10", + "log", + "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 = "tracing-futures" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab7bb6f14721aa00656086e9335d363c5c8747bae02ebe32ea2c7dece5689b4c" +dependencies = [ + "pin-project 0.4.27", + "tracing", +] + +[[package]] +name = "try-lock" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" [[package]] name = "typenum" @@ -1375,11 +1425,11 @@ dependencies = [ [[package]] name = "unicode-normalization" -version = "0.1.12" +version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5479532badd04e128284890390c1e876ef7a993d0570b3597ae43dfa1d59afa4" +checksum = "6fb19cf769fa8c6a80a162df694621ebeb4dafb606470b2b2fce0be40a98a977" dependencies = [ - "smallvec", + "tinyvec", ] [[package]] @@ -1390,15 +1440,15 @@ checksum = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0" [[package]] name = "unicode-width" -version = "0.1.7" +version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479" +checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" [[package]] name = "unicode-xid" -version = "0.2.0" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" +checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" [[package]] name = "url" @@ -1447,11 +1497,11 @@ checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" [[package]] name = "wasm-bindgen" -version = "0.2.63" +version = "0.2.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c2dc4aa152834bc334f506c1a06b866416a8b6697d5c9f75b9a689c8486def0" +checksum = "1ac64ead5ea5f05873d7c12b545865ca2b8d28adfc50a49b84770a3a97265d42" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "serde", "serde_json", "wasm-bindgen-macro", @@ -1459,9 +1509,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.63" +version = "0.2.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ded84f06e0ed21499f6184df0e0cb3494727b0c5da89534e0fcc55c51d812101" +checksum = "f22b422e2a757c35a73774860af8e112bff612ce6cb604224e8e47641a9e4f68" dependencies = [ "bumpalo", "lazy_static", @@ -1474,11 +1524,11 @@ dependencies = [ [[package]] name = "wasm-bindgen-futures" -version = "0.4.13" +version = "0.4.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64487204d863f109eb77e8462189d111f27cb5712cc9fdb3461297a76963a2f6" +checksum = "b7866cab0aa01de1edf8b5d7936938a7e397ee50ce24119aef3e1eaa3b6171da" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "js-sys", "wasm-bindgen", "web-sys", @@ -1486,9 +1536,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.63" +version = "0.2.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "838e423688dac18d73e31edce74ddfac468e37b1506ad163ffaf0a46f703ffe3" +checksum = "6b13312a745c08c469f0b292dd2fcd6411dba5f7160f593da6ef69b64e407038" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -1496,9 +1546,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.63" +version = "0.2.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3156052d8ec77142051a533cdd686cba889537b213f948cd1d20869926e68e92" +checksum = "f249f06ef7ee334cc3b8ff031bfc11ec99d00f34d86da7498396dc1e3b1498fe" dependencies = [ "proc-macro2", "quote", @@ -1509,15 +1559,15 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.63" +version = "0.2.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c9ba19973a58daf4db6f352eda73dc0e289493cd29fb2632eb172085b6521acd" +checksum = "1d649a3145108d7d3fbcde896a468d1bd636791823c9921135218ad89be08307" [[package]] name = "web-sys" -version = "0.3.40" +version = "0.3.45" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b72fe77fd39e4bd3eaa4412fd299a0be6b3dfe9d2597e2f1c20beb968f41d17" +checksum = "4bf6ef87ad7ae8008e15a355ce696bed26012b7caa21605188cfd8214ab51e2d" dependencies = [ "js-sys", "wasm-bindgen", @@ -1531,9 +1581,9 @@ checksum = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" [[package]] name = "winapi" -version = "0.3.8" +version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" dependencies = [ "winapi-i686-pc-windows-gnu", "winapi-x86_64-pc-windows-gnu", @@ -1557,7 +1607,7 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" dependencies = [ - "winapi 0.3.8", + "winapi 0.3.9", ] [[package]] @@ -1572,7 +1622,7 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0120db82e8a1e0b9fb3345a539c478767c0048d842860994d96113d5b667bd69" dependencies = [ - "winapi 0.3.8", + "winapi 0.3.9", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 0bab98e..b85fc06 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,22 +9,23 @@ description = "Manage mimeapps.list and default applications with ease" [dependencies] pest = "2.1.3" pest_derive = "2.1.0" -clap = "3.0.0-beta.1" +clap = "3.0.0-beta.2" url = "2.1.1" itertools = "0.9.0" json = "0.12.4" shlex = "0.1.1" -thiserror = "1.0.19" +thiserror = "1.0.22" ascii_table = "3.0.1" xdg = "2.2.0" mime = "0.3.16" -regex = { version = "1.3.9", default-features = false, features = ["std"] } -mime-db = "0.1.5" +regex = { version = "1.4.2", default-features = false, features = ["std"] } +mime-db = "1.1.0" atty = "0.2.14" confy = "0.4.0" -serde = "1.0.111" +serde = "1.0.117" xdg-mime = "0.3.2" -freedesktop_entry_parser = "0.2.2" +freedesktop_entry_parser = "1.1.0" +once_cell = "1.4.1" [profile.release] opt-level=3 diff --git a/src/apps/system.rs b/src/apps/system.rs index 4d07339..c078654 100644 --- a/src/apps/system.rs +++ b/src/apps/system.rs @@ -5,7 +5,7 @@ use crate::{ use mime::Mime; use std::{collections::HashMap, convert::TryFrom, ffi::OsStr}; -#[derive(Debug)] +#[derive(Debug, Default)] pub struct SystemApps(pub HashMap>); impl SystemApps { diff --git a/src/apps/user.rs b/src/apps/user.rs index 6d09e56..0a84093 100644 --- a/src/apps/user.rs +++ b/src/apps/user.rs @@ -1,17 +1,18 @@ use crate::{ apps::SystemApps, common::{DesktopEntry, Handler}, - Error, Result, + Error, Result, CONFIG, }; use mime::Mime; use pest::Parser; use std::{ collections::{HashMap, VecDeque}, + io::Read, path::PathBuf, str::FromStr, }; -#[derive(Debug, pest_derive::Parser)] +#[derive(Debug, Default, pest_derive::Parser)] #[grammar = "common/ini.pest"] pub struct MimeApps { added_associations: HashMap>, @@ -20,21 +21,17 @@ pub struct MimeApps { } impl MimeApps { - pub fn add_handler(&mut self, mime: Mime, handler: Handler) -> Result<()> { - let handlers = self.default_apps.entry(mime).or_default(); - handlers.push_back(handler); - self.save()?; - Ok(()) + pub fn add_handler(&mut self, mime: Mime, handler: Handler) { + self.default_apps + .entry(mime) + .or_default() + .push_back(handler); } - pub fn set_handler(&mut self, mime: Mime, handler: Handler) -> Result<()> { - self.default_apps.insert(mime, { - let mut handlers = VecDeque::with_capacity(1); - handlers.push_back(handler); - handlers - }); - self.save()?; - Ok(()) + + pub fn set_handler(&mut self, mime: Mime, handler: Handler) { + self.default_apps.insert(mime, vec![handler].into()); } + pub fn remove_handler(&mut self, mime: &Mime) -> Result<()> { if let Some(_removed) = self.default_apps.remove(mime) { self.save()?; @@ -42,11 +39,20 @@ impl MimeApps { Ok(()) } - pub fn get_handler(&self, mime: &Mime) -> Result { - let config = crate::config::Config::load()?; + pub fn get_handler(&self, mime: &Mime) -> Result { + self.get_handler_from_user(mime) + .or_else(|_| { + let wildcard = + Mime::from_str(&format!("{}/*", mime.type_())).unwrap(); + self.get_handler_from_user(&wildcard) + }) + .or_else(|_| self.get_handler_from_added_associations(mime)) + } + + fn get_handler_from_user(&self, mime: &Mime) -> Result { match self.default_apps.get(mime) { - Some(handlers) if config.enable_selector && handlers.len() > 1 => { + Some(handlers) if CONFIG.enable_selector && handlers.len() > 1 => { let handlers = handlers .into_iter() .map(|h| (h, h.get_entry().unwrap().name)) @@ -54,7 +60,7 @@ impl MimeApps { let handler = { let name = - config.select(handlers.iter().map(|h| h.1.clone()))?; + CONFIG.select(handlers.iter().map(|h| h.1.clone()))?; handlers .into_iter() @@ -67,21 +73,21 @@ impl MimeApps { Ok(handler) } Some(handlers) => Ok(handlers.get(0).unwrap().clone()), - None => match self - .added_associations - .get(mime) - .map(|h| h.get(0).unwrap().clone()) - .or_else(|| self.system_apps.get_handler(mime)) - .ok_or(Error::NotFound(mime.to_string())) - { - Ok(h) => Ok(h), - Err(Error::NotFound(_)) if mime.type_() == "text" => self - .get_handler(&mime::TEXT_PLAIN) - .map_err(|_| Error::NotFound(mime.to_string())), - Err(e) => Err(e), - }, + None => Err(Error::NotFound(mime.to_string())), } } + + fn get_handler_from_added_associations( + &self, + mime: &Mime, + ) -> Result { + self.added_associations + .get(mime) + .map(|h| h.get(0).unwrap().clone()) + .or_else(|| self.system_apps.get_handler(mime)) + .ok_or(Error::NotFound(mime.to_string())) + } + pub fn show_handler(&self, mime: &Mime, output_json: bool) -> Result<()> { let handler = self.get_handler(mime)?; let output = if output_json { @@ -104,7 +110,16 @@ impl MimeApps { Ok(config) } pub fn read() -> Result { - let raw_conf = std::fs::read_to_string(Self::path()?)?; + let raw_conf = { + let mut buf = String::new(); + std::fs::OpenOptions::new() + .write(true) + .create(true) + .read(true) + .open(Self::path()?)? + .read_to_string(&mut buf)?; + buf + }; let file = Self::parse(Rule::file, &raw_conf)?.next().unwrap(); let mut current_section_name = "".to_string(); @@ -165,6 +180,7 @@ impl MimeApps { let f = std::fs::OpenOptions::new() .read(true) + .create(true) .write(true) .truncate(true) .open(Self::path()?)?; @@ -226,3 +242,43 @@ impl MimeApps { Ok(()) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn wildcard_mimes() -> Result<()> { + let mut user_apps = MimeApps::default(); + user_apps.add_handler( + Mime::from_str("video/*").unwrap(), + Handler::assume_valid("mpv.desktop".into()), + ); + user_apps.add_handler( + Mime::from_str("video/webm").unwrap(), + Handler::assume_valid("brave.desktop".into()), + ); + + assert_eq!( + user_apps + .get_handler(&Mime::from_str("video/mp4")?)? + .to_string(), + "mpv.desktop" + ); + assert_eq!( + user_apps + .get_handler(&Mime::from_str("video/asdf")?)? + .to_string(), + "mpv.desktop" + ); + + assert_eq!( + user_apps + .get_handler(&Mime::from_str("video/webm")?)? + .to_string(), + "brave.desktop" + ); + + Ok(()) + } +} diff --git a/src/common/desktop_entry.rs b/src/common/desktop_entry.rs index 2ef9fbc..8835182 100644 --- a/src/common/desktop_entry.rs +++ b/src/common/desktop_entry.rs @@ -1,4 +1,4 @@ -use crate::{Error, Result}; +use crate::{Error, Result, CONFIG}; use mime::Mime; use std::{ convert::TryFrom, @@ -17,7 +17,7 @@ pub struct DesktopEntry { pub(crate) mimes: Vec, } -#[derive(PartialEq, Eq)] +#[derive(PartialEq, Eq, Copy, Clone)] pub enum Mode { Launch, Open, @@ -55,16 +55,16 @@ impl DesktopEntry { Ok(()) } - pub fn get_cmd(&self, arg: Vec) -> Result<(String, Vec)> { + pub fn get_cmd(&self, args: Vec) -> Result<(String, Vec)> { let special = regex::Regex::new("%(f|F|u|U)").unwrap(); let mut split = shlex::split(&self.exec) .unwrap() .into_iter() .flat_map(|s| match s.as_str() { - "%f" | "%F" | "%u" | "%U" => arg.clone(), + "%f" | "%F" | "%u" | "%U" => args.clone(), s if special.is_match(s) => vec![special - .replace_all(s, arg.clone().join(" ").as_str()) + .replace_all(s, args.clone().join(" ").as_str()) .into()], _ => vec![s], }) @@ -73,9 +73,8 @@ impl DesktopEntry { // If the entry expects a terminal (emulator), but this process is not running in one, we // launch a new one. if self.term && !atty::is(atty::Stream::Stdout) { - let config = crate::config::Config::load()?; let terminal_emulator_args = - shlex::split(&config.terminal_emulator).unwrap(); + shlex::split(&CONFIG.terminal_emulator).unwrap(); split = terminal_emulator_args .into_iter() .chain(split.into_iter()) @@ -87,32 +86,29 @@ impl DesktopEntry { } fn parse_file(path: &Path) -> Option { - let raw = std::fs::read(&path).ok()?; - let parsed = freedesktop_entry_parser::parse_entry(&raw) - .filter_map(Result::ok) - .find(|s| s.title == b"Desktop Entry")?; + let raw_entry = freedesktop_entry_parser::parse_entry(&path).ok()?; + let section = raw_entry.section("Desktop Entry"); let mut entry = DesktopEntry::default(); entry.file_name = path.file_name()?.to_owned(); - for attr in parsed.attrs { + for attr in section.attrs().into_iter().filter(|a| a.has_value()) { match attr.name { - b"Name" if entry.name == "" => { - entry.name = String::from_utf8(attr.value.into()).ok()?; + "Name" if entry.name == "" => { + entry.name = attr.value.unwrap().into(); } - b"Exec" => { - entry.exec = String::from_utf8(attr.value.into()).ok()? - } - b"MimeType" => { - let mut mimes = String::from_utf8(attr.value.into()) - .ok()? + "Exec" => entry.exec = attr.value.unwrap().into(), + "MimeType" => { + let mut mimes = attr + .value + .unwrap() .split(";") .filter_map(|m| Mime::from_str(m).ok()) .collect::>(); mimes.pop(); entry.mimes = mimes; } - b"Terminal" => entry.term = attr.value == b"true", + "Terminal" => entry.term = attr.value.unwrap() == "true", _ => {} } } diff --git a/src/common/mime_types.rs b/src/common/mime_types.rs index 4e5f7a8..2bb8a69 100644 --- a/src/common/mime_types.rs +++ b/src/common/mime_types.rs @@ -18,8 +18,8 @@ impl MimeType { [m] if m == &mime::APPLICATION_OCTET_STREAM => { Err(Error::Ambiguous(ext.into())) } - [] => unreachable!(), [guess, ..] => Ok(guess.clone()), + [] => unreachable!(), } } } @@ -28,17 +28,16 @@ impl TryFrom<&str> for MimeType { type Error = Error; fn try_from(arg: &str) -> Result { - if let Ok(url) = url::Url::parse(arg) { - if url.scheme() == "file" { - return Self::try_from(url.path()) + match url::Url::parse(arg) { + Ok(url) if url.scheme() == "file" => { + Self::try_from(&*PathBuf::from(url.path())) } - Ok(Self( + Ok(url) => Ok(Self( format!("x-scheme-handler/{}", url.scheme()) .parse::() .unwrap(), - )) - } else { - Self::try_from(&*PathBuf::from(arg)) + )), + Err(_) => Self::try_from(&*PathBuf::from(arg)), } } } @@ -46,17 +45,29 @@ impl TryFrom<&str> for MimeType { impl TryFrom<&Path> for MimeType { type Error = Error; fn try_from(path: &Path) -> Result { - match xdg_mime::SharedMimeInfo::new() - .guess_mime_type() - .path(&path) - .guess() - .mime_type() - { - guess if guess == &mime::APPLICATION_OCTET_STREAM => { - Err(Error::Ambiguous(path.to_owned())) + use mime::APPLICATION_OCTET_STREAM as UNKNOWN; + let db = xdg_mime::SharedMimeInfo::new(); + + let name_guess = || match path.file_name() { + Some(f) => db.get_mime_types_from_file_name(&f.to_string_lossy()) + [0] + .clone(), + None => UNKNOWN, + }; + + let content_guess = db.guess_mime_type().path(&path).guess(); + + let mime = match (name_guess(), content_guess.mime_type().clone()) { + (m1, m2) if m1 == UNKNOWN && m2 == UNKNOWN => { + return Err(Error::Ambiguous(path.to_owned())) } - guess => Ok(Self(guess.clone())), - } + (m1, m2) if m1 == UNKNOWN => m2, + (m1, m2) if m2 == UNKNOWN => m1, + (m1, m2) if m1 != m2 => m2, + (m1, _) => m1, + }; + + Ok(Self(mime.clone())) } } @@ -110,6 +121,12 @@ mod tests { Ok(()) } + #[test] + fn filename_priority() -> Result<()> { + assert_eq!(MimeType::try_from("./tests/p.html")?.0, "text/html"); + Ok(()) + } + #[test] fn from_ext() -> Result<()> { assert_eq!(".mp3".parse::()?.0, "audio/mpeg"); diff --git a/src/config.rs b/src/config.rs index e213a4d..0cb87d6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,49 +1,31 @@ -use std::env; use crate::{Error, Result}; +use once_cell::sync::Lazy; use serde::{Deserialize, Serialize}; -fn default_terminal() -> std::string::String{ - match env::var("TERMINAL") { - Ok(val) => val, - Err(_) => "xterm -e".to_owned() - } -} - -fn default_selector() -> std::string::String{ - "rofi -dmenu -p 'Open With: '".to_owned() -} - -fn default_enable_selector() -> bool { - false -} +pub static CONFIG: Lazy = Lazy::new(Config::load); #[derive(Serialize, Deserialize)] +#[serde(default)] pub struct Config { - #[serde(default="default_enable_selector")] pub enable_selector: bool, - - #[serde(default="default_selector")] pub selector: String, - - #[serde(default="default_terminal")] pub terminal_emulator: String, } - impl Default for Config { fn default() -> Self { // This seems repetitive but serde does not uses Default Config { - enable_selector: default_enable_selector(), - selector: default_selector(), - terminal_emulator: default_terminal() + enable_selector: false, + selector: std::env::var("TERMINAL").unwrap_or("xterm -e".into()), + terminal_emulator: "rofi -dmenu -p 'Open With: '".into(), } } } impl Config { - pub fn load() -> Result { - Ok(confy::load("handlr")?) + pub fn load() -> Self { + confy::load("handlr").unwrap() } pub fn select>( diff --git a/src/main.rs b/src/main.rs index 7285dd0..ccccf68 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,6 @@ +use config::CONFIG; use error::{Error, Result}; +use once_cell::sync::Lazy; mod apps; mod cli; @@ -12,16 +14,20 @@ fn main() -> Result<()> { use common::MimeType; use std::convert::TryFrom; + // create config if it doesn't exist + Lazy::force(&CONFIG); + let mut apps = apps::MimeApps::read()?; - crate::config::Config::load()?; let res = || -> Result<()> { match Cmd::parse() { Cmd::Set { mime, handler } => { - apps.set_handler(mime.0, handler)?; + apps.set_handler(mime.0, handler); + apps.save()?; } Cmd::Add { mime, handler } => { - apps.add_handler(mime.0, handler)?; + apps.add_handler(mime.0, handler); + apps.save()?; } Cmd::Launch { mime, args } => { apps.get_handler(&mime.0)?.launch(args)?; diff --git a/tests/p.html b/tests/p.html new file mode 100644 index 0000000..55595b5 --- /dev/null +++ b/tests/p.html @@ -0,0 +1,3 @@ + +

asdf

+