From c65b298e609b6f51a75c13a32ec6097fad96f842 Mon Sep 17 00:00:00 2001 From: Integral Date: Thu, 21 Nov 2024 00:54:16 +0800 Subject: [PATCH 1/2] fix: throw an error when try to view a directory --- CHANGELOG.md | 6 ++++++ src/main.rs | 9 ++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 79a1c50..2bf2ea9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# unreleased + +## Bugfixes + +- Throw an error when try to view a directory, see #234 (@Integral-Tech) + # v0.15.0 ## Features diff --git a/src/main.rs b/src/main.rs index a881d05..d760a95 100644 --- a/src/main.rs +++ b/src/main.rs @@ -234,7 +234,14 @@ fn run() -> Result<()> { let stdin = io::stdin(); let mut reader = match opt.file { - Some(filename) => Input::File(File::open(filename)?), + Some(filename) => { + let file = File::open(&filename)?; + if file.metadata()?.is_dir() { + return Err(anyhow!("{} is a directory.", filename.display())); + } + + Input::File(file) + } None => Input::Stdin(stdin.lock()), }; From 19198c2aaccaaecbce9882c10fcd6def600ca060 Mon Sep 17 00:00:00 2001 From: David Peter Date: Fri, 27 Dec 2024 14:23:02 +0100 Subject: [PATCH 2/2] Minor updates --- src/main.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index d760a95..4c257bb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ use std::path::PathBuf; use clap::builder::ArgPredicate; use clap::{ArgAction, Parser, ValueEnum}; -use anyhow::{anyhow, Context, Result}; +use anyhow::{anyhow, bail, Context, Result}; use const_format::formatcp; @@ -235,10 +235,10 @@ fn run() -> Result<()> { let mut reader = match opt.file { Some(filename) => { - let file = File::open(&filename)?; - if file.metadata()?.is_dir() { - return Err(anyhow!("{} is a directory.", filename.display())); + if filename.is_dir() { + bail!("'{}' is a directory.", filename.to_string_lossy()); } + let file = File::open(&filename)?; Input::File(file) }