From 2372495e1a0acb9ffead7651d8ed36a3bb98a15b Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Wed, 18 Mar 2026 11:19:51 +0800 Subject: Add new protocol crate with basic types and operations --- protocol/src/context.rs | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 protocol/src/context.rs (limited to 'protocol/src/context.rs') diff --git a/protocol/src/context.rs b/protocol/src/context.rs new file mode 100644 index 0000000..4fdddb9 --- /dev/null +++ b/protocol/src/context.rs @@ -0,0 +1,71 @@ +use framework::space::{Space, SpaceRoot}; + +pub struct ProtocolContext +where + User: SpaceRoot, + Server: SpaceRoot, +{ + /// Current context's Vault + pub remote: Option>, + + /// Current context's Workspace + pub user: Option>, +} + +impl ProtocolContext +where + User: SpaceRoot, + Server: SpaceRoot, +{ + /// Create a new local context with a user + pub fn new_local(user: Space) -> Self { + Self { + remote: None, + user: Some(user), + } + } + + /// Create a new remote context with a server + pub fn new_remote(server: Space) -> Self { + Self { + remote: Some(server), + user: None, + } + } +} + +impl ProtocolContext +where + User: SpaceRoot, + Server: SpaceRoot, +{ + /// Check if the context is remote (has a server) + pub fn is_remote(&self) -> bool { + self.remote.is_some() + } + + /// Check if the context is local (has a user) + pub fn is_local(&self) -> bool { + self.user.is_some() + } + + /// Get the remote vault if it exists + pub fn remote(&self) -> Option<&Space> { + self.remote.as_ref() + } + + /// Get the local workspace if it exists + pub fn local(&self) -> Option<&Space> { + self.user.as_ref() + } + + /// Unwrap the local workspace, panics if not local + pub fn unwrap_local(&self) -> &Space { + self.user.as_ref().expect("Not a local context") + } + + /// Unwrap the remote vault, panics if not remote + pub fn unwrap_remote(&self) -> &Space { + self.remote.as_ref().expect("Not a remote context") + } +} -- cgit