From 2ee0f0cac60b7891af44216b76d31c6e694cb1fa Mon Sep 17 00:00:00 2001 From: Simon Repp Date: Tue, 2 Sep 2025 11:00:18 +0200 Subject: [PATCH] Extract EditorConfig into own module --- src/editor.rs | 56 +++-------------------------------------- src/editor/config.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 53 deletions(-) create mode 100644 src/editor/config.rs diff --git a/src/editor.rs b/src/editor.rs index 6cf10f0..fe28020 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -1,31 +1,25 @@ // SPDX-FileCopyrightText: 2024-2025 Simon Repp // SPDX-License-Identifier: AGPL-3.0-or-later -use std::fs; - use serde_derive::{Deserialize, Serialize}; use crate::{SitePath, Translations}; mod actix; mod components; +mod config; mod endpoints; pub mod routes; pub mod server; mod widgets; +pub use config::EditorConfig; + use components::Checkbox; use components::Input; use components::Select; use components::Textarea; -#[derive(Debug, Deserialize, Serialize)] -pub struct EditorConfig { - pub language_code: String, - pub theme_key: String, - pub tree_view_order: TreeViewOrder -} - /// Represents the page we are currently viewing, i.e. some element in the /// site tree or an auxiliary page like the jobs overview. pub enum Location<'a> { @@ -57,50 +51,6 @@ pub enum TreeViewOrder { TitleDesc } -impl EditorConfig { - pub fn new( - language_code: &str, - theme_key: &str, - tree_view_order: TreeViewOrder - ) -> EditorConfig { - EditorConfig { - language_code: language_code.into(), - theme_key: theme_key.into(), - tree_view_order - } - } - - /// Persists editor configuration to a hyper8/ directory in the user - /// system's config directory. - pub fn persist(&self) { - if let Some(config_dir) = dirs::config_dir() { - let app_dir = config_dir.join("hyper8"); - let config_file = app_dir.join("editor.bincode"); - - let config_serialized = bincode::serialize(&self).unwrap(); - - let _ = fs::create_dir(&app_dir); - let _ = fs::write(&config_file, config_serialized); - } - } - - /// Retrieves editor configuration from a hyper8/ directory in the user - /// system's config directory. - pub fn retrieve() -> Option { - if let Some(config_dir) = dirs::config_dir() { - let config_file = config_dir.join("hyper8").join("editor.bincode"); - - if let Ok(serialized) = fs::read(&config_file) { - if let Ok(editor_config) = bincode::deserialize(&serialized) { - return Some(editor_config); - } - } - } - - None - } -} - impl<'a> Location<'a> { pub fn active_subpage(&self, site_path: &SitePath) -> Option<&Subpage> { match self { diff --git a/src/editor/config.rs b/src/editor/config.rs new file mode 100644 index 0000000..c2c1172 --- /dev/null +++ b/src/editor/config.rs @@ -0,0 +1,59 @@ +// SPDX-FileCopyrightText: 2024-2025 Simon Repp +// SPDX-License-Identifier: AGPL-3.0-or-later + +use std::fs; + +use serde_derive::{Deserialize, Serialize}; + +use super::TreeViewOrder; + +#[derive(Debug, Deserialize, Serialize)] +pub struct EditorConfig { + pub language_code: String, + pub theme_key: String, + pub tree_view_order: TreeViewOrder +} + +impl EditorConfig { + pub fn new( + language_code: &str, + theme_key: &str, + tree_view_order: TreeViewOrder + ) -> EditorConfig { + EditorConfig { + language_code: language_code.into(), + theme_key: theme_key.into(), + tree_view_order + } + } + + /// Persists editor configuration to a hyper8/ directory in the user + /// system's config directory. + pub fn persist(&self) { + if let Some(config_dir) = dirs::config_dir() { + let app_dir = config_dir.join("hyper8"); + let config_file = app_dir.join("editor.bincode"); + + let config_serialized = bincode::serialize(&self).unwrap(); + + let _ = fs::create_dir(&app_dir); + let _ = fs::write(&config_file, config_serialized); + } + } + + /// Retrieves editor configuration from a hyper8/ directory in the user + /// system's config directory. + pub fn retrieve() -> Option { + if let Some(config_dir) = dirs::config_dir() { + let config_file = config_dir.join("hyper8").join("editor.bincode"); + + if let Ok(serialized) = fs::read(&config_file) { + if let Ok(editor_config) = bincode::deserialize(&serialized) { + return Some(editor_config); + } + } + } + + None + } +}