mirror of
https://github.com/Dangoware/dmp-core.git
synced 2025-04-19 17:42:56 -05:00
Can now sort by song fields
This commit is contained in:
parent
140bae703a
commit
450750f5a0
1 changed files with 37 additions and 14 deletions
|
@ -85,7 +85,7 @@ impl Song {
|
||||||
*
|
*
|
||||||
* let tag = this_song.get_tag(Tag::Title);
|
* let tag = this_song.get_tag(Tag::Title);
|
||||||
*
|
*
|
||||||
* assert_eq!(tag, "Title");
|
* assert_eq!(tag, "Some Song Title");
|
||||||
* ```
|
* ```
|
||||||
**/
|
**/
|
||||||
pub fn get_tag(&self, target_key: &Tag) -> Option<&String> {
|
pub fn get_tag(&self, target_key: &Tag) -> Option<&String> {
|
||||||
|
@ -97,11 +97,11 @@ impl Song {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_field(&self, target_field: &str) -> Box<dyn Any> {
|
pub fn get_field(&self, target_field: &str) -> Option<String> {
|
||||||
match target_field {
|
match target_field {
|
||||||
"location" => Box::new(self.location.clone()),
|
"location" => Some(self.location.clone().to_string()),
|
||||||
"plays" => Box::new(self.plays.clone()),
|
"plays" => Some(self.plays.clone().to_string()),
|
||||||
_ => todo!()
|
_ => None // Other field types is not yet supported
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -383,6 +383,9 @@ impl MusicLibrary {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Query the database, returning a list of [Song]s
|
/// Query the database, returning a list of [Song]s
|
||||||
|
///
|
||||||
|
/// The order in which the `sort_by` `Vec` is arranged
|
||||||
|
/// determines the output sorting
|
||||||
pub fn query(
|
pub fn query(
|
||||||
&self,
|
&self,
|
||||||
query_string: &String, // The query itself
|
query_string: &String, // The query itself
|
||||||
|
@ -426,18 +429,38 @@ impl MusicLibrary {
|
||||||
|
|
||||||
new_songs.par_sort_by(|a, b| {
|
new_songs.par_sort_by(|a, b| {
|
||||||
for opt in sort_by {
|
for opt in sort_by {
|
||||||
let tag_a = match a.get_tag(&opt) {
|
let tag_a = match opt {
|
||||||
Some(tag) => tag,
|
Tag::Field(field_selection) => {
|
||||||
None => continue
|
match a.get_field(field_selection) {
|
||||||
|
Some(field_value) => field_value,
|
||||||
|
None => continue
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => {
|
||||||
|
match a.get_tag(&opt) {
|
||||||
|
Some(tag_value) => tag_value.to_owned(),
|
||||||
|
None => continue
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let tag_b = match b.get_tag(&opt) {
|
let tag_b = match opt {
|
||||||
Some(tag) => tag,
|
Tag::Field(field_selection) => {
|
||||||
None => continue
|
match b.get_field(field_selection) {
|
||||||
|
Some(field_value) => field_value,
|
||||||
|
None => continue
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => {
|
||||||
|
match b.get_tag(&opt) {
|
||||||
|
Some(tag_value) => tag_value.to_owned(),
|
||||||
|
None => continue
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Try to parse the tags as f64 (floating-point numbers)
|
// Try to parse the tags as f64
|
||||||
if let (Ok(num_a), Ok(num_b)) = (tag_a.parse::<f64>(), tag_b.parse::<f64>()) {
|
if let (Ok(num_a), Ok(num_b)) = (tag_a.parse::<i32>(), tag_b.parse::<i32>()) {
|
||||||
// If parsing succeeds, compare as numbers
|
// If parsing succeeds, compare as numbers
|
||||||
if num_a < num_b {
|
if num_a < num_b {
|
||||||
return std::cmp::Ordering::Less;
|
return std::cmp::Ordering::Less;
|
||||||
|
@ -454,7 +477,7 @@ impl MusicLibrary {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If all tags are equal, sort by some default criteria (e.g., song title)
|
// If all tags are equal, sort by title
|
||||||
a.get_tag(&Tag::Title).cmp(&b.get_tag(&Tag::Title))
|
a.get_tag(&Tag::Title).cmp(&b.get_tag(&Tag::Title))
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue