From 748c8a3353df887ee4b01e0e1327aa95c1c7225a Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Fri, 27 Feb 2026 06:16:23 +0800 Subject: Add remote flag to IndexSource and parsing support --- systems/sheet/src/index_source.rs | 72 +++++++++++++++++++++++++++++++++------ 1 file changed, 61 insertions(+), 11 deletions(-) (limited to 'systems/sheet/src/index_source.rs') diff --git a/systems/sheet/src/index_source.rs b/systems/sheet/src/index_source.rs index b22f5a6..e322670 100644 --- a/systems/sheet/src/index_source.rs +++ b/systems/sheet/src/index_source.rs @@ -4,6 +4,8 @@ use serde::{Deserialize, Serialize}; /// Points to a unique resource address in Vault #[derive(Debug, Clone, Copy, Serialize, Deserialize)] pub struct IndexSource { + remote: bool, + /// The index ID of the resource id: u32, @@ -15,8 +17,31 @@ pub struct IndexSource { impl IndexSource { /// Create IndexSource - pub fn new(id: u32, ver: u16) -> Self { - IndexSource { id, ver } + pub fn new(remote: bool, id: u32, ver: u16) -> Self { + IndexSource { remote, id, ver } + } + + /// Create IndexSource To Local Namespace + pub fn new_local(id: u32, ver: u16) -> Self { + IndexSource { + remote: false, + id, + ver, + } + } + + /// Create IndexSource To Remote Namespace + pub fn new_remote(id: u32, ver: u16) -> Self { + IndexSource { + remote: true, + id, + ver, + } + } + + /// Check if the IndexSource points to a remote namespace + pub fn is_remote(&self) -> bool { + self.remote } /// Get index ID from IndexSource @@ -34,7 +59,7 @@ impl IndexSource { impl PartialEq for IndexSource { fn eq(&self, other: &Self) -> bool { - &self.id == &other.id && &self.ver == &other.ver + &self.remote == &other.remote && &self.id == &other.id && &self.ver == &other.ver } } @@ -44,6 +69,7 @@ impl Eq for IndexSource {} impl std::hash::Hash for IndexSource { fn hash(&self, state: &mut H) { + self.remote.hash(state); self.id.hash(state); self.ver.hash(state); } @@ -55,9 +81,13 @@ impl<'a> TryFrom<&'a str> for IndexSource { type Error = &'static str; fn try_from(value: &'a str) -> Result { - let parts: Vec<&str> = value.split('/').collect(); + let trimmed = value.trim(); + let remote = !trimmed.starts_with('~'); + let value_without_tilde = if remote { trimmed } else { &trimmed[1..] }; + + let parts: Vec<&str> = value_without_tilde.split('/').collect(); if parts.len() != 2 { - return Err("Invalid format: expected 'id/version'"); + return Err("Invalid format: expected '[~]id/version'"); } let id_str = parts[0].trim(); @@ -74,9 +104,7 @@ impl<'a> TryFrom<&'a str> for IndexSource { .parse::() .map_err(|_| "Version must be a valid u16")?; - // Check for overflow (though parsing already validates range) - // Additional bounds checks can be added here if needed - Ok(Self { id, ver }) + Ok(Self { remote, id, ver }) } } @@ -88,9 +116,9 @@ impl TryFrom for IndexSource { } } -impl From for (u32, u16) { +impl From for (bool, u32, u16) { fn from(src: IndexSource) -> Self { - (src.id, src.ver) + (src.remote, src.id, src.ver) } } @@ -105,12 +133,34 @@ impl IndexSource { pub fn set_version(&mut self, version: u16) { self.ver = version; } + + /// Set the remote flag of IndexSource + pub fn set_is_remote(&mut self, remote: bool) { + self.remote = remote; + } + + /// Convert IndexSource to local namespace + pub fn to_local(&mut self) { + self.remote = false; + } + + /// Convert IndexSource to remote namespace + pub fn to_remote(&mut self) { + self.remote = true; + } } // Implement Display for IndexSourceBuf and IndexSource impl std::fmt::Display for IndexSource { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}/{}", self.id.to_string(), self.ver.to_string()) + let local_symbol = if self.remote { "" } else { "~" }; + write!( + f, + "{}{}/{}", + local_symbol, + self.id.to_string(), + self.ver.to_string() + ) } } -- cgit