Skip to content

Commit

Permalink
fix non-idiomatic rust, arg.len error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
iwyatt committed Nov 10, 2023
1 parent 57432eb commit 993829e
Showing 1 changed file with 28 additions and 30 deletions.
58 changes: 28 additions & 30 deletions taskchampion/xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@
use regex::Regex;
use std::env;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::io::{Seek, Write};
use std::io::{BufRead, BufReader, Seek, Write};
use std::path::{Path, PathBuf};

/// The const MSRV_PATHS_REGEX in /xtask/main.rs is an array of tuples
/// of the form (PATH, REGEX) where PATH and REGEX are literals where
/// PATH is a file that updates the Minimum Supported Rust Version
/// and REGEX is the pattern to find the appropriate line in the file

// Increment length of array when adding tuples of (PATH, REGEX).
const MSRV_PATH_REGEX: [(&str, &str); 1] = [(
"xtask/Cargo.toml",
r#"rust-version = "[0-9]+("|\.|[0-9])+""#,
)];
/// Tuples of the form (PATH, REGEX) where PATH and REGEX are literals where PATH is a file that
/// conains the Minimum Supported Rust Version and REGEX is the pattern to find the appropriate
/// line in the file. PATH is relative to the `taskchampion/` directory in the repo.
const MSRV_PATH_REGEX: &[(&str, &str)] = &[
(
"../.github/workflows/checks.yml",
r#"toolchain: "[0-9.]+*" # MSRV"#,
),
("../.github/workflows/rust-tests.yml", r#""[0-9.]+" # MSRV"#),
(
"taskchampion/src/lib.rs",
r#"Rust version [0-9.]* and higher"#,
),
];

pub fn main() -> anyhow::Result<()> {
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?);
Expand Down Expand Up @@ -54,9 +57,10 @@ fn codegen(workspace_dir: &Path) -> anyhow::Result<()> {
/// Each line where the regex matches will have all values of the form `#.##` replaced with the given MSRV.
fn msrv(args: Vec<String>, workspace_dir: &Path) -> anyhow::Result<()> {
// check that (X.Y) argument is (mostly) valid:
if !args[2].chars().all(|c| c.is_numeric() || c == '.') {
if args.len() < 3 || !args[2].chars().all(|c| c.is_numeric() || c == '.') {
anyhow::bail!("xtask: Invalid argument format. Xtask MSRV argument takes the form \"X.Y(y)\", where XYy are numbers. eg: `cargo run xtask MSRV 1.68`");
}
let version_replacement_string = &args[2];

// set regex for replacing version number only within the pattern found within a line
let re_msrv_version = Regex::new(r"([0-9]+(\.|[0-9]+|))+")?;
Expand All @@ -65,28 +69,27 @@ fn msrv(args: Vec<String>, workspace_dir: &Path) -> anyhow::Result<()> {
for msrv_file in MSRV_PATH_REGEX {
let mut is_pattern_in_file = false;

let file_path = workspace_dir.join(msrv_file.0);
let path: &Path = Path::new(&file_path);
if !&path.exists() {
let path = workspace_dir.join(msrv_file.0);
let path = Path::new(&path);
if !path.exists() {
anyhow::bail!("xtask: path does not exist {}", &path.display());
};

let mut file: File = File::options().read(true).write(true).open(path)?;
let mut file = File::options().read(true).write(true).open(path)?;
let reader = BufReader::new(&file);

// set search string and the replacement string for version number content
let re_msrv_pattern = Regex::new(msrv_file.1)?;
let version_replacement_string = &args[2];

// for each line in file
let mut file_string = String::new();
for line in reader.lines() {
let line_ref = &line?;
let line = &line?;

// if rust version pattern is found and is different, update it
if let Some(pattern_offset) = re_msrv_pattern.find(line_ref) {
if let Some(pattern_offset) = re_msrv_pattern.find(line) {
if !pattern_offset.as_str().contains(version_replacement_string) {
file_string += &re_msrv_version.replace(line_ref, version_replacement_string);
file_string += &re_msrv_version.replace(line, version_replacement_string);

file_string += "\n";

Expand All @@ -95,23 +98,18 @@ fn msrv(args: Vec<String>, workspace_dir: &Path) -> anyhow::Result<()> {
}
}

file_string += line_ref;
file_string += line;
file_string += "\n";
}

// if pattern was found and updated, write to disk
if is_pattern_in_file {
// Set the file length to the file_string length
let _ = file.set_len(file_string.len() as u64);
file.set_len(file_string.len() as u64)?;

// set the cursor to the beginning of the file and write
let _ = file.seek(std::io::SeekFrom::Start(0));
let file_write_result = file.write(file_string.as_bytes());

// if error, print error messege and exit
if file_write_result.is_err() {
anyhow::bail!("xtask: unable to write file to disk: {}", &path.display());
}
file.seek(std::io::SeekFrom::Start(0))?;
file.write_all(file_string.as_bytes())?;

// notify user this file was updated
println!(
Expand Down

0 comments on commit 993829e

Please sign in to comment.