From bfc3fbc6e128cca4d4acb615cb69b596320709c1 Mon Sep 17 00:00:00 2001 From: Empire Phoenix Date: Wed, 7 May 2025 00:01:23 +0200 Subject: [PATCH] cargo fmt --- rust/build.rs | 64 ++++++++++++++++----------------- rust/src/config.rs | 2 +- rust/src/main.rs | 30 ++++++---------- rust/src/plant_hal.rs | 31 +++++----------- rust/src/plant_state.rs | 11 ++---- rust/src/util.rs | 2 +- rust/src/webserver/webserver.rs | 17 ++++----- 7 files changed, 63 insertions(+), 94 deletions(-) diff --git a/rust/build.rs b/rust/build.rs index 1499088..b0169e5 100644 --- a/rust/build.rs +++ b/rust/build.rs @@ -3,39 +3,39 @@ use std::process::Command; use vergen::EmitBuilder; fn main() { println!("cargo:rerun-if-changed=./src/src_webpack"); - Command::new("rm") - .arg("./src/webserver/bundle.js") - .output() - .unwrap(); - - match Command::new("cmd").spawn() { - Ok(_) => { - println!("Assuming build on windows"); - let output = Command::new("cmd") - .arg("/K") - .arg("npx") - .arg("webpack") - .current_dir("./src_webpack") - .output() - .unwrap(); - println!("status: {}", output.status); - println!("stdout: {}", String::from_utf8_lossy(&output.stdout)); - println!("stderr: {}", String::from_utf8_lossy(&output.stderr)); - assert!(output.status.success()); - } - Err(_) => { - println!("Assuming build on linux"); - let output = Command::new("npx") - .arg("webpack") - .current_dir("./src_webpack") - .output() - .unwrap(); - println!("status: {}", output.status); - println!("stdout: {}", String::from_utf8_lossy(&output.stdout)); - println!("stderr: {}", String::from_utf8_lossy(&output.stderr)); - assert!(output.status.success()); - } + Command::new("rm") + .arg("./src/webserver/bundle.js") + .output() + .unwrap(); + + match Command::new("cmd").spawn() { + Ok(_) => { + println!("Assuming build on windows"); + let output = Command::new("cmd") + .arg("/K") + .arg("npx") + .arg("webpack") + .current_dir("./src_webpack") + .output() + .unwrap(); + println!("status: {}", output.status); + println!("stdout: {}", String::from_utf8_lossy(&output.stdout)); + println!("stderr: {}", String::from_utf8_lossy(&output.stderr)); + assert!(output.status.success()); } + Err(_) => { + println!("Assuming build on linux"); + let output = Command::new("npx") + .arg("webpack") + .current_dir("./src_webpack") + .output() + .unwrap(); + println!("status: {}", output.status); + println!("stdout: {}", String::from_utf8_lossy(&output.stdout)); + println!("stderr: {}", String::from_utf8_lossy(&output.stderr)); + assert!(output.status.success()); + } + } embuild::espidf::sysenv::output(); let _ = EmitBuilder::builder().all_git().all_build().emit(); diff --git a/rust/src/config.rs b/rust/src/config.rs index 847bcc7..f70fd25 100644 --- a/rust/src/config.rs +++ b/rust/src/config.rs @@ -114,4 +114,4 @@ impl Default for PlantConfig { moisture_sensor_max_frequency: None, // No override by default } } -} \ No newline at end of file +} diff --git a/rust/src/main.rs b/rust/src/main.rs index 29b92ef..e4a6b93 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -5,8 +5,8 @@ use std::{ use anyhow::{bail, Result}; use chrono::{DateTime, Datelike, Timelike}; -use chrono_tz::Tz::UTC; use chrono_tz::Tz; +use chrono_tz::Tz::UTC; use esp_idf_hal::delay::Delay; use esp_idf_sys::{ esp_ota_get_app_partition_count, esp_ota_get_running_partition, esp_ota_get_state_partition, @@ -196,9 +196,7 @@ fn safe_main() -> Result<()> { } let config: PlantControllerConfig = match board.get_config() { - Ok(valid) => { - valid - } + Ok(valid) => valid, Err(err) => { log( LogMessage::ConfigModeMissingConfig, @@ -270,14 +268,11 @@ fn safe_main() -> Result<()> { Ok(_) => { println!("Started ap, continuing") } - Err(err) => println!( - "Could not start config override ap mode due to {}", - err - ), + Err(err) => println!("Could not start config override ap mode due to {}", err), } } - let timezone = match &config.timezone { + let timezone = match &config.timezone { Some(tz_str) => tz_str.parse::().unwrap_or_else(|_| { println!("Invalid timezone '{}', falling back to UTC", tz_str); UTC @@ -285,7 +280,6 @@ fn safe_main() -> Result<()> { None => UTC, // Fallback to UTC if no timezone is set }; - let timezone_time = cur.with_timezone(&timezone); println!( "Running logic at utc {} and {} {}", @@ -361,13 +355,9 @@ fn safe_main() -> Result<()> { &format!("{}", value), "", ), - TankError::BoardError(err) => log( - LogMessage::TankSensorBoardError, - 0, - 0, - "", - &err.to_string() - ), + TankError::BoardError(err) => { + log(LogMessage::TankSensorBoardError, 0, 0, "", &err.to_string()) + } } // disabled cannot trigger this because of wrapping if is_enabled board.general_fault(true); @@ -459,7 +449,7 @@ fn safe_main() -> Result<()> { (plant_id + 1) as u32, plant_config.pump_time_s as u32, &dry_run.to_string(), - "", + "", ); board.store_last_pump_time(plant_id, cur); board.last_pump_time(plant_id); @@ -624,7 +614,9 @@ fn wait_infinity(wait_type: WaitType, reboot_now: Arc) -> ! { drop(lock); vTaskDelay(delay); - if wait_type == WaitType::MqttConfig && !STAY_ALIVE.load(std::sync::atomic::Ordering::Relaxed) { + if wait_type == WaitType::MqttConfig + && !STAY_ALIVE.load(std::sync::atomic::Ordering::Relaxed) + { reboot_now.store(true, std::sync::atomic::Ordering::Relaxed); } if reboot_now.load(std::sync::atomic::Ordering::Relaxed) { diff --git a/rust/src/plant_hal.rs b/rust/src/plant_hal.rs index 9fb0bc2..96fef55 100644 --- a/rust/src/plant_hal.rs +++ b/rust/src/plant_hal.rs @@ -545,9 +545,7 @@ impl PlantCtrlBoard<'_> { } pub fn consecutive_pump_count(&mut self, plant: usize) -> u32 { - unsafe { - CONSECUTIVE_WATERING_PLANT[plant] - } + unsafe { CONSECUTIVE_WATERING_PLANT[plant] } } pub fn fault(&self, plant: usize, enable: bool) { @@ -568,9 +566,7 @@ impl PlantCtrlBoard<'_> { } pub fn low_voltage_in_cycle(&mut self) -> bool { - unsafe { - LOW_VOLTAGE_DETECTED - } + unsafe { LOW_VOLTAGE_DETECTED } } pub fn any_pump(&mut self, enable: bool) -> Result<()> { @@ -639,8 +635,7 @@ impl PlantCtrlBoard<'_> { self.sensor_multiplexer(sensor_channel)?; self.shift_register.decompose()[MS_4].set_low()?; - self.shift_register.decompose()[SENSOR_ON] - .set_high()?; + self.shift_register.decompose()[SENSOR_ON].set_high()?; let delay = Delay::new_default(); let measurement = 100; // TODO what is this scaling factor? what is its purpose? @@ -652,8 +647,7 @@ impl PlantCtrlBoard<'_> { delay.delay_ms(measurement); self.signal_counter.counter_pause()?; self.shift_register.decompose()[MS_4].set_high()?; - self.shift_register.decompose()[SENSOR_ON] - .set_low()?; + self.shift_register.decompose()[SENSOR_ON].set_low()?; delay.delay_ms(10); let unscaled = self.signal_counter.get_counter_value()? as i32; let hz = unscaled as f32 * factor; @@ -804,7 +798,7 @@ impl PlantCtrlBoard<'_> { } //destroy backup header - let dummy : [u8;0] = []; + let dummy: [u8; 0] = []; self.backup_config(&dummy)?; Ok(()) @@ -812,9 +806,7 @@ impl PlantCtrlBoard<'_> { pub fn get_rtc_time(&mut self) -> Result> { match self.rtc.datetime() { - OkStd(rtc_time) => { - Ok(rtc_time.and_utc()) - } + OkStd(rtc_time) => Ok(rtc_time.and_utc()), Err(err) => { bail!("Error getting rtc time {:?}", err) } @@ -1037,7 +1029,7 @@ impl PlantCtrlBoard<'_> { let mut wait_for_roundtrip = 0; while wait_for_roundtrip < 100 { - wait_for_roundtrip+=1; + wait_for_roundtrip += 1; match round_trip_ok.load(std::sync::atomic::Ordering::Relaxed) { true => { println!("Round trip registered, proceeding"); @@ -1094,12 +1086,7 @@ impl PlantCtrlBoard<'_> { println!("Some error assembling full_topic 2"); bail!("Some error assembling full_topic 2") }; - let publish = client.publish( - &full_topic, - ExactlyOnce, - true, - message, - ); + let publish = client.publish(&full_topic, ExactlyOnce, true, message); Delay::new(10).delay_ms(50); match publish { OkStd(message_id) => { @@ -1109,7 +1096,7 @@ impl PlantCtrlBoard<'_> { String::from_utf8_lossy(message), message_id ); - Ok(()) + Ok(()) } Err(err) => { println!( diff --git a/rust/src/plant_state.rs b/rust/src/plant_state.rs index 86b41a9..b7c9750 100644 --- a/rust/src/plant_state.rs +++ b/rust/src/plant_state.rs @@ -2,10 +2,7 @@ use chrono::{DateTime, TimeDelta, Utc}; use chrono_tz::Tz; use serde::{Deserialize, Serialize}; -use crate::{ - config::PlantConfig, - in_time_range, plant_hal, -}; +use crate::{config::PlantConfig, in_time_range, plant_hal}; const MOIST_SENSOR_MAX_FREQUENCY: f32 = 6500.; // 60kHz (500Hz margin) const MOIST_SENSOR_MIN_FREQUENCY: f32 = 150.; // this is really, really dry, think like cactus levels @@ -239,9 +236,7 @@ impl PlantState { false } } - PlantWateringMode::TimerOnly => { - !self.pump_in_timeout(plant_conf, current_time) - } + PlantWateringMode::TimerOnly => !self.pump_in_timeout(plant_conf, current_time), } } @@ -308,4 +303,4 @@ pub struct PlantInfo<'a> { last_pump: Option>, /// next time when pump should activate next_pump: Option>, -} \ No newline at end of file +} diff --git a/rust/src/util.rs b/rust/src/util.rs index fbb0728..78f24a5 100644 --- a/rust/src/util.rs +++ b/rust/src/util.rs @@ -7,4 +7,4 @@ impl LimitPrecision for f32 { let factor = 10_f32.powi(precision); (self * factor).round() / factor } -} \ No newline at end of file +} diff --git a/rust/src/webserver/webserver.rs b/rust/src/webserver/webserver.rs index b6e59a1..195b184 100644 --- a/rust/src/webserver/webserver.rs +++ b/rust/src/webserver/webserver.rs @@ -82,10 +82,12 @@ fn get_time( ) -> Result, anyhow::Error> { let mut board = BOARD_ACCESS.lock().unwrap(); let native = board - .time().map(|t| t.to_rfc3339()) + .time() + .map(|t| t.to_rfc3339()) .unwrap_or("error".to_string()); let rtc = board - .get_rtc_time().map(|t| t.to_rfc3339()) + .get_rtc_time() + .map(|t| t.to_rfc3339()) .unwrap_or("error".to_string()); let data = LoadData { @@ -137,8 +139,6 @@ fn get_live_moisture( anyhow::Ok(Some(json)) } - - fn get_config( _request: &mut Request<&mut EspHttpConnection>, ) -> Result, anyhow::Error> { @@ -373,16 +373,12 @@ fn flash_bq(filename: &str, dryrun: bool) -> anyhow::Result<()> { anyhow::Ok(()) } - - fn query_param(uri: &str, param_name: &str) -> Option { println!("{uri} get {param_name}"); let parsed = Url::parse(&format!("http://127.0.0.1/{uri}")).unwrap(); let value = parsed.query_pairs().find(|it| it.0 == param_name); match value { - Some(found) => { - Some(found.1.into_owned()) - } + Some(found) => Some(found.1.into_owned()), None => None, } } @@ -678,7 +674,6 @@ pub fn httpd(reboot_now: Arc) -> Box> { server } - fn cors_response( request: Request<&mut EspHttpConnection>, status: u16, @@ -743,4 +738,4 @@ fn read_up_to_bytes_from_request( let allvec = data_store.concat(); println!("Raw data {}", from_utf8(&allvec)?); Ok(allvec) -} \ No newline at end of file +}