cargo fmt

This commit is contained in:
NGnius (Graham) 2024-02-02 20:26:43 -05:00
parent 23f5f607cc
commit d714b98dee
10 changed files with 123 additions and 57 deletions

View file

@ -53,13 +53,13 @@ debug = false
strip = true
lto = true
codegen-units = 1
opt-level = 3
[profile.docker]
inherits = "release"
debug = false
strip = true
lto = "thin"
codegen-units = 16
opt-level = 2
codegen-units = 8
debug-assertions = false
overflow-checks = false

View file

@ -111,14 +111,17 @@ pub fn load_variant(
) -> impl Fn(super::ApiParameterType) -> super::ApiParameterType {
let sender = Mutex::new(sender); // Sender is not Sync; this is required for safety
let setter = move |variant: u64, variant_name: Option<String>| {
log::debug!("load_variant(variant: {}, variant_name: {:?})", variant, variant_name);
log::debug!(
"load_variant(variant: {}, variant_name: {:?})",
variant,
variant_name
);
sender
.lock()
.unwrap()
.send(ApiMessage::LoadVariant(
variant,
variant_name
.unwrap_or_else(|| "".to_owned()),
variant_name.unwrap_or_else(|| "".to_owned()),
))
.expect("load_variant send failed")
};

View file

@ -9,11 +9,15 @@ const BASE_URL_FALLBACK: &'static str = "https://powertools.ngni.us";
static BASE_URL: RwLock<Option<String>> = RwLock::new(None);
pub fn set_base_url(base_url: String) {
*BASE_URL.write().expect("Failed to acquire write lock for store base url") = Some(base_url);
*BASE_URL
.write()
.expect("Failed to acquire write lock for store base url") = Some(base_url);
}
fn get_base_url() -> String {
BASE_URL.read().expect("Failed to acquire read lock for store base url")
BASE_URL
.read()
.expect("Failed to acquire read lock for store base url")
.clone()
.unwrap_or_else(|| BASE_URL_FALLBACK.to_owned())
}

View file

@ -26,10 +26,7 @@ impl Args {
}
pub fn is_default(&self) -> bool {
self.port.is_none()
&& self.log.is_none()
&& !self.verbose
&& self.op.is_none()
self.port.is_none() && self.log.is_none() && !self.verbose && self.op.is_none()
}
}

View file

@ -12,7 +12,9 @@ pub fn clean_up() -> Result<(), ()> {
}
}
fn clean_up_io(directories: impl Iterator<Item=impl AsRef<std::path::Path>>) -> std::io::Result<()> {
fn clean_up_io(
directories: impl Iterator<Item = impl AsRef<std::path::Path>>,
) -> std::io::Result<()> {
let results = directories.map(|dir| std::fs::remove_dir_all(dir));
for res in results {
res?;

View file

@ -1,8 +1,8 @@
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::thread::{self, JoinHandle};
use std::sync::mpsc::{channel, Sender};
use std::io::Write;
use std::thread::{self, JoinHandle};
pub fn dump_sys_info() -> Result<(), ()> {
let (tx, rx) = channel();
@ -16,9 +16,7 @@ pub fn dump_sys_info() -> Result<(), ()> {
join_handles.push(read_file(file, tx.clone()));
}
let useful_commands = vec![
"dmidecode",
];
let useful_commands = vec!["dmidecode"];
for cmd in useful_commands.into_iter() {
join_handles.push(execute_command(cmd, tx.clone()));
}
@ -29,38 +27,48 @@ pub fn dump_sys_info() -> Result<(), ()> {
}
}
let mut dump_file = std::fs::File::create("powertools_sys_dump.txt").expect("Failed to create dump file");
let mut dump_file =
std::fs::File::create("powertools_sys_dump.txt").expect("Failed to create dump file");
for response in rx.into_iter() {
dump_file.write(
&format!("{} v{} ###### {} ######\n{}\n",
dump_file
.write(
&format!(
"{} v{} ###### {} ######\n{}\n",
crate::consts::PACKAGE_NAME,
crate::consts::PACKAGE_VERSION,
response.0,
response.1.unwrap_or("[None]".to_owned())
).into_bytes()
).expect("Failed to write to dump file");
)
.into_bytes(),
)
.expect("Failed to write to dump file");
}
Ok(())
}
fn read_file(file: impl AsRef<Path> + Send + 'static, tx: Sender<(String, Option<String>)>) -> JoinHandle<()> {
fn read_file(
file: impl AsRef<Path> + Send + 'static,
tx: Sender<(String, Option<String>)>,
) -> JoinHandle<()> {
thread::spawn(move || {
let file = file.as_ref();
tx.send(
(file.display().to_string(),
std::fs::read_to_string(file).ok())
).expect("Failed to send file contents");
tx.send((
file.display().to_string(),
std::fs::read_to_string(file).ok(),
))
.expect("Failed to send file contents");
})
}
fn execute_command(command: &'static str, tx: Sender<(String, Option<String>)>) -> JoinHandle<()> {
thread::spawn(move || {
tx.send(
(command.to_owned(), Command::new(command)
tx.send((
command.to_owned(),
Command::new(command)
.output()
.map(|out| String::from_utf8_lossy(&out.stdout).into_owned())
.ok()
)).expect("Failed to send command output");
}
)
.ok(),
))
.expect("Failed to send command output");
})
}

View file

@ -50,7 +50,11 @@ fn main() -> Result<(), ()> {
},
#[cfg(not(debug_assertions))]
{
if args.verbose { LevelFilter::Debug } else { LevelFilter::Info }
if args.verbose {
LevelFilter::Debug
} else {
LevelFilter::Info
}
},
Default::default(),
std::fs::File::create(&log_filepath).expect("Failed to create log file"),

View file

@ -71,7 +71,13 @@ impl FileJson {
if setting.name.is_empty() {
setting.name = format!("Variant {}", setting.variant);
}
log::debug!("Inserting setting variant `{}` ({}) for app `{}` ({})", setting.name, setting.variant, file.name, app_id);
log::debug!(
"Inserting setting variant `{}` ({}) for app `{}` ({})",
setting.name,
setting.variant,
file.name,
app_id
);
file.variants.insert(setting.variant, setting.clone());
(file, setting)
} else {
@ -83,15 +89,24 @@ impl FileJson {
if setting.name.is_empty() {
setting.name = format!("Variant {}", setting.variant);
}
log::debug!("Creating new setting variant `{}` ({}) for app `{}` ({})", setting.name, setting.variant, app_name, app_id);
log::debug!(
"Creating new setting variant `{}` ({}) for app `{}` ({})",
setting.name,
setting.variant,
app_name,
app_id
);
let mut setting_variants = HashMap::with_capacity(1);
setting_variants.insert(setting.variant, setting.clone());
(Self {
(
Self {
version: 0,
app_id: app_id,
name: app_name,
variants: setting_variants,
}, setting)
},
setting,
)
};
file.save(path)?;

View file

@ -135,7 +135,8 @@ impl TGeneral for General {
setting: SettingVariant::General,
})
.map(|file| {
file.0.variants
file.0
.variants
.into_iter()
.map(|(id, conf)| crate::api::VariantInfo {
id: id.to_string(),
@ -147,7 +148,11 @@ impl TGeneral for General {
}
fn get_variant_info(&self) -> crate::api::VariantInfo {
log::debug!("Current variant `{}` ({})", self.variant_name, self.variant_id);
log::debug!(
"Current variant `{}` ({})",
self.variant_name,
self.variant_id
);
crate::api::VariantInfo {
id: self.variant_id.to_string(),
name: self.variant_name.clone(),
@ -260,7 +265,10 @@ impl Settings {
let mut valid_ids: Vec<&u64> = settings_file.variants.keys().collect();
valid_ids.sort();
if let Some(id) = valid_ids.get(0) {
Ok(settings_file.variants.get(id).expect("variant id key magically disappeared"))
Ok(settings_file
.variants
.get(id)
.expect("variant id key magically disappeared"))
} else {
Err(SettingError {
msg: format!(
@ -293,7 +301,11 @@ impl Settings {
let json_path = crate::utility::settings_dir().join(&filename);
if json_path.exists() {
if variant == u64::MAX {
log::debug!("Creating new variant `{}` in existing settings file {}", variant_name, json_path.display());
log::debug!(
"Creating new variant `{}` in existing settings file {}",
variant_name,
json_path.display()
);
self.create_and_load_variant(&json_path, app_id, variant_name)?;
} else {
let file_json = FileJson::open(&json_path).map_err(|e| SettingError {
@ -336,7 +348,11 @@ impl Settings {
}
*self.general.persistent() = false;
if variant == u64::MAX {
log::debug!("Creating new variant `{}` in new settings file {}", variant_name, json_path.display());
log::debug!(
"Creating new variant `{}` in new settings file {}",
variant_name,
json_path.display()
);
self.create_and_load_variant(&json_path, app_id, variant_name)?;
}
}
@ -345,7 +361,12 @@ impl Settings {
Ok(*self.general.persistent())
}
fn create_and_load_variant(&mut self, json_path: &PathBuf, app_id: u64, variant_name: String) -> Result<(), SettingError> {
fn create_and_load_variant(
&mut self,
json_path: &PathBuf,
app_id: u64,
variant_name: String,
) -> Result<(), SettingError> {
*self.general.persistent() = true;
self.general.variant_id(u64::MAX);
self.general.variant_name(variant_name.clone());

View file

@ -196,9 +196,20 @@ impl Gpu {
let is_lcd = matches!(self.variant, super::Model::LCD);
let is_lock_feature_enabled = self.limits.extras.quirks.contains("pp_dpm_fclk-static");
if (is_oled && self.limits.extras.quirks.contains("pp_dpm_fclk-reversed-on-OLED"))
|| (is_lcd && self.limits.extras.quirks.contains("pp_dpm_fclk-reversed-on-LCD"))
|| self.limits.extras.quirks.contains("pp_dpm_fclk-reversed") {
if (is_oled
&& self
.limits
.extras
.quirks
.contains("pp_dpm_fclk-reversed-on-OLED"))
|| (is_lcd
&& self
.limits
.extras
.quirks
.contains("pp_dpm_fclk-reversed-on-LCD"))
|| self.limits.extras.quirks.contains("pp_dpm_fclk-reversed")
{
let options_count = self
.sysfs_card
.read_value(GPU_MEMORY_DOWNCLOCK_ATTRIBUTE.to_owned())
@ -213,7 +224,8 @@ impl Gpu {
} else {
use std::fmt::Write;
let mut payload = format!("{}", modifier - max_val);
for i in (0..max_val).rev(/* rev() isn't necessary but it creates a nicer (ascending) order */) {
for i in (0..max_val).rev(/* rev() isn't necessary but it creates a nicer (ascending) order */)
{
write!(payload, " {}", modifier - i)
.expect("Failed to write to memory payload (should be infallible!?)");
}