Skip to content

Commit

Permalink
clippy the things
Browse files Browse the repository at this point in the history
  • Loading branch information
BrettMayson committed Dec 4, 2024
1 parent a2a9735 commit 869a16c
Show file tree
Hide file tree
Showing 56 changed files with 198 additions and 170 deletions.
27 changes: 0 additions & 27 deletions arma/src/conn.rs

This file was deleted.

14 changes: 8 additions & 6 deletions arma/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use std::io::{Read, Write};

use arma_rs::{arma, Extension};
use conn::Conn;
use arma_rs::{arma, Context, ContextState, Extension};
use hemtt_common::arma::control::{
fromarma::{self, Control, Message},
toarma,
};
use interprocess::local_socket::{prelude::*, GenericNamespaced, Stream};

mod conn;
mod photoshoot;

#[arma]
Expand All @@ -19,8 +17,8 @@ fn init() -> Extension {
.finish();
let ctx = ext.context();
let (send, recv) = std::sync::mpsc::channel::<Message>();
ctx.global().set(send);
std::thread::spawn(move || {
Conn::set(send);
let mut socket =
Stream::connect("hemtt_arma".to_ns_name::<GenericNamespaced>().unwrap()).unwrap();
socket.set_nonblocking(true).unwrap();
Expand Down Expand Up @@ -73,8 +71,12 @@ fn init() -> Extension {
ext
}

fn mission(mission: String) {
Conn::get()
fn mission(ctx: Context, mission: String) {
let Some(sender) = ctx.global().get::<std::sync::mpsc::Sender<Message>>() else {
println!("`mission` called without a sender");
return;
};
sender
.send(Message::Control(Control::Mission(mission)))
.unwrap();
}
Expand Down
30 changes: 19 additions & 11 deletions arma/src/photoshoot.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
use arma_rs::Group;
use arma_rs::{Context, ContextState, Group};
use hemtt_common::arma::control::fromarma::{Message, Photoshoot};

use crate::conn::Conn;

pub fn group() -> Group {
Group::new()
.command("ready", ready)
.command("weapon", weapon)
.command("previews", previews)
}

fn ready() {
Conn::get()
.send(Message::Photoshoot(Photoshoot::Ready))
.unwrap();
fn ready(ctx: Context) {
let Some(sender) = ctx.global().get::<std::sync::mpsc::Sender<Message>>() else {
println!("`photoshoot:ready` called without a sender");
return;
};
sender.send(Message::Photoshoot(Photoshoot::Ready)).unwrap();
}

fn weapon(weapon: String) {
Conn::get()
fn weapon(ctx: Context, weapon: String) {
let Some(sender) = ctx.global().get::<std::sync::mpsc::Sender<Message>>() else {
println!("`photoshoot:weapon` called without a sender");
return;
};
sender
.send(Message::Photoshoot(Photoshoot::Weapon(weapon)))
.unwrap();
}

fn previews() {
Conn::get()
fn previews(ctx: Context) {
let Some(sender) = ctx.global().get::<std::sync::mpsc::Sender<Message>>() else {
println!("`photoshoot:previews` called without a sender");
return;
};
sender
.send(Message::Photoshoot(Photoshoot::Previews))
.unwrap();
}
1 change: 1 addition & 0 deletions bin/src/commands/launch/error/bcle2_workshop_not_found.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ impl Code for WorkshopNotFound {
}

impl WorkshopNotFound {
#[must_use]
pub fn code() -> Arc<dyn Code> {
Arc::new(Self {})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ impl Code for WorkshopModNotFound {
}

impl WorkshopModNotFound {
#[must_use]
pub fn code(id: String) -> Arc<dyn Code> {
Arc::new(Self { id })
}
Expand Down
1 change: 1 addition & 0 deletions bin/src/commands/launch/error/bcle4_arma_not_found.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl Code for ArmaNotFound {
}

impl ArmaNotFound {
#[must_use]
pub fn code() -> Arc<dyn Code> {
Arc::new(Self {})
}
Expand Down
1 change: 1 addition & 0 deletions bin/src/commands/launch/error/bcle5_missing_main_prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl Code for MissingMainPrefix {
}

impl MissingMainPrefix {
#[must_use]
pub fn code() -> Arc<dyn Code> {
Arc::new(Self {})
}
Expand Down
1 change: 1 addition & 0 deletions bin/src/commands/launch/error/bcle7_can_not_quicklaunch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ impl Code for CanNotQuickLaunch {
}

impl CanNotQuickLaunch {
#[must_use]
pub fn code(reason: String) -> Arc<dyn Code> {
Arc::new(Self { reason })
}
Expand Down
1 change: 1 addition & 0 deletions bin/src/commands/launch/error/bcle9_mission_absolute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ impl Code for MissionAbsolutePath {
}

impl MissionAbsolutePath {
#[must_use]
pub fn code(reason: String) -> Arc<dyn Code> {
Arc::new(Self { reason })
}
Expand Down
20 changes: 20 additions & 0 deletions bin/src/commands/launch/launcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ pub struct Launcher {
}

impl Launcher {
/// Creates a new launcher
///
/// # Errors
/// [`Error::Io`] if the current directory could not be determined
pub fn new(
launch: &LaunchArgs,
options: &LaunchOptions,
Expand Down Expand Up @@ -73,6 +77,10 @@ impl Launcher {
Ok((report, Some(launcher)))
}

/// Adds the current project to the mod list
///
/// # Errors
/// [`Error::Io`] if the current directory could not be determined
pub fn add_self_mod(&mut self) -> Result<(), Error> {
self.workshop.push({
let mut path = std::env::current_dir()?;
Expand All @@ -86,6 +94,11 @@ impl Launcher {
Ok(())
}

/// Adds a preset to the mod list
///
/// # Errors
/// [`Error::Io`] if the current directory could not be determined
/// [`Error::Io`] if the preset could not be read
pub fn add_preset(&mut self, preset: &str, report: &mut Report) -> Result<(), Error> {
let presets = std::env::current_dir()?.join(".hemtt/presets");
trace!("Loading preset: {}", preset);
Expand All @@ -110,6 +123,13 @@ impl Launcher {
}

#[allow(clippy::too_many_lines)]
/// Launches the game
///
/// # Errors
/// [`Error::Io`] if the current directory could not be determined
///
/// # Panics
/// If regex fails to compile
pub fn launch(
&self,
mut args: Vec<String>,
Expand Down
9 changes: 5 additions & 4 deletions bin/src/commands/launch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ pub fn execute(cmd: &Command) -> Result<Report, Error> {
&config,
cmd.launch.config.as_deref().unwrap_or_default(),
&mut report,
)?;
);
let Some(launch) = launch else {
return Ok(report);
};
Expand Down Expand Up @@ -318,11 +318,12 @@ pub fn execute(cmd: &Command) -> Result<Report, Error> {
Ok(report)
}

/// Read a launch configuration
pub fn read_config(
config: &ProjectConfig,
configs: &[String],
report: &mut Report,
) -> Result<Option<LaunchOptions>, Error> {
) -> Option<LaunchOptions> {
let launch = if configs.is_empty() {
config
.hemtt()
Expand Down Expand Up @@ -351,7 +352,7 @@ pub fn read_config(
hemtt_common::config::LaunchOptions::overlay,
)
} else {
return Ok(None);
return None;
};
Ok(Some(launch))
Some(launch)
}
15 changes: 12 additions & 3 deletions bin/src/controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ impl Controller {
self.actions.push(action);
}

/// Run the controller
///
/// # Errors
/// - [`Error::Io`] if profile files cannot be written to disk in the temporary directory
/// - [`Error::Io`] if there is an issue with the local socket
///
/// # Panics
/// - If an message is not able to be read from the local socket
/// - If a message is in an unexpected format
pub fn run(
self,
ctx: &Context,
Expand Down Expand Up @@ -95,8 +104,8 @@ impl Controller {
let len = u32::from_le_bytes(len_buf);
trace!("Receiving: {}", len);
let mut buf = vec![0u8; len as usize];
socket.read_exact(&mut buf).unwrap();
let buf = String::from_utf8(buf).unwrap();
socket.read_exact(&mut buf).expect("Failed to read message");
let buf = String::from_utf8(buf).expect("Failed to parse message");
let message: fromarma::Message = serde_json::from_str(&buf)?;
trace!("Received: {:?}", message);
if let fromarma::Message::Control(control) = message {
Expand All @@ -115,7 +124,7 @@ impl Controller {
self.actions
.iter()
.find(|a| a.missions(ctx).iter().any(|m| &m.1 == current))
.unwrap()
.expect("No action for mission")
.incoming(ctx, message)
.iter()
.for_each(|m| send(m, &mut socket));
Expand Down
4 changes: 2 additions & 2 deletions bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn main() {
std::panic::set_hook(Box::new(|panic| {
error!("{panic}");
eprintln!(
r#"
"
Oh no! HEMTT has crashed!
This is a bug in HEMTT itself, not necessarily your project.
Even if there is a bug in your project, HEMTT should not crash, but gracefully exit with an error message.
Expand All @@ -18,7 +18,7 @@ GitHub (https://github.com/BrettMayson/HEMTT)
The log from the most recent run can be found in `.hemttout/latest.log`.
It is always best to the include the log and a link to your project when reporting a bug, this will help reproduce the issue.
"#
"
);
std::process::exit(1);
}));
Expand Down
2 changes: 1 addition & 1 deletion bin/src/modules/bom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl Module for BOMCheck {
.filter(|e| e.file_type().is_file())
.filter(|e| !e.path().display().to_string().contains(".hemttout"))
.filter(|e| {
e.path().extension().map_or(false, |e| {
e.path().extension().is_some_and(|e| {
!IGNORED_EXTENSIONS.contains(&e.to_str().unwrap_or_default())
})
})
Expand Down
2 changes: 1 addition & 1 deletion bin/src/modules/hook/error/bhe1_script_not_found.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl ScriptNotFound {
.read_dir()?
.iter()
.filter_map(|x| {
if x.is_file().map_or(false, |x| x) {
if x.is_file().is_ok_and(|x| x) {
Some(x.filename().trim_end_matches(".rhai").to_string())
} else {
None
Expand Down
2 changes: 1 addition & 1 deletion bin/src/modules/rapifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub fn rapify(addon: &Addon, path: &WorkspacePath, ctx: &Context) -> Result<Repo
}
let out = if std::path::Path::new(&path.filename())
.extension()
.map_or(false, |ext| ext.eq_ignore_ascii_case("cpp"))
.is_some_and(|ext| ext.eq_ignore_ascii_case("cpp"))
{
if path.filename() == "config.cpp" {
let (version, cfgpatch) = configreport.required_version();
Expand Down
2 changes: 1 addition & 1 deletion bin/src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ fn filter_codes(
true
} else {
!c.include()
&& c.diagnostic().map_or(true, |d| {
&& c.diagnostic().is_none_or(|d| {
d.labels.is_empty() || !d.labels.iter().all(|l| l.file().is_include())
})
}
Expand Down
8 changes: 8 additions & 0 deletions bin/src/utils/photoshoot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ use crate::error::Error;
pub struct Photoshoot {}

impl Photoshoot {
/// Processes a weapon screenshot
///
/// # Errors
/// [`Error::Image`] if the image could not be loaded
pub fn weapon(name: &str, from: &Path) -> Result<ImageBuffer<Rgba<u8>, Vec<u8>>, Error> {
let path = from.join(format!("{name}.png"));
let mut new = image::open(path)?.into_rgba8();
Expand All @@ -30,6 +34,10 @@ impl Photoshoot {
Ok(new)
}

/// Processes an editor preview screenshot
///
/// # Errors
/// [`Error::Image`] if the image could not be loaded
pub fn preview(path: &Path) -> Result<ImageBuffer<Rgb<u8>, Vec<u8>>, Error> {
let new = image::open(path)?.into_rgb8();
let mut new = image::imageops::resize(&new, 455, 256, image::imageops::FilterType::Nearest);
Expand Down
6 changes: 3 additions & 3 deletions bin/src/utils/sqf/case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fn file(file: &Path) -> Result<bool, Error> {
}
('/', InsideQuote::No, InsideComment::No, _) => {
check_buffer(&mut out, &mut buffer, &wiki);
if out.chars().last().map_or(false, |c| c == '/') {
if out.ends_with('/') {
in_comment = InsideComment::Single;
}
out.push(char);
Expand All @@ -118,13 +118,13 @@ fn file(file: &Path) -> Result<bool, Error> {
}
('*', InsideQuote::No, InsideComment::No, _) => {
check_buffer(&mut out, &mut buffer, &wiki);
if out.chars().last().map_or(false, |c| c == '/') {
if out.ends_with('/') {
in_comment = InsideComment::Multi;
}
out.push(char);
}
('/', InsideQuote::No, InsideComment::Multi, _) => {
if out.chars().last().map_or(false, |c| c == '*') {
if out.ends_with('*') {
in_comment = InsideComment::No;
}
out.push(char);
Expand Down
2 changes: 1 addition & 1 deletion libs/common/src/config/project/hemtt/launch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl LaunchOptions {
.map_or_else(|| "arma3_x64", |e| e.as_str());
if std::path::Path::new(executable)
.extension()
.map_or(false, |ext| ext.eq_ignore_ascii_case("exe"))
.is_some_and(|ext| ext.eq_ignore_ascii_case("exe"))
{
(*executable).to_string()
} else {
Expand Down
Loading

0 comments on commit 869a16c

Please sign in to comment.