Browse Source

Move should_link and message_format to separate functions

pull/21/head
Steve Cook 2 years ago
parent
commit
06bdf9f289
  1. 40
      src/lib.rs
  2. 38
      src/main.rs

40
src/lib.rs

@ -2,7 +2,7 @@ extern crate core;
pub mod commands; pub mod commands;
use crate::commands::CargoCommand; use crate::commands::{CargoCommand, Input};
use cargo_metadata::{Message, MetadataCommand}; use cargo_metadata::{Message, MetadataCommand};
use core::fmt; use core::fmt;
use rustc_version::Channel; use rustc_version::Channel;
@ -14,6 +14,42 @@ use std::process::{Command, ExitStatus, Stdio};
use std::{env, io, process}; use std::{env, io, process};
use tee::TeeReader; use tee::TeeReader;
const DEFAULT_MESSAGE_FORMAT: &str = "json-render-diagnostics";
pub fn get_should_link(input: &mut Input) -> bool {
input.cmd == CargoCommand::Build
|| (input.cmd == CargoCommand::Test
&& if input.cargo_opts.contains(&"--no-run".to_string()) {
false
} else {
input.cargo_opts.push("--no-run".to_string());
true
})
}
pub fn get_message_format(input: &mut Input) -> String {
if let Some(pos) = input
.cargo_opts
.iter()
.position(|s| s.starts_with("--message-format"))
{
let arg = input.cargo_opts.remove(pos);
let format = if let Some((_, format)) = arg.split_once('=') {
format.to_string()
} else {
input.cargo_opts.remove(pos)
};
if !format.starts_with("json") {
eprintln!("error: non-JSON `message-format` is not supported");
process::exit(1);
} else {
format
}
} else {
DEFAULT_MESSAGE_FORMAT.to_string()
}
}
pub fn build_elf( pub fn build_elf(
cmd: CargoCommand, cmd: CargoCommand,
message_format: &str, message_format: &str,
@ -25,7 +61,7 @@ pub fn build_elf(
let mut tee_reader; let mut tee_reader;
let mut stdout_reader; let mut stdout_reader;
let buf_reader: &mut dyn BufRead = if message_format == "json-render-diagnostics" { let buf_reader: &mut dyn BufRead = if message_format == DEFAULT_MESSAGE_FORMAT {
stdout_reader = BufReader::new(command_stdout); stdout_reader = BufReader::new(command_stdout);
&mut stdout_reader &mut stdout_reader
} else { } else {

38
src/main.rs

@ -1,5 +1,8 @@
use cargo_3ds::commands::{Cargo, CargoCommand}; use cargo_3ds::commands::Cargo;
use cargo_3ds::{build_3dsx, build_elf, build_smdh, check_rust_version, get_metadata, link}; use cargo_3ds::{
build_3dsx, build_elf, build_smdh, check_rust_version, get_message_format, get_metadata,
get_should_link, link,
};
use clap::Parser; use clap::Parser;
use std::process; use std::process;
@ -8,35 +11,8 @@ fn main() {
let Cargo::Input(mut input) = Cargo::parse(); let Cargo::Input(mut input) = Cargo::parse();
let should_link = input.cmd == CargoCommand::Build let should_link = get_should_link(&mut input);
|| (input.cmd == CargoCommand::Test let message_format = get_message_format(&mut input);
&& if input.cargo_opts.contains(&"--no-run".to_string()) {
false
} else {
input.cargo_opts.push("--no-run".to_string());
true
});
let message_format = if let Some(pos) = input
.cargo_opts
.iter()
.position(|s| s.starts_with("--message-format"))
{
let arg = input.cargo_opts.remove(pos);
let format = if let Some((_, format)) = arg.split_once('=') {
format.to_string()
} else {
input.cargo_opts.remove(pos)
};
if !format.starts_with("json") {
eprintln!("error: non-JSON `message-format` is not supported");
process::exit(1);
} else {
format
}
} else {
"json-render-diagnostics".to_string()
};
let (status, messages) = build_elf(input.cmd, &message_format, &input.cargo_opts); let (status, messages) = build_elf(input.cmd, &message_format, &input.cargo_opts);

Loading…
Cancel
Save