Mountain/WorkSpace/
WorkSpaceFileService.rs

1// File: Mountain/Source/WorkSpace/WorkSpaceFileService.rs
2// Role: Contains logic for parsing and handling `.code-workspace` files.
3// Responsibilities:
4//   - Define the structure for deserializing `.code-workspace` JSON.
5//   - Parse the file content, resolve relative folder paths to absolute URIs,
6
7//     and construct a list of `WorkSpaceFolderStateDTO`s representing the
8//     workspace.
9
10//! # WorkSpace File Service
11//!
12//! Contains logic for parsing and handling `.code-workspace` files.
13
14#![allow(non_snake_case, non_camel_case_types)]
15
16use std::path::Path;
17
18use Common::Error::CommonError::CommonError;
19use serde::Deserialize;
20use url::Url;
21
22use crate::ApplicationState::DTO::WorkSpaceFolderStateDTO::WorkSpaceFolderStateDTO;
23
24#[derive(Deserialize, Debug)]
25struct WorkSpaceFile {
26	folders:Vec<WorkSpaceFolderEntry>,
27	// Can also contain 'settings', 'extensions', etc.
28}
29
30#[derive(Deserialize, Debug)]
31struct WorkSpaceFolderEntry {
32	path:String,
33}
34
35/// Parses a `.code-workspace` file content and resolves the folder paths.
36///
37/// # Parameters
38/// * `WorkSpaceFilePath`: The absolute path to the `.code-workspace` file.
39/// * `FileContent`: The raw string content of the file.
40///
41/// # Returns
42/// A `Result` containing a vector of `WorkSpaceFolderStateDTO`s.
43pub fn ParseWorkSpaceFile(
44	WorkSpaceFilePath:&Path,
45
46	FileContent:&str,
47) -> Result<Vec<WorkSpaceFolderStateDTO>, CommonError> {
48	let Parsed:WorkSpaceFile = serde_json::from_str(FileContent)
49		.map_err(|Error| CommonError::SerializationError { Description:Error.to_string() })?;
50
51	let WorkSpaceFileDirectory = WorkSpaceFilePath.parent().ok_or_else(|| {
52		CommonError::FileSystemIO {
53			Path:WorkSpaceFilePath.to_path_buf(),
54
55			Description:"Cannot get parent directory of workspace file".to_string(),
56		}
57	})?;
58
59	let Folders:Result<Vec<WorkSpaceFolderStateDTO>, CommonError> = Parsed
60		.folders
61		.into_iter()
62		.enumerate()
63		.map(|(Index, Entry)| {
64			let FolderPath = WorkSpaceFileDirectory.join(Entry.path);
65
66			let CanonicalPath = FolderPath
67				.canonicalize()
68				.map_err(|_| CommonError::FileSystemNotFound(FolderPath.clone()))?;
69
70			let FolderURI = Url::from_directory_path(&CanonicalPath).map_err(|_| {
71				CommonError::InvalidArgument {
72					ArgumentName:"path".into(),
73
74					Reason:format!("Could not convert path '{}' to URL", CanonicalPath.display()),
75				}
76			})?;
77
78			let Name = CanonicalPath
79				.file_name()
80				.and_then(|n| n.to_str())
81				.unwrap_or("untitled-folder")
82				.to_string();
83
84			Ok(WorkSpaceFolderStateDTO { URI:FolderURI, Name, Index })
85		})
86		.collect();
87
88	Folders
89}