Skip to content

Commit

Permalink
Write to Write trait
Browse files Browse the repository at this point in the history
  • Loading branch information
theory committed Aug 1, 2024
1 parent 13651fe commit 76ebf91
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
use std::{error::Error, fs::File};
use std::{
error::Error,
fs::File,
io::{self, Write},
};

use pgxn_meta::Validator;
use serde_json::Value;

fn main() -> Result<(), Box<dyn Error>> {
let file = parse_args()?;
let mut stdout = io::stdout();
let file = parse_args(&mut stdout)?;
validate(&file)?;
Ok(())
}

fn parse_args() -> Result<String, lexopt::Error> {
fn parse_args(out: &mut impl Write) -> Result<String, Box<dyn Error>> {
use lexopt::prelude::*;
let mut file = String::from("META.json");
let mut parser = lexopt::Parser::from_env();

while let Some(arg) = parser.next()? {
match arg {
Short('h') | Long("help") => usage(),
Short('v') | Long("version") => version(),
Short('m') | Long("man") => docs(),
Short('h') | Long("help") => usage(out)?,
Short('v') | Long("version") => version(out)?,
Short('m') | Long("man") => docs(out)?,
Value(val) => file = val.string()?,
_ => return Err(arg.unexpected()),
_ => return Err(Box::new(arg.unexpected())),
}
}

Expand Down Expand Up @@ -48,20 +53,25 @@ fn bin_name() -> String {
String::from(env!("CARGO_BIN_NAME"))
}

fn usage() {
println!(
"Usage: {} [--help | h] [--version | -v] [<path>]",
fn usage(out: &mut impl Write) -> Result<(), Box<dyn Error>> {
writeln!(
out,
"Usage: {} [--help | h] [--version | -v] [<path>]\n\n\
Options:\n\
\x20 -h --help Print this usage statement and exit\n\
\x20 -v --version Print the version number and exit",
bin_name()
);
)?;

std::process::exit(0);
}

fn version() {
println!("{} {}", bin_name(), env!("CARGO_PKG_VERSION"));
fn version(out: &mut impl Write) -> Result<(), Box<dyn Error>> {
writeln!(out, "{} {}", bin_name(), env!("CARGO_PKG_VERSION"))?;
std::process::exit(0);
}

fn docs() {
println!("Docs");
fn docs(out: &mut impl Write) -> Result<(), Box<dyn Error>> {
writeln!(out, "Docs")?;
std::process::exit(0);
}

0 comments on commit 76ebf91

Please sign in to comment.