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