1#![forbid(unsafe_code)]
9#![cfg_attr(docsrs, feature(doc_cfg))]
10#![doc = include_str!("../README.md")]
11
12use std::ffi::{OsStr, OsString};
13
14mod cli;
15mod commands;
16mod output;
17mod rustuse;
18
19use anyhow::Result;
20use clap::Parser;
21
22use crate::cli::Cli;
23
24pub fn run() -> Result<()> {
25 run_from(std::env::args_os())
26}
27
28pub fn run_cargo_subcommand() -> Result<()> {
29 run_from(cargo_subcommand_args())
30}
31
32pub fn run_from<I, T>(args: I) -> Result<()>
33where
34 I: IntoIterator<Item = T>,
35 T: Into<OsString> + Clone,
36{
37 let cli = Cli::parse_from(args);
38 commands::run(cli)
39}
40
41fn cargo_subcommand_args() -> Vec<OsString> {
42 normalize_cargo_subcommand_args(std::env::args_os())
43}
44
45fn normalize_cargo_subcommand_args<I, T>(raw_args: I) -> Vec<OsString>
46where
47 I: IntoIterator<Item = T>,
48 T: Into<OsString>,
49{
50 let mut raw_args = raw_args.into_iter().map(Into::into);
51 let mut args = Vec::new();
52
53 if let Some(binary) = raw_args.next() {
54 args.push(binary);
55 }
56
57 match raw_args.next() {
58 Some(first) if first.as_os_str() == OsStr::new("rustuse") => {},
59 Some(first) => args.push(first),
60 None => {},
61 }
62
63 args.extend(raw_args);
64 args
65}
66
67#[cfg(test)]
68mod tests {
69 use super::*;
70
71 fn os_args(args: &[&str]) -> Vec<OsString> {
72 args.iter().map(OsString::from).collect()
73 }
74
75 #[test]
76 fn direct_cargo_rustuse_args_are_preserved() {
77 assert_eq!(
78 normalize_cargo_subcommand_args(os_args(&["cargo-rustuse", "report", "."])),
79 os_args(&["cargo-rustuse", "report", "."])
80 );
81 }
82
83 #[test]
84 fn cargo_subcommand_name_is_stripped() {
85 assert_eq!(
86 normalize_cargo_subcommand_args(os_args(&["cargo-rustuse", "rustuse", "report", ".",])),
87 os_args(&["cargo-rustuse", "report", "."])
88 );
89 }
90
91 #[test]
92 fn cargo_help_subcommand_shape_is_supported() {
93 assert_eq!(
94 normalize_cargo_subcommand_args(os_args(&["cargo-rustuse", "rustuse", "--help",])),
95 os_args(&["cargo-rustuse", "--help"])
96 );
97 }
98}