Skip to content

Commit

Permalink
fix partialeq on AbsRelTime, clippy error
Browse files Browse the repository at this point in the history
  • Loading branch information
Zainrax committed Dec 4, 2024
1 parent d2f5174 commit ce64d35
Showing 1 changed file with 39 additions and 84 deletions.
123 changes: 39 additions & 84 deletions src/device_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,11 @@ fn default_activate_thermal_throttler() -> bool {
}

fn default_recording_start_time() -> AbsRelTime {
AbsRelTime {
relative_time_seconds: Some(-(60 * 30)),
absolute_time: None,
}
AbsRelTime { relative_time_seconds: Some(-(60 * 30)), absolute_time: None }
}

fn default_recording_stop_time() -> AbsRelTime {
AbsRelTime {
relative_time_seconds: Some(60 * 30),
absolute_time: None,
}
AbsRelTime { relative_time_seconds: Some(60 * 30), absolute_time: None }
}

#[derive(Debug)]
Expand Down Expand Up @@ -137,10 +131,7 @@ where
}
}
}
_ => error!(
"Region '{}': Must be an array of [[x, y], ...] coordinates",
label
),
_ => error!("Region '{}': Must be an array of [[x, y], ...] coordinates", label),
}
regions.insert(label.clone(), region);
}
Expand Down Expand Up @@ -279,10 +270,7 @@ where
if absolute_time.is_none() && relative_time_seconds.is_none() {
Err(Error::custom(format!("Failed to parse window time: {}", s)))
} else {
Ok(AbsRelTime {
absolute_time,
relative_time_seconds,
})
Ok(AbsRelTime { absolute_time, relative_time_seconds })
}
}

Expand Down Expand Up @@ -340,19 +328,13 @@ struct LocationSettings {
longitude: Option<f32>,
altitude: Option<f32>,

#[serde(
deserialize_with = "timestamp_to_u64",
default = "default_location_timestamp"
)]
#[serde(deserialize_with = "timestamp_to_u64", default = "default_location_timestamp")]
timestamp: Option<u64>,
#[serde(
deserialize_with = "location_accuracy_to_f32",
default = "default_location_accuracy"
)]
#[serde(deserialize_with = "location_accuracy_to_f32", default = "default_location_accuracy")]
accuracy: Option<f32>,
}

#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, Copy)]
struct HourMin {
hour: u8,
min: u8,
Expand All @@ -363,20 +345,28 @@ fn timezone_offset_seconds() -> i32 {
// devices' GPS coordinates to work out correct absolute start/end recording window times.
let now = Local::now();
let local_tz = now.timezone();
local_tz
.offset_from_utc_datetime(&now.naive_utc())
.local_minus_utc()
local_tz.offset_from_utc_datetime(&now.naive_utc()).local_minus_utc()
}
#[derive(PartialEq, Clone)]
#[derive(Clone, Copy)]
pub struct AbsRelTime {
absolute_time: Option<HourMin>,
relative_time_seconds: Option<i32>,
}

impl PartialEq for AbsRelTime {
fn eq(&self, other: &Self) -> bool {
match (self.absolute_time, other.absolute_time) {
(Some(t1), Some(t2)) => t1.hour == t2.hour && t1.min == t2.min,
(None, None) => self.relative_time_seconds == other.relative_time_seconds,
_ => false,
}
}
}

impl Debug for AbsRelTime {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let absolute_time = self.absolute_time.clone();
let relative_time = self.relative_time_seconds.clone();
let absolute_time = self.absolute_time;
let relative_time = self.relative_time_seconds;
if let Some(time) = absolute_time {
return f
.debug_struct("AbsoluteTime")
Expand All @@ -385,12 +375,9 @@ impl Debug for AbsRelTime {
.finish();
}
if let Some(time) = relative_time {
return f
.debug_struct("RelativeOffset")
.field("secs", &time)
.finish();
return f.debug_struct("RelativeOffset").field("secs", &time).finish();
}
Err(fmt::Error::default())
Err(fmt::Error)
}
}

Expand Down Expand Up @@ -498,10 +485,7 @@ pub struct AudioSettings {

impl Default for AudioSettings {
fn default() -> Self {
AudioSettings {
audio_mode: default_audio_mode(),
audio_seed: 0,
}
AudioSettings { audio_mode: default_audio_mode(), audio_seed: 0 }
}
}

Expand All @@ -521,10 +505,7 @@ where
if let Ok(mode) = AudioMode::from_str(&audio_mode_raw) {
Ok(mode)
} else {
Err(Error::custom(format!(
"Failed to parse audio mode: {}",
audio_mode_raw
)))
Err(Error::custom(format!("Failed to parse audio mode: {}", audio_mode_raw)))
}
}

Expand Down Expand Up @@ -607,13 +588,7 @@ impl DeviceConfig {
}

pub fn device_name(&self) -> &[u8] {
self.device_info
.as_ref()
.unwrap()
.name
.as_ref()
.unwrap()
.as_bytes()
self.device_info.as_ref().unwrap().name.as_ref().unwrap().as_bytes()
}

pub fn lat_lng(&self) -> (f32, f32) {
Expand Down Expand Up @@ -655,11 +630,7 @@ impl DeviceConfig {

pub fn is_continuous_recorder(&self) -> bool {
self.recording_settings.constant_recorder
|| (self
.recording_window
.start_recording
.absolute_time
.is_some()
|| (self.recording_window.start_recording.absolute_time.is_some()
&& self.recording_window.stop_recording.absolute_time.is_some()
&& self.recording_window.start_recording == self.recording_window.stop_recording)
}
Expand Down Expand Up @@ -720,17 +691,11 @@ impl DeviceConfig {
start_offset = 86_400 + start_offset;
}
let (window_start, window_end) = if !is_absolute_start || !is_absolute_end {
let location = self
.location
.as_ref()
.expect("Relative recording windows require a location");
let location =
self.location.as_ref().expect("Relative recording windows require a location");
let (lat, lng) = (
location
.latitude
.expect("Relative recording windows require a valid latitude"),
location
.longitude
.expect("Relative recording windows require a valid longitude"),
location.latitude.expect("Relative recording windows require a valid latitude"),
location.longitude.expect("Relative recording windows require a valid longitude"),
);
let altitude = location.altitude;
let yesterday_utc = *now_utc - Duration::days(1);
Expand All @@ -743,13 +708,9 @@ impl DeviceConfig {
.unwrap();
let yesterday_sunset =
yesterday_sunset.naive_utc() + Duration::seconds(start_offset as i64);
let (today_sunrise, today_sunset) = sun_times(
now_utc.date(),
lat as f64,
lng as f64,
altitude.unwrap_or(0.0) as f64,
)
.unwrap();
let (today_sunrise, today_sunset) =
sun_times(now_utc.date(), lat as f64, lng as f64, altitude.unwrap_or(0.0) as f64)
.unwrap();
let today_sunrise = today_sunrise.naive_utc() + Duration::seconds(end_offset as i64);
let today_sunset = today_sunset.naive_utc() + Duration::seconds(start_offset as i64);
let tomorrow_utc = *now_utc + Duration::days(1);
Expand Down Expand Up @@ -884,8 +845,7 @@ impl DeviceConfig {
buf.write_u32::<LittleEndian>(device_id).unwrap();
let audio_mode: u8 = self.audio_info.audio_mode.clone().into();
buf.write_u8(audio_mode).unwrap();
buf.write_u32::<LittleEndian>(self.audio_info.audio_seed)
.unwrap();
buf.write_u32::<LittleEndian>(self.audio_info.audio_seed).unwrap();
let (latitude, longitude) = self.lat_lng();
buf.write_f32::<LittleEndian>(latitude).unwrap();
buf.write_f32::<LittleEndian>(longitude).unwrap();
Expand Down Expand Up @@ -917,15 +877,13 @@ impl DeviceConfig {
buf.write_i32::<LittleEndian>(start_seconds_offset).unwrap();
buf.write_u8(if end_is_abs { 1 } else { 0 }).unwrap();
buf.write_i32::<LittleEndian>(end_seconds_offset).unwrap();
buf.write_u8(if self.is_continuous_recorder() { 1 } else { 0 })
.unwrap();
buf.write_u8(if self.use_low_power_mode() { 1 } else { 0 })
.unwrap();
buf.write_u8(if self.is_continuous_recorder() { 1 } else { 0 }).unwrap();
buf.write_u8(if self.use_low_power_mode() { 1 } else { 0 }).unwrap();

let device_name = self.device_name();
let device_name_length = device_name.len().min(63);
buf.write_u8(device_name_length as u8).unwrap();
buf.write(&device_name[0..device_name_length]).unwrap();
buf.write_all(&device_name[0..device_name_length]).unwrap();
}
}

Expand Down Expand Up @@ -970,10 +928,7 @@ pub fn watch_local_config_file_changes(
// Add a path to be watched. All files and directories at that path and
// below will be monitored for changes.
watcher
.watch(
Path::new("/etc/cacophony/config.toml"),
RecursiveMode::NonRecursive,
)
.watch(Path::new("/etc/cacophony/config.toml"), RecursiveMode::NonRecursive)
.map_err(|e| {
error!("File watcher setup error: {e}");
process::exit(1);
Expand Down

0 comments on commit ce64d35

Please sign in to comment.