From 7f56c4c530663d20726fcfb40191cee0db0874f2 Mon Sep 17 00:00:00 2001 From: adrian-kong Date: Tue, 10 May 2022 12:54:45 +1000 Subject: [PATCH] Added input and output parameters to rust sbp2json --- rust/sbp2json/src/bin/json2json.rs | 22 ++++++++++++++++++++-- rust/sbp2json/src/bin/json2sbp.rs | 22 ++++++++++++++++++++-- rust/sbp2json/src/bin/sbp2json.rs | 22 ++++++++++++++++++++-- 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/rust/sbp2json/src/bin/json2json.rs b/rust/sbp2json/src/bin/json2json.rs index 5b6657257f..70f10edadf 100644 --- a/rust/sbp2json/src/bin/json2json.rs +++ b/rust/sbp2json/src/bin/json2json.rs @@ -1,4 +1,7 @@ +use std::fs::File; use std::io; +use std::io::{Read, Write}; +use std::path::PathBuf; use clap::Parser; use sbp::json::{CompactFormatter, HaskellishFloatFormatter}; @@ -12,10 +15,18 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; /// /// Typical usage: /// +/// json2json --input [PATH] --output [PATH] +/// /// cat console-json-log.json | json2json #[derive(Debug, Parser)] #[clap(name = "json2json", verbatim_doc_comment)] struct Options { + /// Path to input file + input: Option, + + /// Path to output file + output: Option, + /// Print debugging messages to standard error #[clap(long)] debug: bool, @@ -42,8 +53,15 @@ fn main() -> Result<()> { env_logger::init(); - let stdin = io::stdin(); - let stdout = io::stdout(); + let stdin: Box = match options.input { + Some(path) => Box::new(File::open(path)?), + _ => Box::new(io::stdin()), + }; + + let stdout: Box = match options.output { + Some(path) => Box::new(File::create(path)?), + _ => Box::new(io::stdout()), + }; if options.float_compat { json2json( diff --git a/rust/sbp2json/src/bin/json2sbp.rs b/rust/sbp2json/src/bin/json2sbp.rs index a1d08d8ad3..614651ac02 100644 --- a/rust/sbp2json/src/bin/json2sbp.rs +++ b/rust/sbp2json/src/bin/json2sbp.rs @@ -1,4 +1,7 @@ +use std::fs::File; use std::io; +use std::io::{Read, Write}; +use std::path::PathBuf; use clap::Parser; @@ -11,10 +14,18 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; /// /// Typical usage: /// +/// json2sbp --input [PATH] --output [PATH] +/// /// cat sbp.json | json2sbp #[derive(Debug, Parser)] #[clap(name = "json2sbp", verbatim_doc_comment)] struct Options { + /// Path to input file + input: Option, + + /// Path to output file + output: Option, + /// Print debugging messages to standard error #[clap(long)] debug: bool, @@ -37,8 +48,15 @@ fn main() -> Result<()> { env_logger::init(); - let stdin = io::stdin(); - let stdout = io::stdout(); + let stdin: Box = match options.input { + Some(path) => Box::new(File::open(path)?), + _ => Box::new(io::stdin()), + }; + + let stdout: Box = match options.output { + Some(path) => Box::new(File::create(path)?), + _ => Box::new(io::stdout()), + }; json2sbp(stdin, stdout, options.buffered, options.fatal_errors) } diff --git a/rust/sbp2json/src/bin/sbp2json.rs b/rust/sbp2json/src/bin/sbp2json.rs index fcba110158..27a6651d7f 100644 --- a/rust/sbp2json/src/bin/sbp2json.rs +++ b/rust/sbp2json/src/bin/sbp2json.rs @@ -1,4 +1,7 @@ +use std::fs::File; use std::io; +use std::io::{Read, Write}; +use std::path::PathBuf; use clap::Parser; use sbp::json::{CompactFormatter, HaskellishFloatFormatter}; @@ -12,6 +15,8 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; /// /// Typical usage: /// +/// sbp2json --input [PATH] --output [PATH] +/// /// cat sbp.dat | sbp2json /// /// Or combined with socat: @@ -20,6 +25,12 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; #[derive(Debug, Parser)] #[structopt(name = "sbp2json", verbatim_doc_comment)] pub struct Options { + /// Path to input file + input: Option, + + /// Path to output file + output: Option, + /// Try to be compatible with the float formatting of the Haskell version of sbp2json #[clap(long)] float_compat: bool, @@ -46,8 +57,15 @@ fn main() -> Result<()> { env_logger::init(); - let stdin = io::stdin(); - let stdout = io::stdout(); + let stdin: Box = match options.input { + Some(path) => Box::new(File::open(path)?), + _ => Box::new(io::stdin()), + }; + + let stdout: Box = match options.output { + Some(path) => Box::new(File::create(path)?), + _ => Box::new(io::stdout()), + }; if options.float_compat { sbp2json(