mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
pr: remove parameter header and get_input_type
pr: Remove parameter header from build_options pr: Remove unnecessary get_input_type
This commit is contained in:
parent
629236bd39
commit
64e2e1dbac
2 changed files with 41 additions and 67 deletions
106
src/pr/pr.rs
106
src/pr/pr.rs
|
@ -112,21 +112,6 @@ impl Default for NumberingMode {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum InputType {
|
|
||||||
Directory,
|
|
||||||
File,
|
|
||||||
StdIn,
|
|
||||||
SymLink,
|
|
||||||
#[cfg(unix)]
|
|
||||||
BlockDevice,
|
|
||||||
#[cfg(unix)]
|
|
||||||
CharacterDevice,
|
|
||||||
#[cfg(unix)]
|
|
||||||
Fifo,
|
|
||||||
#[cfg(unix)]
|
|
||||||
Socket,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Error> for PrError {
|
impl From<Error> for PrError {
|
||||||
fn from(err: Error) -> Self {
|
fn from(err: Error) -> Self {
|
||||||
PrError::EncounteredErrors(err.to_string())
|
PrError::EncounteredErrors(err.to_string())
|
||||||
|
@ -163,6 +148,10 @@ quick_error! {
|
||||||
IsSocket(path: String) {
|
IsSocket(path: String) {
|
||||||
display("pr: cannot open {}, Operation not supported on socket", path)
|
display("pr: cannot open {}, Operation not supported on socket", path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
NotExists(path: String) {
|
||||||
|
display("pr: cannot open {}, No such file or directory", path)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -291,8 +280,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
}
|
}
|
||||||
|
|
||||||
for f in files {
|
for f in files {
|
||||||
let header: &String = &matches.opt_str(STRING_HEADER_OPTION).unwrap_or(f.to_string());
|
let result_options = build_options(&matches, &f);
|
||||||
let result_options = build_options(&matches, header, &f);
|
|
||||||
if result_options.is_err() {
|
if result_options.is_err() {
|
||||||
writeln!(&mut stderr(), "{}", result_options.err().unwrap());
|
writeln!(&mut stderr(), "{}", result_options.err().unwrap());
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -349,7 +337,8 @@ fn print_usage(opts: &mut Options, matches: &Matches) -> i32 {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_options(matches: &Matches, header: &String, path: &String) -> Result<OutputOptions, PrError> {
|
fn build_options(matches: &Matches, path: &String) -> Result<OutputOptions, PrError> {
|
||||||
|
let header: String = matches.opt_str(STRING_HEADER_OPTION).unwrap_or(path.to_string());
|
||||||
let numbering_options: Option<NumberingMode> = matches.opt_str(NUMBERING_MODE_OPTION).map(|i| {
|
let numbering_options: Option<NumberingMode> = matches.opt_str(NUMBERING_MODE_OPTION).map(|i| {
|
||||||
NumberingMode {
|
NumberingMode {
|
||||||
width: i.parse::<usize>().unwrap_or(NumberingMode::default().width),
|
width: i.parse::<usize>().unwrap_or(NumberingMode::default().width),
|
||||||
|
@ -439,7 +428,7 @@ fn build_options(matches: &Matches, header: &String, path: &String) -> Result<Ou
|
||||||
|
|
||||||
Ok(OutputOptions {
|
Ok(OutputOptions {
|
||||||
number: numbering_options,
|
number: numbering_options,
|
||||||
header: header.to_string(),
|
header,
|
||||||
double_space,
|
double_space,
|
||||||
line_separator,
|
line_separator,
|
||||||
last_modified_time,
|
last_modified_time,
|
||||||
|
@ -455,19 +444,39 @@ fn build_options(matches: &Matches, header: &String, path: &String) -> Result<Ou
|
||||||
}
|
}
|
||||||
|
|
||||||
fn open(path: &str) -> Result<Box<Read>, PrError> {
|
fn open(path: &str) -> Result<Box<Read>, PrError> {
|
||||||
match get_input_type(path) {
|
if path == FILE_STDIN {
|
||||||
Some(InputType::Directory) => Err(PrError::IsDirectory(path.to_string())),
|
let stdin = stdin();
|
||||||
#[cfg(unix)]
|
return Ok(Box::new(stdin) as Box<Read>);
|
||||||
Some(InputType::Socket) => {
|
|
||||||
Err(PrError::IsSocket(path.to_string()))
|
|
||||||
}
|
|
||||||
Some(InputType::StdIn) => {
|
|
||||||
let stdin = stdin();
|
|
||||||
Ok(Box::new(stdin) as Box<Read>)
|
|
||||||
}
|
|
||||||
Some(_) => Ok(Box::new(File::open(path).context(path)?) as Box<Read>),
|
|
||||||
None => Err(PrError::UnknownFiletype(path.to_string()))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
metadata(path).map(|i| {
|
||||||
|
let path_string = path.to_string();
|
||||||
|
match i.file_type() {
|
||||||
|
#[cfg(unix)]
|
||||||
|
ft if ft.is_block_device() =>
|
||||||
|
{
|
||||||
|
Err(PrError::UnknownFiletype(path_string))
|
||||||
|
}
|
||||||
|
#[cfg(unix)]
|
||||||
|
ft if ft.is_char_device() =>
|
||||||
|
{
|
||||||
|
Err(PrError::UnknownFiletype(path_string))
|
||||||
|
}
|
||||||
|
#[cfg(unix)]
|
||||||
|
ft if ft.is_fifo() =>
|
||||||
|
{
|
||||||
|
Err(PrError::UnknownFiletype(path_string))
|
||||||
|
}
|
||||||
|
#[cfg(unix)]
|
||||||
|
ft if ft.is_socket() =>
|
||||||
|
{
|
||||||
|
Err(PrError::IsSocket(path_string))
|
||||||
|
}
|
||||||
|
ft if ft.is_dir() => Err(PrError::IsDirectory(path_string)),
|
||||||
|
ft if ft.is_file() || ft.is_symlink() => Ok(Box::new(File::open(path).context(path)?) as Box<Read>),
|
||||||
|
_ => Err(PrError::UnknownFiletype(path_string))
|
||||||
|
}
|
||||||
|
}).unwrap_or(Err(PrError::NotExists(path.to_string())))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pr(path: &str, options: &OutputOptions) -> Result<i32, PrError> {
|
fn pr(path: &str, options: &OutputOptions) -> Result<i32, PrError> {
|
||||||
|
@ -654,38 +663,3 @@ fn trailer_content(options: &OutputOptions) -> Vec<String> {
|
||||||
Vec::new()
|
Vec::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_input_type(path: &str) -> Option<InputType> {
|
|
||||||
if path == FILE_STDIN {
|
|
||||||
return Some(InputType::StdIn);
|
|
||||||
}
|
|
||||||
|
|
||||||
metadata(path).map(|i| {
|
|
||||||
match i.file_type() {
|
|
||||||
#[cfg(unix)]
|
|
||||||
ft if ft.is_block_device() =>
|
|
||||||
{
|
|
||||||
Some(InputType::BlockDevice)
|
|
||||||
}
|
|
||||||
#[cfg(unix)]
|
|
||||||
ft if ft.is_char_device() =>
|
|
||||||
{
|
|
||||||
Some(InputType::CharacterDevice)
|
|
||||||
}
|
|
||||||
#[cfg(unix)]
|
|
||||||
ft if ft.is_fifo() =>
|
|
||||||
{
|
|
||||||
Some(InputType::Fifo)
|
|
||||||
}
|
|
||||||
#[cfg(unix)]
|
|
||||||
ft if ft.is_socket() =>
|
|
||||||
{
|
|
||||||
Some(InputType::Socket)
|
|
||||||
}
|
|
||||||
ft if ft.is_dir() => Some(InputType::Directory),
|
|
||||||
ft if ft.is_file() => Some(InputType::File),
|
|
||||||
ft if ft.is_symlink() => Some(InputType::SymLink),
|
|
||||||
_ => None
|
|
||||||
}
|
|
||||||
}).unwrap_or(None)
|
|
||||||
}
|
|
||||||
|
|
|
@ -18,7 +18,7 @@ fn file_last_modified_time(ucmd: &UCommand, path: &str) -> String {
|
||||||
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_output_multi_files_print_all_chars() {
|
fn test_without_any_options() {
|
||||||
let test_file_path = "test_one_page.log";
|
let test_file_path = "test_one_page.log";
|
||||||
let mut scenario = new_ucmd!();
|
let mut scenario = new_ucmd!();
|
||||||
let value = file_last_modified_time(&scenario, test_file_path);
|
let value = file_last_modified_time(&scenario, test_file_path);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue