diff --git a/config/src/lib.rs b/config/src/lib.rs index e632ef3..9f3cf1d 100644 --- a/config/src/lib.rs +++ b/config/src/lib.rs @@ -62,18 +62,10 @@ impl Config { } fn check_heap(&self) -> Result<()> { - let Self { - heap: Heap { size, pools }, - .. - } = self; + let Self { heap: Heap { size, pools }, .. } = self; let used: u32 = pools.iter().map(|pool| pool.block * pool.capacity).sum(); if used != *size { - bail!( - "{}: `heap.pools` adds up to {}, but `heap.size = {}", - CONFIG_NAME, - used, - size - ); + bail!("{}: `heap.pools` adds up to {}, but `heap.size = {}", CONFIG_NAME, used, size); } Ok(()) } diff --git a/rustfmt.toml b/rustfmt.toml index b4aed7a..95c67fe 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -5,6 +5,8 @@ format_strings = true merge_imports = true newline_style = "Unix" overflow_delimited_expr = true +reorder_impl_items = true unstable_features = true use_field_init_shorthand = true +use_small_heuristics = "Max" version = "Two" diff --git a/src/bin/drone-ld.rs b/src/bin/drone-ld.rs index 952cab6..5e83557 100644 --- a/src/bin/drone-ld.rs +++ b/src/bin/drone-ld.rs @@ -26,9 +26,8 @@ fn main() { let config = Config::read_from_current_dir()?; let script = Registry::new()?.layout_ld(&config)?; let signals = register_signals()?; - let linker = linker_command(script.as_ref(), &args, &[ - "--defsym=_section_size_unknown=0".into(), - ])?; + let linker = + linker_command(script.as_ref(), &args, &["--defsym=_section_size_unknown=0".into()])?; block_with_signals(&signals, true, || run_command(linker))?; let size = size_command(&output)?; let syms = block_with_signals(&signals, true, || run_size(size))? diff --git a/src/cli.rs b/src/cli.rs index 619383d..635ab9f 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -173,9 +173,6 @@ fn parse_color(src: &str) -> Result { "always" => ColorChoice::Always, "never" => ColorChoice::Never, "auto" => ColorChoice::Auto, - _ => bail!( - "argument for --color must be auto, always, or never, but found `{}`", - src - ), + _ => bail!("argument for --color must be auto, always, or never, but found `{}`", src), }) } diff --git a/src/device.rs b/src/device.rs index 693cc43..fbbef2a 100644 --- a/src/device.rs +++ b/src/device.rs @@ -299,54 +299,54 @@ impl Device { Self::Nrf52811 => (crates::Bindings::Nrf, "nrf52811", &[]), Self::Nrf52832 => (crates::Bindings::Nrf, "nrf52832", &[]), Self::Nrf52840 => (crates::Bindings::Nrf, "nrf52840", &[]), - Self::Stm32F100 => (crates::Bindings::Stm32, "stm32f100", &[ - "dma", "gpio", "spi", "tim", - ]), - Self::Stm32F101 => (crates::Bindings::Stm32, "stm32f101", &[ - "dma", "gpio", "spi", "tim", - ]), - Self::Stm32F102 => (crates::Bindings::Stm32, "stm32f102", &[ - "dma", "gpio", "spi", "tim", - ]), - Self::Stm32F103 => (crates::Bindings::Stm32, "stm32f103", &[ - "dma", "gpio", "spi", "tim", - ]), - Self::Stm32F107 => (crates::Bindings::Stm32, "stm32f107", &[ - "dma", "gpio", "spi", "tim", - ]), - Self::Stm32F401 => (crates::Bindings::Stm32, "stm32f401", &[ - "adc", "dma", "exti", "gpio", "tim", - ]), - Self::Stm32F405 => (crates::Bindings::Stm32, "stm32f405", &[ - "adc", "dma", "exti", "gpio", "tim", - ]), - Self::Stm32F407 => (crates::Bindings::Stm32, "stm32f407", &[ - "adc", "dma", "exti", "gpio", "tim", - ]), - Self::Stm32F410 => (crates::Bindings::Stm32, "stm32f410", &[ - "adc", "dma", "exti", "gpio", "tim", - ]), - Self::Stm32F411 => (crates::Bindings::Stm32, "stm32f411", &[ - "adc", "dma", "exti", "gpio", "tim", - ]), - Self::Stm32F412 => (crates::Bindings::Stm32, "stm32f412", &[ - "adc", "dma", "exti", "gpio", "tim", - ]), - Self::Stm32F413 => (crates::Bindings::Stm32, "stm32f413", &[ - "adc", "dma", "exti", "gpio", "tim", - ]), - Self::Stm32F427 => (crates::Bindings::Stm32, "stm32f427", &[ - "adc", "dma", "exti", "gpio", "tim", - ]), - Self::Stm32F429 => (crates::Bindings::Stm32, "stm32f429", &[ - "adc", "dma", "exti", "gpio", "tim", - ]), - Self::Stm32F446 => (crates::Bindings::Stm32, "stm32f446", &[ - "adc", "dma", "exti", "gpio", "tim", - ]), - Self::Stm32F469 => (crates::Bindings::Stm32, "stm32f469", &[ - "adc", "dma", "exti", "gpio", "tim", - ]), + Self::Stm32F100 => { + (crates::Bindings::Stm32, "stm32f100", &["dma", "gpio", "spi", "tim"]) + } + Self::Stm32F101 => { + (crates::Bindings::Stm32, "stm32f101", &["dma", "gpio", "spi", "tim"]) + } + Self::Stm32F102 => { + (crates::Bindings::Stm32, "stm32f102", &["dma", "gpio", "spi", "tim"]) + } + Self::Stm32F103 => { + (crates::Bindings::Stm32, "stm32f103", &["dma", "gpio", "spi", "tim"]) + } + Self::Stm32F107 => { + (crates::Bindings::Stm32, "stm32f107", &["dma", "gpio", "spi", "tim"]) + } + Self::Stm32F401 => { + (crates::Bindings::Stm32, "stm32f401", &["adc", "dma", "exti", "gpio", "tim"]) + } + Self::Stm32F405 => { + (crates::Bindings::Stm32, "stm32f405", &["adc", "dma", "exti", "gpio", "tim"]) + } + Self::Stm32F407 => { + (crates::Bindings::Stm32, "stm32f407", &["adc", "dma", "exti", "gpio", "tim"]) + } + Self::Stm32F410 => { + (crates::Bindings::Stm32, "stm32f410", &["adc", "dma", "exti", "gpio", "tim"]) + } + Self::Stm32F411 => { + (crates::Bindings::Stm32, "stm32f411", &["adc", "dma", "exti", "gpio", "tim"]) + } + Self::Stm32F412 => { + (crates::Bindings::Stm32, "stm32f412", &["adc", "dma", "exti", "gpio", "tim"]) + } + Self::Stm32F413 => { + (crates::Bindings::Stm32, "stm32f413", &["adc", "dma", "exti", "gpio", "tim"]) + } + Self::Stm32F427 => { + (crates::Bindings::Stm32, "stm32f427", &["adc", "dma", "exti", "gpio", "tim"]) + } + Self::Stm32F429 => { + (crates::Bindings::Stm32, "stm32f429", &["adc", "dma", "exti", "gpio", "tim"]) + } + Self::Stm32F446 => { + (crates::Bindings::Stm32, "stm32f446", &["adc", "dma", "exti", "gpio", "tim"]) + } + Self::Stm32F469 => { + (crates::Bindings::Stm32, "stm32f469", &["adc", "dma", "exti", "gpio", "tim"]) + } Self::Stm32L4X1 => (crates::Bindings::Stm32, "stm32l4x1", &[ "dma", "exti", "gpio", "i2c", "rtc", "spi", "tim", "uart", ]), diff --git a/src/heap/generate.rs b/src/heap/generate.rs index 89cb9cd..dc600fa 100644 --- a/src/heap/generate.rs +++ b/src/heap/generate.rs @@ -65,12 +65,7 @@ pub fn display(w: &mut impl Write, layout: &[(u32, u32)]) -> Result<()> { if *capacity == 0 { continue; } - writeln!( - w, - " {{ block = \"{}\", capacity = {} }},", - format_size(*block), - capacity - )?; + writeln!(w, " {{ block = \"{}\", capacity = {} }},", format_size(*block), capacity)?; } writeln!(w, "]")?; Ok(()) @@ -108,12 +103,7 @@ fn generate( shell.reset()?; write!(shell, "# Fragmentation: ")?; shell.set_color(ColorSpec::new().set_bold(true))?; - writeln!( - shell, - "{} / {:.2}%", - frag, - f64::from(frag) / f64::from(size) * 100.0 - )?; + writeln!(shell, "{} / {:.2}%", frag, f64::from(frag) / f64::from(size) * 100.0)?; shell.reset()?; writeln!(shell)?; display(&mut stdout(), &output)?; @@ -155,10 +145,7 @@ fn shrink(input: &[(u32, u32)], output: &mut [(u32, u32)], frag: &mut u32, cutof } fn extend(output: &mut [(u32, u32)], size: u32) { - let mut used = output - .iter() - .map(|(block, capacity)| block * capacity) - .sum::(); + let mut used = output.iter().map(|(block, capacity)| block * capacity).sum::(); let count = output.len() as u32; let free = f64::from(size - used); for ((block, capacity), ratio) in output.iter_mut().zip(ratios(count)) { diff --git a/src/heap/mod.rs b/src/heap/mod.rs index 3b5fc3f..76d7105 100644 --- a/src/heap/mod.rs +++ b/src/heap/mod.rs @@ -21,12 +21,7 @@ pub struct TraceEntry { impl HeapCmd { /// Runs the `drone heap` command. pub fn run(&self, shell: &mut StandardStream) -> Result<()> { - let Self { - trace_file, - size, - big_endian, - heap_sub_cmd, - } = self; + let Self { trace_file, size, big_endian, heap_sub_cmd } = self; let size = size.map(Ok).unwrap_or_else(|| { config::Config::read_from_current_dir().map(|config| config.heap.size) })?; @@ -65,23 +60,12 @@ fn print_stats( shell.reset()?; let mut used = 0; for (size, entry) in trace { - writeln!( - shell, - " {: >6} {:11} {:13}", - format_size(*size), - entry.max, - entry.total - )?; + writeln!(shell, " {: >6} {:11} {:13}", format_size(*size), entry.max, entry.total)?; used += size * entry.max; } write!(shell, "Maximum memory usage: ")?; shell.set_color(ColorSpec::new().set_bold(true))?; - writeln!( - shell, - "{} / {:.2}%", - used, - f64::from(used) / f64::from(size) * 100.0 - )?; + writeln!(shell, "{} / {:.2}%", used, f64::from(used) / f64::from(size) * 100.0)?; shell.reset()?; Ok(()) } diff --git a/src/heap/trace.rs b/src/heap/trace.rs index 0e7c1ad..f7ea78e 100644 --- a/src/heap/trace.rs +++ b/src/heap/trace.rs @@ -88,16 +88,8 @@ fn parser( let mut header = [0; 2]; let mut value = [0; 4]; let parse_u32 = move |bytes| { - let value = if big_endian { - u32::from_be_bytes(bytes) - } else { - u32::from_le_bytes(bytes) - }; - if value == 0 { - Err(Error::InvalidValue) - } else { - Ok(value ^ KEY) - } + let value = if big_endian { u32::from_be_bytes(bytes) } else { u32::from_le_bytes(bytes) }; + if value == 0 { Err(Error::InvalidValue) } else { Ok(value ^ KEY) } }; static move || { loop { diff --git a/src/lib.rs b/src/lib.rs index e6fa7d7..a4fcf1f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -60,11 +60,7 @@ use termcolor::StandardStream; impl Cli { /// Runs the program. pub fn run(self) -> Result<()> { - let Self { - cmd, - color, - verbosity, - } = self; + let Self { cmd, color, verbosity } = self; let log_level = match verbosity { 0 => Level::Error, 1 => Level::Warn, diff --git a/src/new.rs b/src/new.rs index dfd1ae1..10efbc4 100644 --- a/src/new.rs +++ b/src/new.rs @@ -20,16 +20,7 @@ use termcolor::{Color, ColorSpec, StandardStream, WriteColor}; impl NewCmd { /// Runs the `drone new` command. pub fn run(&self, shell: &mut StandardStream) -> Result<()> { - let Self { - path, - device, - flash_size, - ram_size, - probe, - probe_itm, - name, - toolchain, - } = self; + let Self { path, device, flash_size, ram_size, probe, probe_itm, name, toolchain } = self; let registry = Registry::new()?; let name = name.as_ref().map(String::as_str).map_or_else( || { @@ -49,10 +40,8 @@ impl NewCmd { }, Ok, )?; - let underscore_name = name - .chars() - .map(|c| if c == '-' { '_' } else { c }) - .collect::(); + let underscore_name = + name.chars().map(|c| if c == '-' { '_' } else { c }).collect::(); cargo_new(path, &toolchain)?; src_main_rs(path, shell)?; @@ -66,16 +55,7 @@ impl NewCmd { } } cargo_toml(path, &name, &device, ®istry, shell)?; - drone_toml( - path, - &device, - *flash_size, - *ram_size, - &probe, - &probe_itm, - ®istry, - shell, - )?; + drone_toml(path, &device, *flash_size, *ram_size, &probe, &probe_itm, ®istry, shell)?; justfile(path, &device, ®istry, shell)?; rust_toolchain(path, &toolchain, ®istry, shell)?; cargo_config(path, ®istry, shell)?; @@ -191,13 +171,9 @@ fn drone_toml( ) -> Result<()> { let path = path.join("Drone.toml"); let mut file = File::create(&path)?; - let probe = probe - .as_ref() - .unwrap_or_else(|| device.probes().first().unwrap()); + let probe = probe.as_ref().unwrap_or_else(|| device.probes().first().unwrap()); file.write_all( - registry - .new_drone_toml(device, flash_size, ram_size, probe, probe_itm)? - .as_bytes(), + registry.new_drone_toml(device, flash_size, ram_size, probe, probe_itm)?.as_bytes(), )?; print_created(shell, "Drone.toml") } diff --git a/src/probe/bmp.rs b/src/probe/bmp.rs index 545b09c..c6a6468 100644 --- a/src/probe/bmp.rs +++ b/src/probe/bmp.rs @@ -32,13 +32,7 @@ pub struct ResetCmd<'a> { impl ResetCmd<'_> { /// Runs the command. pub fn run(self) -> Result<()> { - let Self { - cmd, - signals, - registry, - config, - config_probe, - } = self; + let Self { cmd, signals, registry, config, config_probe } = self; let ProbeResetCmd {} = cmd; let script = registry.bmp_reset(config)?; let mut gdb = Command::new(&config_probe.gdb_client); @@ -63,13 +57,7 @@ pub struct FlashCmd<'a> { impl FlashCmd<'_> { /// Runs the command. pub fn run(self) -> Result<()> { - let Self { - cmd, - signals, - registry, - config, - config_probe, - } = self; + let Self { cmd, signals, registry, config, config_probe } = self; let ProbeFlashCmd { firmware } = cmd; let script = registry.bmp_flash(config)?; let mut gdb = Command::new(&config_probe.gdb_client); @@ -95,13 +83,7 @@ pub struct GdbCmd<'a> { impl GdbCmd<'_> { /// Runs the command. pub fn run(self) -> Result<()> { - let Self { - cmd, - signals, - registry, - config, - config_probe, - } = self; + let Self { cmd, signals, registry, config, config_probe } = self; let ProbeGdbCmd { firmware, reset } = cmd; let script = registry.bmp_gdb(config, *reset)?; let mut gdb = Command::new(&config_probe.gdb_client); @@ -128,20 +110,8 @@ pub struct ItmCmd<'a> { impl ItmCmd<'_> { /// Runs the command. pub fn run(self) -> Result<()> { - let Self { - cmd, - signals, - registry, - config, - config_probe, - config_probe_itm, - shell, - } = self; - let ProbeItmCmd { - ports, - reset, - itmsink_args, - } = cmd; + let Self { cmd, signals, registry, config, config_probe, config_probe_itm, shell } = self; + let ProbeItmCmd { ports, reset, itmsink_args } = cmd; let uart_endpoint = config_probe_itm.uart_endpoint.as_ref().ok_or_else(|| { anyhow!( @@ -163,10 +133,7 @@ impl ItmCmd<'_> { let (pipe, packet) = block_with_signals(&signals, false, move || { let mut packet = [0]; - OpenOptions::new() - .read(true) - .open(&pipe)? - .read_exact(&mut packet)?; + OpenOptions::new().read(true).open(&pipe)?.read_exact(&mut packet)?; Ok((pipe, packet)) })?; @@ -178,10 +145,7 @@ impl ItmCmd<'_> { let _itmsink = finally(|| itmsink.kill().expect("itmsink wasn't running")); block_with_signals(&signals, false, move || { - OpenOptions::new() - .write(true) - .open(&pipe)? - .write_all(&packet)?; + OpenOptions::new().write(true).open(&pipe)?.write_all(&packet)?; Ok(()) })?; diff --git a/src/probe/mod.rs b/src/probe/mod.rs index d72638c..5a417aa 100644 --- a/src/probe/mod.rs +++ b/src/probe/mod.rs @@ -69,54 +69,23 @@ impl ProbeCmd { match probe_sub_cmd { ProbeSubCmd::Reset(cmd) => { if config_probe.bmp.is_some() { - return bmp::ResetCmd { - cmd, - signals, - registry, - config, - config_probe, - } - .run(); + return bmp::ResetCmd { cmd, signals, registry, config, config_probe }.run(); } else if let Some(config_probe_openocd) = &config_probe.openocd { - return openocd::ResetCmd { - cmd, - signals, - registry, - config_probe_openocd, - } - .run(); + return openocd::ResetCmd { cmd, signals, registry, config_probe_openocd } + .run(); } } ProbeSubCmd::Flash(cmd) => { if config_probe.bmp.is_some() { - return bmp::FlashCmd { - cmd, - signals, - registry, - config, - config_probe, - } - .run(); + return bmp::FlashCmd { cmd, signals, registry, config, config_probe }.run(); } else if let Some(config_probe_openocd) = &config_probe.openocd { - return openocd::FlashCmd { - cmd, - signals, - registry, - config_probe_openocd, - } - .run(); + return openocd::FlashCmd { cmd, signals, registry, config_probe_openocd } + .run(); } } ProbeSubCmd::Gdb(cmd) => { if config_probe.bmp.is_some() { - return bmp::GdbCmd { - cmd, - signals, - registry, - config, - config_probe, - } - .run(); + return bmp::GdbCmd { cmd, signals, registry, config, config_probe }.run(); } else if let Some(config_probe_openocd) = &config_probe.openocd { return openocd::GdbCmd { cmd, @@ -157,10 +126,7 @@ impl ProbeCmd { } } } - bail!( - "Suitable debug probe configuration is not found in `{}`", - config::CONFIG_NAME - ); + bail!("Suitable debug probe configuration is not found in `{}`", config::CONFIG_NAME); } } diff --git a/src/probe/openocd.rs b/src/probe/openocd.rs index ffdd7e1..1baa7d3 100644 --- a/src/probe/openocd.rs +++ b/src/probe/openocd.rs @@ -27,12 +27,7 @@ pub struct ResetCmd<'a> { impl ResetCmd<'_> { /// Runs the command. pub fn run(self) -> Result<()> { - let Self { - cmd, - signals, - registry, - config_probe_openocd, - } = self; + let Self { cmd, signals, registry, config_probe_openocd } = self; let ProbeResetCmd {} = cmd; let commands = registry.openocd_reset()?; let mut openocd = Command::new(&config_probe_openocd.command); @@ -54,12 +49,7 @@ pub struct FlashCmd<'a> { impl FlashCmd<'_> { /// Runs the command. pub fn run(self) -> Result<()> { - let Self { - cmd, - signals, - registry, - config_probe_openocd, - } = self; + let Self { cmd, signals, registry, config_probe_openocd } = self; let ProbeFlashCmd { firmware } = cmd; let commands = registry.openocd_flash(firmware)?; let mut openocd = Command::new(&config_probe_openocd.command); @@ -83,14 +73,7 @@ pub struct GdbCmd<'a> { impl GdbCmd<'_> { /// Runs the command. pub fn run(self) -> Result<()> { - let Self { - cmd, - signals, - registry, - config, - config_probe, - config_probe_openocd, - } = self; + let Self { cmd, signals, registry, config, config_probe, config_probe_openocd } = self; let ProbeGdbCmd { firmware, reset } = cmd; let commands = registry.openocd_gdb_openocd(config)?; @@ -125,19 +108,8 @@ pub struct ItmCmd<'a> { impl ItmCmd<'_> { /// Runs the command. pub fn run(self) -> Result<()> { - let Self { - cmd, - signals, - registry, - config, - config_probe_itm, - config_probe_openocd, - } = self; - let ProbeItmCmd { - ports, - reset, - itmsink_args, - } = cmd; + let Self { cmd, signals, registry, config, config_probe_itm, config_probe_openocd } = self; + let ProbeItmCmd { ports, reset, itmsink_args } = cmd; let mut _pipe_dir = None; let mut itmsink = Command::new("itmsink"); diff --git a/src/templates/helpers.rs b/src/templates/helpers.rs index 2640f63..4f994c5 100644 --- a/src/templates/helpers.rs +++ b/src/templates/helpers.rs @@ -77,16 +77,8 @@ impl HelperDef for IfIncludes { rc: &mut RenderContext<'reg>, out: &mut dyn Output, ) -> HelperResult { - let value = h - .param(0) - .ok_or_else(|| RenderError::new("missing parameter"))? - .render(); - let result = h - .params() - .iter() - .skip(1) - .map(PathAndJson::render) - .any(|param| param == value); + let value = h.param(0).ok_or_else(|| RenderError::new("missing parameter"))?.render(); + let result = h.params().iter().skip(1).map(PathAndJson::render).any(|param| param == value); match if result { h.template() } else { h.inverse() } { Some(t) => t.render(r, ctx, rc, out), None => Ok(()), diff --git a/src/utils.rs b/src/utils.rs index 8c2f2ab..b485fdf 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -164,11 +164,7 @@ pub fn check_root_result(color_choice: ColorChoice, f: impl FnOnce() -> Result<( /// Serialize the value to a string. pub fn ser_to_string(value: T) -> String { - serde_json::to_value(value) - .unwrap() - .as_str() - .unwrap() - .to_string() + serde_json::to_value(value).unwrap().as_str().unwrap().to_string() } /// Deserialize a value from the string.