Skip to content

Commit

Permalink
Migrate to Clap 4
Browse files Browse the repository at this point in the history
  • Loading branch information
kelko committed Oct 5, 2022
1 parent f18e498 commit d5c22ba
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "html-streaming-editor"
version = "0.4.0"
version = "0.4.2"
edition = "2021"
authors = [":kelko: <kelko@me.com>"]
repository = "https://github.com/kelko/html-streaming-editor"
Expand All @@ -17,7 +17,7 @@ keywords = ["html"]
peg = "0.8.0"
tl = "0.7.6"
snafu = { version = "0.7", features = ["backtraces"] }
clap = "3.2"
clap = { version = "4.0.9", features = ["derive"] }
exitcode = "1.1.2"
log = "0.4"
pretty_env_logger = "0.4.0"
Expand Down
45 changes: 26 additions & 19 deletions src/bin/hse.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
extern crate clap;

use clap::Parser;
use std::fs::File;
use std::io::{BufRead, BufReader, BufWriter, Write};

use clap::clap_app;
use std::path::PathBuf;

use html_streaming_editor::{report, HtmlStreamingEditor};

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
/// File name of the Input. `-` for stdin (default)
#[arg(short, long, value_name = "INPUT")]
input: Option<PathBuf>,

/// File name of the Output. `-` for stdout (default)
#[arg(short, long, value_name = "OUTPUT")]
output: Option<PathBuf>,

/// Single string with the command pipeline to perform
pipeline: String,
}

fn main() {
pretty_env_logger::init();

let options = clap_app!(hse =>
(version: "0.4.0")
(author: ":kelko:")
(about: "Html Streaming Editor")
(@arg input: -i --input +takes_value "File name of the Input. `-` for stdin (default)")
(@arg output: -o --output +takes_value "File name of the Output. `-` for stdout (default)")
(@arg COMMANDS: +required "Single string with the command pipeline to perform")
)
.get_matches();

let input_path = options.value_of("input").unwrap_or("-").to_string();
let output_path = options.value_of("output").unwrap_or("-").to_string();
let commands = options.value_of("COMMANDS").expect("COMMANDS must be set");

let mut input_reader: Box<dyn BufRead> = if input_path == "-" {
let cli = Cli::parse();

let input_path = cli.input.unwrap_or(PathBuf::from("-"));
let output_path = cli.output.unwrap_or(PathBuf::from("-"));
let pipeline_definition = cli.pipeline;

let mut input_reader: Box<dyn BufRead> = if input_path.to_str() == Some("-") {
Box::new(std::io::stdin().lock())
} else {
let input_file = if let Ok(file) = File::open(input_path) {
Expand All @@ -37,7 +44,7 @@ fn main() {
Box::new(BufReader::new(input_file))
};

let mut output_writer: Box<dyn Write> = if output_path == "-" {
let mut output_writer: Box<dyn Write> = if output_path.to_str() == Some("-") {
Box::new(std::io::stdout().lock())
} else {
let output_file = if let Ok(file) = File::create(output_path) {
Expand All @@ -51,7 +58,7 @@ fn main() {
};

let editor = HtmlStreamingEditor::new(&mut input_reader, &mut output_writer);
match editor.run(commands) {
match editor.run(&pipeline_definition) {
Ok(_) => (),
Err(e) => report(&e),
}
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ impl<'a> HtmlStreamingEditor<'a> {
HtmlStreamingEditor { input, output }
}

pub fn run(self, commands: &str) -> Result<(), StreamingEditorError> {
let pipeline = parsing::grammar::pipeline(&commands).context(ParsingPipelineFailedSnafu)?;
pub fn run(self, pipeline_definition: &str) -> Result<(), StreamingEditorError> {
let pipeline =
parsing::grammar::pipeline(&pipeline_definition).context(ParsingPipelineFailedSnafu)?;
debug!("Parsed Pipeline: {:#?}", &pipeline);

let mut string_content = String::new();
Expand Down

0 comments on commit d5c22ba

Please sign in to comment.