Skip to content

Commit df98d85

Browse files
authored
Merge pull request #89 from rustcoreutils/hacking
PathBuf conversions
2 parents 08c2534 + dec7be1 commit df98d85

File tree

3 files changed

+31
-28
lines changed

3 files changed

+31
-28
lines changed

text/src/comm.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use gettextrs::{bind_textdomain_codeset, textdomain};
1515
use plib::PROJECT_NAME;
1616
use std::fs;
1717
use std::io::{self, BufRead, Write};
18+
use std::path::PathBuf;
1819

1920
const NO1: u32 = 1 << 0;
2021
const NO2: u32 = 1 << 1;
@@ -37,10 +38,10 @@ struct Args {
3738
no_dup: bool,
3839

3940
/// Comparison file1
40-
file1: String,
41+
file1: PathBuf,
4142

4243
/// Comparison file2
43-
file2: String,
44+
file2: PathBuf,
4445
}
4546

4647
fn line_out(lead_dup: &'static str, outmask: u32, curtype: u32, s: &str) -> io::Result<()> {
@@ -75,15 +76,15 @@ fn line_out(lead_dup: &'static str, outmask: u32, curtype: u32, s: &str) -> io::
7576
Ok(())
7677
}
7778

78-
fn open_file(filename: &str) -> io::Result<io::BufReader<fs::File>> {
79-
Ok(io::BufReader::new(fs::File::open(filename)?))
79+
fn open_file(pathname: &PathBuf) -> io::Result<io::BufReader<fs::File>> {
80+
Ok(io::BufReader::new(fs::File::open(pathname)?))
8081
}
8182

8283
fn comm_file(
8384
mask: u32,
8485
lead_dup: &'static str,
85-
file1name: &str,
86-
file2name: &str,
86+
file1name: &PathBuf,
87+
file2name: &PathBuf,
8788
) -> io::Result<()> {
8889
// open files, or stdin
8990
let mut rdr1 = open_file(file1name)?;

text/src/csplit.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use plib::PROJECT_NAME;
1919
use regex::Regex;
2020
use std::fs::{self, File, OpenOptions};
2121
use std::io::{self, BufRead, Error, ErrorKind, Read, Write};
22+
use std::path::PathBuf;
2223

2324
/// csplit - split files based on context
2425
#[derive(Parser, Debug)]
@@ -41,7 +42,7 @@ struct Args {
4142
suppress: bool,
4243

4344
/// File to read as input.
44-
filename: String,
45+
filename: PathBuf,
4546

4647
/// Operands defining context on which to split.
4748
operands: Vec<String>,
@@ -189,7 +190,7 @@ fn csplit_file(args: &Args, ctx: SplitOps, new_files: &mut Vec<String>) -> io::R
189190
let mut split_options = ctx.ops;
190191
// open file, or stdin
191192
let file: Box<dyn Read> = {
192-
if args.filename == "-" {
193+
if args.filename == PathBuf::from("-") {
193194
Box::new(io::stdin().lock())
194195
} else {
195196
Box::new(fs::File::open(&args.filename)?)
@@ -605,7 +606,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
605606
let mut new_files = vec![];
606607
if let Err(err) = csplit_file(&args, ctx, &mut new_files) {
607608
exit_code = 1;
608-
eprintln!("{}: {}", args.filename, err);
609+
eprintln!("{}: {}", args.filename.display(), err);
609610
if !args.keep {
610611
for file_name in new_files.iter() {
611612
fs::remove_file(file_name).unwrap();
@@ -803,7 +804,7 @@ mod tests {
803804
keep: false,
804805
num: 2,
805806
suppress: false,
806-
filename: String::from("test.txt"),
807+
filename: PathBuf::from("test.txt"),
807808
operands: vec![
808809
String::from("/pattern/+1"),
809810
String::from("%skip%10"),
@@ -850,7 +851,7 @@ mod tests {
850851
keep: false,
851852
num: 2,
852853
suppress: false,
853-
filename: String::from("tests/assets/test_file.txt"),
854+
filename: PathBuf::from("tests/assets/test_file.txt"),
854855
operands: vec![String::from("5"), String::from("{3}")],
855856
};
856857

@@ -900,7 +901,7 @@ mod tests {
900901
keep: false,
901902
num: 2,
902903
suppress: false,
903-
filename: String::from("tests/assets/test_file_c"),
904+
filename: PathBuf::from("tests/assets/test_file_c"),
904905
operands: vec![
905906
String::from(r"%main\(%"),
906907
String::from("/^}/+1"),
@@ -957,7 +958,7 @@ mod tests {
957958
keep: false,
958959
num: 2,
959960
suppress: false,
960-
filename: String::from("tests/assets/test_file_c"),
961+
filename: PathBuf::from("tests/assets/test_file_c"),
961962
operands: vec![
962963
String::from(r"%main\(%+1"),
963964
String::from("/^}/+1"),
@@ -1013,7 +1014,7 @@ mod tests {
10131014
keep: false,
10141015
num: 2,
10151016
suppress: false,
1016-
filename: String::from("tests/assets/test_file_c"),
1017+
filename: PathBuf::from("tests/assets/test_file_c"),
10171018
operands: vec![
10181019
String::from(r"%main\(%-1"),
10191020
String::from("/^}/+1"),
@@ -1070,7 +1071,7 @@ mod tests {
10701071
keep: false,
10711072
num: 2,
10721073
suppress: false,
1073-
filename: String::from("tests/assets/test_file_c"),
1074+
filename: PathBuf::from("tests/assets/test_file_c"),
10741075
operands: vec![
10751076
String::from(r"%main\(%"),
10761077
String::from("/^}/"),
@@ -1127,7 +1128,7 @@ mod tests {
11271128
keep: false,
11281129
num: 2,
11291130
suppress: false,
1130-
filename: String::from("tests/assets/test_file_c"),
1131+
filename: PathBuf::from("tests/assets/test_file_c"),
11311132
operands: vec![
11321133
String::from(r"%main\(%"),
11331134
String::from("/^}/-1"),

text/src/cut.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ extern crate clap;
1111
extern crate plib;
1212
use std::io::{self, BufRead, Error, ErrorKind, Read};
1313

14+
use clap::Parser;
1415
use gettextrs::{bind_textdomain_codeset, textdomain};
1516
use plib::PROJECT_NAME;
16-
17-
use clap::Parser;
17+
use std::path::PathBuf;
1818

1919
/// Cut - cut out selected fields of each line of a file
2020
#[derive(Parser, Debug, Clone)]
@@ -45,7 +45,7 @@ struct Args {
4545
no_split: bool,
4646

4747
/// Input files
48-
filenames: Vec<String>,
48+
filenames: Vec<PathBuf>,
4949
}
5050

5151
fn validate_args(args: &Args) -> Result<(), String> {
@@ -292,15 +292,16 @@ fn cut_files(args: Args) -> Result<(), Box<dyn std::error::Error>> {
292292

293293
// open files, or stdin
294294

295-
let readers: Vec<Box<dyn Read>> = if args.filenames.len() == 1 && args.filenames[0] == "-" {
296-
vec![Box::new(io::stdin().lock())]
297-
} else {
298-
let mut bufs: Vec<Box<dyn Read>> = vec![];
299-
for file in &args.filenames {
300-
bufs.push(Box::new(std::fs::File::open(file)?))
301-
}
302-
bufs
303-
};
295+
let readers: Vec<Box<dyn Read>> =
296+
if args.filenames.len() == 1 && args.filenames[0] == PathBuf::from("-") {
297+
vec![Box::new(io::stdin().lock())]
298+
} else {
299+
let mut bufs: Vec<Box<dyn Read>> = vec![];
300+
for file in &args.filenames {
301+
bufs.push(Box::new(std::fs::File::open(file)?))
302+
}
303+
bufs
304+
};
304305

305306
// Process each file
306307
for file in readers {

0 commit comments

Comments
 (0)