Mountain/Workspace/WorkspaceFileService/
ParseWorkspaceFile.rs1#![allow(non_snake_case)]
2
3use std::path::Path;
13
14use CommonLibrary::Error::CommonError::CommonError;
15use url::Url;
16
17use crate::{
18 ApplicationState::DTO::WorkspaceFolderStateDTO::WorkspaceFolderStateDTO,
19 Cache::PathCanon::Canonicalize,
20 Workspace::WorkspaceFileService::WorkspaceFile,
21};
22
23pub fn Fn(WorkspaceFilePath:&Path, FileContent:&str) -> Result<Vec<WorkspaceFolderStateDTO>, CommonError> {
24 let Parsed:WorkspaceFile::Struct = serde_json::from_str(FileContent)
25 .map_err(|Error| CommonError::SerializationError { Description:Error.to_string() })?;
26
27 let WorkspaceFileDirectory = WorkspaceFilePath.parent().ok_or_else(|| {
28 CommonError::FileSystemIO {
29 Path:WorkspaceFilePath.to_path_buf(),
30 Description:"Cannot get parent directory of workspace file".to_string(),
31 }
32 })?;
33
34 Parsed
35 .folders
36 .into_iter()
37 .enumerate()
38 .map(|(Index, Entry)| {
39 let FolderPath = WorkspaceFileDirectory.join(Entry.path);
40
41 let CanonicalPath =
42 Canonicalize::Fn(&FolderPath).map_err(|_| CommonError::FileSystemNotFound(FolderPath.clone()))?;
43
44 let FolderURI = Url::from_directory_path(&CanonicalPath).map_err(|_| {
45 CommonError::InvalidArgument {
46 ArgumentName:"path".into(),
47 Reason:format!("Could not convert path '{}' to URL", CanonicalPath.display()),
48 }
49 })?;
50
51 let Name = CanonicalPath
52 .file_name()
53 .and_then(|N| N.to_str())
54 .unwrap_or("untitled-folder")
55 .to_string();
56
57 Ok(WorkspaceFolderStateDTO { URI:FolderURI, Name, Index })
58 })
59 .collect()
60}