cargo_modules/command/dependencies/
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 LayoutAlgorithm {
13    None,
14    Dot,
15    Neato,
16    Twopi,
17    Circo,
18    Fdp,
19    Sfdp,
20}
21
22impl FromStr for LayoutAlgorithm {
23    type Err = &'static str;
24
25    fn from_str(s: &str) -> Result<Self, Self::Err> {
26        match s {
27            "none" => Ok(Self::None),
28            "dot" => Ok(Self::Dot),
29            "neato" => Ok(Self::Neato),
30            "twopi" => Ok(Self::Twopi),
31            "circo" => Ok(Self::Circo),
32            "fdp" => Ok(Self::Fdp),
33            "sfdp" => Ok(Self::Sfdp),
34            _ => Err("Unrecognized layout"),
35        }
36    }
37}
38
39impl Display for LayoutAlgorithm {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        f.write_str(match self {
42            Self::None => "none",
43            Self::Dot => "dot",
44            Self::Neato => "neato",
45            Self::Twopi => "twopi",
46            Self::Circo => "circo",
47            Self::Fdp => "fdp",
48            Self::Sfdp => "sfdp",
49        })
50    }
51}
52
53#[derive(Parser, Clone, PartialEq, Eq, Debug)]
54#[group(id = "GenerateSelectionOptions")]
55pub struct Options {
56    #[command(flatten)]
57    pub general: GeneralOptions,
58
59    #[command(flatten)]
60    pub project: ProjectOptions,
61
62    #[command(flatten)]
63    pub selection: SelectionOptions,
64
65    /// Require graph to be acyclic
66    #[arg(long = "acyclic", conflicts_with = "focus_on")]
67    pub acyclic: bool,
68
69    /// The graph layout algorithm to use
70    /// (e.g. none, dot, neato, twopi, circo, fdp, sfdp).
71    #[arg(long = "layout", default_value = "neato")]
72    pub layout: LayoutAlgorithm,
73
74    /// Focus the graph on a particular path or use-tree's environment,
75    /// e.g. "foo::bar::{self, baz, blee::*}".
76    #[arg(long = "focus-on")]
77    pub focus_on: Option<String>,
78
79    /// The maximum depth of the generated graph
80    /// relative to the crate's root node, or nodes selected by '--focus-on'.
81    #[arg(long = "max-depth")]
82    pub max_depth: Option<usize>,
83
84    /// Analyze with `#[cfg(test)]` enabled (i.e as if built via `cargo test`).
85    #[arg(long = "cfg-test")]
86    pub cfg_test: bool,
87}
88
89// Important:
90// Some of the `--flag` and `--no-flag` arg pairs might look like they have
91// their documentation comments and clap-args are mixed up, but they have to
92// be that way in order to work-around a limitation of clap:
93// https://jwodder.github.io/kbits/posts/clap-bool-negate/
94// https://github.com/clap-rs/clap/issues/815bug)]
95#[derive(Parser, Clone, PartialEq, Eq, Debug)]
96#[group(id = "SelectionOptions")]
97pub struct SelectionOptions {
98    /// Filter out extern items from extern crates from graph.
99    #[arg(long = "no-externs")]
100    pub no_externs: bool,
101
102    /// Filter out functions (e.g. fns, async fns, const fns) from graph.
103    #[arg(long = "no-fns")]
104    pub no_fns: bool,
105
106    /// Filter out modules (e.g. `mod foo`, `mod foo {}`) from graph.
107    #[clap(long = "no-modules")]
108    pub no_modules: bool,
109
110    /// Filter out structural "owns" edges from graph.
111    #[clap(long = "no-owns")]
112    pub no_owns: bool,
113
114    /// Filter out sysroot crates (`std`, `core` & friends) from graph.
115    #[arg(long = "no-sysroot")]
116    pub no_sysroot: bool,
117
118    /// Filter out traits (e.g. trait, unsafe trait) from graph.
119    #[arg(long = "no-traits")]
120    pub no_traits: bool,
121
122    /// Filter out types (e.g. structs, unions, enums) from graph.
123    #[arg(long = "no-types")]
124    pub no_types: bool,
125
126    /// Filter out "use" edges from graph.
127    #[arg(long = "no-uses")]
128    pub no_uses: bool,
129}