scrapyard_core/
projectsettings.rs

1use std::path::{Path, PathBuf};
2
3use mcu::MCUConf;
4
5use cargo::Cargo;
6
7#[derive(Serialize, Deserialize, Debug)]
8enum CodeRegeneration {
9    OverwriteAll,
10    AskOnConflict,
11    KeepUserCode,
12}
13
14#[derive(Serialize, Deserialize, Debug)]
15pub struct ProjectSettings {
16    project_name: String,
17    project_path: PathBuf,
18    resources_path: PathBuf,
19
20    // File generation
21    separate_inits: bool,
22    backup_previous: bool,
23    remove_orphans: bool,
24
25    // Code generation
26    keep_code: CodeRegeneration,
27    diff3_path: PathBuf,
28    cargo: Cargo,
29}
30
31impl ProjectSettings {
32    pub fn new(
33        mcu_conf: &MCUConf,
34        project_path: &Path,
35        resources_path: &Path,
36        diff3_path: &Path,
37    ) -> ProjectSettings {
38        ProjectSettings {
39            project_name: String::new(),
40            project_path: project_path.to_owned(),
41            resources_path: resources_path.to_owned(),
42            separate_inits: false,
43            backup_previous: true,
44            remove_orphans: false,
45            keep_code: CodeRegeneration::AskOnConflict,
46            diff3_path: diff3_path.to_owned(),
47            cargo: Cargo::new(mcu_conf),
48        }
49    }
50
51    pub fn set_project_name(&mut self, project_name: &str) {
52        self.project_name = project_name.to_owned();
53    }
54
55    pub fn get_project_name(&self) -> &str {
56        &self.project_name
57    }
58
59    pub fn set_project_path(&mut self, project_path: &Path) {
60        self.project_path = project_path.to_owned();
61    }
62
63    pub fn get_project_path(&self) -> &Path {
64        &self.project_path
65    }
66
67    pub fn set_resources_path(&mut self, resources_path: &Path) {
68        self.resources_path = resources_path.to_owned();
69    }
70
71    pub fn get_resources_path(&self) -> &Path {
72        &self.resources_path
73    }
74
75    pub fn get_cargo(&self) -> &Cargo {
76        &self.cargo
77    }
78
79    pub fn get_cargo_mut(&mut self) -> &mut Cargo {
80        &mut self.cargo
81    }
82
83    pub fn set_separate_inits(&mut self, value: bool) {
84        self.separate_inits = value;
85    }
86
87    pub fn get_separate_inits(&self) -> bool {
88        self.separate_inits
89    }
90
91    pub fn set_backup_previous(&mut self, value: bool) {
92        self.backup_previous = value;
93    }
94
95    pub fn get_backup_previous(&self) -> bool {
96        self.backup_previous
97    }
98
99    pub fn set_remove_orphans(&mut self, value: bool) {
100        self.remove_orphans = value;
101    }
102
103    pub fn get_remove_orphans(&self) -> bool {
104        self.remove_orphans
105    }
106
107    pub fn set_diff3_path(&mut self, path: &Path) {
108        self.diff3_path = path.to_owned();
109    }
110
111    pub fn get_diff3_path(&self) -> &Path {
112        &self.diff3_path.as_path()
113    }
114}
115
116#[cfg(test)]
117mod tests {
118
119    use super::*;
120    use mcu::MCU;
121
122    #[test]
123    fn it_works() {
124        let sample = Path::new("./samples/STM32F030C6Tx.json");
125        let mcu = MCU::new(sample).unwrap();
126
127        let mcu_conf = mcu.finish();
128        let project_path = Path::new("");
129        let templates_path = Path::new("");
130        let diff3_path = Path::new("");
131
132        let project_settings =
133            ProjectSettings::new(&mcu_conf, &project_path, &templates_path, &diff3_path);
134    }
135}