Skip to content

Commit

Permalink
🧹 refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
vnepogodin committed Apr 24, 2024
1 parent bf3f58b commit b4ac3bf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 40 deletions.
25 changes: 12 additions & 13 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ fn fill_devices() -> Option<ListOfDevicesT> {
let mut pacc = libpci::PCIAccess::new(true);

// Get hardware devices
let pci_devices = pacc.devices().expect("Failed");
let pci_devices = pacc.devices()?;
let mut devices = vec![];

for mut iter in pci_devices.iter_mut() {
Expand All @@ -141,24 +141,24 @@ fn fill_devices() -> Option<ListOfDevicesT> {

// let item_base_class = &iter.base_class().unwrap();
// let item_sub_class = &iter.sub_class().unwrap();
let item_class = iter.class().unwrap();
let item_vendor = iter.vendor().unwrap();
let item_device = iter.device().unwrap();
let item_class = iter.class()?;
let item_vendor = iter.vendor()?;
let item_device = iter.device()?;

devices.push(Device {
dev_type: dev_type.clone(),
class_name: item_class,
device_name: item_device,
vendor_name: item_vendor,
class_id: from_hex(iter.class_id().unwrap() as _, 4).to_string(),
device_id: from_hex(iter.device_id().unwrap() as _, 4).to_string(),
vendor_id: from_hex(iter.vendor_id().unwrap() as _, 4).to_string(),
class_id: from_hex(iter.class_id()? as _, 4),
device_id: from_hex(iter.device_id()? as _, 4),
vendor_id: from_hex(iter.vendor_id()? as _, 4),
sysfs_busid: format!(
"{}:{}:{}.{}",
from_hex(iter.domain().unwrap() as _, 4),
from_hex(iter.bus().unwrap() as _, 2),
from_hex(iter.dev().unwrap() as _, 2),
iter.func().unwrap(),
from_hex(iter.domain()? as _, 4),
from_hex(iter.bus()? as _, 2),
from_hex(iter.dev()? as _, 2),
iter.func()?,
),
sysfs_id: "".to_owned(),
available_profiles: vec![],
Expand Down Expand Up @@ -268,8 +268,7 @@ pub fn get_all_devices_of_profile(devices: &ListOfDevicesT, profile: &Profile) -
}

fn add_profile_sorted(profiles: &mut Vec<Arc<Profile>>, new_profile: &Profile) {
let found = profiles.iter().any(|x| new_profile.name == x.name);
if found {
if profiles.iter().any(|x| new_profile.name == x.name) {
return;
}

Expand Down
11 changes: 4 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn perceed_inst_rem(
working_profiles: &mut Vec<String>,
) -> anyhow::Result<()> {
if let Some(values) = args {
let device_type = values[0].clone();
let device_type = &values[0];
let profile = values[1].to_lowercase();

if "pci" != device_type && "usb" != device_type {
Expand All @@ -63,8 +63,8 @@ fn perceed_autoconf(
is_nonfree: &mut bool,
) -> anyhow::Result<()> {
if let Some(values) = args {
let device_type = values[0].clone();
let driver_type = values[1].clone();
let device_type = &values[0];
let driver_type = &values[1];
*is_nonfree = "nonfree" == driver_type;
*autoconf_class_id = values[2].to_lowercase();

Expand Down Expand Up @@ -243,10 +243,7 @@ fn prepare_autoconfigure(
let profile = profile.unwrap();

// If force is not set, then we skip found profile
let mut skip = false;
if !args.force {
skip = installed_profiles.iter().any(|x| x.name == profile.name);
}
let skip = !args.force && installed_profiles.iter().any(|x| x.name == profile.name);

// Print found profile
if skip {
Expand Down
31 changes: 11 additions & 20 deletions src/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,7 @@ impl Default for Profile {

impl Profile {
pub fn new() -> Self {
Self {
is_nonfree: false,
prof_path: "".to_owned(),
name: "".to_owned(),
desc: "".to_owned(),
packages: "".to_owned(),
post_install: "".to_owned(),
post_remove: "".to_owned(),
priority: 0,
hwd_ids: Vec::from([Default::default()]),
device_name_pattern: None,
}
Self { hwd_ids: vec![Default::default()], ..Default::default() }
}
}

Expand All @@ -79,19 +68,20 @@ pub fn parse_profiles(file_path: &str) -> Result<Vec<Profile>> {
if !value.is_table() {
continue;
}
let value_table = value.as_table().unwrap();

let toplevel_profile = parse_profile(value.as_table().unwrap(), key);
let toplevel_profile = parse_profile(value_table, key);
if toplevel_profile.is_err() {
continue;
}

for (nested_key, nested_value) in value.as_table().unwrap().iter() {
for (nested_key, nested_value) in value_table.iter() {
if !nested_value.is_table() {
continue;
}
let nested_profile_name = format!("{}.{}", key, nested_key);
let mut nested_value_table = nested_value.as_table().unwrap().clone();
merge_table_left(&mut nested_value_table, value.as_table().unwrap());
merge_table_left(&mut nested_value_table, value_table);
let nested_profile = parse_profile(&nested_value_table, &nested_profile_name);
if nested_profile.is_err() {
continue;
Expand All @@ -117,20 +107,21 @@ pub fn get_invalid_profiles(file_path: &str) -> Result<Vec<String>> {
if !value.is_table() {
continue;
}
let value_table = value.as_table().unwrap();

let toplevel_profile = parse_profile(value.as_table().unwrap(), key);
let toplevel_profile = parse_profile(value_table, key);
if toplevel_profile.is_err() {
invalid_profile_list.push(key.to_owned());
continue;
}

for (nested_key, nested_value) in value.as_table().unwrap().iter() {
for (nested_key, nested_value) in value_table.iter() {
if !nested_value.is_table() {
continue;
}
let nested_profile_name = format!("{}.{}", key, nested_key);
let mut nested_value_table = nested_value.as_table().unwrap().clone();
merge_table_left(&mut nested_value_table, value.as_table().unwrap());
merge_table_left(&mut nested_value_table, value_table);
let nested_profile = parse_profile(&nested_value_table, &nested_profile_name);
if nested_profile.is_ok() {
continue;
Expand All @@ -144,15 +135,15 @@ pub fn get_invalid_profiles(file_path: &str) -> Result<Vec<String>> {

fn parse_profile(node: &toml::Table, profile_name: &str) -> Result<Profile> {
let mut profile = Profile {
is_nonfree: node.get("nonfree").and_then(|x| x.as_bool()).unwrap_or(false).to_owned(),
is_nonfree: node.get("nonfree").and_then(|x| x.as_bool()).unwrap_or(false),
prof_path: "".to_owned(),
name: profile_name.to_owned(),
packages: node.get("packages").and_then(|x| x.as_str()).unwrap_or("").to_owned(),
post_install: node.get("post_install").and_then(|x| x.as_str()).unwrap_or("").to_owned(),
post_remove: node.get("post_remove").and_then(|x| x.as_str()).unwrap_or("").to_owned(),
desc: node.get("desc").and_then(|x| x.as_str()).unwrap_or("").to_owned(),
priority: node.get("priority").and_then(|x| x.as_integer()).unwrap_or(0) as i32,
hwd_ids: Vec::from([Default::default()]),
hwd_ids: vec![Default::default()],
device_name_pattern: node
.get("device_name_pattern")
.and_then(|x| x.as_str().map(str::to_string)),
Expand Down

0 comments on commit b4ac3bf

Please sign in to comment.