Mountain/WorkSpace/
WorkSpaceFileService.rs1#![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 }
29
30#[derive(Deserialize, Debug)]
31struct WorkSpaceFolderEntry {
32 path:String,
33}
34
35pub 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}