updated player_priorities to use a HashMap over a Vec, for faster priority finding

This commit is contained in:
Djairo Hougee 2023-05-12 14:31:53 +02:00
parent dd7da4533b
commit c1bbc9a188
1 changed files with 16 additions and 12 deletions

View File

@ -119,9 +119,8 @@ pub struct Config {
pub array_separator: char, pub array_separator: char,
/// Character to insert when a string is truncated. None implies no cut off character is inserted and the strings are truncated as is. /// Character to insert when a string is truncated. None implies no cut off character is inserted and the strings are truncated as is.
pub break_character: Option<char>, pub break_character: Option<char>,
/// Vec of mpris identities, describing what players are considered acceptable. /// Hashmap of mpris identities, describing what players are considered acceptable.
/// Prioritised based on vec index (closer to 0 -> higher priority). pub player_priorities: HashMap<String, u8>,
pub player_priorities: Vec<String>,
/// Characters to use for the xesam:userRating field. /// Characters to use for the xesam:userRating field.
/// If None, default values are used ('-', '/', '+'). /// If None, default values are used ('-', '/', '+').
pub rating_icons: Option<Rating>, pub rating_icons: Option<Rating>,
@ -159,6 +158,9 @@ impl Config {
/// ///
/// TODO: using a HashMap would be more efficient i think. /// TODO: using a HashMap would be more efficient i think.
pub fn find_player_priorities_idx(&self, name: &str) -> u8 { pub fn find_player_priorities_idx(&self, name: &str) -> u8 {
match self.player_priorities.get(name) {
Some(val) => *val,
None => u8::MAX,
} }
} }
@ -172,15 +174,17 @@ impl Config {
/// This function returns the default player_priorities, used when a non-existent config file is requested. /// This function returns the default player_priorities, used when a non-existent config file is requested.
/// The values of these are based on nothing but my own experience; in fact I'm not even sure if the Spotify app's identity is correct. /// The values of these are based on nothing but my own experience; in fact I'm not even sure if the Spotify app's identity is correct.
fn default_player_priorities() -> Vec<String> { fn default_player_priorities() -> HashMap< String, u8> {
vec![ let mut out = HashMap::new();
"Clementine".to_owned(),
"Spotify".to_owned(), out.insert("Clementine".to_owned(), 1);
"mpv".to_owned(), out.insert("Spotify".to_owned(), 2);
"VLC Media Player".to_owned(), out.insert("mpv".to_owned(), 3);
"Firefox".to_owned(), out.insert("VLC Media Player".to_owned(), 4);
"Chromium".to_owned() out.insert("Firefox".to_owned(), 5);
] out.insert("Chromium".to_owned(), 6);
out
} }
/// This function returns the default metadata fields, used when a non-existent config file is requested. /// This function returns the default metadata fields, used when a non-existent config file is requested.