diff --git a/src/lib.rs b/src/lib.rs index d2279ef..5ea8379 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,7 @@ extern crate core; pub mod commands; -use crate::commands::CargoCommand; +use crate::commands::{CargoCommand, Input}; use cargo_metadata::{Message, MetadataCommand}; use core::fmt; use rustc_version::Channel; @@ -14,6 +14,42 @@ use std::process::{Command, ExitStatus, Stdio}; use std::{env, io, process}; 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( cmd: CargoCommand, message_format: &str, @@ -25,7 +61,7 @@ pub fn build_elf( let mut tee_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); &mut stdout_reader } else { diff --git a/src/main.rs b/src/main.rs index 6897aaa..cda6c62 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,8 @@ -use cargo_3ds::commands::{Cargo, CargoCommand}; -use cargo_3ds::{build_3dsx, build_elf, build_smdh, check_rust_version, get_metadata, link}; +use cargo_3ds::commands::Cargo; +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 std::process; @@ -8,35 +11,8 @@ fn main() { let Cargo::Input(mut input) = Cargo::parse(); - let should_link = 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 - }); - - 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 should_link = get_should_link(&mut input); + let message_format = get_message_format(&mut input); let (status, messages) = build_elf(input.cmd, &message_format, &input.cargo_opts);