diff options
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/vcs_data/src/data/sheet.rs | 196 | ||||
| -rw-r--r-- | crates/vcs_data/src/data/vault/sheets.rs | 1 | ||||
| -rw-r--r-- | crates/vcs_data/vcs_data_test/src/test_sheet_creation_management_and_persistence.rs | 70 |
3 files changed, 12 insertions, 255 deletions
diff --git a/crates/vcs_data/src/data/sheet.rs b/crates/vcs_data/src/data/sheet.rs index 7643887..f55689c 100644 --- a/crates/vcs_data/src/data/sheet.rs +++ b/crates/vcs_data/src/data/sheet.rs @@ -2,7 +2,6 @@ use std::{collections::HashMap, path::PathBuf}; use cfg_file::{ConfigFile, config::ConfigFile}; use serde::{Deserialize, Serialize}; -use string_proc::simple_processer::sanitize_file_path; use crate::{ constants::SERVER_FILE_SHEET, @@ -17,26 +16,6 @@ use crate::{ pub type SheetName = String; pub type SheetPathBuf = PathBuf; -pub type InputName = String; -pub type InputRelativePathBuf = PathBuf; - -#[derive(Debug, Clone, Serialize, Deserialize, Eq)] -pub struct InputPackage { - /// Name of the input package - pub name: InputName, - - /// The sheet from which this input package was created - pub from: SheetName, - - /// Files in this input package with their relative paths and virtual file IDs - pub files: Vec<(InputRelativePathBuf, SheetMappingMetadata)>, -} - -impl PartialEq for InputPackage { - fn eq(&self, other: &Self) -> bool { - self.name == other.name - } -} const SHEET_NAME: &str = "{sheet_name}"; @@ -59,9 +38,6 @@ pub struct SheetData { /// The holder of the current sheet, who has full operation rights to the sheet mapping pub(crate) holder: Option<MemberId>, - /// Inputs - pub(crate) inputs: Vec<InputPackage>, - /// Mapping of sheet paths to virtual file IDs pub(crate) mapping: HashMap<SheetPathBuf, SheetMappingMetadata>, @@ -69,9 +45,6 @@ pub struct SheetData { pub(crate) id_mapping: Option<HashMap<VirtualFileId, SheetPathBuf>>, } -#[derive(Default, Serialize, Deserialize, ConfigFile, Clone)] -pub struct SheetInputs {} - #[derive(Debug, Default, Serialize, Deserialize, ConfigFile, Clone, Eq, PartialEq)] pub struct SheetMappingMetadata { pub id: VirtualFileId, @@ -88,20 +61,6 @@ impl<'a> Sheet<'a> { self.data.holder.as_ref() } - /// Get the inputs of this sheet - pub fn inputs(&self) -> &Vec<InputPackage> { - &self.data.inputs - } - - /// Get the names of the inputs of this sheet - pub fn input_names(&self) -> Vec<String> { - self.data - .inputs - .iter() - .map(|input| input.name.clone()) - .collect() - } - /// Get the mapping of this sheet pub fn mapping(&self) -> &HashMap<SheetPathBuf, SheetMappingMetadata> { &self.data.mapping @@ -132,61 +91,6 @@ impl<'a> Sheet<'a> { self.data.holder = Some(holder); } - /// Add an input package to the sheet - pub fn add_input(&mut self, input_package: InputPackage) -> Result<(), std::io::Error> { - if self.data.inputs.iter().any(|input| input == &input_package) { - return Err(std::io::Error::new( - std::io::ErrorKind::AlreadyExists, - format!("Input package '{}' already exists", input_package.name), - )); - } - self.data.inputs.push(input_package); - Ok(()) - } - - /// Deny and remove an input package from the sheet - pub fn deny_input(&mut self, input_name: &InputName) -> Option<InputPackage> { - self.data - .inputs - .iter() - .position(|input| input.name == *input_name) - .map(|pos| self.data.inputs.remove(pos)) - } - - /// Accept an input package and insert to the sheet - pub async fn accept_import( - &mut self, - input_name: &InputName, - insert_to: &SheetPathBuf, - ) -> Result<(), std::io::Error> { - // Remove inputs - let input = self - .inputs() - .iter() - .position(|input| input.name == *input_name) - .map(|pos| self.data.inputs.remove(pos)); - - // Ensure input is not empty - let Some(input) = input else { - return Err(std::io::Error::new( - std::io::ErrorKind::NotFound, - "Empty inputs.", - )); - }; - - // Insert to sheet - for (relative_path, virtual_file_meta) in input.files { - self.add_mapping( - insert_to.join(relative_path), - virtual_file_meta.id, - virtual_file_meta.version, - ) - .await?; - } - - Ok(()) - } - /// Add (or Edit) a mapping entry to the sheet /// /// This operation performs safety checks to ensure the member has the right to add the mapping: @@ -326,101 +230,6 @@ impl<'a> Sheet<'a> { .join(SERVER_FILE_SHEET.replace(SHEET_NAME, name.as_ref())) } - /// Export files from the current sheet as an InputPackage for importing into other sheets - /// - /// This is the recommended way to create InputPackages. It takes a list of sheet paths - /// and generates an InputPackage with optimized relative paths by removing the longest - /// common prefix from all provided paths, then placing the files under a directory - /// named with the output_name. - /// - /// # Example - /// Given paths: - /// - `MyProject/Art/Character/Model/final.fbx` - /// - `MyProject/Art/Character/Texture/final.png` - /// - `MyProject/Art/Character/README.md` - /// - /// With output_name = "MyExport", the resulting package will contain: - /// - `MyExport/Model/final.fbx` - /// - `MyExport/Texture/final.png` - /// - `MyExport/README.md` - /// - /// # Arguments - /// * `output_name` - Name of the output package (will be used as the root directory) - /// * `paths` - List of sheet paths to include in the package - /// - /// # Returns - /// Returns an InputPackage containing the exported files with optimized paths, - /// or an error if paths are empty or files are not found in the sheet mapping - pub fn output_mappings( - &self, - output_name: InputName, - paths: &[SheetPathBuf], - ) -> Result<InputPackage, std::io::Error> { - let output_name = sanitize_file_path(output_name); - - // Return error for empty paths since there's no need to generate an empty package - if paths.is_empty() { - return Err(std::io::Error::new( - std::io::ErrorKind::InvalidInput, - "Cannot generate output package with empty paths", - )); - } - - // Find the longest common prefix among all paths - let common_prefix = Self::find_longest_common_prefix(paths); - - // Create output files with optimized relative paths - let files = paths - .iter() - .map(|path| { - let relative_path = path.strip_prefix(&common_prefix).unwrap_or(path); - let output_path = PathBuf::from(&output_name).join(relative_path); - - self.data - .mapping - .get(path) - .map(|vfid| (output_path, vfid.clone())) - .ok_or_else(|| { - std::io::Error::new( - std::io::ErrorKind::NotFound, - format!("File not found: {:?}", path), - ) - }) - }) - .collect::<Result<Vec<_>, _>>()?; - - Ok(InputPackage { - name: output_name, - from: self.name.clone(), - files, - }) - } - - /// Helper function to find the longest common prefix among all paths - fn find_longest_common_prefix(paths: &[SheetPathBuf]) -> PathBuf { - if paths.is_empty() { - return PathBuf::new(); - } - - let first_path = &paths[0]; - let mut common_components = Vec::new(); - - for (component_idx, first_component) in first_path.components().enumerate() { - for path in paths.iter().skip(1) { - if let Some(component) = path.components().nth(component_idx) { - if component != first_component { - return common_components.into_iter().collect(); - } - } else { - return common_components.into_iter().collect(); - } - } - common_components.push(first_component); - } - - common_components.into_iter().collect() - } - /// Clone the data of the sheet pub fn clone_data(&self) -> SheetData { self.data.clone() @@ -443,11 +252,6 @@ impl SheetData { self.holder.as_ref() } - /// Get the inputs of this sheet data - pub fn inputs(&self) -> &Vec<InputPackage> { - &self.inputs - } - /// Get the mapping of this sheet data pub fn mapping(&self) -> &HashMap<SheetPathBuf, SheetMappingMetadata> { &self.mapping diff --git a/crates/vcs_data/src/data/vault/sheets.rs b/crates/vcs_data/src/data/vault/sheets.rs index 4101d45..c22c849 100644 --- a/crates/vcs_data/src/data/vault/sheets.rs +++ b/crates/vcs_data/src/data/vault/sheets.rs @@ -133,7 +133,6 @@ impl Vault { // Create the sheet file let sheet_data = SheetData { holder: Some(holder.clone()), - inputs: Vec::new(), mapping: HashMap::new(), id_mapping: None, write_count: 0, diff --git a/crates/vcs_data/vcs_data_test/src/test_sheet_creation_management_and_persistence.rs b/crates/vcs_data/vcs_data_test/src/test_sheet_creation_management_and_persistence.rs index 387e7e1..6683d06 100644 --- a/crates/vcs_data/vcs_data_test/src/test_sheet_creation_management_and_persistence.rs +++ b/crates/vcs_data/vcs_data_test/src/test_sheet_creation_management_and_persistence.rs @@ -5,7 +5,7 @@ use vcs_data::{ constants::{SERVER_FILE_SHEET, SERVER_FILE_VAULT}, data::{ member::{Member, MemberId}, - sheet::{InputRelativePathBuf, SheetName}, + sheet::SheetName, vault::{Vault, config::VaultConfig, virtual_file::VirtualFileId}, }, }; @@ -38,17 +38,13 @@ async fn test_sheet_creation_management_and_persistence() -> Result<(), std::io: // Verify sheet properties assert_eq!(sheet.holder(), Some(&member_id)); assert_eq!(sheet.holder(), Some(&member_id)); - assert!(sheet.inputs().is_empty()); assert!(sheet.mapping().is_empty()); // Verify sheet file was created let sheet_path = dir.join(SERVER_FILE_SHEET.replace("{sheet_name}", &sheet_name)); assert!(sheet_path.exists()); - // Test 2: Add input packages to the sheet - let input_name = "source_files".to_string(); - - // First add mapping entries that will be used to generate the input package + // Test 2: Add mapping entries to the sheet let mut sheet = vault.sheet(&sheet_name).await?; // Add mapping entries for the files @@ -68,26 +64,10 @@ async fn test_sheet_creation_management_and_persistence() -> Result<(), std::io: .add_mapping(lib_rs_path.clone(), lib_rs_id.clone(), "1.0.0".to_string()) .await?; - // Use output_mappings to generate the InputPackage - let paths = vec![main_rs_path, lib_rs_path]; - let input_package = sheet.output_mappings(input_name.clone(), &paths)?; - sheet.add_input(input_package)?; + // Verify mappings were added + assert_eq!(sheet.mapping().len(), 2); - // Verify input was added - assert_eq!(sheet.inputs().len(), 1); - let added_input = &sheet.inputs()[0]; - assert_eq!(added_input.name, input_name); - assert_eq!(added_input.files.len(), 2); - assert_eq!( - added_input.files[0].0, - InputRelativePathBuf::from("source_files/main.rs") - ); - assert_eq!( - added_input.files[1].0, - InputRelativePathBuf::from("source_files/lib.rs") - ); - - // Test 3: Add mapping entries + // Test 3: Add more mapping entries let mapping_path = vcs_data::data::sheet::SheetPathBuf::from("output/build.exe"); let virtual_file_id = VirtualFileId::new(); @@ -112,24 +92,15 @@ async fn test_sheet_creation_management_and_persistence() -> Result<(), std::io: // Verify persistence by reloading the sheet let reloaded_sheet = vault.sheet(&sheet_name).await?; assert_eq!(reloaded_sheet.holder(), Some(&member_id)); - assert_eq!(reloaded_sheet.inputs().len(), 1); assert_eq!(reloaded_sheet.mapping().len(), 3); - // Test 5: Remove input package + // Test 5: Remove mapping entry let mut sheet_for_removal = vault.sheet(&sheet_name).await?; - let removed_input = sheet_for_removal.deny_input(&input_name); - assert!(removed_input.is_some()); - let removed_input = removed_input.unwrap(); - assert_eq!(removed_input.name, input_name); - assert_eq!(removed_input.files.len(), 2); - assert_eq!(sheet_for_removal.inputs().len(), 0); - - // Test 6: Remove mapping entry let _removed_virtual_file_id = sheet_for_removal.remove_mapping(&mapping_path).await; // Don't check the return value since it depends on virtual file existence assert_eq!(sheet_for_removal.mapping().len(), 2); - // Test 7: List all sheets in vault + // Test 6: List all sheets in vault let sheet_names = vault.sheet_names()?; assert_eq!(sheet_names.len(), 2); assert!(sheet_names.contains(&sheet_name)); @@ -151,7 +122,7 @@ async fn test_sheet_creation_management_and_persistence() -> Result<(), std::io: assert_eq!(test_sheet_holder, Some(&member_id)); assert_eq!(ref_sheet_holder, Some(&"host".to_string())); - // Test 8: Safe deletion (move to trash) + // Test 7: Safe deletion (move to trash) vault.delete_sheet_safely(&sheet_name).await?; // Verify sheet is not in normal listing but can be restored @@ -159,7 +130,7 @@ async fn test_sheet_creation_management_and_persistence() -> Result<(), std::io: assert_eq!(sheet_names_after_deletion.len(), 1); assert_eq!(sheet_names_after_deletion[0], "ref"); - // Test 9: Restore sheet from trash + // Test 8: Restore sheet from trash let restored_sheet = vault.sheet(&sheet_name).await?; assert_eq!(restored_sheet.holder(), Some(&member_id)); assert_eq!(restored_sheet.holder(), Some(&member_id)); @@ -170,7 +141,7 @@ async fn test_sheet_creation_management_and_persistence() -> Result<(), std::io: assert!(sheet_names_after_restore.contains(&sheet_name)); assert!(sheet_names_after_restore.contains(&"ref".to_string())); - // Test 10: Permanent deletion + // Test 9: Permanent deletion vault.delete_sheet(&sheet_name).await?; // Verify sheet is permanently gone @@ -262,19 +233,7 @@ async fn test_sheet_data_serialization() -> Result<(), std::io::Error> { let sheet_name: SheetName = "test_serialization_sheet".to_string(); let mut sheet = vault.create_sheet(&sheet_name, &member_id).await?; - // Add some inputs - let input_name = "source_files".to_string(); - let _files = [ - ( - InputRelativePathBuf::from("src/main.rs"), - VirtualFileId::new(), - ), - ( - InputRelativePathBuf::from("src/lib.rs"), - VirtualFileId::new(), - ), - ]; - // First add mapping entries + // Add some mappings let main_rs_path = vcs_data::data::sheet::SheetPathBuf::from("src/main.rs"); let lib_rs_path = vcs_data::data::sheet::SheetPathBuf::from("src/lib.rs"); let main_rs_id = VirtualFileId::new(); @@ -291,12 +250,7 @@ async fn test_sheet_data_serialization() -> Result<(), std::io::Error> { .add_mapping(lib_rs_path.clone(), lib_rs_id.clone(), "1.0.0".to_string()) .await?; - // Use output_mappings to generate the InputPackage - let paths = vec![main_rs_path, lib_rs_path]; - let input_package = sheet.output_mappings(input_name.clone(), &paths)?; - sheet.add_input(input_package)?; - - // Add some mappings + // Add more mappings let build_exe_id = VirtualFileId::new(); sheet |
