working on refactoring some functions

This commit is contained in:
Djairo Hougee 2023-05-08 16:41:30 +02:00
parent 758108c925
commit 62a81cfaab
3 changed files with 15 additions and 16 deletions

View File

@ -21,16 +21,6 @@ impl Field {
} }
} }
// I shouldn't need this; remove when done with testing FIXME
impl Default for Field {
fn default() -> Self {
Self {
field: Default::default(),
num_chars: Default::default()
}
}
}
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct Rating { pub struct Rating {
pub nil: char, pub nil: char,
@ -93,6 +83,14 @@ impl Config {
pub fn priorities_to_lower(&mut self) { pub fn priorities_to_lower(&mut self) {
self.player_priorities = self.player_priorities.iter().map(|i| i.to_lowercase()).collect(); self.player_priorities = self.player_priorities.iter().map(|i| i.to_lowercase()).collect();
} }
pub fn find_player_priorities_idx(&self, name: &str) -> i32 {
match self.player_priorities.iter()
.position(|x| x.to_ascii_lowercase().eq(&name.to_ascii_lowercase())) {
Some(idx) => idx as i32,
None => i32::MAX,
}
}
} }
fn ms(str: &str) -> String { fn ms(str: &str) -> String {

View File

@ -11,7 +11,7 @@ fn update_prefix(cfg: &Config, data: &mut Data) {
data.display_prefix = char.clone(); data.display_prefix = char.clone();
trace!("updated prefix to {}", data.display_prefix); trace!("updated prefix to {}", data.display_prefix);
} else { } else {
data.display_prefix = cfg.player_prefixes.get("default").unwrap().clone(); //todo: error handling data.display_prefix = cfg.player_prefixes.get("default").unwrap_or(&'>').clone();
trace!("set prefix to default ({})", data.display_prefix); trace!("set prefix to default ({})", data.display_prefix);
} }
} }

View File

@ -6,7 +6,7 @@ pub fn update_players(
cfg: &Config, cfg: &Config,
mut data: &mut Data, mut data: &mut Data,
) { ) {
let players = pf.find_all().unwrap(); //todo: error handling let players = pf.find_all().unwrap_or(Vec::new());
if players.is_empty() { if players.is_empty() {
data.current_player = None; data.current_player = None;
} else { } else {
@ -14,8 +14,8 @@ pub fn update_players(
for player in players { for player in players {
if cfg.player_priorities.contains(&player.identity().to_owned().to_ascii_lowercase()) { if cfg.player_priorities.contains(&player.identity().to_owned().to_ascii_lowercase()) {
let name = player.identity(); let name = player.identity();
let idx = cfg.player_priorities.iter().position(|x| x.to_ascii_lowercase().eq(&name.to_ascii_lowercase())).unwrap() as i32; //todo: move to function in config; error handling let idx = cfg.find_player_priorities_idx(name);
let status = player.get_playback_status().unwrap(); //todo: error handling if let Ok(status) = player.get_playback_status() {
match status { match status {
mpris::PlaybackStatus::Playing => active[0].push((idx, name.to_owned())), mpris::PlaybackStatus::Playing => active[0].push((idx, name.to_owned())),
mpris::PlaybackStatus::Paused => active[1].push((idx, name.to_owned())), mpris::PlaybackStatus::Paused => active[1].push((idx, name.to_owned())),
@ -23,6 +23,7 @@ pub fn update_players(
}; };
} }
} }
}
if !active[0].is_empty() { if !active[0].is_empty() {
data.current_player = Some(get_lowest(&active[0])); data.current_player = Some(get_lowest(&active[0]));
} else if !active[1].is_empty() { } else if !active[1].is_empty() {