1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
pub mod cores;
use std::collections::HashMap;
use std::fs::File;
use std::fs::{self, DirEntry};
use std::io;
use std::io::BufReader;
use std::path::Path;
use crate::probe::flash::flasher::FlashAlgorithm;
use crate::target::Core;
use crate::target::Target;
pub fn get_target(name: impl AsRef<str>) -> Option<Target> {
let mut map: HashMap<String, Target> = HashMap::new();
load_targets(
dirs::home_dir()
.map(|home| home.join(".config/probe-rs/targets"))
.as_ref()
.map(|path| path.as_path()),
&mut map,
);
let name: String = name.as_ref().into();
map.get(&name.to_ascii_lowercase()).cloned()
}
pub fn get_algorithm(name: impl AsRef<str>) -> Option<FlashAlgorithm> {
let mut map: HashMap<String, FlashAlgorithm> = HashMap::new();
let root = dirs::home_dir().map(|home| home.join(".config/probe-rs/algorithms"));
if let Some(root) = root {
visit_dirs(root.as_path(), &mut map, &load_algorithms_from_dir).unwrap();
let name = root
.join(name.as_ref())
.as_path()
.to_string_lossy()
.to_string();
map.get(&name).cloned()
} else {
log::warn!("Home directory could not be determined while loading algorithms.");
None
}
}
pub fn get_core(name: impl AsRef<str>) -> Option<Box<dyn Core>> {
let map: HashMap<&'static str, Box<dyn Core>> = hashmap! {
"M0" => Box::new(self::cores::m0::M0) as _,
"M4" => Box::new(self::cores::m4::M4) as _,
};
map.get(name.as_ref()).cloned()
}
pub fn load_targets(root: Option<&Path>, map: &mut HashMap<String, Target>) {
if let Some(root) = root {
visit_dirs(root, map, &load_targets_from_dir).unwrap();
} else {
log::warn!("Home directory could not be determined while loading targets.");
}
}
pub fn load_targets_from_dir(dir: &DirEntry, map: &mut HashMap<String, Target>) {
match File::open(dir.path()) {
Ok(file) => {
let reader = BufReader::new(file);
match serde_yaml::from_reader(reader) as serde_yaml::Result<Target> {
Ok(mut target) => {
target.name.make_ascii_lowercase();
map.insert(target.name.clone(), target);
}
Err(e) => log::warn!("Error loading chip definition: {}", e),
}
}
Err(e) => {
log::info!("Unable to load file {:?}.", dir.path());
log::info!("Reason: {:?}", e);
}
}
}
pub fn load_algorithms_from_dir(dir: &DirEntry, map: &mut HashMap<String, FlashAlgorithm>) {
match File::open(dir.path()) {
Ok(file) => {
let reader = BufReader::new(file);
match serde_yaml::from_reader(reader) as serde_yaml::Result<FlashAlgorithm> {
Ok(target) => {
map.insert(dir.path().to_string_lossy().to_string(), target);
}
Err(e) => log::warn!("Error loading chip definition: {}", e),
}
}
Err(e) => {
log::info!("Unable to load file {:?}.", dir.path());
log::info!("Reason: {:?}", e);
}
}
}
fn visit_dirs<T>(
dir: &Path,
map: &mut HashMap<String, T>,
cb: &dyn Fn(&DirEntry, &mut HashMap<String, T>),
) -> io::Result<()> {
if dir.is_dir() {
for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
visit_dirs(&path, map, cb)?;
} else {
cb(&entry, map);
}
}
}
Ok(())
}