diff --git a/Cargo.lock b/Cargo.lock index 5f420e8e..44986f38 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -233,13 +233,10 @@ dependencies = [ [[package]] name = "block-buffer" -version = "0.8.0" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "block-padding 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", - "byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", - "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", - "generic-array 0.14.2 (registry+https://github.com/rust-lang/crates.io-index)", + "generic-array 0.14.4 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -438,6 +435,11 @@ dependencies = [ "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "cpuid-bool" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "crossbeam-deque" version = "0.7.2" @@ -621,7 +623,7 @@ name = "digest" version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "generic-array 0.14.2 (registry+https://github.com/rust-lang/crates.io-index)", + "generic-array 0.14.4 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -869,15 +871,15 @@ name = "generic-array" version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "typenum 1.11.2 (registry+https://github.com/rust-lang/crates.io-index)", + "typenum 1.12.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "generic-array" -version = "0.14.2" +version = "0.14.4" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "typenum 1.11.2 (registry+https://github.com/rust-lang/crates.io-index)", + "typenum 1.12.0 (registry+https://github.com/rust-lang/crates.io-index)", "version_check 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1559,7 +1561,7 @@ dependencies = [ "num-rational 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", - "typenum 1.11.2 (registry+https://github.com/rust-lang/crates.io-index)", + "typenum 1.12.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1736,6 +1738,11 @@ name = "opaque-debug" version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "opaque-debug" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "openssl" version = "0.10.26" @@ -2231,11 +2238,6 @@ dependencies = [ "winreg 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "result" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "rodio" version = "0.9.0" @@ -2422,16 +2424,6 @@ dependencies = [ "syn 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "serde_ini" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "result 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.115 (registry+https://github.com/rust-lang/crates.io-index)", - "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "serde_json" version = "1.0.44" @@ -2466,13 +2458,14 @@ dependencies = [ [[package]] name = "sha-1" -version = "0.9.0" +version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "block-buffer 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "block-buffer 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "cpuid-bool 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "digest 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", - "fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "opaque-debug 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "opaque-debug 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -2600,13 +2593,13 @@ dependencies = [ "percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "rspotify 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.115 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_ini 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "sha-1 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", + "sha-1 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", "structopt 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)", "syslog 4.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-io 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-signal 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "toml 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", "url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)", "whoami 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "xdg 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3078,6 +3071,14 @@ dependencies = [ "tokio 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "toml" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "serde 1.0.115 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "tower-service" version = "0.3.0" @@ -3095,7 +3096,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "typenum" -version = "1.11.2" +version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -3432,7 +3433,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum bitflags 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4efd02e230a02e18f92fc2735f44597385ed02ad8f831e7c1c1156ee5e1ab3a5" "checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" "checksum block-buffer 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c0940dc441f31689269e10ac70eb1002a3a1d3ad1390e030043662eb7fe4688b" -"checksum block-buffer 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "dbcf92448676f82bb7a334c58bbce8b0d43580fb5362a9d608b18879d12a3d31" +"checksum block-buffer 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" "checksum block-cipher-trait 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1c924d49bd09e7c06003acda26cd9742e796e34282ec6c1189404dee0c1f4774" "checksum block-modes 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "31aa8410095e39fdb732909fb5730a48d5bd7c2e3cd76bd1b07b3dbea130c529" "checksum block-padding 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "fa79dedbb091f449f1f39e53edf88d5dbe95f895dae6135a8d7b881fb5af73f5" @@ -3458,6 +3459,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum coreaudio-rs 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f229761965dad3e9b11081668a6ea00f1def7aa46062321b5ec245b834f6e491" "checksum coreaudio-sys 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7e8f5954c1c7ccb55340443e8b29fca24013545a5e7d72c1ca7db4fc02b982ce" "checksum cpal 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d58ae1ed6536b1b233f5e3aeb6997a046ddb4d05e3f61701b58a92eb254a829e" +"checksum cpuid-bool 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "8aebca1129a03dc6dc2b127edd729435bbc4a37e1d5f4d7513165089ceb02634" "checksum crossbeam-deque 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c3aa945d63861bfe624b55d153a39684da1e8c0bc8fba932f7ee3a3c16cea3ca" "checksum crossbeam-epoch 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5064ebdbf05ce3cb95e45c8b086f72263f4166b29b97f6baff7ef7fe047b55ac" "checksum crossbeam-queue 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7c979cd6cfe72335896575c6b5688da489e420d36a27a0b9eb0c73db574b4a4b" @@ -3508,7 +3510,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum futures-util 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "22766cf25d64306bedf0384da004d05c9974ab104fcc4528f1236181c18004c5" "checksum gcc 0.3.55 (registry+https://github.com/rust-lang/crates.io-index)" = "8f5f3913fa0bfe7ee1fd8248b6b9f42a5af4b9d65ec2dd2c3c26132b950ecfc2" "checksum generic-array 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c68f0274ae0e023facc3c97b2e00f076be70e254bc851d972503b328db79b2ec" -"checksum generic-array 0.14.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ac746a5f3bbfdadd6106868134545e684693d54d9d44f6e9588a7d54af0bf980" +"checksum generic-array 0.14.4 (registry+https://github.com/rust-lang/crates.io-index)" = "501466ecc8a30d1d3b7fc9229b122b2ce8ed6e9d9223f1138d4babb253e51817" "checksum gethostname 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e692e296bfac1d2533ef168d0b60ff5897b8b70a4009276834014dd8924cc028" "checksum getopts 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)" = "14dbbfd5c71d70241ecf9e6f13737f7b5ce823821063188d7e46c41d371eebd5" "checksum getrandom 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb" @@ -3590,6 +3592,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum ogg 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d79f1db9148be9d0e174bb3ac890f6030fcb1ed947267c5a91ee4c91b5a91e15" "checksum ogg-sys 0.0.9 (registry+https://github.com/rust-lang/crates.io-index)" = "a95b8c172e17df1a41bf8d666301d3b2c4efeb90d9d0415e2a4dc0668b35fdb2" "checksum opaque-debug 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c" +"checksum opaque-debug 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" "checksum openssl 0.10.26 (registry+https://github.com/rust-lang/crates.io-index)" = "3a3cc5799d98e1088141b8e01ff760112bbd9f19d850c124500566ca6901a585" "checksum openssl-probe 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "77af24da69f9d9341038eba93a073b1fdaaa1b788221b00a69bce9e762cb32de" "checksum openssl-sys 0.9.53 (registry+https://github.com/rust-lang/crates.io-index)" = "465d16ae7fc0e313318f7de5cecf57b2fbe7511fd213978b457e1c96ff46736f" @@ -3645,7 +3648,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum relay 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1576e382688d7e9deecea24417e350d3062d97e32e45d70b1cde65994ff1489a" "checksum remove_dir_all 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4a83fa3702a688b9359eccba92d153ac33fd2e8462f9e0e3fdf155239ea7792e" "checksum reqwest 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c0e798e19e258bf6c30a304622e3e9ac820e483b06a1857a026e1f109b113fe4" -"checksum result 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "194d8e591e405d1eecf28819740abed6d719d1a2db87fc0bcdedee9a26d55560" "checksum rodio 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5d0f961b254e66d147a7b550c78b01308934c97d807a34b417fd0f5a0a0f3a2d" "checksum rpassword 3.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c34fa7bcae7fca3c8471e8417088bbc3ad9af8066b0ecf4f3c0d98a0d772716e" "checksum rspotify 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a924a166cfb1315c8d9c89148e438a1337feb655ce052fc6dc952af8018bad93" @@ -3667,11 +3669,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" "checksum serde 1.0.115 (registry+https://github.com/rust-lang/crates.io-index)" = "e54c9a88f2da7238af84b5101443f0c0d0a3bbdc455e34a5c9497b1903ed55d5" "checksum serde_derive 1.0.115 (registry+https://github.com/rust-lang/crates.io-index)" = "609feed1d0a73cc36a0182a840a9b37b4a82f0b1150369f0536a9e3f2a31dc48" -"checksum serde_ini 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "eb236687e2bb073a7521c021949be944641e671b8505a94069ca37b656c81139" "checksum serde_json 1.0.44 (registry+https://github.com/rust-lang/crates.io-index)" = "48c575e0cc52bdd09b47f330f646cf59afc586e9c4e3ccd6fc1f625b8ea1dad7" "checksum serde_urlencoded 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9ec5d77e2d4c73717816afac02670d5c4f534ea95ed430442cad02e7a6e32c97" "checksum sha-1 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f7d94d0bede923b3cea61f3f1ff57ff8cdfd77b400fb8f9998949e0cf04163df" -"checksum sha-1 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "59520a294fcfdaff2ce8276dc1bdc6170b97abe8043560f70178222e611242fc" +"checksum sha-1 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "170a36ea86c864a3f16dd2687712dd6646f7019f301e57537c7f4dc9f5916770" "checksum sha2 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "27044adfd2e1f077f649f59deb9490d3941d674002f7d062870a60ebe9bd47a0" "checksum shannon 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7ea5b41c9427b56caa7b808cb548a04fb50bb5b9e98590b53f28064ff4174561" "checksum shell-words 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "39acde55a154c4cd3ae048ac78cc21c25f3a0145e44111b523279113dce0d94a" @@ -3727,10 +3728,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum tokio-udp 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "f02298505547f73e60f568359ef0d016d5acd6e830ab9bc7c4a5b3403440121b" "checksum tokio-uds 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "037ffc3ba0e12a0ab4aca92e5234e0dedeb48fddf6ccd260f1f150a36a9f2445" "checksum tokio-util 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "571da51182ec208780505a32528fc5512a8fe1443ab960b3f2f3ef093cd16930" +"checksum toml 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ffc92d160b1eef40665be3a05630d003936a3bc7da7421277846c2613e92c71a" "checksum tower-service 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e987b6bf443f4b5b3b6f38704195592cca41c5bb7aedd3c3693c7081f8289860" "checksum try-lock 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee2aa4715743892880f70885373966c83d73ef1b0838a664ef0c76fffd35e7c2" "checksum try-lock 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e604eb7b43c06650e854be16a2a03155743d3752dd1c943f6829e26b7a36e382" -"checksum typenum 1.11.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6d2783fe2d6b8c1101136184eb41be8b1ad379e4657050b8aaff0c79ee7575f9" +"checksum typenum 1.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33" "checksum unicase 2.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" "checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" "checksum unicode-normalization 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "b561e267b2326bb4cebfc0ef9e68355c7abe6c6f522aeac2f5bf95d56c59bdcf" diff --git a/Cargo.toml b/Cargo.toml index 3cedafa8..226926d6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,6 @@ log = "0.4.6" percent-encoding = "2.1.0" rspotify = "0.8.0" serde = { version = "1.0.115", features = ["derive"] } -serde_ini = "0.2.0" sha-1 = "0.9" structopt = "0.3.17" syslog = "4" @@ -34,6 +33,7 @@ tokio-signal = "0.1" url = "1.7" xdg = "2.2" librespot = { version = "0.1.1", default-features = false, features = ["with-tremor"] } +toml = "0.5.6" [target."cfg(target_os = \"macos\")".dependencies] whoami = "0.9.0" diff --git a/README.md b/README.md index 5222125e..187585ed 100644 --- a/README.md +++ b/README.md @@ -169,19 +169,19 @@ spotifyd --help ### Configuration file -`Spotifyd` is able to load configuration values from a file too. The file has to be named `spotifyd.conf` and reside in the user's configuration directory (`~/.config/spotifyd`) or the system configuration directory (`/etc` or `/etc/xdg/spotifyd`). This also applies to macOS! +`Spotifyd` is able to load configuration values from a [TOML](https://toml.io/en/v0.5.0) file too. The file has to be named `spotifyd.conf` and reside in the user's configuration directory (`~/.config/spotifyd`) or the system configuration directory (`/etc` or `/etc/xdg/spotifyd`). This also applies to macOS! The configuration file consists of two sections, `global` and `spotifyd`, whereas `spotifyd` takes priority over `global`. The configuration file has the following format: -```ini +```toml [global] # Your Spotify account name. -username = username +username = "username" # Your Spotify account password. -password = password +password = "password" # A command that gets executed and can be used to # retrieve your password. @@ -189,7 +189,7 @@ password = password # # This is an alternative to the `password` field. Both # can't be used simultaneously. -password_cmd = command_that_writes_password_to_stdout +password_cmd = "command_that_writes_password_to_stdout" # If set to true, `spotifyd` tries to look up your # password in the system's password storage. @@ -200,30 +200,30 @@ use_keyring = true # The audio backend used to play the your music. To get # a list of possible backends, run `spotifyd --help`. -backend = alsa +backend = "alsa" # The alsa audio device to stream audio to. To get a # list of valid devices, run `aplay -L`, -device = alsa_audio_device # omit for macOS +device = "alsa_audio_device" # omit for macOS # The alsa control device. By default this is the same # name as the `device` field. -control = alsa_audio_device # omit for macOS +control = "alsa_audio_device" # omit for macOS # The alsa mixer used by `spotifyd`. -mixer = PCM +mixer = "PCM" # The volume controller. Each one behaves different to # volume increases. For possible values, run # `spotifyd --help`. -volume_controller = alsa # use softvol for macOS +volume_controller = "alsa" # use softvol for macOS # A command that gets executed in your shell after each song changes. -on_song_change_hook = command_to_run_on_playback_events +on_song_change_hook = "command_to_run_on_playback_events" # The name that gets displayed under the connect tab on # official clients. Spaces are not allowed! -device_name = device_name_in_spotify_connect +device_name = "device_name_in_spotify_connect" # The audio bitrate. 96, 160 or 320 kbit/s bitrate = 160 @@ -234,7 +234,7 @@ bitrate = 160 # # Note: The file path does not get expanded. Environment variables and # shell placeholders like $HOME or ~ don't work! -cache_path = cache_directory +cache_path = "cache_directory" # If set to true, audio data does NOT get cached. no_audio_cache = true @@ -252,12 +252,12 @@ normalisation_pregain = -10 zeroconf_port = 1234 # The proxy `spotifyd` will use to connect to spotify. -proxy = http://proxy.example.org:8080 +proxy = "http://proxy.example.org:8080" # The displayed device type in Spotify clients. # Can be unknown, computer, tablet, smartphone, speaker, tv, # avr (Audio/Video Receiver), stb (Set-Top Box), and audiodongle. -device_type = speaker +device_type = "speaker" ``` #### Alternatives to storing your password in the config file @@ -268,9 +268,9 @@ device_type = speaker For example (using the password-management utility [pass][pass-homepage]). - ```ini + ```toml # ~/.config/spotifyd/spotifyd.conf - password_cmd = pass spotify + password_cmd = "pass spotify" ``` - **`use_keyring`** config entry / **`--use-keyring`** CLI flag diff --git a/src/config.rs b/src/config.rs index 749e3bac..cdfb6838 100644 --- a/src/config.rs +++ b/src/config.rs @@ -9,9 +9,9 @@ use librespot::{ playback::config::{Bitrate as LSBitrate, PlayerConfig}, }; use log::{error, info, warn}; -use serde::{de, Deserialize}; +use serde::{de::Error, de::Unexpected, Deserialize, Deserializer}; use sha1::{Digest, Sha1}; -use std::{fmt, fs, io::BufRead, path::PathBuf, str::FromStr, string::ToString}; +use std::{fmt, fs, path::PathBuf, str::FromStr, string::ToString}; use structopt::{clap::AppSettings, StructOpt}; use url::Url; @@ -170,16 +170,28 @@ impl ToString for DeviceType { static BITRATE_VALUES: &[&str] = &["96", "160", "320"]; /// Spotify's audio bitrate -#[derive(Clone, Copy, Debug, Deserialize, PartialEq, StructOpt)] +#[derive(Clone, Copy, Debug, PartialEq, StructOpt)] pub enum Bitrate { - #[serde(rename = "96")] Bitrate96, - #[serde(rename = "160")] Bitrate160, - #[serde(rename = "320")] Bitrate320, } +impl<'de> Deserialize<'de> for Bitrate { + fn deserialize>(deserializer: D) -> Result { + match u16::deserialize(deserializer) { + Ok(96) => Ok(Self::Bitrate96), + Ok(160) => Ok(Self::Bitrate160), + Ok(320) => Ok(Self::Bitrate320), + Ok(x) => Err(D::Error::invalid_value( + Unexpected::Unsigned(x.into()), + &"a bitrate: 96, 160, 320", + )), + Err(e) => Err(e), + } + } +} + impl FromStr for Bitrate { type Err = ParseError; @@ -203,30 +215,6 @@ impl Into for Bitrate { } } -struct BoolFromStr; - -impl<'de> de::Visitor<'de> for BoolFromStr { - type Value = bool; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("a string containing \"true\" or \"false\"") - } - - fn visit_str(self, s: &str) -> Result - where - E: serde::de::Error, - { - bool::from_str(s).map_err(serde::de::Error::custom) - } -} - -fn de_from_str<'de, D>(deserializer: D) -> Result -where - D: de::Deserializer<'de>, -{ - deserializer.deserialize_str(BoolFromStr) -} - #[derive(Debug, Default, StructOpt)] #[structopt( about = "A Spotify daemon", @@ -281,7 +269,7 @@ pub struct SharedConfigValues { #[cfg_attr( feature = "dbus_keyring", structopt(long), - serde(alias = "use-keyring", default, deserialize_with = "de_from_str") + serde(alias = "use-keyring", default) )] #[cfg_attr(not(feature = "dbus_keyring"), structopt(skip), serde(skip))] use_keyring: bool, @@ -307,7 +295,7 @@ pub struct SharedConfigValues { /// Disable the use of audio cache #[structopt(long)] - #[serde(default, deserialize_with = "de_from_str")] + #[serde(default)] no_audio_cache: bool, /// The audio backend to use @@ -345,7 +333,7 @@ pub struct SharedConfigValues { /// Enable to normalize the volume during playback #[structopt(long)] - #[serde(default, deserialize_with = "de_from_str")] + #[serde(default)] volume_normalisation: bool, /// A custom pregain applied before sending the audio to the output device @@ -366,7 +354,7 @@ pub struct SharedConfigValues { /// Autoplay on connect #[structopt(long)] - #[serde(default, deserialize_with = "de_from_str")] + #[serde(default)] autoplay: bool, } @@ -460,44 +448,24 @@ impl fmt::Debug for SharedConfigValues { impl CliConfig { pub fn load_config_file_values(&mut self) { - let config_file_path = self.config_path.clone().or_else(get_config_file); + let config_file_path = match self.config_path.clone().or_else(get_config_file) { + Some(p) => p, + None => { + info!("No config file specified. Running with default values"); + return; + } + }; + info!("Loading config from {:?}", &config_file_path); - if config_file_path.is_none() { - info!("No config file specified. Running with default values"); - return; - } - let unwrapped_config_file_path = config_file_path.unwrap(); - info!("Loading config from {:?}", &unwrapped_config_file_path); - - let config_file = fs::File::open(&unwrapped_config_file_path); - if config_file.is_err() { - info!( - "Failed to open config file at {:?}", - &unwrapped_config_file_path - ); - return; - } + let content = match fs::read_to_string(config_file_path) { + Ok(s) => s, + Err(e) => { + info!("Failed reading config file: {}", e); + return; + } + }; - let bufreader = std::io::BufReader::new(config_file.unwrap()); - // serde_ini doesn't support inline comments. We treat every hashtag as a comment starter and everything that follows - // it as not part of the key's value. - // The method below will filter out any errors that occur. - // TODO: Is there a cleaner way to do this? One with less allocations. - let comment_free_lines: Vec = bufreader - .lines() - .filter_map(Option::Some) - .map(|x| x.unwrap()) - .map(|mut l: String| { - let last_index = l.rfind('#').unwrap_or_else(|| l.len()); - l.drain(..last_index).collect() - }) - // The password field takes the whole value as the password. We need to remove the space between - // the password and the # character. - .map(|l: String| l.trim().to_string()) - .collect(); - - let comment_free_content = comment_free_lines.join("\n"); - let config_content: FileConfig = serde_ini::from_str(&comment_free_content).unwrap(); + let config_content: FileConfig = toml::from_str(&content).unwrap(); // The call to get_merged_sections consumes the FileConfig! if let Some(merged_sections) = config_content.get_merged_sections() {