Skip to content
This repository has been archived by the owner on Jun 8, 2024. It is now read-only.

Commit

Permalink
feat(config file): warn when multiple configs found
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew15-5 committed Oct 29, 2023
1 parent a4b82ee commit 449c484
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ use lexopt::prelude::*;
use typstfmt_lib::{format, Config};

const VERSION: &str = env!("TYPSTFMT_VERSION");
// Dot file is only used once when reading existing config file, therefore there
// is no need in creating a `const DOT_CONFIG_FILE_NAME`. And even if this
// constant was created, we would have to duplicate the whole string slice,
// because `format!(".{CONFIG_FILE_NAME}")` (non-const function) cannot be
// applied to `const` (or `static`) values in Rust (1.72.1).
// `DOT_CONFIG_FILE_NAME` is not created as a const due to the fact that we
// would have to duplicate the whole string slice, because
// `format!(".{CONFIG_FILE_NAME}")` (non-const function) cannot be applied to
// `const` (or `static`) values in Rust (1.72.1).
const CONFIG_FILE_NAME: &str = "typstfmt.toml";
const HELP: &str = r#"Format Typst code
Expand Down Expand Up @@ -197,9 +196,17 @@ fn main() -> Result<(), lexopt::Error> {

let config = {
let open_config = |file_name| File::options().read(true).open(file_name);
if let Ok(mut f) =
open_config(CONFIG_FILE_NAME).or(open_config(&format!(".{CONFIG_FILE_NAME}")))
{
let config = open_config(CONFIG_FILE_NAME);
let dot_config_file_name = format!(".{CONFIG_FILE_NAME}");
let dot_config = open_config(&dot_config_file_name);
if config.is_ok() && dot_config.is_ok() {
eprintln!(
"Warning! Both \"{first}\" and \"{second}\" are present. Using \"{first}\".",
first = CONFIG_FILE_NAME,
second = dot_config_file_name
);
}
if let Ok(mut f) = config.or(dot_config) {
let mut buf = String::default();
f.read_to_string(&mut buf).unwrap_or_else(|err| {
panic!("Failed to read config file {CONFIG_FILE_NAME:?}: {err}")
Expand Down

0 comments on commit 449c484

Please sign in to comment.