diff --git a/src/main.rs b/src/main.rs index ea7c2ab..9af873f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 @@ -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}")