editor/
editor_settings.rs1use std::fs;
2use std::path::PathBuf;
3
4const EDITOR_SETTINGS_FILE: &str = "editor_settings.ron";
5
6#[derive(Default, serde::Serialize, serde::Deserialize)]
11pub struct EditorSettings {
12 pub preferred_editor: Option<PathBuf>,
13}
14
15impl EditorSettings {
16 fn file_path() -> PathBuf {
17 std::env::current_exe()
18 .ok()
19 .and_then(|exe| exe.parent().map(|dir| dir.join(EDITOR_SETTINGS_FILE)))
20 .unwrap_or_else(|| PathBuf::from(EDITOR_SETTINGS_FILE))
21 }
22
23 pub fn load() -> Self {
24 let Ok(text) = fs::read_to_string(Self::file_path()) else { return Self::default() };
25 ron::from_str(&text).unwrap_or_default()
26 }
27
28 pub fn save(&self) -> anyhow::Result<()> {
29 fs::write(Self::file_path(), ron::ser::to_string_pretty(self, Default::default())?)?;
30 Ok(())
31 }
32}