Skip to content

Commit

Permalink
chore: cargo clippy and fmt fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gmallios committed Apr 12, 2024
1 parent 72cc5ef commit ab4f647
Show file tree
Hide file tree
Showing 59 changed files with 266 additions and 239 deletions.
5 changes: 2 additions & 3 deletions manager-app/src/async_bridge/bridge.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::sync::Arc;

use log::{debug, info, trace};
use tauri::AppHandle;

use tokio::sync::{mpsc, Mutex};

use soundcore_lib::device::SoundcoreBLEDevice;
use soundcore_lib::{
ble::BLEConnectionManager,
device_manager::{create_device_manager, DeviceManager},
Expand Down Expand Up @@ -138,7 +137,7 @@ async fn handle_command<B: BLEConnectionManager>(
.manager
.get_device(payload.addr)
.await;

if let Some(device) = device {
trace!("Setting sound mode for {:?}", addr_clone);
let res = device.set_sound_mode(payload.sound_mode).await;
Expand Down
10 changes: 4 additions & 6 deletions manager-app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
use std::collections::HashMap;
use std::sync::Arc;

use log::{info, trace};
use log::trace;
use mpsc::channel;
use soundcore_lib::api::SoundcoreDeviceState;
use soundcore_lib::btaddr::BluetoothAdrr;
use tauri::api::notification::Notification;

use tauri::async_runtime::{Mutex, RwLock};
use tauri::{AppHandle, Manager};
use tauri_plugin_log::LogTarget;
Expand Down Expand Up @@ -140,12 +140,10 @@ async fn send_bridge_command(
fn handle_state_update<R: tauri::Runtime>(
last_state: Option<SoundcoreDeviceState>,
new_state: SoundcoreDeviceState,
app_handle: AppHandle<R>,
_app_handle: AppHandle<R>,
) {
if let Some(last_state) = last_state {
if last_state == new_state {
return;
}
if last_state == new_state {}
// This is a bit too verbose for now
// TODO: Add a setting to enable/disable this
// TODO: Improve the notification message based on the values changed
Expand Down
4 changes: 2 additions & 2 deletions soundcore-lib/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod state;
mod feature_set;
mod state;

pub use feature_set::*;
pub use state::*;
pub use feature_set::*;
6 changes: 3 additions & 3 deletions soundcore-lib/src/api/feature_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use std::sync::Arc;
use serde::{Deserialize, Serialize};
use typeshare::typeshare;

mod equalizer_features;
mod flags;
mod sound_mode_features;
mod equalizer_features;

pub use equalizer_features::*;
pub use flags::*;
pub use sound_mode_features::*;
pub use equalizer_features::*;

#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[typeshare]
Expand All @@ -28,4 +28,4 @@ impl Default for DeviceFeatureSet {
flags: Arc::new([]),
}
}
}
}
6 changes: 2 additions & 4 deletions soundcore-lib/src/api/feature_set/equalizer_features.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use serde::{Deserialize, Serialize};
use typeshare::typeshare;

#[derive(
Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash,
)]
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash)]
#[typeshare]
pub struct EqualizerFeatures {
pub bands: u8,
pub channels: u8,
}
}
9 changes: 7 additions & 2 deletions soundcore-lib/src/api/state/state.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
use serde::{Deserialize, Serialize};
use typeshare::typeshare;

use crate::{api::DeviceFeatureSet, models::{AgeRange, Battery, ButtonModel, CustomHearID, EQConfiguration, FirmwareVer, HearID, SerialNumber, SideTone, SoundMode, SoundcoreFeatureFlags, TwsStatus, WearDetection}};
use crate::{
api::DeviceFeatureSet,
models::{
AgeRange, Battery, ButtonModel, EQConfiguration, FirmwareVer, HearID, SerialNumber,
SideTone, SoundMode, TwsStatus, WearDetection,
},
};

/// This is a generalized version of the state for all devices
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Clone, Hash, Default)]
Expand All @@ -23,4 +29,3 @@ pub struct SoundcoreDeviceState {
pub hear_id: Option<HearID>,
pub age_range: Option<AgeRange>,
}

8 changes: 3 additions & 5 deletions soundcore-lib/src/ble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ pub trait BLEConnectionManager {
uuid_set: Option<BLEConnectionUuidSet>,
) -> SoundcoreLibResult<Arc<Self::Connection>>;

async fn adapter_events(&self) -> SoundcoreLibResult<tokio::sync::mpsc::Receiver<BLEAdapterEvent>>;
async fn adapter_events(
&self,
) -> SoundcoreLibResult<tokio::sync::mpsc::Receiver<BLEAdapterEvent>>;
}

#[derive(Debug, Serialize, Deserialize, Clone)]
Expand All @@ -43,7 +45,6 @@ pub enum BLEAdapterEvent {
DeviceDisconnected(BluetoothAdrr),
}


#[async_trait]
pub trait BLEConnection {
fn descriptor(&self) -> BLEDeviceDescriptor;
Expand Down Expand Up @@ -112,7 +113,6 @@ pub struct BLEConnectionUuidSet {
pub write_uuid: uuid::Uuid,
}


#[cfg(all(target_os = "windows", feature = "winrt-backend"))]
impl From<WriteType> for ::windows::Devices::Bluetooth::GenericAttributeProfile::GattWriteOption {
fn from(val: WriteType) -> Self {
Expand All @@ -122,5 +122,3 @@ impl From<WriteType> for ::windows::Devices::Bluetooth::GenericAttributeProfile:
}
}
}


6 changes: 5 additions & 1 deletion soundcore-lib/src/ble/btleplug/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,11 @@ impl BLEConnection for BtlePlugConnection {
self.write_characteristic.clone(),
bytes.to_owned(),
);
trace!("Writing bytes: {:#X?} to characteristic: {:?}", bytes, self.write_characteristic);
trace!(
"Writing bytes: {:#X?} to characteristic: {:?}",
bytes,
self.write_characteristic
);
tokio::spawn(async move {
peripheral
.write(&writer_characteristic, &bytes, write_type.into())
Expand Down
2 changes: 1 addition & 1 deletion soundcore-lib/src/ble/btleplug/connection_factory.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use async_trait::async_trait;
use btleplug::api::{Central, Manager as _, Peripheral as _};
use btleplug::api::{Central, Peripheral as _};
use btleplug::platform::{Adapter, Manager, Peripheral};

use crate::ble::{BLEConnectionFactory, BLEDeviceDescriptor};
Expand Down
17 changes: 10 additions & 7 deletions soundcore-lib/src/ble/btleplug/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use weak_table::WeakValueHashMap;

use crate::ble::btleplug::connection::BtlePlugConnection;
use crate::ble::{
BLEConnectionFactory, BLEConnectionManager, BLEConnectionUuidSet, BLEDeviceDescriptor,
BLEAdapterEvent, BLEDeviceScanner,
BLEAdapterEvent, BLEConnectionFactory, BLEConnectionManager, BLEConnectionUuidSet,
BLEDeviceDescriptor, BLEDeviceScanner,
};
use crate::btaddr::BluetoothAdrr;
use crate::error::{SoundcoreLibError, SoundcoreLibResult};
Expand Down Expand Up @@ -107,7 +107,7 @@ impl BLEConnectionManager for BtlePlugBLEManager {
let mut adapter_events = adapter.events().await.unwrap();
tokio::spawn(async move {
while let Some(evt) = adapter_events.next().await {
let event: Option<BLEAdapterEvent> = evt.try_into().ok();
let event: Option<BLEAdapterEvent> = evt.try_into().ok();
if let Some(event) = event {
tx_clone.send(event).await.unwrap();
}
Expand All @@ -118,18 +118,21 @@ impl BLEConnectionManager for BtlePlugBLEManager {
}
}


impl TryInto<BLEAdapterEvent> for CentralEvent {
type Error = SoundcoreLibError;

fn try_into(self) -> Result<BLEAdapterEvent, Self::Error> {
match self {
CentralEvent::DeviceDisconnected(id) => Ok(BLEAdapterEvent::DeviceDisconnected(id.try_into()?)),
CentralEvent::DeviceConnected(id) => Ok(BLEAdapterEvent::DeviceConnected(id.try_into()?)),
CentralEvent::DeviceDisconnected(id) => {
Ok(BLEAdapterEvent::DeviceDisconnected(id.try_into()?))
}
CentralEvent::DeviceConnected(id) => {
Ok(BLEAdapterEvent::DeviceConnected(id.try_into()?))
}
_ => {
warn!("Unhandled CentralEvent: {:?}", self);
Err(SoundcoreLibError::Unknown)
}
}
}
}
}
4 changes: 2 additions & 2 deletions soundcore-lib/src/ble/windows/scanner.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use collections::HashMap;
use std::{collections, sync};
use std::{collections, sync, time::Duration};
use sync::{Arc, Mutex};

use async_trait::async_trait;
Expand Down Expand Up @@ -64,7 +64,7 @@ impl WindowsBLEDeviceScanner {
impl BLEDeviceScanner for WindowsBLEDeviceScanner {
// type Descriptor = WindowsBLEDescriptor;

async fn scan(&self) -> SoundcoreLibResult<Vec<BLEDeviceDescriptor>> {
async fn scan(&self, duration: Option<Duration>) -> SoundcoreLibResult<Vec<BLEDeviceDescriptor>> {
spawn_blocking(move || {
let addr_swap_map =
Arc::new(Mutex::new(HashMap::<BluetoothAdrr, BluetoothAdrr>::new()));
Expand Down
8 changes: 4 additions & 4 deletions soundcore-lib/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use crate::ble::{BLEConnection, WriteType};
use crate::error::{SoundcoreLibError, SoundcoreLibResult};
use crate::models::{EQConfiguration, SoundMode};
use crate::packets::{
DeviceStateResponse, RequestPacketBuilder, RequestPacketKind, ResponsePacket,
SoundModeCommandBuilder, StateTransformationPacket,
RequestPacketBuilder, RequestPacketKind, ResponsePacket, SoundModeCommandBuilder,
StateTransformationPacket,
};
use crate::parsers::TaggedData;
use crate::types::SupportedModels;
Expand Down Expand Up @@ -187,11 +187,11 @@ where
let mut new_state = state_sender.borrow().clone();
new_state.sound_mode = sound_mode;
state_sender.send_replace(new_state);

Ok(())
}

pub async fn set_eq(&self, eq: EQConfiguration) -> SoundcoreLibResult<()> {
pub async fn set_eq(&self, _eq: EQConfiguration) -> SoundcoreLibResult<()> {
todo!()
}
}
10 changes: 8 additions & 2 deletions soundcore-lib/src/device_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::{sync::Arc, time::Duration};

use crate::ble::btleplug::manager::BtlePlugBLEManager;
use crate::ble::BLEAdapterEvent;
#[cfg(any(test, feature = "mock"))]
use crate::mocks::*;
Expand All @@ -17,6 +16,10 @@ use serde::{Deserialize, Serialize};
use tokio::sync::RwLock;
use typeshare::typeshare;

// TODO: Specify clippy & fmt features
#[allow(unused_imports)]
use crate::ble::btleplug::manager::BtlePlugBLEManager;

pub struct DeviceManager<B>
where
B: BLEConnectionManager,
Expand Down Expand Up @@ -57,7 +60,10 @@ where
}
}

pub async fn get_device(&self, addr: BluetoothAdrr) -> Option<Arc<SoundcoreBLEDevice<B::Connection>>> {
pub async fn get_device(
&self,
addr: BluetoothAdrr,
) -> Option<Arc<SoundcoreBLEDevice<B::Connection>>> {
self.ble_devices.read().await.get(&addr).cloned()
}

Expand Down
2 changes: 1 addition & 1 deletion soundcore-lib/src/devices/a3027.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
mod features;

pub use features::*;
pub use features::*;
2 changes: 1 addition & 1 deletion soundcore-lib/src/devices/a3027/features.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use crate::api::{DeviceFeatureSet, EqualizerFeatures, FeatureFlags, SoundModeFeatures};
use crate::api::{DeviceFeatureSet, EqualizerFeatures, SoundModeFeatures};

pub fn a3027_features() -> DeviceFeatureSet {
DeviceFeatureSet {
Expand Down
2 changes: 1 addition & 1 deletion soundcore-lib/src/devices/a3028.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
mod features;

pub use features::*;
pub use features::*;
2 changes: 1 addition & 1 deletion soundcore-lib/src/devices/a3028/features.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use crate::api::{DeviceFeatureSet, EqualizerFeatures, FeatureFlags, SoundModeFeatures};
use crate::api::{DeviceFeatureSet, EqualizerFeatures, SoundModeFeatures};

pub fn a3028_features() -> DeviceFeatureSet {
DeviceFeatureSet {
Expand Down
2 changes: 1 addition & 1 deletion soundcore-lib/src/devices/a3029.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
mod features;

pub use features::*;
pub use features::*;
2 changes: 1 addition & 1 deletion soundcore-lib/src/devices/a3029/features.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use crate::api::{DeviceFeatureSet, EqualizerFeatures, FeatureFlags, SoundModeFeatures};
use crate::api::{DeviceFeatureSet, EqualizerFeatures, SoundModeFeatures};

pub fn a3029_features() -> DeviceFeatureSet {
DeviceFeatureSet {
Expand Down
2 changes: 1 addition & 1 deletion soundcore-lib/src/devices/a3040.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ mod features;
mod sound_mode_update_command;

pub use features::*;
pub use sound_mode_update_command::*;
pub use sound_mode_update_command::*;
2 changes: 1 addition & 1 deletion soundcore-lib/src/devices/a3930.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
mod features;

pub use features::*;
pub use features::*;
6 changes: 2 additions & 4 deletions soundcore-lib/src/devices/a3930/features.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use std::sync::Arc;

use crate::api::{DeviceFeatureSet, EqualizerFeatures, FeatureFlags, SoundModeFeatures};
use crate::api::{DeviceFeatureSet, EqualizerFeatures, SoundModeFeatures};

pub fn a3930_features() -> DeviceFeatureSet {
DeviceFeatureSet {
// A3030 Seems to have no sound modes
sound_mode_features: Some(
SoundModeFeatures::new(&[], &[], true),
),
sound_mode_features: Some(SoundModeFeatures::new(&[], &[], true)),
equalizer_features: Some(EqualizerFeatures {
bands: 8,
channels: 1,
Expand Down
2 changes: 1 addition & 1 deletion soundcore-lib/src/devices/a3951.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
mod features;

pub use features::*;
pub use features::*;
2 changes: 1 addition & 1 deletion soundcore-lib/src/devices/a3951/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ pub fn a3951_features() -> DeviceFeatureSet {
FeatureFlags::WEAR_DETECTION,
]),
}
}
}
3 changes: 1 addition & 2 deletions soundcore-lib/src/mocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ mod connection_factory;
mod connection_manager;
mod scanner;


pub use connection::*;
pub use connection_factory::*;
pub use connection_manager::*;
pub use scanner::*;
pub use scanner::*;
Loading

0 comments on commit ab4f647

Please sign in to comment.