summaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2025-10-06 17:21:49 +0800
committerGitHub <noreply@github.com>2025-10-06 17:21:49 +0800
commitc1d862d6df58173c24604e4dda33db8ce3be3ad7 (patch)
tree171ecf07053748ce7493efaeff5eefbc8645b2cb /crates
parent66a32a8584cf34a881ec45f47d379fb3b1637033 (diff)
parente12f167de8e16baa78c86b09eab75201281d3f95 (diff)
Merge pull request #19 from JustEnoughVCS/jvcs_dev
Jvcs dev
Diffstat (limited to 'crates')
-rw-r--r--crates/system_action/action_macros/src/lib.rs34
-rw-r--r--crates/system_action/src/action.rs21
-rw-r--r--crates/utils/tcp_connection/src/error.rs6
-rw-r--r--crates/vcs_data/Cargo.toml4
4 files changed, 60 insertions, 5 deletions
diff --git a/crates/system_action/action_macros/src/lib.rs b/crates/system_action/action_macros/src/lib.rs
index 5d274ad..a7de9b6 100644
--- a/crates/system_action/action_macros/src/lib.rs
+++ b/crates/system_action/action_macros/src/lib.rs
@@ -55,10 +55,38 @@ fn generate_action_struct(input_fn: ItemFn, _is_local: bool) -> proc_macro2::Tok
}
}
+ impl #struct_name {
+ #fn_vis fn register_to_pool(pool: &mut action_system::action_pool::ActionPool) {
+ pool.register::<#struct_name, #arg_type, #return_type>();
+ }
+
+ #fn_vis async fn process_at_pool<'a>(
+ pool: &'a action_system::action_pool::ActionPool,
+ ctx: action_system::action::ActionContext,
+ #arg_param_name: #arg_type
+ ) -> Result<#return_type, tcp_connection::error::TcpTargetError> {
+ pool.process::<#arg_type, #return_type>(
+ Box::leak(string_proc::snake_case!(stringify!(#action_name_ident)).into_boxed_str()),
+ ctx,
+ #arg_param_name
+ ).await
+ }
+ }
+
+ #[allow(dead_code)]
#[deprecated = "This function is used by #[action_gen] as a template."]
- #[doc = " This function is used by #[action_gen] as a template to generate the struct. "]
- #[doc = " It is forbidden to call it anywhere."]
- #[doc = " and call it using the function name."]
+ #[doc = "Template function for #[[action_gen]] - do not call directly."]
+ #[doc = "Use the generated struct instead."]
+ #[doc = ""]
+ #[doc = "Register the action to the pool."]
+ #[doc = "```rust"]
+ #[doc = "YourActionPascalName::register_to_pool(&mut pool);"]
+ #[doc = "```"]
+ #[doc = ""]
+ #[doc = "Process the action at the pool."]
+ #[doc = "```rust"]
+ #[doc = "let result = YourActionPascalName::process_at_pool(&pool, ctx, arg).await?;"]
+ #[doc = "```"]
#fn_vis #fn_sig #fn_block
}
}
diff --git a/crates/system_action/src/action.rs b/crates/system_action/src/action.rs
index 14f1148..562a142 100644
--- a/crates/system_action/src/action.rs
+++ b/crates/system_action/src/action.rs
@@ -11,13 +11,30 @@ pub trait Action<Args, Return> {
) -> impl std::future::Future<Output = Result<Return, TcpTargetError>> + Send;
}
+#[derive(Default)]
pub struct ActionContext {
// Whether the action is executed locally or remotely
local: bool,
/// The connection instance in the current context,
/// used to interact with the machine on the other end
- instance: ConnectionInstance,
+ instance: Option<ConnectionInstance>,
+}
+
+impl ActionContext {
+ /// Generate local context
+ pub fn local() -> Self {
+ let mut ctx = ActionContext::default();
+ ctx.local = true;
+ ctx
+ }
+
+ /// Generate remote context
+ pub fn remote() -> Self {
+ let mut ctx = ActionContext::default();
+ ctx.local = false;
+ ctx
+ }
}
impl ActionContext {
@@ -32,7 +49,7 @@ impl ActionContext {
}
/// Get the connection instance in the current context
- pub fn instance(&self) -> &ConnectionInstance {
+ pub fn instance(&self) -> &Option<ConnectionInstance> {
&self.instance
}
}
diff --git a/crates/utils/tcp_connection/src/error.rs b/crates/utils/tcp_connection/src/error.rs
index ffcce6f..691e5ee 100644
--- a/crates/utils/tcp_connection/src/error.rs
+++ b/crates/utils/tcp_connection/src/error.rs
@@ -35,6 +35,12 @@ pub enum TcpTargetError {
#[error("Pool already exists: {0}")]
PoolAlreadyExists(String),
+
+ #[error("Not local machine: {0}")]
+ NotLocal(String),
+
+ #[error("Not remote machine: {0}")]
+ NotRemote(String),
}
impl From<io::Error> for TcpTargetError {
diff --git a/crates/vcs_data/Cargo.toml b/crates/vcs_data/Cargo.toml
index 07f1a6a..b3bc4a2 100644
--- a/crates/vcs_data/Cargo.toml
+++ b/crates/vcs_data/Cargo.toml
@@ -4,9 +4,13 @@ edition = "2024"
version.workspace = true
[dependencies]
+
+# Utils
tcp_connection = { path = "../utils/tcp_connection" }
cfg_file = { path = "../utils/cfg_file", features = ["default"] }
string_proc = { path = "../utils/string_proc" }
+
+# Core
action_system = { path = "../system_action" }
# Identity