summaryrefslogtreecommitdiff
path: root/rola-vcs/src/abstracts
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-05-22 22:10:19 +0800
committer魏曹先生 <1992414357@qq.com>2026-06-17 21:02:05 +0800
commit5a5a07c7fad31641d032a743e4e87ffb58ade17d (patch)
treeb6fbd33e8de82e5f9d9e6b99e3cb2102e47fe3ee /rola-vcs/src/abstracts
Initial commitmain
Diffstat (limited to 'rola-vcs/src/abstracts')
-rw-r--r--rola-vcs/src/abstracts/dir_pointer.rs88
1 files changed, 88 insertions, 0 deletions
diff --git a/rola-vcs/src/abstracts/dir_pointer.rs b/rola-vcs/src/abstracts/dir_pointer.rs
new file mode 100644
index 0000000..0e59960
--- /dev/null
+++ b/rola-vcs/src/abstracts/dir_pointer.rs
@@ -0,0 +1,88 @@
+use std::{
+ borrow::Borrow,
+ ops::{Deref, DerefMut},
+ path::PathBuf,
+};
+
+/// Directory Pointer Data
+pub trait DirPtrData {
+ /// Fix the given path
+ ///
+ /// Returns Some(path): use the fixed path
+ /// Returns None: path cannot be fixed, this pointer is invalid
+ #[doc(hidden)]
+ fn fix(raw_path: PathBuf) -> Option<PathBuf>;
+}
+
+#[derive(Debug, Default, Clone)]
+pub struct DirPtr<Data: DirPtrData> {
+ /// Whether the current directory pointer is valid
+ valid: bool,
+
+ /// Data for the directory pointer
+ data: Data,
+
+ /// Path to the directory
+ path: PathBuf,
+}
+
+impl<Data: DirPtrData> DirPtr<Data> {
+ /// Get a reference to the directory pointer's path
+ pub fn path_ref(&self) -> &PathBuf {
+ &self.path
+ }
+
+ /// Get the directory pointer's path
+ pub fn path(&self) -> PathBuf {
+ self.path.clone()
+ }
+
+ /// Returns whether the directory pointer is valid
+ pub fn is_valid(&self) -> bool {
+ self.valid
+ }
+}
+
+impl<Data: DirPtrData + Default> DirPtr<Data> {
+ /// Create a new directory pointer with the given path and default data
+ pub fn new(path: impl Into<PathBuf>) -> Self {
+ let path = path.into();
+ let fixed = Data::fix(path.clone());
+ Self {
+ valid: fixed.is_some(),
+ data: Data::default(),
+ path: fixed.unwrap_or(path),
+ }
+ }
+}
+
+impl<Data: DirPtrData> AsRef<DirPtr<Data>> for DirPtr<Data> {
+ fn as_ref(&self) -> &DirPtr<Data> {
+ self
+ }
+}
+
+impl<Data: DirPtrData> Deref for DirPtr<Data> {
+ type Target = Data;
+
+ fn deref(&self) -> &Self::Target {
+ &self.data
+ }
+}
+
+impl<Data: DirPtrData> DerefMut for DirPtr<Data> {
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ &mut self.data
+ }
+}
+
+impl<Data: DirPtrData> Borrow<Data> for DirPtr<Data> {
+ fn borrow(&self) -> &Data {
+ &self.data
+ }
+}
+
+/// Create a new directory pointer with the given path and default data
+pub fn dir_ptr<Data: DirPtrData + Default>(path: impl Into<PathBuf>) -> DirPtr<Data> {
+ DirPtr::new(path)
+}