Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Commit

Permalink
Implement collecting paths into a HashSet
Browse files Browse the repository at this point in the history
This properly implements the behavior I was expecting where:

- the list of paths will not contain duplicate entries

- it will append entries from STDIN if explicitly requested (I couldn't
  figure out the implicit logic to work in an intuitive way)

- it will default to just the current working directory if no other
  paths have been specified

I believe that paths are not guaranteed to be valid UTF-8, so the proper
way to handle that is to use OsString values instead of String values.
Note that I've also dropped the external bstr crate dependency, since
I'm not 100% confident on how to convert between the two, or if it will
even be beneficial/necessary. I can revist that in the future once the
real parsing functionality is implemented.

See:
- clap-rs/clap#751
  • Loading branch information
elasticdog committed Apr 27, 2020
1 parent c2e7e33 commit 20110fe
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 48 deletions.
27 changes: 0 additions & 27 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Expand Up @@ -9,7 +9,6 @@ description = "Identify and filter files based on their programming language."
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bstr = "0.2.12"
env_logger = "0.7.1"
log = "0.4.8"

Expand Down
33 changes: 30 additions & 3 deletions src/bin/taxonate/args.rs
@@ -1,12 +1,19 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

use clap::{crate_authors, crate_name, crate_version, App, Arg};
use std::{
collections::HashSet,
ffi::{OsStr, OsString},
io::{self, BufRead},
};

use clap::{crate_authors, crate_name, crate_version, App, AppSettings, Arg};
use taxonate::Config;

pub fn parse() -> Result<Config, &'static str> {
let matches = App::new(crate_name!())
.version(crate_version!())
.author(crate_authors!())
.setting(AppSettings::AllowInvalidUtf8)
.about(
"Identify and filter files based on their programming language.\n\n\
Use '--help' instead of '-h' to see a more detailed version of the \
Expand Down Expand Up @@ -63,8 +70,8 @@ pub fn parse() -> Result<Config, &'static str> {
.help("File or directory to identify. Use '-' for standard input.")
.long_help(
"A file or directory to identify. Directories will have \
all files identified recursively. Use a dash ('-') or no \
argument at all to read from standard input.",
all files identified recursively. Use a dash ('-') to \
read from standard input.",
)
.multiple(true),
)
Expand All @@ -76,10 +83,30 @@ pub fn parse() -> Result<Config, &'static str> {
let language = matches.value_of("language").map(String::from);
let list = matches.is_present("list");

let mut paths: HashSet<OsString> = matches
.values_of_os("PATH")
.unwrap_or_default()
.map(OsString::from)
.collect();

// include paths from STDIN, if explicitly requested
if paths.remove(OsStr::new("-")) {
let stdin = io::stdin();
for line in stdin.lock().lines() {
paths.insert(OsString::from(line.unwrap()));
}
}

if paths.is_empty() {
// default to the current working directory
paths.insert(OsString::from("."));
}

Ok(Config {
color,
debug,
language,
list,
paths,
})
}
20 changes: 3 additions & 17 deletions src/lib.rs
@@ -1,18 +1,15 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

use std::{
error::Error,
io::{self, Write},
};
use std::{collections::HashSet, error::Error, ffi::OsString};

use bstr::io::BufReadExt;
use log::debug;

pub struct Config {
pub color: String,
pub debug: String,
pub language: Option<String>,
pub list: bool,
pub paths: HashSet<OsString>,
}

pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
Expand All @@ -23,18 +20,7 @@ pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
&config.language.unwrap_or_default()
);
debug!("'list': {:?}", &config.list);
// debug!(
// "'PATH' values: {:?}",
// matches
// .values_of("PATH")
// .map(|vals| vals.collect::<Vec<_>>())
debug!("'paths': {:?}", &config.paths);

let stdin = io::stdin();
let mut stdout = io::BufWriter::new(io::stdout());

stdin.lock().for_byte_line_with_terminator(|line| {
stdout.write_all(line)?;
Ok(true)
})?;
Ok(())
}

0 comments on commit 20110fe

Please sign in to comment.