cargo_modules/command/structure/
options.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5use std::{fmt::Display, str::FromStr};
6
7use clap::Parser;
8
9use crate::options::{GeneralOptions, ProjectOptions};
10
11#[derive(Copy, Clone, PartialEq, Eq, Debug)]
12pub enum SortBy {
13    Name,
14    Visibility,
15    Kind,
16}
17
18impl FromStr for SortBy {
19    type Err = &'static str;
20
21    fn from_str(s: &str) -> Result<Self, Self::Err> {
22        match s {
23            "name" => Ok(Self::Name),
24            "visibility" => Ok(Self::Visibility),
25            "kind" => Ok(Self::Kind),
26            _ => Err("Unrecognized sort order"),
27        }
28    }
29}
30
31impl Display for SortBy {
32    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33        f.write_str(match self {
34            Self::Name => "name",
35            Self::Visibility => "visibility",
36            Self::Kind => "kind",
37        })
38    }
39}
40
41#[derive(Parser, Clone, PartialEq, Eq, Debug)]
42#[group(id = "GenerateTreeOptions")]
43pub struct Options {
44    #[command(flatten)]
45    pub general: GeneralOptions,
46
47    #[command(flatten)]
48    pub project: ProjectOptions,
49
50    #[command(flatten)]
51    pub selection: SelectionOptions,
52
53    /// The sorting order to use
54    /// (e.g. name, visibility, kind).
55    #[arg(long = "sort-by", default_value = "name")]
56    pub sort_by: SortBy,
57
58    /// Reverses the sorting order.
59    #[arg(long = "sort-reversed")]
60    pub sort_reversed: bool,
61
62    /// Focus the graph on a particular path or use-tree's environment,
63    /// e.g. "foo::bar::{self, baz, blee::*}".
64    #[arg(long = "focus-on")]
65    pub focus_on: Option<String>,
66
67    /// The maximum depth of the generated graph
68    /// relative to the crate's root node, or nodes selected by '--focus-on'.
69    #[arg(long = "max-depth")]
70    pub max_depth: Option<usize>,
71
72    /// Analyze with `#[cfg(test)]` enabled (i.e as if built via `cargo test`).
73    #[arg(long = "cfg-test")]
74    pub cfg_test: bool,
75}
76
77// Important:
78// Some of the `--flag` and `--no-flag` arg pairs might look like they have
79// their documentation comments and clap-args are mixed up, but they have to
80// be that way in order to work-around a limitation of clap:
81// https://jwodder.github.io/kbits/posts/clap-bool-negate/
82// https://github.com/clap-rs/clap/issues/815bug)]
83#[derive(Parser, Clone, PartialEq, Eq, Debug)]
84#[group(id = "SelectionOptions")]
85pub struct SelectionOptions {
86    /// Filter out functions (e.g. fns, async fns, const fns) from tree.
87    #[arg(long = "no-fns")]
88    pub no_fns: bool,
89
90    /// Filter out traits (e.g. trait, unsafe trait) from tree.
91    #[arg(long = "no-traits")]
92    pub no_traits: bool,
93
94    /// Filter out types (e.g. structs, unions, enums) from tree.
95    #[arg(long = "no-types")]
96    pub no_types: bool,
97}