Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Igigog committed Oct 22, 2023
1 parent 0afa3eb commit fd2077f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 24 deletions.
39 changes: 26 additions & 13 deletions src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@


use std::io::Write;

use crate::{Preprocessing, model::Filters, Codec};

use crate::{model::Filters, Codec, Preprocessing};

pub trait Command {
fn write_as_binary(&self, buf: impl Write);
Expand Down Expand Up @@ -101,18 +98,22 @@ impl Command for SetPcm3060Configuration<'_> {
}
}

pub struct SetConfiguration<'a, 'b, 'c>{
pub struct SetConfiguration<'a, 'b, 'c> {
preprocessing: SetPreprocessingConfiguration<'a>,
filter: SetFilterConfiguration<'b>,
codec: SetPcm3060Configuration<'c>
codec: SetPcm3060Configuration<'c>,
}

impl<'a, 'b, 'c> SetConfiguration<'a, 'b, 'c> {
pub fn new(preprocessing: SetPreprocessingConfiguration<'a>, filter: SetFilterConfiguration<'b>, codec: SetPcm3060Configuration<'c>) -> Self {
pub fn new(
preprocessing: SetPreprocessingConfiguration<'a>,
filter: SetFilterConfiguration<'b>,
codec: SetPcm3060Configuration<'c>,
) -> Self {
Self {
preprocessing,
filter,
codec
codec,
}
}
}
Expand All @@ -121,9 +122,10 @@ impl Command for SetConfiguration<'_, '_, '_> {
fn write_as_binary(&self, mut buf: impl Write) {
let _ = buf.write(&(StructureTypes::SetConfiguration as u16).to_le_bytes());
let _ = buf.write(
&((16 + self.filter.0.to_payload().len()
+ self.preprocessing.0.to_payload().len()
+ self.codec.0.to_payload().len()) as u16)
&((16
+ self.filter.0.to_payload().len()
+ self.preprocessing.0.to_payload().len()
+ self.codec.0.to_payload().len()) as u16)
.to_le_bytes(),
);
let _ = &self.preprocessing.write_as_binary(&mut buf);
Expand Down Expand Up @@ -195,7 +197,11 @@ mod tests {
let config = Preprocessing::new(0.0, 0.0, false);
SetPreprocessingConfiguration::new(&config).write_as_binary(&mut buf);
assert!(buf.len() > 0, "Command didn't write anything");
assert_eq!(buf.as_slice(), &[0, 2, 16, 0, 0, 0, 128, 191, 0, 0, 128, 191, 0, 0, 0, 0], "Wrong data");
assert_eq!(
buf.as_slice(),
&[0, 2, 16, 0, 0, 0, 128, 191, 0, 0, 128, 191, 0, 0, 0, 0],
"Wrong data"
);
}

#[test]
Expand Down Expand Up @@ -228,7 +234,14 @@ mod tests {
let codec = SetPcm3060Configuration::new(&codec_config);
SetConfiguration::new(prep, filters, codec).write_as_binary(&mut buf);
assert!(buf.len() > 0, "Command didn't write anything");
assert_eq!(buf.as_slice(), &[4, 0, 32, 0, 0, 2, 16, 0, 0, 0, 128, 191, 0, 0, 128, 191, 0, 0, 0, 0, 1, 2, 4, 0, 2, 2, 8, 0, 0, 0, 0, 0], "Wrong data")
assert_eq!(
buf.as_slice(),
&[
4, 0, 32, 0, 0, 2, 16, 0, 0, 0, 128, 191, 0, 0, 128, 191, 0, 0, 0, 0, 1, 2, 4, 0,
2, 2, 8, 0, 0, 0, 0, 0
],
"Wrong data"
)
}

#[test]
Expand Down
32 changes: 21 additions & 11 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ use std::fs;
use std::fs::File;
use tauri::PathResolver;

mod model;
mod commands;
mod model;

pub const LIBUSB_RECIPIENT_DEVICE: u8 = 0x00;
pub const LIBUSB_REQUEST_TYPE_VENDOR: u8 = 0x02 << 5;
Expand Down Expand Up @@ -258,7 +258,8 @@ fn send_cmd(
//println!("Write {} bytes to {}", buf.len(), interface.output);
match device
.device_handle
.write_bulk(interface.output, &buf, USB_TIMEOUT) {
.write_bulk(interface.output, &buf, USB_TIMEOUT)
{
Ok(_) => (),
Err(err) => {
error!("Failed to write to the configuration interface: {}", err);
Expand Down Expand Up @@ -289,7 +290,10 @@ fn send_cmd(
}
Err(err) => {
connection.error = true;
return Err(format!("Error reading from the configuration inteface: {}", err));
return Err(format!(
"Error reading from the configuration inteface: {}",
err
));
}
}
}
Expand Down Expand Up @@ -401,17 +405,23 @@ fn reboot_bootloader(connection_state: State<Mutex<ConnectionState>>) -> Result<
}

#[tauri::command]
fn read_version_info(connection_state: State<'_, Mutex<ConnectionState>>) -> Result<VersionInfo, String> {
fn read_version_info(
connection_state: State<'_, Mutex<ConnectionState>>,
) -> Result<VersionInfo, String> {
let v = send_cmd(connection_state, GetVersion::new())?;
let version = VersionInfo::from_buf(&v)?;
Ok(version)
}

#[tauri::command]
fn open(serial_number: &str, connection_state: State<Mutex<ConnectionState>>) -> Result<(), String> {
fn open(
serial_number: &str,
connection_state: State<Mutex<ConnectionState>>,
) -> Result<(), String> {
let context = rusb::Context::new().expect("Can't create libusb::Context::new()");

let devices = context.devices()
let devices = context
.devices()
.map_err(|e| format!("Device not found: {}", e))?;

let mut connection = connection_state.lock().unwrap();
Expand All @@ -430,7 +440,9 @@ fn open(serial_number: &str, connection_state: State<Mutex<ConnectionState>>) ->
i
}
None => {
return Err("Could not detect a configuration interface".to_owned());
return Err(
"Could not detect a configuration interface".to_owned()
);
}
};
info!(
Expand Down Expand Up @@ -505,10 +517,8 @@ fn poll_devices(connection_state: State<Mutex<ConnectionState>>) -> PollDeviceSt
}
};

let serial_number_string_index =
device_desc.serial_number_string_index().unwrap();
let serial_number =
handle.read_string_descriptor_ascii(serial_number_string_index);
let serial_number_string_index = device_desc.serial_number_string_index().unwrap();
let serial_number = handle.read_string_descriptor_ascii(serial_number_string_index);

let sn = match serial_number {
Ok(x) => x,
Expand Down

0 comments on commit fd2077f

Please sign in to comment.