1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 20:17:45 +00:00

tsort: use std::io::read_to_string to load data

This commit is contained in:
Jeffrey Finkelstein 2025-01-04 22:51:08 -05:00
parent c9ae4819f5
commit 5cf5acb4a2

View file

@ -5,9 +5,6 @@
//spell-checker:ignore TAOCP //spell-checker:ignore TAOCP
use clap::{crate_version, Arg, Command}; use clap::{crate_version, Arg, Command};
use std::collections::{HashMap, HashSet, VecDeque}; use std::collections::{HashMap, HashSet, VecDeque};
use std::fmt::Write;
use std::fs::File;
use std::io::{stdin, BufReader, Read};
use std::fmt::Display; use std::fmt::Display;
use std::path::Path; use std::path::Path;
use uucore::display::Quotable; use uucore::display::Quotable;
@ -66,25 +63,20 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
.get_one::<String>(options::FILE) .get_one::<String>(options::FILE)
.expect("Value is required by clap"); .expect("Value is required by clap");
let mut stdin_buf; let data = if input == "-" {
let mut file_buf; let stdin = std::io::stdin();
let mut reader = BufReader::new(if input == "-" { std::io::read_to_string(stdin)?
stdin_buf = stdin();
&mut stdin_buf as &mut dyn Read
} else { } else {
let path = Path::new(&input); let path = Path::new(&input);
if path.is_dir() { if path.is_dir() {
return Err(TsortError::IsDir(input.to_string()).into()); return Err(TsortError::IsDir(input.to_string()).into());
} }
file_buf = File::open(path).map_err_context(|| input.to_string())?; std::fs::read_to_string(path)?
&mut file_buf as &mut dyn Read };
});
let mut input_buffer = String::new();
reader.read_to_string(&mut input_buffer)?;
let mut g = Graph::default(); let mut g = Graph::default();
for line in input_buffer.lines() { for line in data.lines() {
let tokens: Vec<_> = line.split_whitespace().collect(); let tokens: Vec<_> = line.split_whitespace().collect();
if tokens.is_empty() { if tokens.is_empty() {
break; break;