fully abstracted away filesystem with LegacyFileSystem trait. next I need to start updating to an ID based save/load system with a ModernFileSystem trait
Continuous integration / build (push) Successful in 1m35s

This commit is contained in:
2025-08-22 00:44:54 +01:00
parent 7272d19207
commit bc7cf6b8ed
5 changed files with 108 additions and 121 deletions
+24 -4
View File
@@ -79,11 +79,31 @@ impl Note {
util::saved_status(ui, self.saved, &self.id, &self.name); util::saved_status(ui, self.saved, &self.id, &self.name);
if ui.button("Save").clicked() { ui.horizontal(|ui| {
if let Err(e) = self.save() { if ui.button("Save").clicked() {
eprintln!("Failed to save: {e}"); if let Err(e) = self.save() {
eprintln!("Failed to save: {e}");
}
} }
}
if ui.button("Create Copy").clicked() {
let new_id = uuid::Uuid::new_v4().to_string();
let mut new_note = self.clone();
new_note.id = new_id;
new_note.name = format!("{} (Copy)", self.name);
if let Err(e) = new_note.save() {
eprintln!("Failed to save copy: {e}");
}
}
if ui.button("Delete").clicked() {
if let Err(e) =
FILESYSTEM.delete(Path::new(&format!("notes/{id}.json", id = self.id)))
{
eprintln!("Failed to delete: {e}");
}
}
});
let id = ui.make_persistent_id("note_name"); let id = ui.make_persistent_id("note_name");
egui::collapsing_header::CollapsingState::load_with_default_open(ui.ctx(), id, true) egui::collapsing_header::CollapsingState::load_with_default_open(ui.ctx(), id, true)
+60 -104
View File
@@ -1,7 +1,4 @@
use itertools::Itertools; use std::path::Path;
use std::fs::{self, DirEntry};
// use walkdir::{DirEntry, WalkDir};
use crate::{ use crate::{
PROJECT_FOLDER, RightPanelContent, PROJECT_FOLDER, RightPanelContent,
@@ -10,6 +7,7 @@ use crate::{
asset_editor::Asset, content_editor::ContentSection, object_editor::ObjectInstance, asset_editor::Asset, content_editor::ContentSection, object_editor::ObjectInstance,
tags::Tag, template_editor::Template, tags::Tag, template_editor::Template,
}, },
filesystem::{FILESYSTEM, FsError, LegacyFileSystem},
note_editor::Note, note_editor::Note,
}; };
@@ -165,14 +163,6 @@ impl Explorer {
}); });
} }
/// Recursively renders a tree of documents.
///
/// Each document is represented by a single element in the `documents` array.
/// The `parent_id` parameter is used to filter out documents that do not have the current
/// parent. If `parent_id` is `None`, all documents are rendered.
///
/// `load_doc` is a mutable reference to a `MainEditor`. When a document is clicked, it
/// is loaded into the `MainEditor` and returned as `Some`.
fn render_doc_branch( fn render_doc_branch(
ui: &mut egui::Ui, ui: &mut egui::Ui,
documents: &[ContentSection], documents: &[ContentSection],
@@ -241,95 +231,64 @@ impl Explorer {
} }
fn render_assets(&mut self, ui: &mut egui::Ui, to_load: &mut Option<RightPanelContent>) { fn render_assets(&mut self, ui: &mut egui::Ui, to_load: &mut Option<RightPanelContent>) {
Self::render_asset_dir(ui, to_load, Path::new("assets"));
}
fn render_asset_dir(ui: &mut egui::Ui, to_load: &mut Option<RightPanelContent>, path: &Path) {
let files = FILESYSTEM.lsfiles(path).unwrap();
let dirs = FILESYSTEM.lsdirs(path).unwrap();
let file_name = path.file_name().unwrap().to_str().unwrap().to_string();
egui::collapsing_header::CollapsingState::load_with_default_open( egui::collapsing_header::CollapsingState::load_with_default_open(
ui.ctx(), ui.ctx(),
ui.make_persistent_id("assets"), ui.make_persistent_id(&file_name),
true, false,
) )
.show_header(ui, |ui| { .show_header(ui, |ui| {
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.label("Assets"); ui.label(file_name);
let _clicked = ui.button("+").on_hover_text("Add new item").clicked();
}); });
}) })
.body(|ui| { .body(|ui| {
let entries = fs::read_dir(PROJECT_FOLDER.join("assets")) // recursive call to render the next level of documents
.unwrap() for dir in dirs.iter() {
.filter_map(Result::ok) Self::render_asset_dir(ui, to_load, dir);
.sorted_by(|a, b| { }
// Directories first, then files
let a_is_dir = a.file_type().unwrap().is_dir();
let b_is_dir = b.file_type().unwrap().is_dir();
if a_is_dir == b_is_dir {
a.file_name().cmp(&b.file_name())
} else if a_is_dir {
std::cmp::Ordering::Less
} else {
std::cmp::Ordering::Greater
}
})
.collect::<Vec<_>>();
for entry in entries { for file in files.iter() {
Self::render_entry(ui, to_load, &entry); Self::render_asset(ui, to_load, file);
} }
}); });
} }
fn render_entry(ui: &mut egui::Ui, to_load: &mut Option<RightPanelContent>, entry: &DirEntry) { fn render_asset(ui: &mut egui::Ui, to_load: &mut Option<RightPanelContent>, path: &Path) {
let file_type = entry.file_type().unwrap(); let file_name = path.file_name().unwrap().to_str().unwrap().to_string();
let is_dir = file_type.is_dir();
let file_name = entry.file_name().to_str().unwrap().to_string();
let path = entry.path();
if is_dir { if ui
let entries = fs::read_dir(path) .selectable_label(false, format!("📄 {file_name}"))
.unwrap() .clicked()
.filter_map(Result::ok) {
.collect::<Vec<_>>(); // use asset::load to get the file at the path
let asset_path = path.strip_prefix(PROJECT_FOLDER.join("assets")).unwrap();
egui::collapsing_header::CollapsingState::load_with_default_open( let asset = Asset::open(asset_path.to_string_lossy().to_string());
ui.ctx(), *to_load = Some(RightPanelContent::Asset(Box::new(asset)));
ui.make_persistent_id(&file_name),
false,
)
.show_header(ui, |ui| {
ui.horizontal(|ui| {
ui.label(file_name);
let _clicked = ui.button("+").on_hover_text("Add new item").clicked();
});
})
.body(|ui| {
// recursive call to render the next level of documents
for entry in entries {
Self::render_entry(ui, to_load, &entry);
}
});
} else {
// Handle file
if ui
.selectable_label(false, format!("📄 {file_name}"))
.clicked()
{
// use asset::load to get the file at the path
let asset_path = path.strip_prefix(PROJECT_FOLDER.join("assets")).unwrap();
let asset = Asset::open(asset_path.to_string_lossy().to_string());
*to_load = Some(RightPanelContent::Asset(Box::new(asset)));
}
} }
} }
// load templates from the templates folder // load templates from the templates folder
fn load_templates(&mut self) -> std::io::Result<()> { fn load_templates(&mut self) -> Result<(), FsError> {
let templates_folder = PROJECT_FOLDER.join("templates"); let path = Path::new("templates");
if !templates_folder.exists() {
std::fs::create_dir_all(&templates_folder)?; if !FILESYSTEM.exists(path) {
FILESYSTEM.mkdir(path)?;
} }
let mut templates = Vec::new(); let mut templates = Vec::new();
for entry in std::fs::read_dir(&templates_folder).unwrap() { for entry in FILESYSTEM.lsfiles(path)? {
let path = entry.unwrap().path(); match FILESYSTEM.read::<Template>(&entry) {
match Template::load(path.file_stem().unwrap().to_str().unwrap()) {
Ok(t) => templates.push(t), Ok(t) => templates.push(t),
Err(err) => eprintln!("Could not parse file {path:?}: {err}"), Err(err) => eprintln!("Could not parse file {entry:?}: {err}"),
} }
} }
self.templates = templates; self.templates = templates;
@@ -338,17 +297,16 @@ impl Explorer {
} }
// load objects from the objects folder // load objects from the objects folder
fn load_objects(&mut self) -> std::io::Result<()> { fn load_objects(&mut self) -> Result<(), FsError> {
let objects_folder = PROJECT_FOLDER.join("objects"); let path = Path::new("objects");
if !objects_folder.exists() { if !FILESYSTEM.exists(path) {
std::fs::create_dir_all(&objects_folder)?; FILESYSTEM.mkdir(path)?;
} }
let mut objects = Vec::new(); let mut objects = Vec::new();
for entry in std::fs::read_dir(&objects_folder).unwrap() { for entry in FILESYSTEM.lsfiles(path)? {
let path = entry.unwrap().path(); match FILESYSTEM.read::<ObjectInstance>(&entry) {
match ObjectInstance::load(path.file_stem().unwrap().to_str().unwrap()) {
Ok(o) => objects.push(o), Ok(o) => objects.push(o),
Err(err) => eprintln!("Could not parse file {path:?}: {err}"), Err(err) => eprintln!("Could not parse file {entry:?}: {err}"),
} }
} }
self.objects = objects; self.objects = objects;
@@ -357,16 +315,15 @@ impl Explorer {
} }
// load notes from the notes folder // load notes from the notes folder
fn load_notes(&mut self) -> std::io::Result<()> { fn load_notes(&mut self) -> Result<(), FsError> {
let notes_folder = PROJECT_FOLDER.join("notes"); let path = Path::new("notes");
if !notes_folder.exists() { if !FILESYSTEM.exists(path) {
std::fs::create_dir_all(&notes_folder)?; FILESYSTEM.mkdir(path)?;
} }
let mut notes = Vec::new(); let mut notes = Vec::new();
for entry in std::fs::read_dir(&notes_folder).unwrap() { for entry in FILESYSTEM.lsfiles(path)? {
let path = entry.unwrap().path(); match FILESYSTEM.read::<Note>(&entry) {
match Note::load(path.file_stem().unwrap().to_str().unwrap()) {
Ok(note) => notes.push(note), Ok(note) => notes.push(note),
Err(err) => eprintln!("Could not parse file {path:?}: {err}"), Err(err) => eprintln!("Could not parse file {path:?}: {err}"),
} }
@@ -378,18 +335,17 @@ impl Explorer {
} }
// load documents from the documents folder // load documents from the documents folder
fn load_documents(&mut self) -> std::io::Result<()> { fn load_documents(&mut self) -> Result<(), FsError> {
let documents_folder = PROJECT_FOLDER.join("documents"); let path = Path::new("documents");
if !documents_folder.exists() { if !FILESYSTEM.exists(path) {
std::fs::create_dir_all(&documents_folder)?; FILESYSTEM.mkdir(path)?;
} }
let mut documents = Vec::new(); let mut documents = Vec::new();
for entry in std::fs::read_dir(&documents_folder).unwrap() { for entry in FILESYSTEM.lsfiles(path)? {
let path = entry.unwrap().path(); match FILESYSTEM.read::<ContentSection>(&entry) {
match ContentSection::load(path.file_stem().unwrap().to_str().unwrap()) {
Ok(document) => documents.push(MainEditor::open(document)), Ok(document) => documents.push(MainEditor::open(document)),
Err(err) => eprintln!("Could not parse file {path:?}: {err}"), Err(err) => eprintln!("Could not parse file {entry:?}: {err}"),
} }
} }
@@ -398,7 +354,7 @@ impl Explorer {
Ok(()) Ok(())
} }
fn load_tags(&mut self) -> std::io::Result<()> { fn load_tags(&mut self) -> Result<(), FsError> {
self.tags = Tag::load_all(); self.tags = Tag::load_all();
Ok(()) Ok(())
} }
+2 -2
View File
@@ -29,12 +29,12 @@ pub trait LegacyFileSystem {
fn read_bytes(&self, path: &Path) -> Result<Vec<u8>, FsError>; fn read_bytes(&self, path: &Path) -> Result<Vec<u8>, FsError>;
fn write<T: Serialize>(&self, path: &Path, data: T) -> Result<(), FsError>; fn write<T: Serialize>(&self, path: &Path, data: T) -> Result<(), FsError>;
fn delete(&self, path: &Path) -> Result<(), FsError>; fn delete(&self, path: &Path) -> Result<(), FsError>;
fn lsfiles(&self, path: &Path) -> Result<Vec<PathBuf>, FsError>;
fn lsdirs(&self, path: &Path) -> Result<Vec<PathBuf>, FsError>;
fn mkdir(&self, path: &Path) -> Result<(), FsError>; fn mkdir(&self, path: &Path) -> Result<(), FsError>;
fn rename(&self, path: &Path, new_path: &Path) -> Result<(), FsError>; fn rename(&self, path: &Path, new_path: &Path) -> Result<(), FsError>;
#[allow(unused)] #[allow(unused)]
fn exists(&self, path: &Path) -> bool; fn exists(&self, path: &Path) -> bool;
fn config_path(&self) -> PathBuf; fn config_path(&self) -> PathBuf;
} }
+22 -11
View File
@@ -1,6 +1,3 @@
// ────────────────────────────────────────────────────────────────
// Imports
// ────────────────────────────────────────────────────────────────
use std::fs; use std::fs;
use std::io::{ErrorKind, Read}; use std::io::{ErrorKind, Read};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
@@ -10,11 +7,6 @@ use serde::de::DeserializeOwned;
use crate::filesystem::{FsError, LegacyFileSystem}; use crate::filesystem::{FsError, LegacyFileSystem};
// ────────────────────────────────────────────────────────────────
// Concrete implementation
// ────────────────────────────────────────────────────────────────
/// The concrete filesystem. All paths are interpreted relative to
/// `project_root`.
pub struct NativeFileSystem { pub struct NativeFileSystem {
project_root: PathBuf, project_root: PathBuf,
} }
@@ -34,9 +26,6 @@ impl NativeFileSystem {
} }
} }
// ────────────────────────────────────────────────────────────────
// Implementation of the trait
// ────────────────────────────────────────────────────────────────
impl LegacyFileSystem for NativeFileSystem { impl LegacyFileSystem for NativeFileSystem {
fn read<T: DeserializeOwned>(&self, path: &Path) -> Result<T, FsError> { fn read<T: DeserializeOwned>(&self, path: &Path) -> Result<T, FsError> {
let full_path = self.full_path(path); let full_path = self.full_path(path);
@@ -80,6 +69,28 @@ impl LegacyFileSystem for NativeFileSystem {
fs::create_dir_all(full_path).map_err(FsError::Io) fs::create_dir_all(full_path).map_err(FsError::Io)
} }
fn lsfiles(&self, path: &Path) -> Result<Vec<PathBuf>, FsError> {
let full_path = self.full_path(path);
let paths = fs::read_dir(full_path)?
.filter_map(|res| res.ok())
.filter(|entry| entry.file_type().unwrap().is_file())
.map(|entry| entry.path())
.collect();
Ok(paths)
}
fn lsdirs(&self, path: &Path) -> Result<Vec<PathBuf>, FsError> {
let full_path = self.full_path(path);
let paths = fs::read_dir(full_path)?
.filter_map(|res| res.ok())
.filter(|entry| entry.file_type().unwrap().is_dir())
.map(|entry| entry.path())
.collect();
Ok(paths)
}
fn exists(&self, path: &Path) -> bool { fn exists(&self, path: &Path) -> bool {
let full_path = self.full_path(path); let full_path = self.full_path(path);
full_path.exists() full_path.exists()
View File