diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-03-18 11:19:51 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-03-18 11:19:51 +0800 |
| commit | 2372495e1a0acb9ffead7651d8ed36a3bb98a15b (patch) | |
| tree | 5f330cdf1616b1e56a7b85b2b2530cdf1422ed54 /systems/workspace/src | |
| parent | 6f8906f06f3efd009275dc23f861f5aaba76ce72 (diff) | |
Add new protocol crate with basic types and operations
Diffstat (limited to 'systems/workspace/src')
| -rw-r--r-- | systems/workspace/src/workspace/manager.rs | 1 | ||||
| -rw-r--r-- | systems/workspace/src/workspace/manager/sheet_state.rs | 73 |
2 files changed, 74 insertions, 0 deletions
diff --git a/systems/workspace/src/workspace/manager.rs b/systems/workspace/src/workspace/manager.rs index adee9b7..58eb409 100644 --- a/systems/workspace/src/workspace/manager.rs +++ b/systems/workspace/src/workspace/manager.rs @@ -4,6 +4,7 @@ use constants::workspace::files::workspace_file_config; use framework::space::Space; pub mod id_aliases; +pub mod sheet_state; pub struct WorkspaceManager { space: Space<Workspace>, diff --git a/systems/workspace/src/workspace/manager/sheet_state.rs b/systems/workspace/src/workspace/manager/sheet_state.rs new file mode 100644 index 0000000..5603adf --- /dev/null +++ b/systems/workspace/src/workspace/manager/sheet_state.rs @@ -0,0 +1,73 @@ +use std::path::PathBuf; + +use crate::workspace::manager::WorkspaceManager; +use asset_system::asset::ReadOnlyAsset; +use constants::workspace::files::{workspace_file_current_sheet, workspace_file_sheet}; +use framework::space::error::SpaceError; +use just_fmt::snake_case; +use sheet_system::sheet::{Sheet, SheetData}; + +impl WorkspaceManager { + /// Read the name of the currently active Sheet + pub async fn using_sheet_name(&self) -> Result<Option<String>, SpaceError> { + match self + .space + .read_to_string(workspace_file_current_sheet()) + .await + { + Ok(s) => Ok(Some(s.trim().to_string())), + Err(SpaceError::Io(io_error)) => match io_error.kind() { + std::io::ErrorKind::NotFound => Ok(None), + _ => Err(SpaceError::Io(io_error)), + }, + Err(e) => Err(e), + } + } + + /// Set the name of the currently active Sheet + pub async fn edit_using_sheet_name(&self, name: &str) -> Result<(), SpaceError> { + self.space + .write(workspace_file_current_sheet(), name.as_bytes()) + .await + } + + /// Read a sheet from the workspace space by name + /// + /// Simple read of Sheet data, no disk write operations involved + pub async fn read_sheet(&self, sheet_name: &str) -> Option<Sheet> { + let sheet_name = snake_case!(sheet_name); + let sheet_path = self.get_sheet_path(&sheet_name); + + let mut sheet_data = SheetData::empty(); + if sheet_path.exists() { + // If reading fails, treat it as if the sheet does not exist and return `None` + sheet_data.full_read(sheet_path).await.ok()?; + return Some(sheet_data.pack(sheet_name)); + } else { + None + } + } + + /// Get a resource pointing to local Sheet data by name + /// + /// Can be used to load content, edit, and transactionally write + pub fn get_sheet_data_asset(&self, sheet_name: &str) -> Option<ReadOnlyAsset<SheetData>> { + let sheet_name = snake_case!(sheet_name); + let sheet_path = self.get_sheet_path(&sheet_name); + if sheet_path.exists() { + return Some(sheet_path.into()); + } + None + } + + /// Get the local filesystem path for a sheet by name + pub fn get_sheet_path(&self, sheet_name: impl AsRef<str>) -> PathBuf { + let sheet_name = sheet_name.as_ref(); + self.space + .local_path(workspace_file_sheet(&sheet_name)) + // The `local_path` only produces path formatting errors. + // If the path cannot be guaranteed to be correct, + // execution should not continue, so we unwrap() + .unwrap() + } +} |
