Add basic functionality

This commit is contained in:
2022-05-08 19:19:02 +02:00
parent 3f2349f0d0
commit 023671a278
2 changed files with 43 additions and 2 deletions

View File

@@ -1,5 +1,45 @@
fn main() {
println!("Hello, world!");
use std::fs::{File, read_to_string};
use std::io::{BufWriter, Error, Write};
use clap::Parser;
fn main() -> Result<(), Error> {
let args = Args::parse();
let wordlist = read_to_string(args.filepath)?;
let mut list: Vec<&str> = wordlist.split_ascii_whitespace().collect();
let lowerbound = list.iter().position(|&x| x.contains("\\begin{acronym}"));
let upperbound = list.iter().position(|&x| x.contains("\\end{acronym}"));
match (lowerbound, upperbound) {
(Some(lower), Some(upper)) => {
println!("Sort range {}-{}", lower, upper);
sort_range(lower + 1, upper, &mut list)
},
_ => {
println!("No bounds found.");
()
}
}
let mut writer = BufWriter::new(File::create(args.filepath)?);
writer.write(list.join("\n").as_bytes())?;
println!("Done.");
Ok(())
}
fn sort_range(lowerbound: usize, upperbound: usize, list: &mut Vec<&str>) {
list[lowerbound..upperbound].sort_unstable();
}
/// Sorts the acronym block of a .tex file
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Args {
/// The filepath to the .tex file
#[clap(short, long)]
filepath: String,
}