Add initial code

This commit is contained in:
Austen Adler 2022-01-22 16:29:02 -05:00
commit 211c7b7e26
4 changed files with 88 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

42
Cargo.lock generated Normal file
View File

@ -0,0 +1,42 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aho-corasick"
version = "0.7.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
dependencies = [
"memchr",
]
[[package]]
name = "memchr"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a"
[[package]]
name = "regex"
version = "1.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
[[package]]
name = "rust-selection-sort"
version = "0.1.0"
dependencies = [
"regex",
]

9
Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "rust-selection-sort"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
regex = "1"

36
src/main.rs Normal file
View File

@ -0,0 +1,36 @@
#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
use regex::Regex;
use std::env;
fn main() {
let args = env::args().collect::<Vec<String>>();
assert!(args.len() < 2, "Usage: rust-selection-sort REGEX SEL1 [SEL2 ...]");
let replacement_re = &args[1];
let re = Regex::new(replacement_re).unwrap_or_else(|_| panic!(
"Invalid regular expression: {}",
replacement_re
));
let mut zipped = args
.iter()
.skip(2)
.zip(args.iter().skip(2).map(|a| {
let captures = re.captures(a)?;
captures.get(1).or_else(|| captures.get(0)).map(|m| m.as_str())
}))
.collect::<Vec<(&String, Option<&str>)>>();
zipped.sort_by(|(a, a_key), (b, b_key)| {
let a = a_key.unwrap_or(a);
let b = b_key.unwrap_or(b);
a.cmp(b)
});
for i in &zipped {
print!("{}\0", i.0);
// TODO: Allow debugging with -d
// println!("\n\tSort key: {:?}", i.1);
}
}