Skip to content

Commit 7513699

Browse files
committed
asa, expand: PathBuf conversion
1 parent d845adf commit 7513699

File tree

2 files changed

+13
-24
lines changed

2 files changed

+13
-24
lines changed

text/src/asa.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ extern crate plib;
1717
use clap::Parser;
1818
use gettextrs::{bind_textdomain_codeset, gettext, textdomain};
1919
use plib::PROJECT_NAME;
20-
use std::fs;
21-
use std::io::{self, BufRead, Read};
20+
use std::io::{self, BufRead};
21+
use std::path::PathBuf;
2222

2323
/// asa - interpret carriage-control characters
2424
#[derive(Parser, Debug)]
2525
#[command(author, version, about, long_about)]
2626
struct Args {
2727
/// Files to read as input.
28-
files: Vec<String>,
28+
files: Vec<PathBuf>,
2929
}
3030

3131
struct AsaState {
@@ -69,14 +69,8 @@ impl AsaState {
6969
}
7070
}
7171

72-
fn asa_file(filename: &str) -> io::Result<()> {
73-
let file: Box<dyn Read>;
74-
if filename == "" {
75-
file = Box::new(io::stdin().lock());
76-
} else {
77-
file = Box::new(fs::File::open(filename)?);
78-
}
79-
let mut reader = io::BufReader::new(file);
72+
fn asa_file(pathname: &PathBuf) -> io::Result<()> {
73+
let mut reader = plib::io::input_reader(pathname, false)?;
8074
let mut line_no: usize = 0;
8175
let mut state = AsaState::new();
8276

@@ -142,15 +136,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
142136

143137
// if no files, read from stdin
144138
if args.files.is_empty() {
145-
args.files.push(String::new());
139+
args.files.push(PathBuf::new());
146140
}
147141

148142
let mut exit_code = 0;
149143

150144
for filename in &args.files {
151145
if let Err(e) = asa_file(filename) {
152146
exit_code = 1;
153-
eprintln!("{}: {}", filename, e);
147+
eprintln!("{}: {}", filename.display(), e);
154148
}
155149
}
156150

text/src/expand.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ extern crate plib;
1313
use clap::Parser;
1414
use gettextrs::{bind_textdomain_codeset, textdomain};
1515
use plib::PROJECT_NAME;
16-
use std::fs;
1716
use std::io::{self, BufWriter, Read, Write};
17+
use std::path::PathBuf;
1818

1919
/// expand - convert tabs to spaces
2020
#[derive(Parser, Debug)]
@@ -25,7 +25,7 @@ struct Args {
2525
tablist: Option<String>,
2626

2727
/// Files to read as input.
28-
files: Vec<String>,
28+
files: Vec<PathBuf>,
2929
}
3030

3131
enum TabList {
@@ -67,14 +67,9 @@ fn space_out(column: &mut usize, writer: &mut BufWriter<dyn Write>) -> io::Resul
6767
Ok(())
6868
}
6969

70-
fn expand_file(tablist: &TabList, filename: &str) -> io::Result<()> {
70+
fn expand_file(tablist: &TabList, pathname: &PathBuf) -> io::Result<()> {
7171
// open file, or stdin
72-
let mut file: Box<dyn Read>;
73-
if filename == "" {
74-
file = Box::new(io::stdin().lock());
75-
} else {
76-
file = Box::new(fs::File::open(filename)?);
77-
}
72+
let mut file = plib::io::input_stream(pathname, false)?;
7873

7974
let mut raw_buffer = [0; plib::BUFSZ];
8075
let mut writer = BufWriter::new(io::stdout());
@@ -160,15 +155,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
160155

161156
// if no files, read from stdin
162157
if args.files.is_empty() {
163-
args.files.push(String::new());
158+
args.files.push(PathBuf::new());
164159
}
165160

166161
let mut exit_code = 0;
167162

168163
for filename in &args.files {
169164
if let Err(e) = expand_file(&tablist, filename) {
170165
exit_code = 1;
171-
eprintln!("{}: {}", filename, e);
166+
eprintln!("{}: {}", filename.display(), e);
172167
}
173168
}
174169

0 commit comments

Comments
 (0)