Extract EditorConfig into own module

This commit is contained in:
Simon Repp
2025-09-02 11:00:18 +02:00
parent 1666c2c222
commit 2ee0f0cac6
2 changed files with 62 additions and 53 deletions
+3 -53
View File
@@ -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<EditorConfig> {
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 {
+59
View File
@@ -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<EditorConfig> {
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
}
}