1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use std::{path::PathBuf, str::FromStr};

use structopt::{clap::ArgGroup, StructOpt};

use crate::commands::Command;

#[derive(StructOpt, Clone, PartialEq, Debug)]
pub struct Options {
    #[structopt(hidden = true, possible_value("modules"))]
    pub dummy: Option<String>,

    #[structopt(subcommand)]
    pub command: Command,
}

impl Options {
    pub fn sanitized_command(self) -> Command {
        let mut command = self.command;
        command.sanitize();
        command
    }
}

pub mod graph {
    use super::*;

    #[derive(StructOpt, Clone, PartialEq, Debug)]
    pub struct Options {
        /// Focus the graph on a particular path's environment.
        #[structopt(long = "focus-on")]
        pub focus_on: Option<String>,

        /// The maximum depth of the generated graph
        /// relative to the node selected by '--focus-on'.
        #[structopt(long = "max-depth")]
        pub max_depth: Option<usize>,

        /// Include types (e.g. structs, enums).
        #[structopt(long = "with-types")]
        pub with_types: bool,

        /// Include tests (e.g. `#[test] fn …`).
        #[structopt(long = "with-tests")]
        pub with_tests: bool,

        /// Include orphaned modules (i.e. unused files in /src).
        #[structopt(long = "with-orphans")]
        pub with_orphans: bool,
    }
}

pub mod generate {
    use super::*;

    pub mod graph {
        use super::*;

        #[derive(Clone, PartialEq, Debug)]
        pub enum LayoutAlgorithm {
            Dot,
            Neato,
            Twopi,
            Circo,
            Fdp,
            Sfdp,
        }

        impl FromStr for LayoutAlgorithm {
            type Err = &'static str;

            fn from_str(s: &str) -> Result<Self, Self::Err> {
                match s {
                    "dot" => Ok(Self::Dot),
                    "neato" => Ok(Self::Neato),
                    "twopi" => Ok(Self::Twopi),
                    "circo" => Ok(Self::Circo),
                    "fdp" => Ok(Self::Fdp),
                    "sfdp" => Ok(Self::Sfdp),
                    _ => Err("Unrecognized layout"),
                }
            }
        }

        impl ToString for LayoutAlgorithm {
            fn to_string(&self) -> String {
                match self {
                    Self::Dot => "dot".to_owned(),
                    Self::Neato => "neato".to_owned(),
                    Self::Twopi => "twopi".to_owned(),
                    Self::Circo => "circo".to_owned(),
                    Self::Fdp => "fdp".to_owned(),
                    Self::Sfdp => "sfdp".to_owned(),
                }
            }
        }

        #[derive(StructOpt, Clone, PartialEq, Debug)]
        pub struct Options {
            #[structopt(flatten)]
            pub general: crate::options::general::Options,

            #[structopt(flatten)]
            pub project: crate::options::project::Options,

            #[structopt(flatten)]
            pub graph: crate::options::graph::Options,

            /// The graph layout algorithm to use
            /// (e.g. dot, neato, twopi, circo, fdp, sfdp).
            #[structopt(long = "layout", default_value = "neato")]
            pub layout: crate::options::generate::graph::LayoutAlgorithm,

            /// Include used modules and types
            #[structopt(long = "with-uses")]
            pub with_uses: bool,

            /// Include used modules and types from extern crates
            #[structopt(long = "with-externs")]
            pub with_externs: bool,
        }
    }

    pub mod tree {
        use super::*;

        #[derive(StructOpt, Clone, PartialEq, Debug)]
        pub struct Options {
            #[structopt(flatten)]
            pub general: crate::options::general::Options,

            #[structopt(flatten)]
            pub project: crate::options::project::Options,

            #[structopt(flatten)]
            pub graph: crate::options::graph::Options,
        }
    }
}

pub mod project {
    use super::*;

    #[derive(StructOpt, Clone, PartialEq, Debug)]
    #[structopt(group = ArgGroup::with_name("target-group"))]
    pub struct Options {
        /// Process only this package's library.
        #[structopt(long = "lib", group = "target-group")]
        pub lib: bool,

        /// Process only the specified binary.
        #[structopt(long = "bin", group = "target-group")]
        pub bin: Option<String>,

        /// Package to process (see `cargo help pkgid`).
        #[structopt(short = "p", long = "package")]
        pub package: Option<String>,

        /// Do not activate the `default` feature.
        #[structopt(long = "no-default-features")]
        pub no_default_features: bool,

        /// Activate all available features.
        #[structopt(long = "all-features")]
        pub all_features: bool,

        /// List of features to activate.
        /// This will be ignored if `--cargo-all-features` is provided.
        #[structopt(long = "features")]
        pub features: Vec<String>,

        /// Analyze for target triple.
        #[structopt(long = "target")]
        pub target: Option<String>,

        /// Analyze with `#[cfg(test)]` enabled.
        #[structopt(long = "cfg-test")]
        pub cfg_test: bool,

        /// Include sysroot crates (`std`, `core` & friends) in analysis.
        #[structopt(long = "with-sysroot")]
        pub with_sysroot: bool,

        /// Path to Cargo.toml.
        #[structopt(long = "manifest-path", parse(from_os_str), default_value = ".")]
        pub manifest_path: PathBuf,
    }
}

pub mod general {
    use super::*;

    #[derive(StructOpt, Clone, PartialEq, Debug)]
    #[structopt(group = ArgGroup::with_name("target-group"))]
    pub struct Options {
        /// Use verbose output.
        #[structopt(long = "verbose")]
        pub verbose: bool,
    }
}