probably broken tbh
Continuous integration / build (push) Failing after 3m15s

This commit is contained in:
2025-08-20 22:57:16 +01:00
parent c891a8be58
commit 7b051208f3
14 changed files with 611 additions and 257 deletions
+2 -2
View File
@@ -1,6 +1,6 @@
use egui::{TextEdit, vec2};
use crate::{PROJECT_FOLDER, util};
use crate::{PROJECT_FOLDER, filesystem::Id, util};
#[derive(Debug, Clone)]
pub struct Asset {
@@ -48,7 +48,7 @@ impl Asset {
pub fn ui(&mut self, ui: &mut egui::Ui) {
ui.vertical(|ui| {
util::saved_status(ui, self.saved, &self.name, &self.new_name);
util::saved_status(ui, self.saved, &Id::new(), &self.new_name);
if ui.input(|i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl)
|| ui.button("Save").clicked()
+46 -34
View File
@@ -3,8 +3,9 @@ use egui_commonmark::{CommonMarkCache, CommonMarkViewer};
use serde::{self, Deserialize, Serialize};
use crate::{
PROJECT_FOLDER,
FILESYSTEM, PROJECT_FOLDER,
editors::{settings_editor::ProjectSettings, tags::Tag},
filesystem::{FileSystem, Id},
util,
};
@@ -46,21 +47,20 @@ pub struct ContentSection {
#[serde(default)]
pub title: String,
#[serde(default)]
pub id: String,
pub id: Id,
#[serde(default)]
pub description: String,
#[serde(default)]
pub tags: Vec<String>,
pub tags: Vec<Id>,
#[serde(default)]
pub content: String,
// parent id
#[serde(default)]
pub parent: Option<String>,
pub parent: Option<Id>,
#[serde(skip)]
pub saved: bool,
@@ -70,7 +70,7 @@ impl ContentSection {
pub fn new() -> Self {
Self {
title: String::new(),
id: uuid::Uuid::new_v4().to_string(),
id: Id::new(),
description: String::new(),
tags: Vec::new(),
content: String::new(),
@@ -79,24 +79,29 @@ impl ContentSection {
}
}
pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> {
let path = PROJECT_FOLDER
.join("documents")
.join(format!("{}.json", &self.id));
pub fn save<F: FileSystem>(
&mut self,
filesystem: &F,
) -> Result<(), Box<dyn std::error::Error>> {
let documents_dir = PROJECT_FOLDER.join("documents");
if filesystem.exists(&self.id) {
filesystem.write(&self.id, self.clone())?;
} else {
let _new_id = filesystem.create(&documents_dir, self.clone())?;
// Note: The filesystem creates its own ID, but we keep our existing ID for consistency
}
let content = serde_json::to_string_pretty(self)?;
std::fs::write(path, content)?;
self.saved = true;
Ok(())
}
pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
let path = PROJECT_FOLDER.join("documents").join(format!("{id}.json"));
let content = std::fs::read_to_string(&path)?;
let mut section: Self = serde_json::from_str(&content)?;
pub fn load<F: FileSystem>(
filesystem: &F,
id: &Id,
) -> Result<Self, Box<dyn std::error::Error>> {
let mut section: Self = filesystem.read(id)?;
section.saved = true;
section.id = id.to_string();
section.id = id.clone();
Ok(section)
}
@@ -139,11 +144,16 @@ impl MainEditor {
}
}
pub fn render_ui(&mut self, project: &mut ProjectSettings, ui: &mut egui::Ui) {
pub fn render_ui<F: FileSystem>(
&mut self,
project: &mut ProjectSettings,
filesystem: &F,
ui: &mut egui::Ui,
) {
ui.vertical(|ui| {
// check for Ctrl+S to save
if ui.input(|i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl) {
if let Err(e) = self.content.save() {
if let Err(e) = self.content.save(filesystem) {
eprintln!("Failed to save: {e}");
}
}
@@ -160,7 +170,7 @@ impl MainEditor {
ui.horizontal(|ui| {
// save button
if ui.button("Save").clicked() {
if let Err(e) = self.content.save() {
if let Err(e) = self.content.save(filesystem) {
eprintln!("Failed to save: {e}");
}
}
@@ -168,26 +178,23 @@ impl MainEditor {
// create copy button
if ui.button("Create Copy").clicked() {
let mut copy = self.clone();
copy.content.id = uuid::Uuid::new_v4().to_string();
copy.content.id = Id::new();
copy.content.title = format!("{} (Copy)", self.content.title);
copy.content.save().unwrap();
FILESYSTEM.clone(&self.content.id, &copy.content.id);
// TODO: Fix save call to pass filesystem
// copy.content.save().unwrap();
}
// delete button
if ui.button("Delete").clicked() {
std::fs::remove_file(
PROJECT_FOLDER
.join("documents")
.join(format!("{}.json", self.content.id)),
)
.unwrap();
filesystem.delete(&self.content.id).unwrap();
*self = Self::new();
}
// revert changes button
if ui.button("Revert changes").clicked() {
self.content = ContentSection::load(&self.content.id).unwrap();
self.content = ContentSection::load(filesystem, &self.content.id).unwrap();
}
// preview toggle
@@ -267,7 +274,12 @@ impl MainEditor {
self.editor_ui(ui, project);
}
pub fn ui(&mut self, ctx: &egui::Context, project: &mut ProjectSettings) {
pub fn ui<F: FileSystem>(
&mut self,
ctx: &egui::Context,
project: &mut ProjectSettings,
filesystem: &F,
) {
// Show the editor window if enabled
let mut show = self.show_editor;
if show {
@@ -278,11 +290,11 @@ impl MainEditor {
.default_height(800.0)
.open(&mut show)
.show(ctx, |ui| {
self.render_ui(project, ui);
self.render_ui(project, filesystem, ui);
});
} else {
egui::CentralPanel::default().show(ctx, |ui| {
self.render_ui(project, ui);
self.render_ui(project, filesystem, ui);
});
}
}
+17 -14
View File
@@ -1,9 +1,14 @@
use std::fs;
use egui::TextEdit;
use serde::{Deserialize, Serialize};
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use crate::{PROJECT_FOLDER, editors::tags::Tag, util};
use crate::{
FILESYSTEM, PROJECT_FOLDER,
editors::tags::Tag,
filesystem::{FileSystem, Id},
util,
};
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Note {
@@ -14,10 +19,9 @@ pub struct Note {
pub subject: String,
#[serde(default)]
pub tags: Vec<String>,
pub tags: Vec<Id>,
#[serde(skip)]
pub id: String,
pub id: Id,
#[serde(skip)]
pub saved: bool,
@@ -26,7 +30,7 @@ pub struct Note {
impl Default for Note {
fn default() -> Self {
Self {
id: uuid::Uuid::new_v4().to_string(),
id: Id::new(),
name: "New Note".to_string(),
subject: "".to_string(),
content: "".to_string(),
@@ -39,7 +43,7 @@ impl Default for Note {
impl Note {
pub fn new() -> Self {
Self {
id: uuid::Uuid::new_v4().to_string(),
id: Id::new(),
name: "New Note".to_string(),
subject: "".to_string(),
content: "".to_string(),
@@ -50,17 +54,16 @@ impl Note {
pub fn save(&mut self) -> std::io::Result<()> {
let id = &self.id;
let path = PROJECT_FOLDER.join("notes").join(format!("{id}.json"));
fs::write(path, serde_json::to_string(&self)?)?;
let data = serde_json::to_string(&self)?;
FILESYSTEM.write(id, data).unwrap();
self.saved = true;
Ok(())
}
pub fn load(id: &str) -> std::io::Result<Self> {
let path = PROJECT_FOLDER.join("notes").join(format!("{id}.json"));
let content = fs::read_to_string(path)?;
let mut note: Note = serde_json::from_str(&content)?;
note.id = id.to_string();
pub fn load(id: &Id) -> std::io::Result<Self> {
let mut note: Note = FILESYSTEM.read(id).unwrap();
note.id = id.clone();
note.saved = true;
Ok(note)
}
+26 -43
View File
@@ -1,30 +1,30 @@
use core::f32;
use egui::{CollapsingHeader, RichText, Sense, TextEdit, Ui, UiBuilder, vec2};
use serde::{Deserialize, Serialize};
use std::path::Path;
use crate::{
PROJECT_FOLDER, RightPanelContent,
FILESYSTEM, PROJECT_FOLDER, RightPanelContent,
editors::{
tags::Tag,
template_editor::{FieldValue, Template},
},
filesystem::{FileSystem, Id},
util,
};
pub type ObjectId = String;
#[derive(Debug, Serialize, Deserialize)]
pub struct ObjectInstance {
// template info
pub id: ObjectId,
pub template_id: String,
pub id: Id,
pub template_id: Id,
// instance info
pub name: String,
pub fields: std::collections::HashMap<String, FieldValue>,
#[serde(default)]
pub tags: Vec<String>,
pub tags: Vec<Id>,
#[serde(skip)]
pub saved: bool,
@@ -50,8 +50,8 @@ impl Clone for ObjectInstance {
impl Default for ObjectInstance {
fn default() -> Self {
Self {
id: uuid::Uuid::new_v4().to_string(),
template_id: "new_template_instance".to_string(),
id: Id::new(),
template_id: Id::new(),
name: "new_object".to_string(),
fields: std::collections::HashMap::new(),
tags: Vec::new(),
@@ -69,33 +69,25 @@ impl ObjectInstance {
fields.insert(field.name.clone(), FieldValue::from_type(&field.field_type));
}
Self {
id: uuid::Uuid::new_v4().to_string(),
template_id: template.id.clone(),
name: "new_object".to_string(),
let instance = Self {
fields,
tags: Vec::new(),
saved: false,
dialog: None,
}
template_id: template.id.clone(),
..Default::default()
};
let _ = FILESYSTEM.create(Path::new("./objects"), instance.clone());
instance
}
pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> {
let path = PROJECT_FOLDER
.join("objects")
.join(format!("{}.json", &self.id));
let content = serde_json::to_string_pretty(self)?;
std::fs::write(&path, content)?;
FILESYSTEM.write(&self.id, self.clone())?;
self.saved = true;
Ok(())
}
pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
let path = PROJECT_FOLDER.join("objects").join(format!("{id}.json"));
let content = std::fs::read_to_string(&path)?;
let mut instance: ObjectInstance = serde_json::from_str(&content)?;
pub fn load(id: &Id) -> Result<Self, Box<dyn std::error::Error>> {
let mut instance: ObjectInstance = FILESYSTEM.read(id)?;
instance.saved = true;
Ok(instance)
}
@@ -127,23 +119,14 @@ impl ObjectInstance {
}
if ui.button("Create Copy").clicked() {
let mut copy = self.clone();
copy.id = uuid::Uuid::new_v4().to_string();
copy.dialog = None;
copy.name = format!("{} (Copy)", self.name);
copy.save().unwrap();
let new_id = Id::new();
FILESYSTEM.clone(&self.id, &new_id).unwrap();
let copy = Self::load(&new_id).unwrap();
*right_panel = Some(RightPanelContent::Object(Box::new(copy)));
}
if ui.button("Delete").clicked() {
std::fs::remove_file(
PROJECT_FOLDER
.join("objects")
.join(format!("{}.json", self.id)),
)
.unwrap();
FILESYSTEM.delete(&self.id).unwrap();
*right_panel = Some(RightPanelContent::None);
}
});
@@ -299,12 +282,12 @@ impl ObjectInstance {
}
fn selector_ui(
selected: &mut ObjectId,
selected: &mut Id,
objects: &mut [ObjectInstance],
ui: &mut egui::Ui,
saved: &mut bool,
) {
if !selected.is_empty() {
if !selected.to_string().is_empty() {
if let Ok(object) = ObjectInstance::load(selected) {
ui.strong(&object.name);
}
@@ -340,7 +323,7 @@ impl ObjectInstance {
}
if ui.button("Remove").clicked() {
*selected = String::new();
*selected = Id::default();
*saved = false;
}
});
+2 -2
View File
@@ -4,7 +4,7 @@ use egui_extras::DatePickerButton;
use serde::{Deserialize, Serialize};
use std::{io::Read, path::PathBuf, sync::LazyLock};
use crate::{PROJECT_FOLDER, util::saved_status};
use crate::{PROJECT_FOLDER, filesystem::Id, util::saved_status};
#[derive(Serialize, Deserialize, Clone)]
pub struct ProjectSettings {
@@ -86,7 +86,7 @@ impl ProjectSettings {
#[allow(dead_code)]
pub fn ui(&mut self, ui: &mut egui::Ui) {
// save state
saved_status(ui, self.saved, "N/A", "Project Settings");
saved_status(ui, self.saved, &Id::default(), "Project Settings");
if ui.button("Save").clicked() {
self.save();
}
+13 -7
View File
@@ -1,11 +1,15 @@
use egui::{Response, RichText, TextEdit};
use serde::{Deserialize, Serialize};
use crate::{PROJECT_FOLDER, util};
use crate::{
FILESYSTEM, PROJECT_FOLDER,
filesystem::{FileSystem, Id},
util,
};
#[derive(Serialize, Deserialize)]
pub struct Tag {
pub id: String,
pub id: Id,
pub name: String,
pub description: String,
pub color: egui::Color32,
@@ -20,7 +24,7 @@ pub struct Tag {
impl Default for Tag {
fn default() -> Self {
Self {
id: uuid::Uuid::new_v4().to_string(),
id: Id::new(),
name: String::new(),
description: String::new(),
color: egui::Color32::from_rgb(20, 20, 20),
@@ -128,7 +132,7 @@ impl Tag {
});
}
pub fn selector_ui(tag_ids: &mut Vec<String>, ui: &mut egui::Ui, saved: Option<&mut bool>) {
pub fn selector_ui(tag_ids: &mut Vec<Id>, ui: &mut egui::Ui, saved: Option<&mut bool>) {
// remove duplicate tag ids
tag_ids.sort();
tag_ids.dedup();
@@ -202,9 +206,11 @@ impl Tag {
});
}
pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
let path = PROJECT_FOLDER.join("tags").join(format!("{id}.json"));
Ok(serde_json::from_str(&std::fs::read_to_string(path)?)?)
pub fn load(id: &Id) -> Result<Self, Box<dyn std::error::Error>> {
let mut tag: Self = FILESYSTEM.read(id)?;
tag.saved = true;
tag.id = id.clone();
Ok(tag)
}
pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> {
+13 -19
View File
@@ -4,8 +4,9 @@ use egui::ScrollArea;
use serde::{Deserialize, Serialize};
use crate::{
PROJECT_FOLDER, RightPanelContent,
FILESYSTEM, PROJECT_FOLDER, RightPanelContent,
editors::object_editor::ObjectInstance,
filesystem::{self, FileSystem, Id},
util::{self, Error},
};
@@ -16,7 +17,7 @@ pub enum FieldType {
MultiLine,
Date,
Number,
Link { template_id: Option<String> },
Link { template_id: Option<Id> },
Links,
}
@@ -47,8 +48,8 @@ pub enum FieldValue {
MultiLine(String),
Date(NaiveDate),
Number(f64),
Link(String),
Links(Vec<String>),
Link(Id),
Links(Vec<Id>),
}
impl FieldValue {
@@ -59,7 +60,7 @@ impl FieldValue {
FieldType::MultiLine => Self::MultiLine(String::new()),
FieldType::Date => Self::Date(NaiveDate::from_ymd_opt(1970, 1, 1).unwrap()),
FieldType::Number => Self::Number(0.0),
FieldType::Link { template_id: None } => Self::Link(String::new()),
FieldType::Link { template_id: None } => Self::Link(Id::default()),
FieldType::Link {
template_id: Some(template_id),
} => Self::Link(template_id.clone()),
@@ -88,7 +89,7 @@ pub struct FieldDefinition {
#[derive(Serialize, Deserialize)]
pub struct Template {
pub name: String,
pub id: String,
pub id: Id,
pub description: Option<String>,
pub fields: Vec<FieldDefinition>,
@@ -154,7 +155,7 @@ impl Default for Template {
fn default() -> Self {
Self {
name: "New Template".to_string(),
id: uuid::Uuid::new_v4().to_string(),
id: Id::new(),
description: Some(String::from("Placeholder description")),
fields: Vec::new(),
saved: false,
@@ -170,22 +171,15 @@ impl Default for Template {
}
impl Template {
pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
let path = PROJECT_FOLDER.join("templates").join(format!("{id}.json"));
let content = std::fs::read_to_string(&path)?;
let mut template: Self = serde_json::from_str(&content)?;
pub fn load(id: &Id) -> Result<Self, Box<dyn std::error::Error>> {
let mut template: Self = FILESYSTEM.read(id)?;
template.saved = true;
template.id = id.clone();
Ok(template)
}
pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> {
let path = PROJECT_FOLDER
.join("templates")
.join(format!("{}.json", &self.id));
let content = serde_json::to_string_pretty(self)?;
std::fs::write(path, content)?;
FILESYSTEM.write(&self.id, self.clone())?;
self.saved = true;
Ok(())
}
@@ -225,7 +219,7 @@ impl Template {
if ui.button("Create Copy").clicked() {
let mut copy = self.clone();
copy.id = uuid::Uuid::new_v4().to_string();
copy.id = Id::new();
copy.name = format!("{} (Copy)", self.name);
copy.save().unwrap();
}
+16 -8
View File
@@ -10,6 +10,7 @@ use crate::{
asset_editor::Asset, content_editor::ContentSection, object_editor::ObjectInstance,
tags::Tag, template_editor::Template,
},
filesystem::Id,
note_editor::Note,
};
@@ -182,7 +183,7 @@ impl Explorer {
// Filter documents that have the current parent (or no parent if this is the root)
let child_docs: Vec<&ContentSection> = documents
.iter()
.filter(|doc| doc.parent.as_deref() == parent_id)
.filter(|doc| doc.parent.as_ref().map(|id| id.as_str()) == parent_id)
.collect();
for doc in child_docs {
@@ -207,7 +208,7 @@ impl Explorer {
})
.body(|ui| {
// recursive call to render the next level of documents
Self::render_doc_branch(ui, documents, Some(&doc.id), load_doc);
Self::render_doc_branch(ui, documents, Some(doc.id.as_str()), load_doc);
});
}
}
@@ -327,7 +328,7 @@ impl Explorer {
let mut templates = Vec::new();
for entry in std::fs::read_dir(&templates_folder).unwrap() {
let path = entry.unwrap().path();
match Template::load(path.file_stem().unwrap().to_str().unwrap()) {
match Template::load(&Id::from_path(&path)) {
Ok(t) => templates.push(t),
Err(err) => eprintln!("Could not parse file {path:?}: {err}"),
}
@@ -346,7 +347,7 @@ impl Explorer {
let mut objects = Vec::new();
for entry in std::fs::read_dir(&objects_folder).unwrap() {
let path = entry.unwrap().path();
match ObjectInstance::load(path.file_stem().unwrap().to_str().unwrap()) {
match ObjectInstance::load(&Id::from_path(&path)) {
Ok(o) => objects.push(o),
Err(err) => eprintln!("Could not parse file {path:?}: {err}"),
}
@@ -366,7 +367,7 @@ impl Explorer {
for entry in std::fs::read_dir(&notes_folder).unwrap() {
let path = entry.unwrap().path();
match Note::load(path.file_stem().unwrap().to_str().unwrap()) {
match Note::load(&Id::from_path(&path)) {
Ok(note) => notes.push(note),
Err(err) => eprintln!("Could not parse file {path:?}: {err}"),
}
@@ -387,9 +388,16 @@ impl Explorer {
for entry in std::fs::read_dir(&documents_folder).unwrap() {
let path = entry.unwrap().path();
match ContentSection::load(path.file_stem().unwrap().to_str().unwrap()) {
Ok(document) => documents.push(MainEditor::open(document)),
Err(err) => eprintln!("Could not parse file {path:?}: {err}"),
// TODO: Update to use FileSystem API
// For now, read files directly until we refactor the loading system
if let Ok(content) = std::fs::read_to_string(&path) {
if let Ok(document) =
serde_json::from_str::<crate::editors::content_editor::ContentSection>(&content)
{
documents.push(MainEditor::open(document));
} else {
eprintln!("Could not parse file {path:?}");
}
}
}
+137
View File
@@ -0,0 +1,137 @@
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use std::{
fmt,
ops::{Deref, DerefMut},
path::{Path, PathBuf},
};
use crate::FILESYSTEM;
#[cfg(feature = "native")]
pub mod native;
#[cfg(feature = "web")]
pub mod web;
#[derive(Clone, Debug, PartialEq, PartialOrd, Ord, Eq, Hash, Serialize, Deserialize)]
pub struct Id(String);
impl Id {
pub fn new() -> Self {
Self(uuid::Uuid::new_v4().to_string())
}
}
impl AsRef<str> for Id {
fn as_ref(&self) -> &str {
&self.0
}
}
impl Id {
pub fn as_str(&self) -> &str {
&self.0
}
pub fn from_path(path: &Path) -> Self {
Self(path.file_name().unwrap().to_str().unwrap().to_string())
}
}
impl fmt::Display for Id {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl Default for Id {
fn default() -> Self {
Id(String::new())
}
}
#[derive(Debug)]
pub enum FileSystemError {
FileNotFound(Id, String),
DirectoryNotFound(PathBuf, String),
}
impl fmt::Display for FileSystemError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
FileSystemError::FileNotFound(id, message) => {
write!(f, "File not found: {} - {}", id, message)
}
FileSystemError::DirectoryNotFound(id, message) => {
write!(f, "Directory not found: {} - {}", id.display(), message)
}
}
}
}
impl std::error::Error for FileSystemError {}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileTree {
pub name: String,
pub path: PathBuf,
pub is_directory: bool,
pub id: Option<Id>,
pub children: Vec<FileTree>,
}
pub trait FileSystem {
fn new(root: impl AsRef<Path>) -> Self;
fn create(&self, directory: &Path, data: impl Serialize) -> Result<Id, FileSystemError>;
fn read<T: DeserializeOwned>(&self, id: &Id) -> Result<T, FileSystemError>;
fn write(&self, id: &Id, data: impl Serialize) -> Result<(), FileSystemError>;
fn borrow<T: DeserializeOwned + Serialize>(
&self,
id: &Id,
) -> Result<FileBorrow<T>, FileSystemError> {
let file = self.read(id)?;
Ok(FileBorrow {
id: id.clone(),
file,
})
}
fn delete(&self, id: &Id) -> Result<(), FileSystemError>;
fn clone(&self, id: &Id, new_id: &Id) -> Result<(), FileSystemError>;
fn exists(&self, id: &Id) -> bool;
fn lsdir(&self, id: &Id) -> Result<Vec<String>, FileSystemError>;
fn file_tree(&self, root_path: &Path) -> Result<FileTree, FileSystemError>;
}
pub struct FileBorrow<T: DeserializeOwned + Serialize> {
pub id: Id,
pub file: T,
}
impl<T: DeserializeOwned + Serialize> Deref for FileBorrow<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.file
}
}
impl<T: DeserializeOwned + Serialize> DerefMut for FileBorrow<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.file
}
}
impl<T: DeserializeOwned + Serialize> Drop for FileBorrow<T> {
fn drop(&mut self) {
FILESYSTEM.write(&self.id, &self.file).unwrap();
}
}
+327
View File
@@ -0,0 +1,327 @@
use std::collections::HashMap;
use std::fs::{self, File};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock};
use image::EncodableLayout;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_json::to_string;
use crate::filesystem::FileSystemError;
use super::FileSystem;
pub struct NativeFileSystem {
root: PathBuf,
index: Arc<RwLock<HashMap<super::Id, PathBuf>>>,
}
impl NativeFileSystem {
/// Rebuild the entire index by scanning the filesystem
fn rebuild_index(&self) -> Result<(), std::io::Error> {
let mut index = HashMap::new();
Self::scan_directory(&self.root, &mut index)?;
if let Ok(mut idx) = self.index.write() {
*idx = index;
}
Ok(())
}
/// Recursively scan a directory and populate the index
fn scan_directory(
dir: &Path,
index: &mut HashMap<super::Id, PathBuf>,
) -> Result<(), std::io::Error> {
for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
// Try to parse the filename as a UUID (our Id format)
if let Ok(uuid) = uuid::Uuid::parse_str(name) {
let id = super::Id(uuid.to_string());
index.insert(id, path.clone());
}
}
if path.is_dir() {
Self::scan_directory(&path, index)?;
}
}
Ok(())
}
/// Add an entry to the index
fn add_to_index(&self, id: super::Id, path: PathBuf) {
if let Ok(mut index) = self.index.write() {
index.insert(id, path);
}
}
/// Remove an entry from the index
fn remove_from_index(&self, id: &super::Id) {
if let Ok(mut index) = self.index.write() {
index.remove(id);
}
}
/// Get path from index, with fallback to filesystem scan if not found
fn find_path_by_id(&self, id: &super::Id) -> Option<PathBuf> {
// First try the index
if let Ok(index) = self.index.read() {
if let Some(path) = index.get(id) {
// Verify the file still exists
if path.exists() {
return Some(path.clone());
}
}
}
// Fallback: scan filesystem and update index
let target_name = id.to_string();
let mut stack = vec![self.root.clone()];
while let Some(dir) = stack.pop() {
let entries = match fs::read_dir(&dir) {
Ok(e) => e,
Err(_) => continue,
};
for entry in entries.flatten() {
let path = entry.path();
if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
if name == target_name {
// Update index with found path
self.add_to_index(id.clone(), path.clone());
return Some(path);
}
}
if path.is_dir() {
stack.push(path);
}
}
}
None
}
/// Build a file tree recursively from a given path
fn build_file_tree(&self, path: &Path) -> Result<super::FileTree, FileSystemError> {
let name = path
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("")
.to_string();
let is_directory = path.is_dir();
// Check if this path corresponds to an ID in our index
let id = if let Ok(index) = self.index.read() {
index
.iter()
.find(|(_, indexed_path)| indexed_path.as_path() == path)
.map(|(id, _)| id.clone())
} else {
None
};
let mut children = Vec::new();
if is_directory {
for entry in fs::read_dir(path).map_err(|e| {
FileSystemError::DirectoryNotFound(path.to_path_buf(), e.to_string())
})? {
let entry = entry.map_err(|e| {
FileSystemError::DirectoryNotFound(path.to_path_buf(), e.to_string())
})?;
let child_path = entry.path();
match self.build_file_tree(&child_path) {
Ok(child_tree) => children.push(child_tree),
Err(e) => eprintln!(
"Warning: Failed to build tree for {}: {}",
child_path.display(),
e
),
}
}
}
Ok(super::FileTree {
name,
path: path.to_path_buf(),
is_directory,
id,
children,
})
}
}
impl FileSystem for NativeFileSystem {
fn new(root: impl AsRef<Path>) -> Self {
let fs = Self {
root: root.as_ref().to_path_buf(),
index: Arc::new(RwLock::new(HashMap::new())),
};
fs.rebuild_index().unwrap_or_else(|e| {
eprintln!("Warning: Failed to build initial index: {e}");
});
fs
}
fn create(&self, directory: &Path, data: impl Serialize) -> Result<super::Id, FileSystemError> {
let dir = if directory.is_absolute() {
directory.to_path_buf()
} else {
self.root.join(directory)
};
if !dir.exists() {
fs::create_dir_all(&dir).map_err(|e| {
FileSystemError::DirectoryNotFound(dir.to_path_buf(), e.to_string())
})?;
}
if !dir.is_dir() {
return Err(FileSystemError::DirectoryNotFound(
dir.to_path_buf(),
"".to_string(),
));
}
let id = super::Id::new();
let file_path = dir.join(id.to_string());
let mut file = File::create(&file_path)
.map_err(|e| FileSystemError::DirectoryNotFound(dir.to_path_buf(), e.to_string()))?;
file.write_all(serde_json::to_string(&data).unwrap().as_bytes())
.map_err(|e| FileSystemError::DirectoryNotFound(dir.to_path_buf(), e.to_string()))?;
// Add to index
self.add_to_index(id.clone(), file_path);
Ok(id)
}
fn read<T: DeserializeOwned>(&self, id: &super::Id) -> Result<T, FileSystemError> {
let path = self.find_path_by_id(id).ok_or_else(|| {
FileSystemError::FileNotFound(id.clone(), "No path found!".to_string())
})?;
let val = serde_json::from_reader(
File::open(path)
.map_err(|_| FileSystemError::FileNotFound(id.clone(), "".to_string()))?,
)
.map_err(|_| FileSystemError::FileNotFound(id.clone(), "".to_string()))?;
Ok(val)
}
fn write(&self, id: &super::Id, data: impl Serialize) -> Result<(), FileSystemError> {
let path = self.find_path_by_id(id).ok_or_else(|| {
FileSystemError::FileNotFound(id.clone(), "No path found!".to_string())
})?;
let mut file = File::create(path)
.map_err(|_| FileSystemError::FileNotFound(id.clone(), "".to_string()))?;
file.write_all(serde_json::to_string(&data).unwrap().as_bytes())
.map_err(|_| FileSystemError::FileNotFound(id.clone(), "".to_string()))?;
Ok(())
}
fn delete(&self, id: &super::Id) -> Result<(), FileSystemError> {
let path = self.find_path_by_id(id).ok_or_else(|| {
FileSystemError::FileNotFound(id.clone(), "No path found!".to_string())
})?;
let result = if path.is_dir() {
fs::remove_dir_all(path)
} else {
fs::remove_file(path)
};
// Remove from index if deletion was successful
if result.is_ok() {
self.remove_from_index(id);
}
result.map_err(|e| FileSystemError::FileNotFound(id.clone(), e.to_string()))
}
fn clone(&self, id: &super::Id, new_id: &super::Id) -> Result<(), FileSystemError> {
let src = self.find_path_by_id(id).ok_or_else(|| {
FileSystemError::FileNotFound(id.clone(), "No path found!".to_string())
})?;
let parent = src.parent().map(Path::to_path_buf).ok_or_else(|| {
FileSystemError::FileNotFound(id.clone(), "No parent found!".to_string())
})?;
let dst = parent.join(new_id.to_string());
let result = if src.is_dir() {
// Simple recursive dir copy
fn copy_dir(src: &Path, dst: &Path) -> std::io::Result<()> {
fs::create_dir_all(dst)?;
for entry in fs::read_dir(src)? {
let entry = entry?;
let ty = entry.file_type()?;
let sp = entry.path();
let dp = dst.join(entry.file_name());
if ty.is_dir() {
copy_dir(&sp, &dp)?;
} else if ty.is_file() {
fs::copy(&sp, &dp)?;
}
}
Ok(())
}
copy_dir(&src, &dst)
} else {
fs::copy(&src, &dst).map(|_| ())
};
// Add cloned file/directory to index if copy was successful
if result.is_ok() {
self.add_to_index(new_id.clone(), dst);
}
result.map_err(|e| FileSystemError::FileNotFound(id.clone(), e.to_string()))
}
fn exists(&self, id: &super::Id) -> bool {
self.find_path_by_id(id).is_some()
}
fn lsdir(&self, id: &super::Id) -> Result<Vec<String>, FileSystemError> {
let path = match self.find_path_by_id(id) {
Some(p) if p.is_dir() => p,
Some(p) => p.parent().map(Path::to_path_buf).ok_or_else(|| {
FileSystemError::DirectoryNotFound(PathBuf::from(id.to_string()), "".to_string())
})?,
None => {
return Err(FileSystemError::DirectoryNotFound(
PathBuf::from(id.to_string()),
id.to_string(),
));
}
};
let mut entries = Vec::new();
for entry in fs::read_dir(&path)
.map_err(|e| FileSystemError::DirectoryNotFound(path.clone(), e.to_string()))?
{
let entry = entry
.map_err(|e| FileSystemError::DirectoryNotFound(path.clone(), e.to_string()))?;
if let Some(name) = entry.file_name().to_str() {
entries.push(name.to_string());
}
}
Ok(entries)
}
fn file_tree(&self, root_path: &Path) -> Result<super::FileTree, FileSystemError> {
self.build_file_tree(root_path)
}
}
View File
-125
View File
@@ -1,125 +0,0 @@
use std::io::{self, Read, Write};
use std::path::{Path, PathBuf};
/// Platform-agnostic file system operations
trait FileSystem {
fn read_file(&self, path: &Path) -> io::Result<Vec<u8>>;
fn write_file(&self, path: &Path, contents: &[u8]) -> io::Result<()>;
fn create_dir_all(&self, path: &Path) -> io::Result<()>;
fn read_dir(&self, path: &Path) -> io::Result<Vec<PathBuf>>;
fn exists(&self, path: &Path) -> bool;
}
/// Native filesystem implementation
#[cfg(feature = "native")]
struct NativeFileSystem;
#[cfg(feature = "native")]
impl FileSystem for NativeFileSystem {
fn read_file(&self, path: &Path) -> io::Result<Vec<u8>> {
std::fs::read(path)
}
fn write_file(&self, path: &Path, contents: &[u8]) -> io::Result<()> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
std::fs::write(path, contents)
}
fn create_dir_all(&self, path: &Path) -> io::Result<()> {
std::fs::create_dir_all(path)
}
fn read_dir(&self, path: &Path) -> io::Result<Vec<PathBuf>> {
Ok(std::fs::read_dir(path)?
.filter_map(Result::ok)
.map(|entry| entry.path())
.collect())
}
fn exists(&self, path: &Path) -> bool {
path.exists()
}
}
/// Web filesystem implementation
#[cfg(feature = "web")]
struct WebFileSystem;
#[cfg(feature = "web")]
impl WebFileSystem {
fn new() -> Self {
// Initialize web-specific storage if needed
Self
}
}
#[cfg(feature = "web")]
impl FileSystem for WebFileSystem {
fn read_file(&self, path: &Path) -> io::Result<Vec<u8>> {
// In a real implementation, this would use web_sys and IndexedDB
// This is a simplified version that won't actually work
Err(io::Error::new(
io::ErrorKind::Other,
"Web filesystem not implemented",
))
}
fn write_file(&self, path: &Path, contents: &[u8]) -> io::Result<()> {
// In a real implementation, this would use web_sys and IndexedDB
// This is a simplified version that won't actually work
Err(io::Error::new(
io::ErrorKind::Other,
"Web filesystem not implemented",
))
}
fn create_dir_all(&self, _path: &Path) -> io::Result<()> {
// In web, directories are virtual and created automatically
Ok(())
}
fn read_dir(&self, _path: &Path) -> io::Result<Vec<PathBuf>> {
// In a real implementation, this would list files from IndexedDB
// This is a simplified version that returns an empty list
Ok(Vec::new())
}
fn exists(&self, _path: &Path) -> bool {
// In a real implementation, this would check IndexedDB
false
}
}
#[cfg(feature = "web")]
#[cfg(test)]
mod tests {
use super::*;
use std::fs::File;
use tempfile::tempdir;
#[test]
fn test_native_fs() {
let temp_dir = tempdir().unwrap();
let file_path = temp_dir.path().join("test.txt");
let fs = NativeFileSystem;
// Test write and read
let test_data = b"Hello, world!";
fs.write_file(&file_path, test_data).unwrap();
let read_data = fs.read_file(&file_path).unwrap();
assert_eq!(read_data, test_data);
// Test exists
assert!(fs.exists(&file_path));
assert!(!fs.exists(&temp_dir.path().join("nonexistent")));
// Test create_dir_all and read_dir
let dir_path = temp_dir.path().join("subdir");
fs.create_dir_all(&dir_path).unwrap();
let entries = fs.read_dir(temp_dir.path()).unwrap();
assert_eq!(entries.len(), 2); // Should contain both the file and the subdirectory
}
}
+9 -2
View File
@@ -10,7 +10,8 @@ mod explorer;
#[cfg(feature = "llm")]
mod llm_integration;
mod index;
mod filesystem;
mod util;
use crate::{
@@ -19,6 +20,7 @@ use crate::{
settings_editor::ProjectSettings, tags::Tag, template_editor::Template,
},
explorer::Explorer,
filesystem::{FileSystem, native::NativeFileSystem},
};
static VERSION: &str = "0.1.0";
@@ -28,6 +30,9 @@ static PROJECT_FOLDER: LazyLock<PathBuf> = LazyLock::new(|| {
path
});
pub static FILESYSTEM: LazyLock<NativeFileSystem> =
LazyLock::new(|| NativeFileSystem::new(&*PROJECT_FOLDER));
fn main() {
let app = Interface::new();
let options = eframe::NativeOptions {
@@ -47,6 +52,7 @@ pub struct Interface {
editor: content_editor::MainEditor,
explorer: Explorer,
project: ProjectSettings,
filesystem: NativeFileSystem,
}
impl eframe::App for Interface {
@@ -92,6 +98,7 @@ impl Interface {
editor: content_editor::MainEditor::new(),
explorer: Explorer::new(),
project: ProjectSettings::load(),
filesystem: NativeFileSystem::new(&*PROJECT_FOLDER),
}
}
@@ -205,7 +212,7 @@ impl Interface {
// render main content area
fn render_main_content(&mut self, ctx: &egui::Context) {
self.editor.ui(ctx, &mut self.project);
self.editor.ui(ctx, &mut self.project, &self.filesystem);
}
// configure appearance of UI elements
+3 -1
View File
@@ -3,6 +3,8 @@ use egui::{
scroll_area::{ScrollBarVisibility, ScrollSource},
};
use crate::filesystem::Id;
pub struct Error {
message: String,
visible: bool,
@@ -26,7 +28,7 @@ impl Error {
}
}
pub fn saved_status(ui: &mut egui::Ui, saved: bool, id: &str, name: &str) {
pub fn saved_status(ui: &mut egui::Ui, saved: bool, id: &Id, name: &str) {
ui.group(|ui| {
ui.set_max_width(ui.available_width());