modernize code
This commit is contained in:
parent
2a610965a1
commit
18a56f4454
@ -1,11 +1,11 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "mpdyn"
|
name = "mpdyn"
|
||||||
version = "0.1.2"
|
version = "0.1.3"
|
||||||
authors = ["Julien Rabier <taziden@flexiden.org>"]
|
authors = ["Julien Rabier <taziden@flexiden.org>"]
|
||||||
edition = "2021"
|
edition = "2024"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
mpdrs = "0.1.0"
|
mpdrs = "0.1.0"
|
||||||
rand = "0.8.4"
|
rand = "0.9.2"
|
||||||
|
|||||||
20
src/main.rs
20
src/main.rs
@ -1,6 +1,6 @@
|
|||||||
use mpdrs::lsinfo::LsInfoResponse;
|
use mpdrs::lsinfo::LsInfoResponse;
|
||||||
use mpdrs::{Client, Song};
|
use mpdrs::{Client, Song};
|
||||||
use rand::prelude::*;
|
use rand::{prelude::*, rng};
|
||||||
use std::{error::Error, thread, time};
|
use std::{error::Error, thread, time};
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn Error>> {
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
@ -40,16 +40,18 @@ fn populate_playlist(a: u32) -> Result<(), Box<dyn Error>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn get_random_tracks(n: u32) -> Result<Vec<Song>, Box<dyn Error>> {
|
fn get_random_tracks(n: u32) -> Result<Vec<Song>, Box<dyn Error>> {
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rng();
|
||||||
|
|
||||||
let query_albums = Client::default().lsinfo("/")?;
|
let query_albums = Client::default().lsinfo("/")?;
|
||||||
|
|
||||||
let tracks: Vec<Song> = query_albums
|
let tracks: Vec<Song> = Iterator::filter_map(
|
||||||
.choose_multiple(&mut rng, n.try_into()?)
|
query_albums
|
||||||
.filter(|&x| match_directory(x).is_ok())
|
.choose_multiple(&mut rng, n.try_into()?)
|
||||||
.map(find_path)
|
.filter(|&x| match_directory(x).is_ok())
|
||||||
.filter_map(|x| Some(get_random_track(x)).unwrap())
|
.map(find_path),
|
||||||
.collect();
|
get_random_track,
|
||||||
|
)
|
||||||
|
.collect();
|
||||||
|
|
||||||
Ok(tracks)
|
Ok(tracks)
|
||||||
}
|
}
|
||||||
@ -65,7 +67,7 @@ fn match_directory(response: &LsInfoResponse) -> Result<i32, i32> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn get_random_track(path: &str) -> Option<Song> {
|
fn get_random_track(path: &str) -> Option<Song> {
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rng();
|
||||||
|
|
||||||
match Client::default().lsinfo(path) {
|
match Client::default().lsinfo(path) {
|
||||||
Ok(songs) => match songs.choose(&mut rng) {
|
Ok(songs) => match songs.choose(&mut rng) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user