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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
//! FIXME discuss about which errors to print and when to panic
use std::{iter::FilterMap, path::Path, slice::Iter};
const SUPPORTED_FAMILIES: [&str; 9] = [
"stm32f0",
"stm32f4",
"stm32g0",
"stm32l0",
"stm32l1",
"stm32l4",
"stm32h7",
"stm32wb55",
"stm32wl55",
];
const SEPARATOR_START: &str = "# BEGIN GENERATED FEATURES\n";
const SEPARATOR_END: &str = "# END GENERATED FEATURES\n";
const HELP: &str = "# Generated by stm32-gen-features. DO NOT EDIT.\n";
/// True if the chip named `name` is supported else false
fn is_supported(name: &str) -> bool {
SUPPORTED_FAMILIES
.iter()
.any(|family| name.starts_with(family))
}
type SupportedIter<'a> = FilterMap<
Iter<'a, (String, Vec<String>)>,
fn(&(String, Vec<String>)) -> Option<(&String, &Vec<String>)>,
>;
trait FilterSupported {
fn supported(&self) -> SupportedIter;
}
impl FilterSupported for &[(String, Vec<String>)] {
/// Get a new Vec with only the supported chips
fn supported(&self) -> SupportedIter {
self.iter()
.filter_map(|(name, cores)| is_supported(name).then(|| (name, cores)))
}
}
/// Get the list of all the chips and their supported cores
///
/// Print errors to `stderr` when something is returned by the glob but is not in the returned
/// [`Vec`]
///
/// This function is slow because all the yaml files are parsed.
pub fn chip_names_and_cores() -> Vec<(String, Vec<String>)> {
glob::glob("../stm32-data/data/chips/*.yaml")
.unwrap()
.filter_map(|entry| entry.map_err(|e| eprintln!("{:?}", e)).ok())
.filter_map(|entry| {
if let Some(name) = entry.file_stem().and_then(|stem| stem.to_str()) {
Some((name.to_lowercase(), chip_cores(&entry)))
} else {
eprintln!("{:?} is not a regular file", entry);
None
}
})
.collect()
}
/// Get the list of the cores of a chip by its associated file
///
/// # Panic
/// Panics if the file does not exist or if it contains yaml syntax errors.
/// Panics if "cores" is not an array.
fn chip_cores(path: &Path) -> Vec<String> {
let file_contents = std::fs::read_to_string(path).unwrap();
let doc = &yaml_rust::YamlLoader::load_from_str(&file_contents).unwrap()[0];
doc["cores"]
.as_vec()
.unwrap_or_else(|| panic!("{:?}:[cores] is not an array", path))
.iter()
.enumerate()
.map(|(i, core)| {
core["name"]
.as_str()
.unwrap_or_else(|| panic!("{:?}:[cores][{}][name] is not a string", path, i))
.to_owned()
})
.collect()
}
/// Generate data needed in `../embassy-stm32/Cargo.toml`
///
/// Print errors to `stderr` when something is returned by the glob but is not in the returned
/// [`Vec`]
///
/// # Panic
/// Panics if a file contains yaml syntax errors or if a value does not have a consistent type
pub fn embassy_stm32_needed_data(names_and_cores: &[(String, Vec<String>)]) -> String {
let mut result = String::new();
for (chip_name, cores) in names_and_cores.supported() {
if cores.len() > 1 {
for core_name in cores.iter() {
result += &format!(
"{chip}_{core} = [ \"stm32-metapac/{chip}_{core}\" ]\n",
chip = chip_name,
core = core_name
);
}
} else {
result += &format!("{chip} = [ \"stm32-metapac/{chip}\" ]\n", chip = chip_name);
}
}
result
}
/// Generate data needed in `../stm32-metapac/Cargo.toml`
///
/// Print errors to `stderr` when something is returned by the glob but is not in the returned
/// [`Vec`]
///
/// # Panic
/// Panics if a file contains yaml syntax errors or if a value does not have a consistent type
pub fn stm32_metapac_needed_data(names_and_cores: &[(String, Vec<String>)]) -> String {
let mut result = String::new();
for (chip_name, cores) in names_and_cores {
if cores.len() > 1 {
for core_name in cores {
result += &format!("{}_{} = []\n", chip_name, core_name);
}
} else {
result += &format!("{} = []\n", chip_name);
}
}
result
}
/// Get contents before and after generated contents
///
/// # Panic
/// Panics when a separator cound not be not found
fn split_cargo_toml_contents(contents: &str) -> (&str, &str) {
let (before, remainder) = contents
.split_once(SEPARATOR_START)
.unwrap_or_else(|| panic!("missing \"{}\" tag", SEPARATOR_START));
let (_, after) = remainder
.split_once(SEPARATOR_END)
.unwrap_or_else(|| panic!("missing \"{}\" tag", SEPARATOR_END));
(before, after)
}
/// Generates new contents for Cargo.toml
///
/// # Panic
/// Panics when a separator cound not be not found
pub fn generate_cargo_toml_file(previous_text: &str, new_contents: &str) -> String {
let (before, after) = split_cargo_toml_contents(previous_text);
before.to_owned() + SEPARATOR_START + HELP + new_contents + SEPARATOR_END + after
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn stm32f407vg_is_supported() {
assert!(is_supported("stm32f407vg"))
}
#[test]
fn abcdef_is_not_supported() {
assert!(!is_supported("abcdef"))
}
#[test]
#[ignore]
fn stm32f407vg_yaml_file_exists_and_is_supported() {
assert!(chip_names_and_cores()
.as_slice()
.supported()
.into_iter()
.any(|(name, _)| { name == "stm32f407vg" }))
}
#[test]
fn keeps_text_around_separators() {
let initial = "\
before
# BEGIN GENERATED FEATURES
# END GENERATED FEATURES
after
";
let expected = "\
before
# BEGIN GENERATED FEATURES
# Generated by stm32-gen-features. DO NOT EDIT.
a = [\"b\"]
# END GENERATED FEATURES
after
";
let new_contents = String::from("a = [\"b\"]\n");
assert_eq!(generate_cargo_toml_file(initial, &new_contents), expected);
}
#[test]
#[should_panic]
fn does_not_generate_if_separators_are_missing() {
let initial = "\
before
# END GENERATED FEATURES
after
";
let new_contents = String::from("a = [\"b\"]\n");
generate_cargo_toml_file(initial, &new_contents);
}
}
|