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

Commit

Permalink
Refine the module split and Config struct definition
Browse files Browse the repository at this point in the history
The Config struct makes sense to remain in the library, and the command
line argument parsing should be a module within the binary crate.
To split binary modules into multiple files, you must put it in a
subdirectory. This setup should make the library more testable.

See:
- https://doc.rust-lang.org/cargo/guide/project-layout.html
- https://doc.rust-lang.org/stable/reference/visibility-and-privacy.html
  • Loading branch information
elasticdog committed Apr 26, 2020
1 parent 2beb0b1 commit c2e7e33
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 100 deletions.
85 changes: 85 additions & 0 deletions src/bin/taxonate/args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

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

pub fn parse() -> Result<Config, &'static str> {
let matches = App::new(crate_name!())
.version(crate_version!())
.author(crate_authors!())
.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 \
help text.",
)
.long_about("Identify and filter files based on their programming language.")
.arg(
Arg::with_name("color")
.help("Specifies when to use colored output")
.short("c")
.long("color")
.takes_value(true)
.value_name("WHEN")
.possible_values(&["auto", "always", "never"])
.env("TAXONATE_COLOR")
.default_value("auto"),
)
.arg(
Arg::with_name("debug")
.help("Adjusts the log level for debugging")
.short("d")
.long("debug")
.takes_value(true)
.value_name("LEVEL")
.possible_values(&["error", "warning", "info", "debug", "trace"])
.env("TAXONATE_DEBUG")
.default_value("error"),
)
.arg(
Arg::with_name("language")
.help("Outputs files identified as the given language")
.long_help(
"Filters output to only show files identified as the given \
programming language",
)
.short("l")
.long("language")
.takes_value(true)
.value_name("LANGUAGE")
.env("TAXONATE_LANGUAGE"),
)
.arg(
Arg::with_name("list")
.help("Lists supported programming languages")
.long_help(
"Displays a list of supported programming languages for \
filtering output",
)
.short("L")
.long("list-languages"),
)
.arg(
Arg::with_name("PATH")
.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.",
)
.multiple(true),
)
.get_matches();

// unwrap is safe when an arg is required or we specify a default
let color = matches.value_of("color").unwrap().to_string();
let debug = matches.value_of("debug").unwrap().to_string();
let language = matches.value_of("language").map(String::from);
let list = matches.is_present("list");

Ok(Config {
color,
debug,
language,
list,
})
}
4 changes: 2 additions & 2 deletions src/bin/taxonate.rs → src/bin/taxonate/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

use std::process;

use taxonate::config::Config;
mod args;

fn main() {
let config = match Config::new() {
let config = match args::parse() {
Ok(cfg) => cfg,
Err(e) => {
eprintln!("Config error: {}", e);
Expand Down
94 changes: 0 additions & 94 deletions src/config.rs

This file was deleted.

14 changes: 10 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

use std::error::Error;
use std::io::{self, Write};
use std::{
error::Error,
io::{self, Write},
};

use crate::config::Config;
use bstr::io::BufReadExt;
use log::debug;

pub mod config;
pub struct Config {
pub color: String,
pub debug: String,
pub language: Option<String>,
pub list: bool,
}

pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
debug!("'color' WHEN: {:?}", &config.color);
Expand Down

0 comments on commit c2e7e33

Please sign in to comment.