diff --git a/src/parse.rs b/src/cli.rs similarity index 67% rename from src/parse.rs rename to src/cli.rs index fe35509..252de45 100644 --- a/src/parse.rs +++ b/src/cli.rs @@ -1,12 +1,9 @@ //! Utilities for reading the command line arguments use crate::logging::*; -use crate::regexes::*; use clap::Parser; -use log::Level::{Error, Trace}; +use log::Level::Error; use log::LevelFilter; -use std::fs; -use std::io::Read; /// Command line arguments #[allow(missing_docs)] @@ -112,48 +109,3 @@ impl Cli { } } } - -/// Add a missing extension and read the file -pub fn read(file: &str, logs: &mut Vec) -> Option<(String, String)> { - // check if file has an accepted extension - let has_ext = EXTENSIONS.iter().any(|e| file.ends_with(e)); - // if no valid extension, try adding .tex - let mut new_file = file.to_owned(); - if !has_ext { - new_file.push_str(".tex"); - }; - if let Ok(text) = fs::read_to_string(&new_file) { - return Some((new_file, text)); - } - if has_ext { - record_file_log(logs, Error, file, "Could not open file."); - } else { - record_file_log(logs, Error, file, "File type invalid."); - } - None -} - -/// Attempt to read from STDIN, return filename `` and text -pub fn read_stdin(logs: &mut Vec) -> Option<(String, String)> { - let mut text = String::new(); - match std::io::stdin().read_to_string(&mut text) { - Ok(bytes) => { - record_file_log( - logs, - Trace, - "", - &format!("Read {bytes} bytes."), - ); - Some((String::from(""), text)) - } - Err(e) => { - record_file_log( - logs, - Error, - "", - &format!("Could not read from STDIN: {e}"), - ); - None - } - } -} diff --git a/src/format.rs b/src/format.rs index 5281705..e797bf4 100644 --- a/src/format.rs +++ b/src/format.rs @@ -3,7 +3,7 @@ use crate::ignore::*; use crate::indent::*; use crate::logging::*; -use crate::parse::*; +use crate::cli::*; use crate::regexes::{ENV_BEGIN, ENV_END, ITEM}; use crate::subs::*; use crate::verbatim::*; diff --git a/src/indent.rs b/src/indent.rs index 01294bb..e3b04d3 100644 --- a/src/indent.rs +++ b/src/indent.rs @@ -4,7 +4,7 @@ use crate::comments::*; use crate::format::*; use crate::ignore::*; use crate::logging::*; -use crate::parse::*; +use crate::cli::*; use crate::regexes::*; use crate::verbatim::*; use core::cmp::max; diff --git a/src/main.rs b/src/main.rs index a9d7dfd..3620c6b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,8 @@ mod format; mod ignore; mod indent; mod logging; -mod parse; +mod cli; +mod read; mod regexes; mod subs; mod verbatim; @@ -28,7 +29,8 @@ mod wrap; mod write; use crate::format::*; use crate::logging::*; -use crate::parse::*; +use crate::cli::*; +use crate::read::*; use crate::write::*; #[cfg(test)] diff --git a/src/read.rs b/src/read.rs new file mode 100644 index 0000000..79e1da2 --- /dev/null +++ b/src/read.rs @@ -0,0 +1,50 @@ +use crate::logging::*; +use crate::regexes::*; +use log::Level::{Error, Trace}; +use std::io::Read; +use std::fs; + +/// Add a missing extension and read the file +pub fn read(file: &str, logs: &mut Vec) -> Option<(String, String)> { + // check if file has an accepted extension + let has_ext = EXTENSIONS.iter().any(|e| file.ends_with(e)); + // if no valid extension, try adding .tex + let mut new_file = file.to_owned(); + if !has_ext { + new_file.push_str(".tex"); + }; + if let Ok(text) = fs::read_to_string(&new_file) { + return Some((new_file, text)); + } + if has_ext { + record_file_log(logs, Error, file, "Could not open file."); + } else { + record_file_log(logs, Error, file, "File type invalid."); + } + None +} + +/// Attempt to read from STDIN, return filename `` and text +pub fn read_stdin(logs: &mut Vec) -> Option<(String, String)> { + let mut text = String::new(); + match std::io::stdin().read_to_string(&mut text) { + Ok(bytes) => { + record_file_log( + logs, + Trace, + "", + &format!("Read {bytes} bytes."), + ); + Some((String::from(""), text)) + } + Err(e) => { + record_file_log( + logs, + Error, + "", + &format!("Could not read from STDIN: {e}"), + ); + None + } + } +} diff --git a/src/wrap.rs b/src/wrap.rs index 81e930f..00de13a 100644 --- a/src/wrap.rs +++ b/src/wrap.rs @@ -3,7 +3,7 @@ use crate::comments::*; use crate::format::*; use crate::logging::*; -use crate::parse::*; +use crate::cli::*; use log::Level::{Trace, Warn}; /// Check if a line needs wrapping diff --git a/src/write.rs b/src/write.rs index be522f8..4ccce81 100644 --- a/src/write.rs +++ b/src/write.rs @@ -2,7 +2,7 @@ use crate::fs; use crate::logging::*; -use crate::parse::*; +use crate::cli::*; use log::Level::Error; use std::path;