#![forbid(unsafe_code)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![doc = include_str!("../README.md")]
use std::ffi::{OsStr, OsString};
mod cli;
mod commands;
mod output;
mod rustuse;
use anyhow::Result;
use clap::Parser;
use crate::cli::Cli;
pub fn run() -> Result<()> {
run_from(std::env::args_os())
}
pub fn run_cargo_subcommand() -> Result<()> {
run_from(cargo_subcommand_args())
}
pub fn run_from<I, T>(args: I) -> Result<()>
where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
{
let cli = Cli::parse_from(args);
commands::run(cli)
}
fn cargo_subcommand_args() -> Vec<OsString> {
normalize_cargo_subcommand_args(std::env::args_os())
}
fn normalize_cargo_subcommand_args<I, T>(raw_args: I) -> Vec<OsString>
where
I: IntoIterator<Item = T>,
T: Into<OsString>,
{
let mut raw_args = raw_args.into_iter().map(Into::into);
let mut args = Vec::new();
if let Some(binary) = raw_args.next() {
args.push(binary);
}
match raw_args.next() {
Some(first) if first.as_os_str() == OsStr::new("rustuse") => {},
Some(first) => args.push(first),
None => {},
}
args.extend(raw_args);
args
}
#[cfg(test)]
mod tests {
use super::*;
fn os_args(args: &[&str]) -> Vec<OsString> {
args.iter().map(OsString::from).collect()
}
#[test]
fn direct_cargo_rustuse_args_are_preserved() {
assert_eq!(
normalize_cargo_subcommand_args(os_args(&["cargo-rustuse", "report", "."])),
os_args(&["cargo-rustuse", "report", "."])
);
}
#[test]
fn cargo_subcommand_name_is_stripped() {
assert_eq!(
normalize_cargo_subcommand_args(os_args(&["cargo-rustuse", "rustuse", "report", ".",])),
os_args(&["cargo-rustuse", "report", "."])
);
}
#[test]
fn cargo_help_subcommand_shape_is_supported() {
assert_eq!(
normalize_cargo_subcommand_args(os_args(&["cargo-rustuse", "rustuse", "--help",])),
os_args(&["cargo-rustuse", "--help"])
);
}
}