From 4effbd209edf96637d7da2b7d29ea1a6de3c637a Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Wed, 11 Mar 2026 17:38:08 +0800 Subject: Add config system and space macro with workspace dependencies --- systems/_framework/src/space.rs | 61 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 7 deletions(-) (limited to 'systems/_framework/src/space.rs') diff --git a/systems/_framework/src/space.rs b/systems/_framework/src/space.rs index 47d3c0d..c57fa26 100644 --- a/systems/_framework/src/space.rs +++ b/systems/_framework/src/space.rs @@ -10,7 +10,7 @@ use std::{ pub mod error; -pub struct Space { +pub struct Space { path_format_cfg: PathFormatConfig, content: T, @@ -18,7 +18,7 @@ pub struct Space { current_dir: Option, } -impl Space { +impl Space { /// Create a new `Space` instance with the given content. pub fn new(content: T) -> Self { Space { @@ -32,6 +32,49 @@ impl Space { } } + /// Initialize a space at the given path. + /// + /// Checks if a space exists at the given path. If not, creates a new space + /// by calling `T::create_space()` at that path. + pub async fn init(&self, path: impl AsRef) -> Result<(), SpaceError> { + let path = path.as_ref(); + let pattern = T::get_pattern(); + + if !find_space_root_with(path.to_path_buf(), pattern).is_ok() { + T::create_space(path).await?; + } + Ok(()) + } + + /// Create a new space at the given path with the specified name. + /// + /// The full path is constructed as `path/name`. Checks if a space already + /// exists at that location. If not, creates a new space by calling + /// `T::create_space()` at that path. + pub async fn create(&self, path: impl AsRef, name: &str) -> Result<(), SpaceError> { + let full_path = path.as_ref().join(name); + self.init(full_path).await + } + + /// Initialize a space in the current directory. + /// + /// Checks if a space exists in the current directory. If not, creates a new space + /// by calling `T::create_space()` at the current directory. + pub async fn init_here(&self) -> Result<(), SpaceError> { + let current_dir = self.current_dir()?; + self.init(current_dir).await + } + + /// Create a new space in the current directory with the specified name. + /// + /// The full path is constructed as `current_dir/name`. Checks if a space already + /// exists at that location. If not, creates a new space by calling + /// `T::create_space()` at that path. + pub async fn create_here(&self, name: &str) -> Result<(), SpaceError> { + let current_dir = self.current_dir()?; + self.create(current_dir, name).await + } + /// Consume the `Space`, returning the inner content. pub fn into_inner(self) -> T { self.content @@ -101,7 +144,7 @@ impl Space { } } -impl Space { +impl Space { /// Convert a relative path to an absolute path within the space. /// /// The path is formatted according to the space's path format configuration. @@ -309,27 +352,31 @@ impl Space { } } -impl From for Space { +impl From for Space { fn from(content: T) -> Self { Space::::new(content) } } -impl AsRef for Space { +impl AsRef for Space { fn as_ref(&self) -> &T { &self.content } } -impl Deref for Space { +impl Deref for Space { type Target = T; fn deref(&self) -> &Self::Target { &self.as_ref() } } -pub trait SpaceContent { +pub trait SpaceRoot: Sized { + /// Get the pattern used to identify the space root fn get_pattern() -> SpaceRootFindPattern; + + /// Given a non-space directory, implement logic to make it a space-recognizable directory + fn create_space(path: &Path) -> impl Future> + Send; } pub enum SpaceRootFindPattern { -- cgit