Skip to content

Commit c426e1a

Browse files
feat: support reading config from .protols.toml (#52)
1 parent a0f6e51 commit c426e1a

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

src/config/workspace.rs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::formatter::clang::ClangFormatter;
99

1010
use super::ProtolsConfig;
1111

12-
const CONFIG_FILE_NAME: &str = "protols.toml";
12+
const CONFIG_FILE_NAMES: [&str; 2] = [".protols.toml", "protols.toml"];
1313

1414
pub struct WorkspaceProtoConfigs {
1515
workspaces: HashSet<Url>,
@@ -26,13 +26,24 @@ impl WorkspaceProtoConfigs {
2626
}
2727
}
2828

29+
fn get_config_file_path(wpath: &PathBuf) -> Option<PathBuf> {
30+
for file in CONFIG_FILE_NAMES {
31+
let p = Path::new(&wpath).join(file);
32+
match std::fs::exists(&p) {
33+
Ok(exists) if exists => return Some(p),
34+
_ => continue,
35+
}
36+
}
37+
None
38+
}
39+
2940
pub fn add_workspace(&mut self, w: &WorkspaceFolder) {
3041
let Ok(wpath) = w.uri.to_file_path() else {
3142
return;
3243
};
3344

34-
let p = Path::new(&wpath).join(CONFIG_FILE_NAME);
35-
let content = std::fs::read_to_string(p).unwrap_or_default();
45+
let path = Self::get_config_file_path(&wpath).unwrap_or_default();
46+
let content = std::fs::read_to_string(path).unwrap_or_default();
3647

3748
let wr: ProtolsConfig = basic_toml::from_str(&content).unwrap_or_default();
3849
let fmt = ClangFormatter::new(
@@ -84,7 +95,7 @@ mod test {
8495
use insta::assert_yaml_snapshot;
8596
use tempfile::tempdir;
8697

87-
use super::WorkspaceProtoConfigs;
98+
use super::{WorkspaceProtoConfigs, CONFIG_FILE_NAMES};
8899

89100
#[test]
90101
fn test_get_for_workspace() {
@@ -153,4 +164,24 @@ mod test {
153164
"clang-format"
154165
);
155166
}
167+
168+
#[test]
169+
fn test_loading_different_config_files() {
170+
let tmpdir = tempdir().expect("failed to create temp directory");
171+
172+
for file in CONFIG_FILE_NAMES {
173+
let f = tmpdir.path().join(file);
174+
std::fs::write(f, include_str!("input/protols-valid.toml")).unwrap();
175+
176+
let mut ws = WorkspaceProtoConfigs::new();
177+
ws.add_workspace(&WorkspaceFolder {
178+
uri: Url::from_directory_path(tmpdir.path()).unwrap(),
179+
name: "Test".to_string(),
180+
});
181+
182+
// check we really loaded the config file
183+
let workspace = Url::from_file_path(tmpdir.path().join("foobar.proto")).unwrap();
184+
assert!(ws.get_workspace_for_uri(&workspace).is_some());
185+
}
186+
}
156187
}

0 commit comments

Comments
 (0)