Skip to main content

ninja_xtask/
cli.rs

1use std::fmt::Debug;
2
3use bitflags::bitflags;
4use clap::{Parser, Subcommand};
5use clap_cargo::style::CLAP_STYLING as CARGO_STYLING;
6
7#[derive(Parser)]
8#[command(name = "cargo")]
9#[command(bin_name = "cargo")]
10#[command(styles = CARGO_STYLING)]
11pub enum CargoCmd {
12    #[command(subcommand)]
13    Ninja(NinjaCommand),
14}
15
16#[derive(Subcommand)]
17#[command(version)]
18pub enum NinjaCommand {
19    /// fmt, lint & test then stage everything in git if all is good
20    ///
21    /// To provide any specfic environment variables for the executed cargo commands
22    /// create a *workspace* metadata section in Cargo.toml (if this is a single crate, you
23    /// can add the section to the crate Cargo.toml without a problem, if it's a workspace
24    /// add it to the workspace Cargo.toml). For example:
25    ///
26    /// ```toml
27    /// [workspace.metadata.ninja-xtask]
28    /// env = {
29    ///     "LD_LIBRARY_PATH" = false,  # unset variable, do not inherit
30    ///     "RUSTFLAGS" = "flag"        # set variable, overwriting inherited value
31    /// }
32    /// ```
33    Stage {
34        /// add --deny warnings to clippy invocations
35        #[arg(long)]
36        strict: bool,
37        /// output in json format
38        #[arg(long)]
39        json: bool,
40    },
41    /// build (optionally with zigbuild for a given glibc version)
42    Build {
43        /// build for a specific glibc version (WSL-Ubuntu is 2.35)
44        #[arg(short, long)]
45        glibc: Option<String>,
46        /// build a release build (default is cargo's default profile, usually debug)
47        #[arg(short, long)]
48        release: bool,
49        /// build for a given target
50        #[arg(long)]
51        target: Option<String>,
52    },
53}
54
55bitflags! {
56    #[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
57    pub struct CheckFlags: u32 {
58        const STRICT = 0b00000001;
59        const JSON = 0b00000010;
60    }
61}
62
63impl From<&NinjaCommand> for CheckFlags {
64    fn from(xtask: &NinjaCommand) -> Self {
65        match xtask {
66            NinjaCommand::Stage { strict, json } => {
67                let mut flags = Self::default();
68                flags.set(Self::STRICT, *strict);
69                flags.set(Self::JSON, *json);
70                flags
71            }
72            NinjaCommand::Build { .. } => CheckFlags::default(),
73        }
74    }
75}