Skip to main content

Mountain/Workspace/WorkspaceFileService/
ParseWorkspaceFile.rs

1#![allow(non_snake_case)]
2
3//! Parse a `.code-workspace` file's content and resolve every folder path to a
4//! `file://` URI.
5//!
6//! TODO: zero callers as of 2026-05-02. The parser exists in case Mountain
7//! ever owns `.code-workspace` ingestion (currently delegated through Wind).
8//! Remove if the boundary stays on the Wind side.
9
10use std::path::Path;
11
12use CommonLibrary::Error::CommonError::CommonError;
13use url::Url;
14
15use crate::{
16	ApplicationState::DTO::WorkspaceFolderStateDTO::WorkspaceFolderStateDTO,
17	Cache::PathCanon::Canonicalize,
18	Workspace::WorkspaceFileService::WorkspaceFile,
19};
20
21pub fn Fn(WorkspaceFilePath:&Path, FileContent:&str) -> Result<Vec<WorkspaceFolderStateDTO>, CommonError> {
22	let Parsed:WorkspaceFile::Struct = serde_json::from_str(FileContent)
23		.map_err(|Error| CommonError::SerializationError { Description:Error.to_string() })?;
24
25	let WorkspaceFileDirectory = WorkspaceFilePath.parent().ok_or_else(|| {
26		CommonError::FileSystemIO {
27			Path:WorkspaceFilePath.to_path_buf(),
28			Description:"Cannot get parent directory of workspace file".to_string(),
29		}
30	})?;
31
32	Parsed
33		.folders
34		.into_iter()
35		.enumerate()
36		.map(|(Index, Entry)| {
37			let FolderPath = WorkspaceFileDirectory.join(Entry.path);
38
39			let CanonicalPath =
40				Canonicalize::Fn(&FolderPath).map_err(|_| CommonError::FileSystemNotFound(FolderPath.clone()))?;
41
42			let FolderURI = Url::from_directory_path(&CanonicalPath).map_err(|_| {
43				CommonError::InvalidArgument {
44					ArgumentName:"path".into(),
45					Reason:format!("Could not convert path '{}' to URL", CanonicalPath.display()),
46				}
47			})?;
48
49			let Name = CanonicalPath
50				.file_name()
51				.and_then(|N| N.to_str())
52				.unwrap_or("untitled-folder")
53				.to_string();
54
55			Ok(WorkspaceFolderStateDTO { URI:FolderURI, Name, Index })
56		})
57		.collect()
58}