1use super::{
2 constants::PROJECT_FILE, dependencies::EsteemDependencies,
3 AddEsteemDevelopmentDependency, AddEsteemRequiredDependency, LibraryError,
4 RemoveEsteemDevelopmentDependency, RemoveEsteemRequiredDependency, WriteDependencies,
5};
6use serde::{Deserialize, Serialize};
7use serde_json::Value;
8use std::{
9 collections::BTreeMap,
10 fs::read_to_string,
11 path::{Path, PathBuf},
12};
13
14#[derive(Debug, Serialize, Deserialize, Clone)]
15#[serde(rename_all = "camelCase")]
16pub struct EsteemProject {
17 #[serde(skip_serializing, skip_deserializing)]
19 pub name: String,
20
21 #[serde(skip_serializing, skip_deserializing)]
23 description_file_path: PathBuf,
24
25 #[serde(default)]
27 pub dependencies: EsteemDependencies,
28
29 #[serde(flatten)]
31 other: BTreeMap<String, Value>,
32}
33
34impl EsteemProject {
35 pub fn from_project_path(name: String, path: &Path) -> Result<Self, LibraryError> {
36 trace!("Reading from file: {:?}", &path.display());
37 let description_file_path = path.join(PROJECT_FILE);
38 let project_file = read_to_string(&description_file_path);
39 match project_file {
40 Ok(data) => {
41 let mut partial_project: Self = serde_json::from_str(&data).unwrap();
42 partial_project.description_file_path = description_file_path;
43 partial_project.name = name;
44 Ok(partial_project)
45 }
46 Err(_) => {
47 error!("Unable to find file: {:?}", description_file_path);
48 Err(LibraryError(format!(
49 "Could not find file: {description_file_path:?}"
50 )))
51 }
52 }
53 }
54}
55
56impl AddEsteemRequiredDependency for EsteemProject {
57 fn add_required_dependency(&mut self, dependency: String) {
58 self.dependencies.add_required_dependency(dependency);
59 }
60}
61
62impl AddEsteemDevelopmentDependency for EsteemProject {
63 fn add_development_dependency(&mut self, dependency: String) {
64 self.dependencies.add_development_dependency(dependency);
65 }
66}
67
68impl WriteDependencies for EsteemProject {
69 fn get_path(&self) -> PathBuf {
70 self.description_file_path.clone()
71 }
72}
73
74impl RemoveEsteemRequiredDependency for EsteemProject {
75 fn remove_required_dependency(
76 &mut self,
77 dependency: String,
78 ) -> Result<(), LibraryError> {
79 self.dependencies.remove_required_dependency(dependency)
80 }
81}
82
83impl RemoveEsteemDevelopmentDependency for EsteemProject {
84 fn remove_development_dependency(
85 &mut self,
86 dependency: String,
87 ) -> Result<(), LibraryError> {
88 self.dependencies.remove_development_dependency(dependency)
89 }
90}