Skip to content

Commit

Permalink
Fixes for pedantic clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
nikarh committed Apr 1, 2024
1 parent ece1885 commit 216e0e9
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 71 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@ local-ip-address = "0.6.1"

[lints.clippy]
pedantic = { level = "deny", priority = -1 }
module_name_repetitions = "allow"
similar_names = "allow"
unwrap_used = "deny"
expect_used = "deny"
22 changes: 11 additions & 11 deletions deny.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
[graph]
targets = ["x86_64-unknown-linux-musl"]
targets = [
"x86_64-pc-windows-msvc",
"x86_64-apple-darwin",
"x86_64-unknown-linux-gnu",
]
all-features = true
no-default-features = false

Expand All @@ -15,17 +19,9 @@ yanked = "deny"
[licenses]
version = 2
private = { ignore = true }
allow = [
"MIT",
"Apache-2.0",
"MPL-2.0",
"BSD-3-Clause",
"Zlib",
]
allow = ["MIT", "Apache-2.0", "MPL-2.0", "BSD-3-Clause", "Zlib"]
confidence-threshold = 0.9
exceptions = [
{ name = "unicode-ident", allow = ["Unicode-DFS-2016"] },
]
exceptions = [{ name = "unicode-ident", allow = ["Unicode-DFS-2016"] }]

[bans]
multiple-versions = "deny"
Expand All @@ -37,6 +33,10 @@ allow-wildcard-paths = true

skip = [
{ name = "syn", version = "1.0.109", reason = "local-ip-address" },
{ name = "windows-sys", version = "0.48.0", reason = "local-ip-address and colored" },
{ name = "windows_x86_64_msvc", version = "0.48.5", reason = "local-ip-address and colored" },
{ name = "windows_x86_64_gnu", version = "0.48.5", reason = "local-ip-address and colored" },
{ name = "windows-targets", version = "0.48.5", reason = "local-ip-address and colored" },
]

[sources]
Expand Down
6 changes: 4 additions & 2 deletions src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use anyhow::{anyhow, Context};
use log::error;
use rustc_version::Channel;

pub fn check_rust_version() {
let rust_version = rustc_version::version_meta().unwrap();
pub fn rust_version() -> anyhow::Result<()> {
let rust_version = rustc_version::version_meta()?;

if rust_version.channel > Channel::Nightly {
error!(
Expand All @@ -23,6 +23,8 @@ pub fn check_rust_version() {
);
process::exit(1);
}

Ok(())
}

pub fn set_cargo_config_env() -> anyhow::Result<()> {
Expand Down
51 changes: 27 additions & 24 deletions src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
process::{Command, Stdio},
};

use crate::ftp;
use crate::{check, ftp};
use anyhow::{bail, Context};
use cargo_metadata::{camino::Utf8PathBuf, Artifact, Message, Package};
use clap::{Args, Subcommand};
Expand Down Expand Up @@ -37,7 +37,7 @@ pub struct Build {
#[arg(allow_hyphen_values = true)]
#[arg(global = true)]
#[arg(name = "CARGO_ARGS")]
build_args: Vec<String>,
cargo_args: Vec<String>,
}

#[derive(Subcommand, Debug)]
Expand Down Expand Up @@ -131,6 +131,8 @@ impl ExecutableArtifact {

impl Executor for Build {
fn execute(&self) -> anyhow::Result<()> {
check::rust_version()?;

let ctx = BuildContext::new(self)?;

match &self.cmd {
Expand All @@ -154,7 +156,7 @@ impl Executor for Build {

if args.update {
let files = ctx.eboot_uploads(&artifacts)?;
ctx.upload(&files, &args.connection.clone().required()?)?;
upload(&files, &args.connection.clone().required()?)?;
}

if args.run {
Expand Down Expand Up @@ -188,7 +190,7 @@ impl Executor for Build {
}

if !upload_files.is_empty() {
ctx.upload(&upload_files, &args.eboot.connection.clone().required()?)?;
upload(&upload_files, &args.eboot.connection.clone().required()?)?;
}

if args.eboot.run {
Expand Down Expand Up @@ -240,7 +242,7 @@ impl<'a> BuildContext<'a> {
.arg(VITA_TARGET)
.arg("--message-format")
.arg("json-render-diagnostics")
.args(&self.command.build_args)
.args(&self.command.cargo_args)
.stdin(Stdio::inherit())
.stdout(Stdio::piped())
.stderr(Stdio::inherit());
Expand Down Expand Up @@ -399,7 +401,7 @@ impl<'a> BuildContext<'a> {

let files = WalkDir::new(&assets)
.into_iter()
.filter_map(|e| e.ok())
.filter_map(std::result::Result::ok)
.filter(|e| e.file_type().is_file());

for file in files {
Expand Down Expand Up @@ -429,6 +431,7 @@ impl<'a> BuildContext<'a> {
Ok(())
}

#[allow(clippy::unused_self)]
fn vpk_uploads(
&self,
artifacts: &[ExecutableArtifact],
Expand Down Expand Up @@ -472,24 +475,6 @@ impl<'a> BuildContext<'a> {
.collect::<anyhow::Result<Vec<_>>>()
}

fn upload(&self, files: &[(Utf8PathBuf, String)], conn: &ConnectionArgs) -> anyhow::Result<()> {
if files.is_empty() {
return Ok(());
}

let mut ftp = ftp::connect(conn)?;

for (src, dest) in files {
info!("{} {src} {} {dest}", "Uploading".blue(), "file to".blue());

let src = File::open(src).context("Unable to open source file")?;
ftp.put_file(dest, &mut BufReader::new(src))
.context("Failed to upload file")?;
}

Ok(())
}

fn run(&self, artifacts: &[ExecutableArtifact], conn: &ConnectionArgs) -> anyhow::Result<()> {
if let Some(art) = artifacts.last() {
let title_id = art
Expand All @@ -511,6 +496,24 @@ impl<'a> BuildContext<'a> {
}
}

fn upload(files: &[(Utf8PathBuf, String)], conn: &ConnectionArgs) -> anyhow::Result<()> {
if files.is_empty() {
return Ok(());
}

let mut ftp = ftp::connect(conn)?;

for (src, dest) in files {
info!("{} {src} {} {dest}", "Uploading".blue(), "file to".blue());

let src = File::open(src).context("Unable to open source file")?;
ftp.put_file(dest, &mut BufReader::new(src))
.context("Failed to upload file")?;
}

Ok(())
}

trait CommandExt {
fn pass_env<K, V>(&mut self, key: K, default: impl Fn() -> V) -> &mut Command
where
Expand Down
6 changes: 3 additions & 3 deletions src/commands/coredump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl Executor for Coredump {
bail!("vita-parse-core failed");
}
} else {
warn!("{}", "No coredump files found.".yellow())
warn!("{}", "No coredump files found.".yellow());
}
}
CoredumpCmd::Clean(args) => {
Expand All @@ -135,15 +135,15 @@ impl Executor for Coredump {
info!("{}: {file}", "Deleting file".blue());

match ftp.rm(file) {
Ok(_) => {}
Ok(()) => {}
Err(FtpError::UnexpectedResponse(e))
if String::from_utf8_lossy(&e.body).contains("226 File deleted") => {}
Err(e) => return Err(e).context("Unable to delete file"),
}
}

if counter == 0 {
warn!("{}", "No coredump files found.".yellow())
warn!("{}", "No coredump files found.".yellow());
}
}
}
Expand Down
39 changes: 23 additions & 16 deletions src/commands/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,10 @@ impl PrincessLogConfig {
}

fn serialize(&self) -> Vec<u8> {
let flags = match self.kernel_debug {
true => NLM_CONFIG_FLAGS_BIT_QAF_DEBUG_PRINTF,
false => 0,
let flags = if self.kernel_debug {
NLM_CONFIG_FLAGS_BIT_QAF_DEBUG_PRINTF
} else {
0
};

let mut res = Vec::with_capacity(16);
Expand Down Expand Up @@ -120,18 +121,24 @@ impl Logs {
info!("{}", "Found existing config".blue());

match PrincessLogConfig::parse(&mut file) {
Ok(c) => match c.magic == MAGIC {
true => info!(
"{} {ip}:{port} {} {kdbg}",
"Existing config has address".yellow(),
"and kernel debug print is".yellow(),
ip = c.ip,
port = c.port,
kdbg = c.kernel_debug
),
false => warn!("{}", "Existing config has invalid magic".red()),
},
Err(err) => warn!("{}: {err}", "Failed to parse existing config".red()),
Ok(c) => {
if c.magic == MAGIC {
info!(
"{} {ip}:{port} {} {kdbg}",
"Existing config has address".yellow(),
"and kernel debug print is".yellow(),
ip = c.ip,
port = c.port,
kdbg = c.kernel_debug
);
} else {
warn!("{}", "Existing config has invalid magic".red());
}
}

Err(err) => {
warn!("{}: {err}", "Failed to parse existing config".red());
}
};
}
Err(err) => {
Expand Down Expand Up @@ -191,7 +198,7 @@ impl Logs {
break;
}
Ok(bytes_read) => {
print!("{}", String::from_utf8_lossy(&buffer[..bytes_read]))
print!("{}", String::from_utf8_lossy(&buffer[..bytes_read]));
}
Err(e) => {
error!("{}: {}", "Error reading from client", e);
Expand Down
7 changes: 5 additions & 2 deletions src/commands/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ impl Executor for Upload {
)
.context("Uploading file failed")?;
} else if source.is_dir() {
for file in WalkDir::new(source).into_iter().filter_map(|e| e.ok()) {
for file in WalkDir::new(source)
.into_iter()
.filter_map(std::result::Result::ok)
{
let source_path = file.path();

let destination = format!(
Expand Down Expand Up @@ -97,7 +100,7 @@ impl Executor for Upload {
if ftp.cwd(&destination).is_err() {
info!("{} {destination}", "Creating directory".blue());
match ftp.mkdir(&destination) {
Ok(_) => {}
Ok(()) => {}
Err(FtpError::UnexpectedResponse(e))
if String::from_utf8_lossy(&e.body)
.starts_with("226 Directory created.") => {}
Expand Down
4 changes: 1 addition & 3 deletions src/ftp.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::ops::Deref;

use anyhow::Context;
use colored::Colorize;
use log::info;
Expand All @@ -8,7 +6,7 @@ use suppaftp::FtpStream;
use crate::commands::ConnectionArgs;

pub fn connect(conn: &ConnectionArgs) -> anyhow::Result<FtpStream> {
let ip = conn.vita_ip.deref();
let ip = &*conn.vita_ip;
let port = conn.ftp_port;

info!("{} {ip}:{port}", "Connecting to Vita FTP server".blue());
Expand Down
5 changes: 1 addition & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ mod ftp;
mod meta;
mod nc;

use check::check_rust_version;
use clap::Parser;
use colored::Colorize;
use commands::{Cargo, Executor};
Expand All @@ -26,11 +25,9 @@ fn main() {
})
.init();

check_rust_version();

let Cargo::Input(input) = Cargo::parse();
match input.cmd.execute() {
Ok(_) => {}
Ok(()) => {}
Err(e) => {
error!("{}", format!("{e:?}").red());
std::process::exit(1);
Expand Down
14 changes: 9 additions & 5 deletions src/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ pub struct PackageMetadata {
impl Default for PackageMetadata {
fn default() -> Self {
Self {
title_id: Default::default(),
title_name: Default::default(),
assets: Default::default(),
title_id: None,
title_name: None,
assets: None,
build_std: default_build_std(),
vita_strip_flags: default_vita_strip_flags(),
vita_make_fself_flags: default_vita_make_fself_flags(),
Expand All @@ -112,7 +112,7 @@ pub fn parse_crate_metadata(

let pkg = match artifact {
Some(artifact) => meta.packages.iter().find(|p| p.id == artifact.package_id),
None => meta.workspace_default_packages().first().cloned(),
None => meta.workspace_default_packages().first().copied(),
};

if let Some(pkg) = pkg {
Expand All @@ -126,5 +126,9 @@ pub fn parse_crate_metadata(
}
}

Ok((Default::default(), pkg.cloned(), meta.target_directory))
Ok((
PackageMetadata::default(),
pkg.cloned(),
meta.target_directory,
))
}
2 changes: 1 addition & 1 deletion src/nc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn nc(ip: &str, port: u16, command: &str) -> anyhow::Result<()> {

let mut stream =
TcpStream::connect((ip, port)).context("Unable to connect to command server")?;
let command = format!("{}\n", command);
let command = format!("{command}\n");
stream
.write_all(command.as_bytes())
.context("Unable to write to TCP socket")?;
Expand Down

0 comments on commit 216e0e9

Please sign in to comment.