From 023671a278f738de33d16b2c138609417d5439d6 Mon Sep 17 00:00:00 2001 From: structix Date: Sun, 8 May 2022 19:19:02 +0200 Subject: [PATCH] Add basic functionality --- Cargo.toml | 1 + src/main.rs | 44 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c5e2de9..244f8c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +clap = {version = "3.1.17", features = ["derive"]} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 85aad77..7f49f19 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, }