Skip to content

Commit 60cda22

Browse files
committed
zephyr-build: Run cargo fmt
Run `cargo fmt` with the default rules. This gives us a baseline to be able to require properly formatted code for future changes. Signed-off-by: David Brown <[email protected]>
1 parent 1143ae9 commit 60cda22

File tree

6 files changed

+86
-79
lines changed

6 files changed

+86
-79
lines changed

zephyr-build/src/devicetree.rs

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ mod ordmap;
2828
mod output;
2929
mod parse;
3030

31-
pub use augment::{Augment, load_augments};
31+
pub use augment::{load_augments, Augment};
3232

3333
/// Representation of a parsed device tree.
3434
pub struct DeviceTree {
@@ -97,8 +97,7 @@ impl DeviceTree {
9797
pub fn new<P1: AsRef<Path>, P2: AsRef<Path>>(dts_path: P1, dt_gen: P2) -> DeviceTree {
9898
let ords = OrdMap::new(dt_gen);
9999

100-
let dts = std::fs::read_to_string(dts_path)
101-
.expect("Reading zephyr.dts file");
100+
let dts = std::fs::read_to_string(dts_path).expect("Reading zephyr.dts file");
102101
let dt = parse::parse(&dts, &ords);
103102

104103
// Walk the node tree, fixing any phandles to include their reference.
@@ -167,32 +166,32 @@ impl Node {
167166
/// Returns the slice of values of a property with this name as `Some` or `None` if the property
168167
/// does not exist.
169168
fn get_property(&self, name: &str) -> Option<&[Value]> {
170-
self.properties
171-
.iter()
172-
.find_map(|p| if p.name == name { Some(p.value.as_slice()) } else { None })
169+
self.properties.iter().find_map(|p| {
170+
if p.name == name {
171+
Some(p.value.as_slice())
172+
} else {
173+
None
174+
}
175+
})
173176
}
174177

175178
/// Attempt to retrieve the named property, as a single entry of Words.
176179
fn get_words(&self, name: &str) -> Option<&[Word]> {
177-
self.get_property(name)
178-
.and_then(|p| {
179-
match p {
180-
&[Value::Words(ref w)] => Some(w.as_ref()),
181-
_ => None,
182-
}
183-
})
180+
self.get_property(name).and_then(|p| match p {
181+
&[Value::Words(ref w)] => Some(w.as_ref()),
182+
_ => None,
183+
})
184184
}
185185

186186
/// Get a property that consists of a single number.
187187
fn get_number(&self, name: &str) -> Option<u32> {
188-
self.get_words(name)
189-
.and_then(|p| {
190-
if let &[Word::Number(n)] = p {
191-
Some(n)
192-
} else {
193-
None
194-
}
195-
})
188+
self.get_words(name).and_then(|p| {
189+
if let &[Word::Number(n)] = p {
190+
Some(n)
191+
} else {
192+
None
193+
}
194+
})
196195
}
197196

198197
/// Get a property that consists of multiple numbers.
@@ -210,14 +209,13 @@ impl Node {
210209

211210
/// Get a property that is a single string.
212211
fn get_single_string(&self, name: &str) -> Option<&str> {
213-
self.get_property(name)
214-
.and_then(|p| {
215-
if let &[Value::String(ref text)] = p {
216-
Some(text.as_ref())
217-
} else {
218-
None
219-
}
220-
})
212+
self.get_property(name).and_then(|p| {
213+
if let &[Value::String(ref text)] = p {
214+
Some(text.as_ref())
215+
} else {
216+
None
217+
}
218+
})
221219
}
222220
}
223221

@@ -253,8 +251,7 @@ impl Phandle {
253251
return;
254252
}
255253

256-
let node = labels.get(&self.name).cloned()
257-
.expect("Missing phandle");
254+
let node = labels.get(&self.name).cloned().expect("Missing phandle");
258255
*self.node.borrow_mut() = Some(node);
259256
}
260257

@@ -276,8 +273,7 @@ impl Word {
276273
// To avoid recursion, the debug printer for Phandle just prints the name.
277274
impl std::fmt::Debug for Phandle {
278275
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
279-
fmt
280-
.debug_struct("Phandle")
276+
fmt.debug_struct("Phandle")
281277
.field("name", &self.name)
282278
.finish_non_exhaustive()
283279
}

zephyr-build/src/devicetree/augment.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,7 @@ pub enum Rule {
8787
/// Matches if this node has one of the listed compatible strings. The the 'level' property
8888
/// indicates how many levels up in the tree. Zero means match the current node, 1 means the
8989
/// parent node, and so on.
90-
Compatible {
91-
names: Vec<String>,
92-
level: usize,
93-
},
90+
Compatible { names: Vec<String>, level: usize },
9491
/// Matches at the root of tree.
9592
Root,
9693
}
@@ -143,9 +140,7 @@ pub enum Action {
143140
impl Action {
144141
fn generate(&self, _name: &Ident, node: &Node, tree: &DeviceTree) -> TokenStream {
145142
match self {
146-
Action::Instance { raw, device } => {
147-
raw.generate(node, device)
148-
}
143+
Action::Instance { raw, device } => raw.generate(node, device),
149144
Action::Labels => {
150145
let nodes = tree.labels.iter().map(|(k, v)| {
151146
let name = dt_to_lower_id(k);

zephyr-build/src/devicetree/ordmap.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
//!
33
//! The OrdMap provides a mapping between nodes on the devicetree, and their "ord" index.
44
5-
use std::{collections::BTreeMap, fs::File, io::{BufRead, BufReader}, path::Path, str::FromStr};
5+
use std::{
6+
collections::BTreeMap,
7+
fs::File,
8+
io::{BufRead, BufReader},
9+
path::Path,
10+
str::FromStr,
11+
};
612

713
use regex::Regex;
814

@@ -19,8 +25,7 @@ impl OrdMap {
1925
let mut c_name = "".to_string();
2026
let mut dt_path = "".to_string();
2127

22-
let fd = File::open(path)
23-
.expect("Opening devicetree_generated.h");
28+
let fd = File::open(path).expect("Opening devicetree_generated.h");
2429
for line in BufReader::new(fd).lines() {
2530
let line = line.expect("Reading from devicetree_generated.h");
2631

zephyr-build/src/devicetree/output.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use super::{augment::Augment, DeviceTree, Node, Property, Value, Word};
1919
impl DeviceTree {
2020
/// Generate a TokenStream for the Rust representation of this device tree.
2121
pub fn to_tokens(&self, augments: &[Box<dyn Augment>]) -> TokenStream {
22-
2322
// Root is a little special. Since we don't want a module for this (it will be provided
2423
// above where it is included, so it can get documentation and attributes), we use None for
2524
// the name.
@@ -39,15 +38,19 @@ impl DeviceTree {
3938
Ok(())
4039
}
4140

42-
fn node_walk(&self, node: &Node, name: Option<&str>, augments: &[Box<dyn Augment>]) -> TokenStream {
43-
let children = node.children.iter().map(|child| {
44-
self.node_walk(child.as_ref(), Some(&child.name), augments)
45-
});
41+
fn node_walk(
42+
&self,
43+
node: &Node,
44+
name: Option<&str>,
45+
augments: &[Box<dyn Augment>],
46+
) -> TokenStream {
47+
let children = node
48+
.children
49+
.iter()
50+
.map(|child| self.node_walk(child.as_ref(), Some(&child.name), augments));
4651
// Simplistic first pass, turn the properties into constents of the formatted text of the
4752
// property.
48-
let props = node.properties.iter().map(|prop| {
49-
self.property_walk(prop)
50-
});
53+
let props = node.properties.iter().map(|prop| self.property_walk(prop));
5154
let ord = node.ord;
5255

5356
// Open the parent as a submodule. This is the same as 'super', so not particularly useful.
@@ -114,7 +117,7 @@ impl DeviceTree {
114117
pub mod #tag {
115118
pub use #route::*;
116119
}
117-
}
120+
};
118121
}
119122
_ => (),
120123
}
@@ -172,7 +175,12 @@ impl Property {
172175
fn output_path<W: Write>(&self, write: &mut W, name: &str) -> Result<()> {
173176
if let Some(value) = self.get_single_value() {
174177
if let Value::Phandle(_) = value {
175-
writeln!(write, "cargo:rustc-cfg=dt=\"{}::{}\"", name, fix_id(&self.name))?;
178+
writeln!(
179+
write,
180+
"cargo:rustc-cfg=dt=\"{}::{}\"",
181+
name,
182+
fix_id(&self.name)
183+
)?;
176184
}
177185
}
178186
Ok(())

zephyr-build/src/devicetree/parse.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
66
use std::{cell::RefCell, collections::BTreeMap, rc::Rc};
77

8-
use pest::{iterators::{Pair, Pairs}, Parser};
8+
use pest::{
9+
iterators::{Pair, Pairs},
10+
Parser,
11+
};
912
use pest_derive::Parser;
1013

1114
use crate::devicetree::Phandle;
@@ -17,8 +20,7 @@ use super::{ordmap::OrdMap, DeviceTree, Node, Property, Value, Word};
1720
pub struct Dts;
1821

1922
pub fn parse(text: &str, ords: &OrdMap) -> DeviceTree {
20-
let pairs = Dts::parse(Rule::file, text)
21-
.expect("Parsing zephyr.dts");
23+
let pairs = Dts::parse(Rule::file, text).expect("Parsing zephyr.dts");
2224

2325
let b = TreeBuilder::new(ords);
2426
b.walk(pairs)
@@ -123,7 +125,7 @@ fn decode_property(node: Pair<'_, Rule>) -> Property {
123125
// No escapes at this point.
124126
let text = pair.as_str();
125127
// Remove the quotes.
126-
let text = &text[1..text.len()-1];
128+
let text = &text[1..text.len() - 1];
127129
value.push(Value::String(text.to_string()));
128130
}
129131
Rule::bytes => {
@@ -132,7 +134,10 @@ fn decode_property(node: Pair<'_, Rule>) -> Property {
132134
r => panic!("rule: {:?}", r),
133135
}
134136
}
135-
Property { name: name.unwrap(), value }
137+
Property {
138+
name: name.unwrap(),
139+
value,
140+
}
136141
}
137142

138143
fn decode_words<'i>(node: Pair<'i, Rule>) -> Vec<Word> {
@@ -205,7 +210,7 @@ impl<'a, 'b> LazyName<'a, 'b> {
205210
name: "/".to_string(),
206211
path: "/".to_string(),
207212
ord,
208-
})
213+
}),
209214
}
210215
} else {
211216
LazyName {
@@ -232,11 +237,7 @@ impl<'a, 'b> LazyName<'a, 'b> {
232237
path.push_str(&name);
233238
// println!("node: {:?}", path);
234239
let ord = self.ords.0[&path];
235-
self.info = Some(Info {
236-
name,
237-
path,
238-
ord,
239-
});
240+
self.info = Some(Info { name, path, ord });
240241
}
241242

242243
fn path_ref(&self) -> &str {

zephyr-build/src/lib.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
// This builds a program that is run on the compilation host before the code is compiled. It can
1212
// output configuration settings that affect the compilation.
1313

14-
use std::io::{BufRead, BufReader, Write};
1514
use std::env;
1615
use std::fs::File;
16+
use std::io::{BufRead, BufReader, Write};
1717
use std::path::Path;
1818
use std::process::{Command, Stdio};
1919

@@ -37,7 +37,7 @@ pub fn export_bool_kconfig() {
3737

3838
let file = File::open(&dotconfig).expect("Unable to open dotconfig");
3939
for line in BufReader::new(file).lines() {
40-
let line = line.expect("reading line from dotconfig");
40+
let line = line.expect("reading line from dotconfig");
4141
if let Some(caps) = config_y.captures(&line) {
4242
println!("cargo:rustc-cfg={}", &caps[1]);
4343
}
@@ -66,25 +66,27 @@ pub fn build_kconfig_mod() {
6666
let line = line.expect("reading line from dotconfig");
6767
if let Some(caps) = config_hex.captures(&line) {
6868
writeln!(&mut f, "#[allow(dead_code)]").unwrap();
69-
writeln!(&mut f, "pub const {}: usize = {};",
70-
&caps[1], &caps[2]).unwrap();
69+
writeln!(&mut f, "pub const {}: usize = {};", &caps[1], &caps[2]).unwrap();
7170
} else if let Some(caps) = config_int.captures(&line) {
7271
writeln!(&mut f, "#[allow(dead_code)]").unwrap();
73-
writeln!(&mut f, "pub const {}: isize = {};",
74-
&caps[1], &caps[2]).unwrap();
72+
writeln!(&mut f, "pub const {}: isize = {};", &caps[1], &caps[2]).unwrap();
7573
} else if let Some(caps) = config_str.captures(&line) {
7674
writeln!(&mut f, "#[allow(dead_code)]").unwrap();
77-
writeln!(&mut f, "pub const {}: &'static str = {};",
78-
&caps[1], &caps[2]).unwrap();
75+
writeln!(
76+
&mut f,
77+
"pub const {}: &'static str = {};",
78+
&caps[1], &caps[2]
79+
)
80+
.unwrap();
7981
}
8082
}
8183
}
8284

8385
/// Parse the finalized DTS file, generating the Rust devicetree file.
8486
fn import_dt() -> DeviceTree {
8587
let zephyr_dts = env::var("ZEPHYR_DTS").expect("ZEPHYR_DTS must be set");
86-
let gen_include = env::var("BINARY_DIR_INCLUDE_GENERATED")
87-
.expect("BINARY_DIR_INCLUDE_GENERATED must be set");
88+
let gen_include =
89+
env::var("BINARY_DIR_INCLUDE_GENERATED").expect("BINARY_DIR_INCLUDE_GENERATED must be set");
8890

8991
let generated = format!("{}/devicetree_generated.h", gen_include);
9092
DeviceTree::new(&zephyr_dts, generated)
@@ -138,10 +140,7 @@ pub fn dt_cfgs() {
138140

139141
/// Determine if `rustfmt` is in the path, and can be excecuted. Returns false on any kind of error.
140142
pub fn has_rustfmt() -> bool {
141-
match Command::new("rustfmt")
142-
.arg("--version")
143-
.status()
144-
{
143+
match Command::new("rustfmt").arg("--version").status() {
145144
Ok(st) if st.success() => true,
146145
_ => false,
147146
}
@@ -159,7 +158,10 @@ fn write_formatted(file: File, tokens: TokenStream) {
159158
.expect("Failed to run rustfmt");
160159
// TODO: Handle the above failing.
161160

162-
let mut stdin = rustfmt.stdin.as_ref().expect("Stdin should have been opened by spawn");
161+
let mut stdin = rustfmt
162+
.stdin
163+
.as_ref()
164+
.expect("Stdin should have been opened by spawn");
163165
writeln!(stdin, "{}", tokens).expect("Writing to rustfmt");
164166

165167
match rustfmt.wait() {

0 commit comments

Comments
 (0)