-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce "sort specs" to allow fine-grained sorting of files in torr…
…ents Sort specs are of the form `KEY:ORDER`, and allow sorting files in a torrent by multiple criteria. Multiple sort specs can be passed with `--sort-by` upon torrent creation. type: added
- Loading branch information
Showing
8 changed files
with
220 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use crate::common::*; | ||
|
||
#[derive(Clone, Copy, Debug, PartialEq, IntoStaticStr, EnumString)] | ||
#[strum(serialize_all = "kebab-case")] | ||
pub(crate) enum SortKey { | ||
Path, | ||
Size, | ||
} | ||
|
||
impl SortKey { | ||
pub(crate) fn name(self) -> &'static str { | ||
self.into() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use crate::common::*; | ||
|
||
#[derive(Clone, Copy, Debug, PartialEq, IntoStaticStr, EnumString)] | ||
#[strum(serialize_all = "kebab-case")] | ||
pub(crate) enum SortOrder { | ||
Ascending, | ||
Descending, | ||
} | ||
|
||
impl SortOrder { | ||
pub(crate) fn name(self) -> &'static str { | ||
self.into() | ||
} | ||
} | ||
|
||
impl Default for SortOrder { | ||
fn default() -> Self { | ||
Self::Ascending | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
use crate::common::*; | ||
|
||
#[derive(Clone, Copy, Debug, PartialEq)] | ||
pub(crate) struct SortSpec { | ||
key: SortKey, | ||
order: SortOrder, | ||
} | ||
|
||
impl SortSpec { | ||
pub(crate) fn compare(specs: &[SortSpec], a: &FileInfo, b: &FileInfo) -> Ordering { | ||
let mut specs = specs.to_vec(); | ||
|
||
specs.push(SortSpec::default()); | ||
|
||
Self::compare_specs(&specs, a, b) | ||
} | ||
|
||
fn compare_specs(specs: &[SortSpec], a: &FileInfo, b: &FileInfo) -> Ordering { | ||
specs.iter().fold(Ordering::Equal, |ordering, spec| { | ||
ordering.then_with(|| spec.compare_file_info(a, b)) | ||
}) | ||
} | ||
|
||
fn compare_file_info(self, a: &FileInfo, b: &FileInfo) -> Ordering { | ||
let ordering = match self.key { | ||
SortKey::Path => a.path.cmp(&b.path), | ||
SortKey::Size => a.length.cmp(&b.length), | ||
}; | ||
|
||
match self.order { | ||
SortOrder::Ascending => ordering, | ||
SortOrder::Descending => ordering.reverse(), | ||
} | ||
} | ||
} | ||
|
||
impl Default for SortSpec { | ||
fn default() -> Self { | ||
Self { | ||
key: SortKey::Path, | ||
order: SortOrder::default(), | ||
} | ||
} | ||
} | ||
|
||
impl FromStr for SortSpec { | ||
type Err = strum::ParseError; | ||
|
||
fn from_str(text: &str) -> Result<Self, Self::Err> { | ||
if let Some(index) = text.find(':') { | ||
Ok(SortSpec { | ||
key: text[..index].parse()?, | ||
order: text[index + 1..].parse()?, | ||
}) | ||
} else { | ||
Ok(SortSpec { | ||
key: text.parse()?, | ||
order: SortOrder::default(), | ||
}) | ||
} | ||
} | ||
} | ||
|
||
impl Display for SortSpec { | ||
fn fmt(&self, f: &mut Formatter) -> fmt::Result { | ||
write!(f, "{}:{}", self.key.name(), self.order.name()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn default() { | ||
assert_eq!( | ||
SortSpec::default(), | ||
SortSpec { | ||
key: SortKey::Path, | ||
order: SortOrder::Ascending | ||
} | ||
); | ||
} | ||
|
||
#[test] | ||
fn parse() { | ||
assert_eq!( | ||
SortSpec { | ||
key: SortKey::Path, | ||
order: SortOrder::Ascending | ||
}, | ||
"path:ascending".parse().unwrap() | ||
); | ||
} | ||
} |
Oops, something went wrong.