M src/lib.rs => src/lib.rs +15 -15
@@ 5,28 5,18 @@ pub mod bitstream;
use bitstream::{read_header, Header, Bitstream};
-fn format_header(header: &Header) -> String {
- format!("design = {}\npart = {}\ndate = {}\ntime = {}\nbitstream length = {}",
- header.design_name, header.part_name, header.design_date,
- header.design_time, header.bitstream_length)
-}
-
-fn convert_bitstream<R: Read, W: Write>(writer: &mut W, bitstream: &mut Bitstream<R>) -> io::Result<()> {
- bitstream.try_for_each(|w1| w1.and_then(|w2| writer.write_all(&w2.to_le_bytes())))
-}
-
pub fn run<R: BufRead>(input: &mut R, should_show_info: bool, should_convert: bool) -> io::Result<()> {
let header = read_header(input)?;
if should_show_info {
let message = format_header(&header);
- // If not converting we write the info to stdout, otherwise stderr to keep it
- // separate from the converted bitstream.
- if !should_convert {
- println!("{}", message);
- } else {
+ // If converting, write the info to stderr to keep it separate from the converted
+ // bitstream which is written to stdout.
+ if should_convert {
eprintln!("{}", message);
+ } else {
+ println!("{}", message);
}
}
@@ 38,3 28,13 @@ pub fn run<R: BufRead>(input: &mut R, should_show_info: bool, should_convert: bo
Ok(())
}
+
+fn format_header(header: &Header) -> String {
+ format!("design = {}\npart = {}\ndate = {}\ntime = {}\nbitstream length = {}",
+ header.design_name, header.part_name, header.design_date,
+ header.design_time, header.bitstream_length)
+}
+
+fn convert_bitstream<R: Read, W: Write>(writer: &mut W, bitstream: &mut Bitstream<R>) -> io::Result<()> {
+ bitstream.try_for_each(|w1| w1.and_then(|w2| writer.write_all(&w2.to_le_bytes())))
+}
M src/main.rs => src/main.rs +19 -12
@@ 7,6 7,24 @@ use std::process;
use clap::{App, Arg};
fn main() {
+ let (path, show_info, only_show_info) = parse_args();
+
+ let input: Box<dyn Read> = match path.as_str() {
+ "-" => Box::new(io::stdin()),
+ _ => Box::new(File::open(path).expect("error opening bitstream"))
+ };
+
+ let mut input = BufReader::new(input);
+ let should_show_info = show_info || only_show_info;
+ let should_convert = !only_show_info;
+
+ if let Err(e) = bsr::run(&mut input, should_show_info, should_convert) {
+ eprintln!("{}", e);
+ process::exit(1);
+ }
+}
+
+fn parse_args() -> (String, bool, bool) {
let matches = App::new("bsr")
.arg(Arg::with_name("show_info")
.short("i")
@@ 25,16 43,5 @@ fn main() {
let show_info = matches.is_present("show_info");
let only_show_info = matches.is_present("only_show_info");
- let input: Box<dyn Read> = match path {
- "-" => Box::new(io::stdin()),
- _ => Box::new(File::open(path).expect("error opening bitstream"))
- };
-
- let should_show_info = show_info || only_show_info;
- let should_convert = !only_show_info;
-
- if let Err(e) = bsr::run(&mut BufReader::new(input), should_show_info, should_convert) {
- eprintln!("{}", e);
- process::exit(1);
- }
+ (path.to_string(), show_info, only_show_info)
}