Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into pr-refactor-logic
Browse files Browse the repository at this point in the history
  • Loading branch information
cdesaintguilhem committed Oct 15, 2024
2 parents 52fe1f7 + 3f658fd commit 0e4ec8c
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 55 deletions.
10 changes: 10 additions & 0 deletions notes.org
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
#+title: tex-fmt
* Release process
** Update version number in Cargo.toml
** Push to GitHub and check tests pass
** Create a git tag
*** git tag vX.X.X
*** git push --tags
** Publish to crates.io with cargo publish
** Publish GitHub release with notes
*** GitHub binaries published automatically with actions
** Publish in nixpkgs when bot makes pull request
50 changes: 1 addition & 49 deletions src/parse.rs → src/cli.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -112,48 +109,3 @@ impl Cli {
}
}
}

/// Add a missing extension and read the file
pub fn read(file: &str, logs: &mut Vec<Log>) -> 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 `<STDIN>` and text
pub fn read_stdin(logs: &mut Vec<Log>) -> Option<(String, String)> {
let mut text = String::new();
match std::io::stdin().read_to_string(&mut text) {
Ok(bytes) => {
record_file_log(
logs,
Trace,
"<STDIN>",
&format!("Read {bytes} bytes."),
);
Some((String::from("<STDIN>"), text))
}
Err(e) => {
record_file_log(
logs,
Error,
"<STDIN>",
&format!("Could not read from STDIN: {e}"),
);
None
}
}
}
2 changes: 1 addition & 1 deletion src/format.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! Core methodology for formatting a file
use crate::cli::*;
use crate::ignore::*;
use crate::indent::*;
use crate::logging::*;
use crate::parse::*;
use crate::regexes::{ENV_BEGIN, ENV_END, ITEM};
use crate::subs::*;
use crate::verbatim::*;
Expand Down
2 changes: 1 addition & 1 deletion src/indent.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! Utilities for indenting source lines
use crate::cli::*;
use crate::comments::*;
use crate::format::*;
use crate::logging::*;
use crate::parse::*;
use crate::regexes::*;
use core::cmp::max;
use log::Level::{Trace, Warn};
Expand Down
6 changes: 4 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,22 @@ use clap::Parser;
use std::fs;
use std::process::ExitCode;

mod cli;
mod comments;
mod format;
mod ignore;
mod indent;
mod logging;
mod parse;
mod read;
mod regexes;
mod subs;
mod verbatim;
mod wrap;
mod write;
use crate::cli::*;
use crate::format::*;
use crate::logging::*;
use crate::parse::*;
use crate::read::*;
use crate::write::*;

#[cfg(test)]
Expand Down
52 changes: 52 additions & 0 deletions src/read.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//! Utilities for reading files
use crate::logging::*;
use crate::regexes::*;
use log::Level::{Error, Trace};
use std::fs;
use std::io::Read;

/// Add a missing extension and read the file
pub fn read(file: &str, logs: &mut Vec<Log>) -> 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 `<STDIN>` and text
pub fn read_stdin(logs: &mut Vec<Log>) -> Option<(String, String)> {
let mut text = String::new();
match std::io::stdin().read_to_string(&mut text) {
Ok(bytes) => {
record_file_log(
logs,
Trace,
"<STDIN>",
&format!("Read {bytes} bytes."),
);
Some((String::from("<STDIN>"), text))
}
Err(e) => {
record_file_log(
logs,
Error,
"<STDIN>",
&format!("Could not read from STDIN: {e}"),
);
None
}
}
}
2 changes: 1 addition & 1 deletion src/wrap.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! Utilities for wrapping long lines
use crate::cli::*;
use crate::comments::*;
use crate::format::*;
use crate::logging::*;
use crate::parse::*;
use log::Level::{Trace, Warn};
use unicode_width::UnicodeWidthChar;

Expand Down
2 changes: 1 addition & 1 deletion src/write.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Utilities for writing formatted files
use crate::cli::*;
use crate::fs;
use crate::logging::*;
use crate::parse::*;
use log::Level::Error;
use std::path;

Expand Down

0 comments on commit 0e4ec8c

Please sign in to comment.