diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-01-02 23:28:28 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-01-03 00:43:49 +0800 |
| commit | f5b47fcbee422d23bfada5681a98e599918dbe4b (patch) | |
| tree | 27925d2147a52828ba4ca4c33443944849d5458f /crates | |
| parent | 118f24df4024099b6fbdd6d74fca46ae089ac07a (diff) | |
Refactor config loading to read files after format detection
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/utils/cfg_file/src/config.rs | 46 | ||||
| -rw-r--r-- | crates/vcs_actions/src/actions/local_actions.rs | 8 | ||||
| -rw-r--r-- | crates/vcs_data/src/constants.rs | 26 |
3 files changed, 45 insertions, 35 deletions
diff --git a/crates/utils/cfg_file/src/config.rs b/crates/utils/cfg_file/src/config.rs index 8f9edc9..daf5e10 100644 --- a/crates/utils/cfg_file/src/config.rs +++ b/crates/utils/cfg_file/src/config.rs @@ -115,13 +115,7 @@ pub trait ConfigFile: Serialize + for<'a> Deserialize<'a> + Default { )); } - // Open file - let mut file = fs::File::open(&file_path).await?; - - let mut contents = String::new(); - file.read_to_string(&mut contents).await?; - - // Determine file format + // Determine file format first let format = file_path .file_name() .and_then(|name| name.to_str()) @@ -130,16 +124,36 @@ pub trait ConfigFile: Serialize + for<'a> Deserialize<'a> + Default { // Deserialize based on format let result = match format { - ConfigFormat::Yaml => serde_yaml::from_str(&contents) - .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?, - ConfigFormat::Toml => toml::from_str(&contents) - .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?, - ConfigFormat::Ron => ron::from_str(&contents) - .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?, - ConfigFormat::Json => serde_json::from_str(&contents) - .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?, + ConfigFormat::Yaml => { + let mut file = fs::File::open(&file_path).await?; + let mut contents = String::new(); + file.read_to_string(&mut contents).await?; + serde_yaml::from_str(&contents) + .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))? + } + ConfigFormat::Toml => { + let mut file = fs::File::open(&file_path).await?; + let mut contents = String::new(); + file.read_to_string(&mut contents).await?; + toml::from_str(&contents) + .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))? + } + ConfigFormat::Ron => { + let mut file = fs::File::open(&file_path).await?; + let mut contents = String::new(); + file.read_to_string(&mut contents).await?; + ron::from_str(&contents) + .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))? + } + ConfigFormat::Json => { + let mut file = fs::File::open(&file_path).await?; + let mut contents = String::new(); + file.read_to_string(&mut contents).await?; + serde_json::from_str(&contents) + .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))? + } ConfigFormat::Bincode => { - // For Bincode, we need to read the file as bytes + // For Bincode, we need to read the file as bytes directly let bytes = fs::read(&file_path).await?; bincode2::deserialize(&bytes) .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))? diff --git a/crates/vcs_actions/src/actions/local_actions.rs b/crates/vcs_actions/src/actions/local_actions.rs index 8799930..93ef4d5 100644 --- a/crates/vcs_actions/src/actions/local_actions.rs +++ b/crates/vcs_actions/src/actions/local_actions.rs @@ -262,9 +262,7 @@ pub async fn update_to_latest_info_action( )) .await else { - return Err(TcpTargetError::NotFound( - "Latest info not found.".to_string(), - )); + return Err(TcpTargetError::Io("Read latest info failed".to_string())); }; // Collect all local versions @@ -349,9 +347,7 @@ pub async fn update_to_latest_info_action( )) .await else { - return Err(TcpTargetError::NotFound( - "Latest info not found.".to_string(), - )); + return Err(TcpTargetError::Io("Read latest info failed".to_string())); }; // Collect files that need to know the holder diff --git a/crates/vcs_data/src/constants.rs b/crates/vcs_data/src/constants.rs index 6437073..569d0cc 100644 --- a/crates/vcs_data/src/constants.rs +++ b/crates/vcs_data/src/constants.rs @@ -15,18 +15,18 @@ pub const VAULT_HOST_NAME: &str = "host"; pub const SERVER_FILE_VAULT: &str = "./vault.toml"; // Server - Sheets -pub const SERVER_SUFFIX_SHEET_FILE: &str = ".json"; -pub const SERVER_SUFFIX_SHEET_FILE_NO_DOT: &str = "json"; +pub const SERVER_SUFFIX_SHEET_FILE: &str = ".bcfg"; +pub const SERVER_SUFFIX_SHEET_FILE_NO_DOT: &str = "bcfg"; pub const REF_SHEET_NAME: &str = "ref"; pub const SERVER_PATH_SHEETS: &str = "./sheets/"; pub const SERVER_PATH_SHARES: &str = "./sheets/shares/{sheet_name}/"; -pub const SERVER_FILE_SHEET: &str = "./sheets/{sheet_name}.json"; -pub const SERVER_FILE_SHEET_SHARE: &str = "./sheets/shares/{sheet_name}/{share_id}.json"; +pub const SERVER_FILE_SHEET: &str = "./sheets/{sheet_name}.bcfg"; +pub const SERVER_FILE_SHEET_SHARE: &str = "./sheets/shares/{sheet_name}/{share_id}.bcfg"; // Server - Members pub const SERVER_PATH_MEMBERS: &str = "./members/"; pub const SERVER_PATH_MEMBER_PUB: &str = "./key/"; -pub const SERVER_FILE_MEMBER_INFO: &str = "./members/{member_id}.toml"; +pub const SERVER_FILE_MEMBER_INFO: &str = "./members/{member_id}.bcfg"; pub const SERVER_FILE_MEMBER_PUB: &str = "./key/{member_id}.pem"; // Server - Virtual File Storage @@ -34,8 +34,8 @@ pub const SERVER_PATH_VF_TEMP: &str = "./.temp/{temp_name}"; pub const SERVER_PATH_VF_ROOT: &str = "./storage/"; pub const SERVER_PATH_VF_STORAGE: &str = "./storage/{vf_index}/{vf_id}/"; pub const SERVER_FILE_VF_VERSION_INSTANCE: &str = "./storage/{vf_index}/{vf_id}/{vf_version}.rf"; -pub const SERVER_FILE_VF_META: &str = "./storage/{vf_index}/{vf_id}/meta.json"; -pub const SERVER_NAME_VF_META: &str = "meta.json"; +pub const SERVER_FILE_VF_META: &str = "./storage/{vf_index}/{vf_id}/meta.bcfg"; +pub const SERVER_NAME_VF_META: &str = "meta.bcfg"; // Server - Updates pub const SERVER_FILE_UPDATES: &str = "./.updates.txt"; @@ -56,17 +56,17 @@ pub const CLIENT_FOLDER_WORKSPACE_ROOT_NAME: &str = ".jv"; pub const CLIENT_FILE_WORKSPACE: &str = "./.jv/workspace.toml"; // Client - Latest Information -pub const CLIENT_FILE_LATEST_INFO: &str = "./.jv/latest/{account}.vault.yaml"; -pub const CLIENT_FILE_LATEST_DATA: &str = "./.jv/latest/{account}.file.yaml"; +pub const CLIENT_FILE_LATEST_INFO: &str = "./.jv/latest/{account}.vault.bcfg"; +pub const CLIENT_FILE_LATEST_DATA: &str = "./.jv/latest/{account}.file.bcfg"; // Client - Local -pub const CLIENT_SUFFIX_LOCAL_SHEET_FILE: &str = ".yaml"; -pub const CLIENT_SUFFIX_CACHED_SHEET_FILE: &str = ".yaml"; +pub const CLIENT_SUFFIX_LOCAL_SHEET_FILE: &str = ".bcfg"; +pub const CLIENT_SUFFIX_CACHED_SHEET_FILE: &str = ".bcfg"; pub const CLIENT_PATH_LOCAL_DRAFT: &str = "./.jv/drafts/{account}/{sheet_name}/"; pub const CLIENT_PATH_LOCAL_SHEET: &str = "./.jv/sheets/local/"; -pub const CLIENT_FILE_LOCAL_SHEET: &str = "./.jv/sheets/local/{account}/{sheet_name}.yaml"; +pub const CLIENT_FILE_LOCAL_SHEET: &str = "./.jv/sheets/local/{account}/{sheet_name}.bcfg"; pub const CLIENT_PATH_CACHED_SHEET: &str = "./.jv/sheets/cached/"; -pub const CLIENT_FILE_CACHED_SHEET: &str = "./.jv/sheets/cached/{sheet_name}.yaml"; +pub const CLIENT_FILE_CACHED_SHEET: &str = "./.jv/sheets/cached/{sheet_name}.bcfg"; pub const CLIENT_FILE_LOCAL_SHEET_NOSET: &str = "./.jv/.temp/wrong.json"; pub const CLIENT_FILE_MEMBER_HELD_NOSET: &str = "./.jv/.temp/wrong.json"; |
