1 Commits

Author SHA1 Message Date
zxq5 7b051208f3 probably broken tbh
Continuous integration / build (push) Failing after 3m15s
2025-08-20 22:57:16 +01:00
17 changed files with 823 additions and 586 deletions
+2 -2
View File
@@ -6,8 +6,8 @@
"files.trimFinalNewlines": true,
"files.trimTrailingWhitespace": true,
"rust-analyzer.cargo.features": [
"native",
"llm"
"llm",
"native"
],
"rust-analyzer.cargo.noDefaultFeatures": true,
"rust-analyzer.cargo.allFeatures": false
+7 -10
View File
@@ -1,10 +1,6 @@
use egui::{TextEdit, vec2};
use crate::{
PROJECT_FOLDER,
filesystem::{FILESYSTEM, FsError, LegacyFileSystem},
util,
};
use crate::{PROJECT_FOLDER, filesystem::Id, util};
#[derive(Debug, Clone)]
pub struct Asset {
@@ -29,14 +25,15 @@ impl Asset {
println!("old_path: {old_path:?}");
println!("new_path: {new_path:?}");
if let Err(FsError::Io(err)) = FILESYSTEM.rename(&old_path, &new_path) {
// move from src dir to name path
if let Err(err) = std::fs::rename(&old_path, &new_path) {
match err.kind() {
std::io::ErrorKind::NotFound => {
let dir = new_path.parent().unwrap();
if !dir.exists() {
FILESYSTEM.mkdir(dir).unwrap();
std::fs::create_dir_all(dir).unwrap();
}
FILESYSTEM.rename(&old_path, &new_path).unwrap();
std::fs::rename(&old_path, &new_path).unwrap();
}
_ => panic!("Failed to rename file: {err}"),
}
@@ -51,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()
@@ -76,7 +73,7 @@ impl Asset {
ui.separator();
if let Ok(bytes) = FILESYSTEM.read_bytes(&Self::path(&self.name)) {
if let Ok(bytes) = std::fs::read(Self::path(&self.name)) {
let image_source = egui::ImageSource::Bytes {
uri: std::borrow::Cow::Owned(self.name.clone()),
bytes: bytes.into(),
+53 -40
View File
@@ -1,12 +1,11 @@
use std::path::Path;
use egui::TextEdit;
use egui_commonmark::{CommonMarkCache, CommonMarkViewer};
use serde::{self, Deserialize, Serialize};
use crate::{
FILESYSTEM, PROJECT_FOLDER,
editors::{settings_editor::ProjectSettings, tags::Tag},
filesystem::{FILESYSTEM, LegacyFileSystem},
filesystem::{FileSystem, Id},
util,
};
@@ -45,21 +44,23 @@ impl Clone for MainEditor {
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct ContentSection {
#[serde(default)]
pub title: String,
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,
@@ -69,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(),
@@ -78,19 +79,29 @@ impl ContentSection {
}
}
pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> {
FILESYSTEM.write(
Path::new(&format!("documents/{id}.json", id = &self.id)),
self.clone(),
)?;
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
}
self.saved = true;
Ok(())
}
pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
let mut section: Self = FILESYSTEM.read(Path::new(&format!("documents/{id}.json")))?;
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)
}
@@ -133,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}");
}
}
@@ -154,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}");
}
}
@@ -162,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() {
FILESYSTEM
.delete(Path::new(&format!(
"documents/{id}.json",
id = &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
@@ -189,9 +202,7 @@ impl MainEditor {
// assistant toggle
#[cfg(feature = "llm")]
if project.ai_enabled() {
ui.checkbox(&mut self.show_ai, "AI Assistant");
}
ui.checkbox(&mut self.show_ai, "AI Assistant");
// editor toggle
ui.checkbox(&mut self.editor_separate_window, "Pop out editor");
@@ -239,7 +250,7 @@ impl MainEditor {
ui.separator();
#[cfg(feature = "llm")]
if self.show_ai && project.ai_enabled() {
if self.show_ai {
let dialog = &mut self.dialog;
dialog.content = self.content.content.clone();
@@ -263,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 {
@@ -274,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);
});
}
}
@@ -320,7 +336,7 @@ impl MainEditor {
});
}
fn editor_ui(&mut self, ui: &mut egui::Ui, _project: &mut ProjectSettings) {
fn editor_ui(&mut self, ui: &mut egui::Ui, project: &mut ProjectSettings) {
let _response = egui::ScrollArea::both()
.auto_shrink([false, false])
.id_salt("editor_scroll")
@@ -338,7 +354,7 @@ impl MainEditor {
ui.set_min_width(max_width as f32);
let response = ui.add(
ui.add(
TextEdit::multiline(&mut self.content.content)
.id_source("MainEditor_editor")
.font(egui::TextStyle::Monospace)
@@ -348,9 +364,6 @@ impl MainEditor {
.hint_text("Type here...")
.desired_width(max_width as f32),
);
if response.changed() {
self.content.saved = false;
}
});
});
});
+20 -42
View File
@@ -1,40 +1,36 @@
use std::path::Path;
use std::fs;
use egui::TextEdit;
use serde::{Deserialize, Serialize};
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use crate::{
FILESYSTEM, PROJECT_FOLDER,
editors::tags::Tag,
filesystem::{FILESYSTEM, LegacyFileSystem},
filesystem::{FileSystem, Id},
util,
};
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Note {
pub id: String,
pub name: String,
#[serde(default)]
pub content: String,
#[serde(default)]
pub subject: String,
#[serde(default)]
pub tags: Vec<String>,
pub tags: Vec<Id>,
pub id: Id,
#[serde(skip)]
#[serde(default = "default_saved")]
pub saved: bool,
}
pub fn default_saved() -> bool {
true
}
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(),
@@ -47,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(),
@@ -56,16 +52,18 @@ impl Note {
}
}
pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> {
pub fn save(&mut self) -> std::io::Result<()> {
let id = &self.id;
FILESYSTEM.write(Path::new(&format!("notes/{id}.json")), self.clone())?;
let data = serde_json::to_string(&self)?;
FILESYSTEM.write(id, data).unwrap();
self.saved = true;
Ok(())
}
pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
let mut note: Self = FILESYSTEM.read(Path::new(&format!("notes/{id}.json")))?;
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)
}
@@ -79,31 +77,11 @@ impl Note {
util::saved_status(ui, self.saved, &self.id, &self.name);
ui.horizontal(|ui| {
if ui.button("Save").clicked() {
if let Err(e) = self.save() {
eprintln!("Failed to save: {e}");
}
if ui.button("Save").clicked() {
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");
egui::collapsing_header::CollapsingState::load_with_default_open(ui.ctx(), id, true)
+26 -36
View File
@@ -4,29 +4,27 @@ 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, LegacyFileSystem},
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,
@@ -52,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(),
@@ -71,26 +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 id = &self.id;
FILESYSTEM.write(Path::new(&format!("objects/{id}.json")), self.clone())?;
FILESYSTEM.write(&self.id, self.clone())?;
self.saved = true;
Ok(())
}
pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
let mut instance: Self = FILESYSTEM.read(Path::new(&format!("objects/{id}.json")))?;
pub fn load(id: &Id) -> Result<Self, Box<dyn std::error::Error>> {
let mut instance: ObjectInstance = FILESYSTEM.read(id)?;
instance.saved = true;
Ok(instance)
}
@@ -122,21 +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() {
let id = &self.id;
FILESYSTEM
.delete(Path::new(&format!("objects/{id}.json")))
.expect("Failed to delete object");
FILESYSTEM.delete(&self.id).unwrap();
*right_panel = Some(RightPanelContent::None);
}
});
@@ -267,7 +257,7 @@ impl ObjectInstance {
if !value.is_empty() {
let path = PROJECT_FOLDER.join("assets").join(&value);
if let Ok(bytes) = FILESYSTEM.read_bytes(&path) {
if let Ok(bytes) = std::fs::read(&path) {
let image_source = egui::ImageSource::Bytes {
uri: std::borrow::Cow::Owned(path.to_str().unwrap().to_string()),
bytes: bytes.into(),
@@ -292,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);
}
@@ -333,7 +323,7 @@ impl ObjectInstance {
}
if ui.button("Remove").clicked() {
*selected = String::new();
*selected = Id::default();
*saved = false;
}
});
+127 -196
View File
@@ -2,12 +2,9 @@ use chrono::NaiveDate;
use egui::TextEdit;
use egui_extras::DatePickerButton;
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use std::{io::Read, path::PathBuf, sync::LazyLock};
use crate::{
filesystem::{FILESYSTEM, LegacyFileSystem},
util::saved_status,
};
use crate::{PROJECT_FOLDER, filesystem::Id, util::saved_status};
#[derive(Serialize, Deserialize, Clone)]
pub struct ProjectSettings {
@@ -17,7 +14,6 @@ pub struct ProjectSettings {
project_description: String,
// AI settings
#[cfg(feature = "llm")]
pub ai_context: String,
// settings
@@ -33,6 +29,17 @@ pub struct ProjectSettings {
pub saved: bool,
}
static GLOBAL_SETTINGS_PATH: LazyLock<String> =
LazyLock::new(|| match std::env::var("XDG_CONFIG_HOME") {
Ok(path) => path + "/worldcoder/settings.json",
Err(_) => {
eprintln!(
"XDG_CONFIG_HOME not set, using default path of ~/.config/worldcoder/settings.json"
);
"~/.config/worldcoder/settings.json".to_string()
}
});
impl ProjectSettings {
#[allow(dead_code)]
pub fn new() -> Self {
@@ -40,10 +47,25 @@ impl ProjectSettings {
}
pub fn load() -> Self {
if let Ok(mut proj) = FILESYSTEM.read::<Self>(Path::new("project.json")) {
let project_path = PROJECT_FOLDER.join("project.json");
let mut file = if let Ok(file) = std::fs::File::open(project_path) {
file
} else {
return Self::default();
};
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
if let Ok(mut proj) = serde_json::from_str::<Self>(&contents) {
proj.saved = true;
// load global settings
proj.global_settings = EditorSettings::load_global();
// load local overrides
proj.local_overrides = EditorSettings::load();
proj
} else {
Self::default()
@@ -51,9 +73,9 @@ impl ProjectSettings {
}
pub fn save(&mut self) {
FILESYSTEM
.write(Path::new("project.json"), self.clone())
.unwrap();
let project_path = PROJECT_FOLDER.join("project.json");
let content = serde_json::to_string_pretty(self).unwrap();
std::fs::write(project_path, content).unwrap();
self.global_settings.save();
self.local_overrides.save();
@@ -61,104 +83,14 @@ impl ProjectSettings {
self.saved = true;
}
#[allow(unused)]
fn config_str_override(
label: &str,
field: &mut Option<String>,
default: &str,
ui: &mut egui::Ui,
) -> bool {
let mut changed = false;
ui.label(label);
if let Some(value) = field {
if ui.text_edit_singleline(value).changed() {
changed = true;
};
if ui.button("Remove Override").clicked() {
*field = None;
changed = true;
}
} else if ui.button("Override").clicked() {
*field = Some(default.to_string());
changed = true;
}
ui.end_row();
changed
}
#[allow(unused)]
fn config_bool_override(
label: &str,
field: &mut Option<bool>,
default: bool,
ui: &mut egui::Ui,
) -> bool {
let mut changed = false;
ui.label(label);
if let Some(value) = field {
if ui.checkbox(value, "Enable AI").changed() {
changed = true;
};
if ui.button("Remove Override").clicked() {
*field = None;
changed = true;
}
} else if ui.button("Override").clicked() {
*field = Some(default);
changed = true;
}
ui.end_row();
changed
}
#[allow(unused)]
fn config_str(field: &mut String, label: &str, ui: &mut egui::Ui) -> bool {
let mut changed = false;
ui.label(label);
if ui.text_edit_singleline(field).changed() {
changed = true;
}
ui.end_row();
changed
}
#[allow(unused)]
fn config_bool(label: &str, field: &mut bool, ui: &mut egui::Ui) -> bool {
let mut changed = false;
ui.label(label);
if ui.checkbox(field, "Enable AI").changed() {
changed = true;
}
ui.end_row();
changed
}
#[allow(unused)]
#[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();
}
if ui.input(|i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl) {
self.save();
}
ui.separator();
// project settings
@@ -167,65 +99,80 @@ impl ProjectSettings {
.striped(true)
.num_columns(2)
.show(ui, |ui| {
if Self::config_str(&mut self.project_name, "Project Name", ui) { self.saved = false };
if Self::config_str(&mut self.project_author, "Project Author", ui) { self.saved = false };
if Self::config_str(&mut self.project_description, "Project Description", ui) { self.saved = false };
ui.label("Project Name");
ui.text_edit_singleline(&mut self.project_name);
ui.label("Date");
if ui.add(DatePickerButton::new(&mut self.date)).changed() { self.saved = false };
ui.end_row();
#[cfg(feature = "llm")]
{
ui.label("AI Context Prompt");
if ui.add(TextEdit::multiline(&mut self.ai_context)
.font(egui::TextStyle::Monospace)
.interactive(true)
.frame(false)
.lock_focus(true)
.hint_text("What is this project about? what should the LLM know when generating content for this project?")).changed() { self.saved = false };
ui.end_row();
}
ui.label("Project Author");
ui.text_edit_singleline(&mut self.project_author);
ui.end_row();
ui.label("Project Description");
ui.text_edit_singleline(&mut self.project_description);
ui.end_row();
ui.label("Date");
ui.add(DatePickerButton::new(&mut self.date));
ui.end_row();
ui.label("AI Context Prompt");
ui.add(TextEdit::multiline(&mut self.ai_context)
.font(egui::TextStyle::Monospace)
.interactive(true)
.frame(false)
.lock_focus(true)
.hint_text("What is this project about? what should the LLM know when generating content for this project?"));
ui.end_row();
});
ui.separator();
// local settings overrides for editor
ui.heading("Local Overrides");
egui::Grid::new("local overrides")
.striped(true)
.num_columns(2)
.show(ui, |ui| {
#[cfg(feature = "llm")]
if ProjectSettings::config_str_override(
"LLM API URI",
&mut self.local_overrides.llm_api_uri,
"http://localhost:1234",
ui,
) {
self.saved = false;
ui.label("Enable AI");
if let Some(ai_enabled) = &mut self.local_overrides.ai_enabled {
ui.checkbox(ai_enabled, "Enable AI");
if ui.button("Remove Override").clicked() {
self.local_overrides.ai_enabled = None;
}
} else if ui.button("Override").clicked() {
self.local_overrides.ai_enabled = Some(true);
}
#[cfg(feature = "llm")]
if ProjectSettings::config_str_override(
"LLM API Key",
&mut self.local_overrides.llm_api_key,
"1234",
ui,
) {
self.saved = false;
ui.end_row();
ui.label("LLM API URI");
if let Some(llm_api_uri) = &mut self.local_overrides.llm_api_uri {
ui.text_edit_singleline(llm_api_uri);
if ui.button("Remove Override").clicked() {
self.local_overrides.llm_api_uri = None;
}
} else if ui.button("Override").clicked() {
self.local_overrides.llm_api_uri = Some("http://localhost:1234".to_string());
}
#[cfg(feature = "llm")]
if ProjectSettings::config_bool_override(
"Enable AI",
&mut self.local_overrides.ai_enabled,
true,
ui,
) {
self.saved = false;
ui.end_row();
ui.label("LLM API Key");
if let Some(llm_api_key) = &mut self.local_overrides.llm_api_key {
ui.text_edit_singleline(llm_api_key);
if ui.button("Remove Override").clicked() {
self.local_overrides.llm_api_key = None;
}
} else if ui.button("Override").clicked() {
self.local_overrides.llm_api_key = Some("1234".to_string());
}
ui.end_row();
});
ui.separator();
@@ -236,37 +183,23 @@ impl ProjectSettings {
.striped(true)
.num_columns(2)
.show(ui, |ui| {
#[cfg(feature = "llm")]
if Self::config_bool(
"Enable AI",
self.global_settings.ai_enabled.as_mut().unwrap(),
ui,
) {
self.saved = false;
}
ui.label("Enable AI");
ui.checkbox(&mut self.global_settings.ai_enabled.unwrap(), "Enable AI");
#[cfg(feature = "llm")]
if Self::config_str(
self.global_settings.llm_api_uri.as_mut().unwrap(),
"LLM API URI",
ui,
) {
self.saved = false
};
ui.end_row();
#[cfg(feature = "llm")]
if Self::config_str(
self.global_settings.llm_api_key.as_mut().unwrap(),
"LLM API Key",
ui,
) {
self.saved = false
};
ui.label("LLM API URI");
ui.text_edit_singleline(self.global_settings.llm_api_uri.as_mut().unwrap());
ui.end_row();
ui.label("LLM API Key");
ui.text_edit_singleline(self.global_settings.llm_api_key.as_mut().unwrap());
ui.end_row();
});
}
#[cfg(feature = "llm")]
#[allow(unused)]
pub fn ai_enabled(&mut self) -> bool {
let client = reqwest::blocking::Client::new();
@@ -303,9 +236,8 @@ impl Default for ProjectSettings {
project_author: "Your Name".to_string(),
project_description: "Description of your project".to_string(),
#[cfg(feature = "llm")]
ai_context: "".to_string(),
global_settings: EditorSettings::default(),
global_settings: EditorSettings::new(),
local_overrides: EditorSettings::new(),
// window state
@@ -317,14 +249,10 @@ impl Default for ProjectSettings {
#[derive(Serialize, Deserialize, Clone)]
pub struct EditorSettings {
pub dark_theme: Option<bool>,
#[cfg(feature = "llm")]
pub llm_api_uri: Option<String>,
#[cfg(feature = "llm")]
pub llm_api_key: Option<String>,
#[cfg(feature = "llm")]
pub ai_enabled: Option<bool>,
pub dark_theme: Option<bool>,
#[serde(skip)]
is_global: bool,
@@ -333,11 +261,8 @@ pub struct EditorSettings {
impl Default for EditorSettings {
fn default() -> Self {
Self {
#[cfg(feature = "llm")]
llm_api_uri: Some("http://localhost:1234".to_string()),
#[cfg(feature = "llm")]
llm_api_key: Some("".to_string()),
#[cfg(feature = "llm")]
ai_enabled: Some(true),
dark_theme: Some(true),
@@ -350,11 +275,8 @@ impl Default for EditorSettings {
impl EditorSettings {
pub fn new() -> Self {
Self {
#[cfg(feature = "llm")]
llm_api_uri: None,
#[cfg(feature = "llm")]
llm_api_key: None,
#[cfg(feature = "llm")]
ai_enabled: None,
dark_theme: None,
@@ -363,34 +285,43 @@ impl EditorSettings {
}
pub fn load() -> Self {
if let Ok(mut settings) = FILESYSTEM.read::<Self>(Path::new("settings.json")) {
settings.is_global = false;
return settings;
}
let path = PROJECT_FOLDER.join("settings.json");
let mut file = if let Ok(file) = std::fs::File::open(path) {
file
} else {
return Self::default();
};
Self::new()
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
serde_json::from_str(&contents).unwrap()
}
pub fn save(&self) {
let content = serde_json::to_string_pretty(self).unwrap();
let path = if self.is_global {
FILESYSTEM.config_path()
PathBuf::from(GLOBAL_SETTINGS_PATH.clone())
} else {
PathBuf::from("settings.json")
PROJECT_FOLDER.join("settings.json")
};
FILESYSTEM.write(path.as_path(), self.clone()).unwrap()
std::fs::write(path, content).unwrap();
}
pub fn load_global() -> Self {
let path = FILESYSTEM.config_path();
let path = PathBuf::from(GLOBAL_SETTINGS_PATH.clone());
if !path.exists() {
FILESYSTEM.mkdir(path.parent().unwrap()).unwrap();
FILESYSTEM.write(path.as_path(), Self::default()).unwrap();
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
let content = serde_json::to_string_pretty(&Self::default()).unwrap();
std::fs::write(&path, content).unwrap();
// return a default config
return Self::default();
}
let mut settings = FILESYSTEM.read::<Self>(path.as_path()).unwrap();
settings.is_global = true;
settings
let content = std::fs::read_to_string(path).unwrap();
serde_json::from_str(&content).unwrap()
}
}
+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>> {
+30 -18
View File
@@ -2,12 +2,11 @@ use chrono::NaiveDate;
use core::fmt;
use egui::ScrollArea;
use serde::{Deserialize, Serialize};
use std::path::Path;
use crate::{
RightPanelContent,
FILESYSTEM, PROJECT_FOLDER, RightPanelContent,
editors::object_editor::ObjectInstance,
filesystem::{FILESYSTEM, LegacyFileSystem},
filesystem::{self, FileSystem, Id},
util::{self, Error},
};
@@ -18,7 +17,7 @@ pub enum FieldType {
MultiLine,
Date,
Number,
Link { template_id: Option<String> },
Link { template_id: Option<Id> },
Links,
}
@@ -49,8 +48,8 @@ pub enum FieldValue {
MultiLine(String),
Date(NaiveDate),
Number(f64),
Link(String),
Links(Vec<String>),
Link(Id),
Links(Vec<Id>),
}
impl FieldValue {
@@ -61,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()),
@@ -90,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>,
@@ -156,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,
@@ -172,15 +171,15 @@ impl Default for Template {
}
impl Template {
pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
let mut template = FILESYSTEM.read::<Self>(Path::new(&format!("templates/{id}.json")))?;
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 id = &self.id;
FILESYSTEM.write(Path::new(&format!("templates/{id}.json")), self.clone())?;
FILESYSTEM.write(&self.id, self.clone())?;
self.saved = true;
Ok(())
}
@@ -198,6 +197,16 @@ impl Template {
ScrollArea::vertical().show(ui, |ui| {
ui.vertical(|ui| {
// ui.group(|ui| {
// ui.horizontal(|ui| {
// if self.saved {
// ui.label(RichText::new("✓ Saved").color(egui::Color32::GREEN));
// } else {
// ui.label(RichText::new("* Unsaved").color(egui::Color32::YELLOW));
// }
// ui.label(format!("id: {}", self.id));
// });
// });
util::saved_status(ui, self.saved, &self.id, &self.name);
// Save/Cancel buttons
@@ -210,16 +219,19 @@ 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();
}
if ui.button("Delete").clicked() {
let id = &self.id;
FILESYSTEM
.delete(Path::new(&format!("templates/{id}.json")))
.unwrap();
std::fs::remove_file(
PROJECT_FOLDER
.join("templates")
.join(format!("{}.json", self.id)),
)
.unwrap();
*new_instance = Some(RightPanelContent::None);
}
+115 -63
View File
@@ -1,4 +1,7 @@
use std::path::Path;
use itertools::Itertools;
use std::fs::{self, DirEntry};
// use walkdir::{DirEntry, WalkDir};
use crate::{
PROJECT_FOLDER, RightPanelContent,
@@ -7,7 +10,7 @@ use crate::{
asset_editor::Asset, content_editor::ContentSection, object_editor::ObjectInstance,
tags::Tag, template_editor::Template,
},
filesystem::{FILESYSTEM, FsError, LegacyFileSystem},
filesystem::Id,
note_editor::Note,
};
@@ -163,6 +166,14 @@ 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(
ui: &mut egui::Ui,
documents: &[ContentSection],
@@ -172,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 {
@@ -197,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);
});
}
}
@@ -231,64 +242,95 @@ impl Explorer {
}
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(
ui.ctx(),
ui.make_persistent_id(&file_name),
false,
ui.make_persistent_id("assets"),
true,
)
.show_header(ui, |ui| {
ui.horizontal(|ui| {
ui.label(file_name);
let _clicked = ui.button("+").on_hover_text("Add new item").clicked();
ui.label("Assets");
});
})
.body(|ui| {
// recursive call to render the next level of documents
for dir in dirs.iter() {
Self::render_asset_dir(ui, to_load, dir);
}
let entries = fs::read_dir(PROJECT_FOLDER.join("assets"))
.unwrap()
.filter_map(Result::ok)
.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 file in files.iter() {
Self::render_asset(ui, to_load, file);
for entry in entries {
Self::render_entry(ui, to_load, &entry);
}
});
}
fn render_asset(ui: &mut egui::Ui, to_load: &mut Option<RightPanelContent>, path: &Path) {
let file_name = path.file_name().unwrap().to_str().unwrap().to_string();
fn render_entry(ui: &mut egui::Ui, to_load: &mut Option<RightPanelContent>, entry: &DirEntry) {
let file_type = entry.file_type().unwrap();
let is_dir = file_type.is_dir();
let file_name = entry.file_name().to_str().unwrap().to_string();
let path = entry.path();
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)));
if is_dir {
let entries = fs::read_dir(path)
.unwrap()
.filter_map(Result::ok)
.collect::<Vec<_>>();
egui::collapsing_header::CollapsingState::load_with_default_open(
ui.ctx(),
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
fn load_templates(&mut self) -> Result<(), FsError> {
let path = Path::new("templates");
if !FILESYSTEM.exists(path) {
FILESYSTEM.mkdir(path)?;
fn load_templates(&mut self) -> std::io::Result<()> {
let templates_folder = PROJECT_FOLDER.join("templates");
if !templates_folder.exists() {
std::fs::create_dir_all(&templates_folder)?;
}
let mut templates = Vec::new();
for entry in FILESYSTEM.lsfiles(path)? {
match FILESYSTEM.read::<Template>(&entry) {
for entry in std::fs::read_dir(&templates_folder).unwrap() {
let path = entry.unwrap().path();
match Template::load(&Id::from_path(&path)) {
Ok(t) => templates.push(t),
Err(err) => eprintln!("Could not parse file {entry:?}: {err}"),
Err(err) => eprintln!("Could not parse file {path:?}: {err}"),
}
}
self.templates = templates;
@@ -297,16 +339,17 @@ impl Explorer {
}
// load objects from the objects folder
fn load_objects(&mut self) -> Result<(), FsError> {
let path = Path::new("objects");
if !FILESYSTEM.exists(path) {
FILESYSTEM.mkdir(path)?;
fn load_objects(&mut self) -> std::io::Result<()> {
let objects_folder = PROJECT_FOLDER.join("objects");
if !objects_folder.exists() {
std::fs::create_dir_all(&objects_folder)?;
}
let mut objects = Vec::new();
for entry in FILESYSTEM.lsfiles(path)? {
match FILESYSTEM.read::<ObjectInstance>(&entry) {
for entry in std::fs::read_dir(&objects_folder).unwrap() {
let path = entry.unwrap().path();
match ObjectInstance::load(&Id::from_path(&path)) {
Ok(o) => objects.push(o),
Err(err) => eprintln!("Could not parse file {entry:?}: {err}"),
Err(err) => eprintln!("Could not parse file {path:?}: {err}"),
}
}
self.objects = objects;
@@ -315,15 +358,16 @@ impl Explorer {
}
// load notes from the notes folder
fn load_notes(&mut self) -> Result<(), FsError> {
let path = Path::new("notes");
if !FILESYSTEM.exists(path) {
FILESYSTEM.mkdir(path)?;
fn load_notes(&mut self) -> std::io::Result<()> {
let notes_folder = PROJECT_FOLDER.join("notes");
if !notes_folder.exists() {
std::fs::create_dir_all(&notes_folder)?;
}
let mut notes = Vec::new();
for entry in FILESYSTEM.lsfiles(path)? {
match FILESYSTEM.read::<Note>(&entry) {
for entry in std::fs::read_dir(&notes_folder).unwrap() {
let path = entry.unwrap().path();
match Note::load(&Id::from_path(&path)) {
Ok(note) => notes.push(note),
Err(err) => eprintln!("Could not parse file {path:?}: {err}"),
}
@@ -335,17 +379,25 @@ impl Explorer {
}
// load documents from the documents folder
fn load_documents(&mut self) -> Result<(), FsError> {
let path = Path::new("documents");
if !FILESYSTEM.exists(path) {
FILESYSTEM.mkdir(path)?;
fn load_documents(&mut self) -> std::io::Result<()> {
let documents_folder = PROJECT_FOLDER.join("documents");
if !documents_folder.exists() {
std::fs::create_dir_all(&documents_folder)?;
}
let mut documents = Vec::new();
for entry in FILESYSTEM.lsfiles(path)? {
match FILESYSTEM.read::<ContentSection>(&entry) {
Ok(document) => documents.push(MainEditor::open(document)),
Err(err) => eprintln!("Could not parse file {entry:?}: {err}"),
for entry in std::fs::read_dir(&documents_folder).unwrap() {
let path = entry.unwrap().path();
// 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:?}");
}
}
}
@@ -354,7 +406,7 @@ impl Explorer {
Ok(())
}
fn load_tags(&mut self) -> Result<(), FsError> {
fn load_tags(&mut self) -> std::io::Result<()> {
self.tags = Tag::load_all();
Ok(())
}
+118 -84
View File
@@ -1,15 +1,11 @@
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use std::{
collections::HashMap,
io,
fmt,
ops::{Deref, DerefMut},
path::{Path, PathBuf},
sync::LazyLock,
};
use serde::{Serialize, de::DeserializeOwned};
#[cfg(feature = "native")]
use crate::PROJECT_FOLDER;
use crate::filesystem::native::NativeFileSystem;
use crate::FILESYSTEM;
#[cfg(feature = "native")]
pub mod native;
@@ -17,87 +13,125 @@ pub mod native;
#[cfg(feature = "web")]
pub mod web;
pub static FILESYSTEM: LazyLock<NativeFileSystem> = LazyLock::new(|| {
#[cfg(feature = "native")]
return NativeFileSystem::new(PROJECT_FOLDER.clone());
#[cfg(feature = "web")]
return Box::new(web::WebFileSystem::new());
});
pub trait LegacyFileSystem {
fn read<T: DeserializeOwned>(&self, path: &Path) -> Result<T, FsError>;
fn read_bytes(&self, path: &Path) -> Result<Vec<u8>, FsError>;
fn write<T: Serialize>(&self, path: &Path, data: T) -> 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 rename(&self, path: &Path, new_path: &Path) -> Result<(), FsError>;
#[allow(unused)]
fn exists(&self, path: &Path) -> bool;
fn config_path(&self) -> PathBuf;
}
// ────────────────────────────────────────────────────────────────
// Custom error type
// ────────────────────────────────────────────────────────────────
#[derive(Debug)]
pub enum FsError {
Io(io::Error),
Serde(serde_json::Error),
}
impl std::fmt::Display for FsError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
FsError::Io(e) => write!(f, "IO error: {e}"),
FsError::Serde(e) => write!(f, "Serialization error: {e}"),
}
}
}
impl std::error::Error for FsError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
FsError::Io(e) => Some(e),
FsError::Serde(e) => Some(e),
}
}
}
// Convert the two underlying error types into our own
impl From<io::Error> for FsError {
fn from(err: io::Error) -> Self {
FsError::Io(err)
}
}
impl From<serde_json::Error> for FsError {
fn from(err: serde_json::Error) -> Self {
FsError::Serde(err)
}
}
#[allow(dead_code)]
#[derive(Clone, Debug, PartialEq, PartialOrd, Ord, Eq, Hash, Serialize, Deserialize)]
pub struct Id(String);
#[allow(dead_code)]
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 load<T: DeserializeOwned>(&self, id: Id) -> Result<T, FsError>;
fn save<T: Serialize>(&self, id: Id, data: T) -> Result<(), FsError>;
fn mkdir(&self, path: Path) -> Result<(), FsError>;
fn exists(&self, path: Path) -> bool;
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>;
}
#[allow(dead_code)]
pub struct Index {
file_cache: HashMap<Id, PathBuf>,
project_root: Directory,
pub struct FileBorrow<T: DeserializeOwned + Serialize> {
pub id: Id,
pub file: T,
}
#[allow(dead_code)]
pub struct Directory {
name: String,
id: Id,
children: HashMap<Id, Directory>,
files: Vec<Id>,
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();
}
}
+294 -83
View File
@@ -1,116 +1,327 @@
use std::fs;
use std::io::{ErrorKind, Read};
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 serde::Serialize;
use image::EncodableLayout;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_json::to_string;
use crate::filesystem::{FsError, LegacyFileSystem};
use crate::filesystem::FileSystemError;
use super::FileSystem;
pub struct NativeFileSystem {
project_root: PathBuf,
root: PathBuf,
index: Arc<RwLock<HashMap<super::Id, PathBuf>>>,
}
impl NativeFileSystem {
/// Create a new instance.
pub fn new(root: impl Into<PathBuf>) -> Self {
Self {
project_root: root.into(),
/// 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);
}
}
/// Resolve the user supplied *relative* path against the project root.
#[inline]
fn full_path(&self, path: &Path) -> PathBuf {
self.project_root.join(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 LegacyFileSystem for NativeFileSystem {
fn read<T: DeserializeOwned>(&self, path: &Path) -> Result<T, FsError> {
let full_path = self.full_path(path);
let file = fs::File::open(full_path).map_err(FsError::Io)?;
serde_json::from_reader(file).map_err(FsError::Serde)
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 read_bytes(&self, path: &Path) -> Result<Vec<u8>, FsError> {
let full_path = self.full_path(path);
let mut contents = Vec::new();
fs::File::open(full_path)?.read_to_end(&mut contents)?;
Ok(contents)
}
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)
};
fn write<T: Serialize>(&self, path: &Path, data: T) -> Result<(), FsError> {
let full_path = self.full_path(path);
// Ensure the parent directory exists.
if let Some(parent) = full_path.parent() {
fs::create_dir_all(parent)?;
if !dir.exists() {
fs::create_dir_all(&dir).map_err(|e| {
FileSystemError::DirectoryNotFound(dir.to_path_buf(), e.to_string())
})?;
}
let file = fs::File::create(full_path)?;
serde_json::to_writer(file, &data).map_err(FsError::Serde)
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 delete(&self, path: &Path) -> Result<(), FsError> {
let full_path = self.full_path(path);
match fs::remove_file(&full_path) {
Ok(()) => Ok(()),
Err(e) if e.kind() == ErrorKind::IsADirectory => {
// Remove a directory tree.
fs::remove_dir_all(full_path).map_err(FsError::Io)
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(())
}
Err(e) => Err(FsError::Io(e)),
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 mkdir(&self, path: &Path) -> Result<(), FsError> {
let full_path = self.full_path(path);
fs::create_dir_all(full_path).map_err(FsError::Io)
fn exists(&self, id: &super::Id) -> bool {
self.find_path_by_id(id).is_some()
}
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();
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(),
));
}
};
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 {
let full_path = self.full_path(path);
full_path.exists()
}
fn rename(&self, path: &Path, other: &Path) -> Result<(), FsError> {
let full_path = self.full_path(path);
let full_other = self.full_path(other);
fs::rename(full_path, full_other).map_err(FsError::Io)
}
fn config_path(&self) -> PathBuf {
match std::env::var("HOME") {
Ok(path) => PathBuf::from(path + "/.config/worldcoder/settings.json"),
Err(_) => {
eprintln!(
"XDG_CONFIG_HOME not set, using default path of ~/.config/worldcoder/settings.json"
);
"~/.config/worldcoder/settings.json".into()
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
-1
View File
@@ -440,7 +440,6 @@ pub struct AIInput {
pub system_prompt: String,
pub user_prompt: String,
pub previous_content: String,
#[allow(unused)]
pub structure: Option<String>,
}
+9 -3
View File
@@ -10,16 +10,17 @@ mod explorer;
#[cfg(feature = "llm")]
mod llm_integration;
mod util;
mod filesystem;
mod util;
use crate::{
editors::{
asset_editor::Asset, content_editor, note_editor, object_editor::ObjectInstance,
settings_editor::ProjectSettings, tags::Tag, template_editor::Template,
},
explorer::Explorer,
filesystem::{FileSystem, native::NativeFileSystem},
};
static VERSION: &str = "0.1.0";
@@ -29,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 {
@@ -48,6 +52,7 @@ pub struct Interface {
editor: content_editor::MainEditor,
explorer: Explorer,
project: ProjectSettings,
filesystem: NativeFileSystem,
}
impl eframe::App for Interface {
@@ -93,6 +98,7 @@ impl Interface {
editor: content_editor::MainEditor::new(),
explorer: Explorer::new(),
project: ProjectSettings::load(),
filesystem: NativeFileSystem::new(&*PROJECT_FOLDER),
}
}
@@ -206,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
View File
+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());
+6
View File
@@ -0,0 +1,6 @@
{
"llm_api_uri": "http://localhost:1234",
"llm_api_key": "",
"ai_enabled": true,
"dark_theme": true
}