summaryrefslogtreecommitdiff
path: root/crates/env
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2025-09-20 16:29:22 +0800
committer魏曹先生 <1992414357@qq.com>2025-09-20 16:29:22 +0800
commitd00835377141b1e6089a7222a8ecc6af2a3613de (patch)
tree93ed9ea3a7968add33c019d86e033b2327f2f6f4 /crates/env
parent89a204dc2a52f26ca0352be0d7708b0aa5e0a6cc (diff)
Add env crates
Diffstat (limited to 'crates/env')
-rw-r--r--crates/env/Cargo.toml11
-rw-r--r--crates/env/src/current.rs73
-rw-r--r--crates/env/src/lib.rs6
-rw-r--r--crates/env/src/local.rs2
-rw-r--r--crates/env/src/local/config.rs25
-rw-r--r--crates/env/src/local/manage.rs1
-rw-r--r--crates/env/src/member.rs55
-rw-r--r--crates/env/src/vault.rs2
-rw-r--r--crates/env/src/vault/config.rs34
-rw-r--r--crates/env/src/vault/manage.rs1
10 files changed, 210 insertions, 0 deletions
diff --git a/crates/env/Cargo.toml b/crates/env/Cargo.toml
new file mode 100644
index 0000000..a7d1ca5
--- /dev/null
+++ b/crates/env/Cargo.toml
@@ -0,0 +1,11 @@
+[package]
+name = "env"
+edition = "2024"
+version.workspace = true
+
+[dependencies]
+cfg_file = { path = "../utils/cfg_file", features = ["default"] }
+string_proc = { path = "../utils/string_proc" }
+
+uuid = { version = "1.18.1", features = ["v4", "serde"] }
+serde = { version = "1.0.219", features = ["derive"] }
diff --git a/crates/env/src/current.rs b/crates/env/src/current.rs
new file mode 100644
index 0000000..efa4117
--- /dev/null
+++ b/crates/env/src/current.rs
@@ -0,0 +1,73 @@
+use crate::constants::*;
+use std::io::{self, Error};
+use std::{env::set_current_dir, path::PathBuf};
+
+/// Find the nearest vault or local workspace and correct the `current_dir` to it
+pub fn correct_current_dir() -> Result<(), io::Error> {
+ if let Some(local_workspace) = current_local_path() {
+ set_current_dir(local_workspace)?;
+ return Ok(());
+ }
+ if let Some(vault) = current_vault_path() {
+ set_current_dir(vault)?;
+ return Ok(());
+ }
+ Err(Error::new(
+ io::ErrorKind::NotFound,
+ "Could not find any vault or local workspace!",
+ ))
+}
+
+/// Get the nearest Vault directory from `current_dir`
+pub fn current_vault_path() -> Option<PathBuf> {
+ let current_dir = std::env::current_dir().ok()?;
+ find_vault_path(current_dir)
+}
+
+/// Get the nearest local workspace from `current_dir`
+pub fn current_local_path() -> Option<PathBuf> {
+ let current_dir = std::env::current_dir().ok()?;
+ find_local_path(current_dir)
+}
+
+/// Get the nearest Vault directory from the specified path
+pub fn find_vault_path(path: impl Into<PathBuf>) -> Option<PathBuf> {
+ let mut current_path = path.into();
+ let vault_file = SERVER_FILE_VAULT;
+
+ loop {
+ let vault_toml_path = current_path.join(vault_file);
+ if vault_toml_path.exists() {
+ return Some(current_path);
+ }
+
+ if let Some(parent) = current_path.parent() {
+ current_path = parent.to_path_buf();
+ } else {
+ break;
+ }
+ }
+
+ None
+}
+
+/// Get the nearest local workspace from the specified path
+pub fn find_local_path(path: impl Into<PathBuf>) -> Option<PathBuf> {
+ let mut current_path = path.into();
+ let workspace_dir = CLIENT_PATH_WORKSPACE_ROOT;
+
+ loop {
+ let jvc_path = current_path.join(workspace_dir);
+ if jvc_path.exists() {
+ return Some(current_path);
+ }
+
+ if let Some(parent) = current_path.parent() {
+ current_path = parent.to_path_buf();
+ } else {
+ break;
+ }
+ }
+
+ None
+}
diff --git a/crates/env/src/lib.rs b/crates/env/src/lib.rs
new file mode 100644
index 0000000..5e755d0
--- /dev/null
+++ b/crates/env/src/lib.rs
@@ -0,0 +1,6 @@
+pub mod constants;
+pub mod current;
+pub mod local;
+#[allow(dead_code)]
+pub mod member;
+pub mod vault;
diff --git a/crates/env/src/local.rs b/crates/env/src/local.rs
new file mode 100644
index 0000000..72092c2
--- /dev/null
+++ b/crates/env/src/local.rs
@@ -0,0 +1,2 @@
+pub mod config;
+pub mod manage;
diff --git a/crates/env/src/local/config.rs b/crates/env/src/local/config.rs
new file mode 100644
index 0000000..dcaf4df
--- /dev/null
+++ b/crates/env/src/local/config.rs
@@ -0,0 +1,25 @@
+use cfg_file::ConfigFile;
+use serde::{Deserialize, Serialize};
+use std::net::SocketAddr;
+
+use crate::constants::CLIENT_FILE_WORKSPACE;
+use crate::constants::PORT;
+
+#[derive(Serialize, Deserialize, ConfigFile)]
+#[cfg_file(path = CLIENT_FILE_WORKSPACE)]
+pub struct LocalConfig {
+ target: SocketAddr,
+}
+
+impl Default for LocalConfig {
+ fn default() -> Self {
+ Self {
+ target: SocketAddr::V4(std::net::SocketAddrV4::new(
+ std::net::Ipv4Addr::new(127, 0, 0, 1),
+ PORT,
+ )),
+ }
+ }
+}
+
+impl LocalConfig {}
diff --git a/crates/env/src/local/manage.rs b/crates/env/src/local/manage.rs
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/crates/env/src/local/manage.rs
@@ -0,0 +1 @@
+
diff --git a/crates/env/src/member.rs b/crates/env/src/member.rs
new file mode 100644
index 0000000..34abe0d
--- /dev/null
+++ b/crates/env/src/member.rs
@@ -0,0 +1,55 @@
+use cfg_file::ConfigFile;
+use serde::{Deserialize, Serialize};
+use string_proc::camel_case;
+use uuid::Uuid;
+
+#[derive(Debug, Eq, Clone, ConfigFile, Serialize, Deserialize)]
+pub struct Member {
+ id: String,
+ uuid: Uuid,
+}
+
+impl Default for Member {
+ fn default() -> Self {
+ Self::new("default_user")
+ }
+}
+
+impl PartialEq for Member {
+ fn eq(&self, other: &Self) -> bool {
+ self.uuid == other.uuid
+ }
+}
+
+impl std::fmt::Display for Member {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(f, "{}", self.id)
+ }
+}
+
+impl std::convert::AsRef<str> for Member {
+ fn as_ref(&self) -> &str {
+ &self.id
+ }
+}
+
+impl Member {
+ /// Create member struct by id
+ pub fn new(new_id: impl Into<String>) -> Self {
+ let uuid = Uuid::new_v4();
+ Self {
+ id: camel_case!(new_id.into()),
+ uuid,
+ }
+ }
+
+ /// Get member id
+ pub fn id(&self) -> String {
+ self.id.clone()
+ }
+
+ /// Get member uuid
+ pub fn uuid(&self) -> Uuid {
+ self.uuid
+ }
+}
diff --git a/crates/env/src/vault.rs b/crates/env/src/vault.rs
new file mode 100644
index 0000000..72092c2
--- /dev/null
+++ b/crates/env/src/vault.rs
@@ -0,0 +1,2 @@
+pub mod config;
+pub mod manage;
diff --git a/crates/env/src/vault/config.rs b/crates/env/src/vault/config.rs
new file mode 100644
index 0000000..dc815c2
--- /dev/null
+++ b/crates/env/src/vault/config.rs
@@ -0,0 +1,34 @@
+use cfg_file::ConfigFile;
+use serde::{Deserialize, Serialize};
+use uuid::Uuid;
+
+use crate::constants::SERVER_FILE_VAULT;
+use crate::member::Member;
+
+#[derive(Default, Serialize, Deserialize, ConfigFile)]
+#[cfg_file(path = SERVER_FILE_VAULT)]
+pub struct VaultConfig {
+ vault_name: String,
+ vault_admin_list: Vec<Uuid>,
+}
+
+impl VaultConfig {
+ // Change name of the vault.
+ pub fn change_name(&mut self, name: impl Into<String>) {
+ self.vault_name = name.into()
+ }
+
+ // Add admin
+ pub fn add_admin(&mut self, member: &Member) {
+ let uuid = member.uuid();
+ if !self.vault_admin_list.contains(&uuid) {
+ self.vault_admin_list.push(uuid);
+ }
+ }
+
+ // Remove admin
+ pub fn remove_admin(&mut self, member: &Member) {
+ let uuid = member.uuid();
+ self.vault_admin_list.retain(|&x| x != uuid);
+ }
+}
diff --git a/crates/env/src/vault/manage.rs b/crates/env/src/vault/manage.rs
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/crates/env/src/vault/manage.rs
@@ -0,0 +1 @@
+