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(Copy, Clone, PartialEq, Eq, Debug)]
54pub enum SplinesType {
55    None,
56    Line,
57    Spline,
58    Ortho,
59}
60
61impl FromStr for SplinesType {
62    type Err = &'static str;
63
64    fn from_str(s: &str) -> Result<Self, Self::Err> {
65        match s {
66            "none" => Ok(Self::None),
67            "line" => Ok(Self::Line),
68            "spline" => Ok(Self::Spline),
69            "ortho" => Ok(Self::Ortho),
70            _ => Err("Unrecognized splines' type"),
71        }
72    }
73}
74
75impl Display for SplinesType {
76    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77        f.write_str(match self {
78            Self::None => "none",
79            Self::Line => "line",
80            Self::Spline => "spline",
81            Self::Ortho => "ortho",
82        })
83    }
84}
85
86#[derive(Parser, Clone, PartialEq, Eq, Debug)]
87#[group(id = "GenerateSelectionOptions")]
88pub struct Options {
89    #[command(flatten)]
90    pub general: GeneralOptions,
91
92    #[command(flatten)]
93    pub project: ProjectOptions,
94
95    #[command(flatten)]
96    pub selection: SelectionOptions,
97
98    /// Require graph to be acyclic
99    #[arg(long = "acyclic", conflicts_with = "focus_on")]
100    pub acyclic: bool,
101
102    /// The graph layout algorithm to use
103    /// (e.g. none, dot, neato, twopi, circo, fdp, sfdp).
104    #[arg(long = "layout", default_value = "neato")]
105    pub layout: LayoutAlgorithm,
106
107    /// The different types to draw lines between nodes
108    /// (e.g. none, line, spline, ortho).
109    #[arg(long = "splines", default_value = "line")]
110    pub splines: SplinesType,
111
112    /// Focus the graph on a particular path or use-tree's environment,
113    /// e.g. "foo::bar::{self, baz, blee::*}".
114    #[arg(long = "focus-on")]
115    pub focus_on: Option<String>,
116
117    /// The maximum depth of the generated graph
118    /// relative to the crate's root node, or nodes selected by '--focus-on'.
119    #[arg(long = "max-depth")]
120    pub max_depth: Option<usize>,
121
122    /// Analyze with `#[cfg(test)]` enabled (i.e as if built via `cargo test`).
123    #[arg(long = "cfg-test")]
124    pub cfg_test: bool,
125}
126
127// Important:
128// Some of the `--flag` and `--no-flag` arg pairs might look like they have
129// their documentation comments and clap-args are mixed up, but they have to
130// be that way in order to work-around a limitation of clap:
131// https://jwodder.github.io/kbits/posts/clap-bool-negate/
132// https://github.com/clap-rs/clap/issues/815bug)]
133#[derive(Parser, Clone, PartialEq, Eq, Debug)]
134#[group(id = "SelectionOptions")]
135pub struct SelectionOptions {
136    /// Filter out extern items from extern crates from graph.
137    #[arg(long = "no-externs")]
138    pub no_externs: bool,
139
140    /// Filter out functions (e.g. fns, async fns, const fns) from graph.
141    #[arg(long = "no-fns")]
142    pub no_fns: bool,
143
144    /// Filter out modules (e.g. `mod foo`, `mod foo {}`) from graph.
145    #[clap(long = "no-modules")]
146    pub no_modules: bool,
147
148    /// Filter out structural "owns" edges from graph.
149    #[clap(long = "no-owns")]
150    pub no_owns: bool,
151
152    /// Filter out sysroot crates (`std`, `core` & friends) from graph.
153    #[arg(long = "no-sysroot")]
154    pub no_sysroot: bool,
155
156    /// Filter out traits (e.g. trait, unsafe trait) from graph.
157    #[arg(long = "no-traits")]
158    pub no_traits: bool,
159
160    /// Filter out types (e.g. structs, unions, enums) from graph.
161    #[arg(long = "no-types")]
162    pub no_types: bool,
163
164    /// Filter out "use" edges from graph.
165    #[arg(long = "no-uses")]
166    pub no_uses: bool,
167
168    /// Filter out `pub(self)` (i.e. private) items
169    #[arg(long = "no-private")]
170    pub no_private: bool,
171
172    /// Filter out `pub(crate)` items
173    #[arg(long = "no-pub-crate")]
174    pub no_pub_crate: bool,
175
176    /// Filter out `pub(module)` items by their module name
177    #[arg(
178        long = "no-pub-module",
179        value_delimiter = ',',
180        conflicts_with = "no_pub_modules"
181    )]
182    pub no_pub_module: Vec<String>,
183
184    /// Filter out `pub(module)` items
185    #[arg(long = "no-pub-modules", conflicts_with = "no_pub_module")]
186    pub no_pub_modules: bool,
187
188    /// Filter out `pub(super)` items
189    #[arg(long = "no-pub-super")]
190    pub no_pub_super: bool,
191}