• Stars
    star
    723
  • Rank 62,657 (Top 2 %)
  • Language
    Rust
  • License
    Mozilla Public Li...
  • Created over 8 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Visualize/analyze a Rust crate's internal structure

cargo-modules

Build Status Downloads Version License

Synopsis

A cargo plugin for showing an overview of a crate's modules.

Motivation

With time, as your Rust projects grow bigger and bigger, it gets more and more important to properly structure your code. Fortunately Rust provides us with a quite sophisticated module system, allowing us to neatly split up our crates into arbitrarily small sub-modules of types and functions. While this helps to avoid monolithic and unstructured chunks of code, it can also make it hard at times to still mentally stay on top of the over-all high-level structure of the project at hand.

This is where cargo-modules comes into play:

Installation

Install cargo-modules via:

cargo install cargo-modules

Usage

Print crate as a tree

cargo modules generate tree <OPTIONS>
Usage: cargo-modules generate tree [OPTIONS]

Options:
      --verbose                        Use verbose output
      --lib                            Process only this package's library
      --bin <BIN>                      Process only the specified binary
  -p, --package <PACKAGE>              Package to process (see `cargo help pkgid`)
      --no-default-features            Do not activate the `default` feature
      --all-features                   Activate all available features
      --features <FEATURES>            List of features to activate. This will be ignored if `--cargo-all-features` is provided
      --target <TARGET>                Analyze for target triple
      --cfg-test                       Analyze with `#[cfg(test)]` enabled
      --no-cfg-test                    Analyze with `#[cfg(test)]` disabled. [default]
      --sysroot                        Include sysroot crates (`std`, `core` & friends) in analysis
      --no-sysroot                     Exclude sysroot crates (`std`, `core` & friends) in analysis. [default]
      --manifest-path <MANIFEST_PATH>  Path to Cargo.toml [default: .]
      --focus-on <FOCUS_ON>            Focus the graph on a particular path or use-tree's environment, e.g. "foo:bar::{self, baz, blee::*}"
      --max-depth <MAX_DEPTH>          The maximum depth of the generated graph relative to the crate's root node, or nodes selected by '--focus-on'
      --types                          Include types (e.g. structs, unions, enums)
      --no-types                       Exclude types (e.g. structs, unions, enums). [default]
      --traits                         Include traits (e.g. trait, unsafe trait)
      --no-traits                      Exclude traits (e.g. trait, unsafe trait). [default]
      --fns                            Include functions (e.g. fns, async fns, const fns)
      --no-fns                         Include functions (e.g. fns, async fns, const fns). [default]
      --tests                          Include tests (e.g. `#[test] fn …`)
      --no-tests                       Exclude tests (e.g. `#[test] fn …`). [default]
      --orphans                        Include orphaned modules (i.e. unused files in /src)
      --no-orphans                     Exclude orphaned modules (i.e. unused files in /src). [default]
  -h, --help                           Print help

The following image is the result of using the following command to generate a tree of the smoke test project within its own repo:

cd ./tests/projects/smoke
cargo-modules generate tree --types --tests --orphans

Output of cargo modules generate tree …

Line Structure

The individual lines are structured as follows:

└── <keyword> <name>: <visibility> <test-attributes>

Line Colors

The <keyword> is highlighted in πŸ”΅ blue to visually separate it from the name. Test modules and functions have their corresponding <test-attributes> (i.e. #[cfg(test)] / #[test]) printed next to them in gray and cyan.

The <visibility> (more info) is further more highlighted by the following colors:

Color Meaning
🟒 green Items visible to all and everything (i.e. pub)
🟑 yellow Items visible to the current crate (i.e. pub(crate))
🟠 orange Items visible to a certain parent module (i.e. pub(in path))
πŸ”΄ red Items visible to the current module (i.e. pub(self), implied by lack of pub …)
🟣 purple Orphaned modules (i.e. a file exists on disk but no corresponding mod …)

Print crate as a graph

cargo modules generate graph <OPTIONS>
Usage: cargo-modules generate graph [OPTIONS]

Options:
      --verbose                        Use verbose output
      --lib                            Process only this package's library
      --bin <BIN>                      Process only the specified binary
  -p, --package <PACKAGE>              Package to process (see `cargo help pkgid`)
      --no-default-features            Do not activate the `default` feature
      --all-features                   Activate all available features
      --features <FEATURES>            List of features to activate. This will be ignored if `--cargo-all-features` is provided
      --target <TARGET>                Analyze for target triple
      --cfg-test                       Analyze with `#[cfg(test)]` enabled
      --no-cfg-test                    Analyze with `#[cfg(test)]` disabled. [default]
      --sysroot                        Include sysroot crates (`std`, `core` & friends) in analysis
      --no-sysroot                     Exclude sysroot crates (`std`, `core` & friends) in analysis. [default]
      --manifest-path <MANIFEST_PATH>  Path to Cargo.toml [default: .]
      --focus-on <FOCUS_ON>            Focus the graph on a particular path or use-tree's environment, e.g. "foo:bar::{self, baz, blee::*}"
      --max-depth <MAX_DEPTH>          The maximum depth of the generated graph relative to the crate's root node, or nodes selected by '--focus-on'
      --types                          Include types (e.g. structs, unions, enums)
      --no-types                       Exclude types (e.g. structs, unions, enums). [default]
      --traits                         Include traits (e.g. trait, unsafe trait)
      --no-traits                      Exclude traits (e.g. trait, unsafe trait). [default]
      --fns                            Include functions (e.g. fns, async fns, const fns)
      --no-fns                         Include functions (e.g. fns, async fns, const fns). [default]
      --tests                          Include tests (e.g. `#[test] fn …`)
      --no-tests                       Exclude tests (e.g. `#[test] fn …`). [default]
      --orphans                        Include orphaned modules (i.e. unused files in /src)
      --no-orphans                     Exclude orphaned modules (i.e. unused files in /src). [default]
      --acyclic                        Require graph to be acyclic
      --layout <LAYOUT>                The graph layout algorithm to use (e.g. none, dot, neato, twopi, circo, fdp, sfdp) [default: neato]
      --no-modules                     Exclude modules (e.g. `mod foo`, `mod foo {}`)
      --modules                        Include modules (e.g. `mod foo`, `mod foo {}`). [default]
      --uses                           Include used modules and types
      --no-uses                        Exclude used modules and types [default]
      --externs                        Include used modules and types from extern crates
      --no-externs                     Exclude used modules and types from extern crates [default]
  -h, --help                           Print help


        If you have xdot installed on your system, you can run this using:
        `cargo modules generate dependencies | xdot -`

The following image is the result of using the following command to generate a graph of the smoke test project within its own repo:

cd ./tests/projects/smoke
cargo-modules generate graph --types --tests --orphans | dot -Tsvg

Output of cargo modules generate graph …

Node Structure

The individual nodes are structured as follows:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ <visibility> <keyword> β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚         <path>         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Node Colors

The <visibility> (more info) is further more highlighted by the following colors:

Color Meaning
πŸ”΅ blue Crates (i.e. their implicit root module)
🟒 green Items visible to all and everything (i.e. pub)
🟑 yellow Items visible to the current crate (i.e. pub(crate))
🟠 orange Items visible to a certain parent module (i.e. pub(in path))
πŸ”΄ red Items visible to the current module (i.e. pub(self), implied by lack of pub …)
🟣 purple Orphaned modules (i.e. a file exists on disk but no corresponding mod …)

Acyclic Mode

cargo-modules's generate graph command checks for the presence of a --acyclic flag. If found it will search for cycles in the directed graph and return an error for any cycles it found.

Running cargo modules generate graph --lib --acyclic on the source of the tool itself emits the following cycle error:

Error: Circular dependency between `cargo_modules::options::general` and `cargo_modules::options::generate`.

β”Œ> cargo_modules::options::general
β”‚  └─> cargo_modules::options::generate::graph
β”‚      └─> cargo_modules::options::generate
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

No-Color Mode

cargo-modules checks for the presence of a NO_COLOR environment variable that, when present (regardless of its value), prevents the addition of color to the console output.

Contributing

Please read CONTRIBUTING.md for details on our code of conduct,
and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

License

This project is licensed under the MPL-2.0 – see the LICENSE.md file for details.

More Repositories

1

DLWidgetMenu

Versatile solution for displaying widget menus. Easily adjustable with custom layouts and/or animations.
Objective-C
414
star
2

Gestalt

An unintrusive & light-weight iOS app-theming library with support for animated theme switching.
Swift
325
star
3

DLAlertView

UIAlertView replacement that can embed custom content views, is fully themable and let's you use a delegate and/or blocks.
Objective-C
286
star
4

Pulsar

A versatile solution for displaying pulse animations as known from Apple Maps.
Swift
158
star
5

Rainbows

A Metal-backed, blazingly fast alternative to CAGradientLayer.
Swift
87
star
6

EventBus

A safe-by-default pure Swift alternative to Cocoa's `NSNotificationCenter`
Swift
76
star
7

swift-watch

Watches over your Swift project's source
Swift
48
star
8

DLConstraintLayout

Implementation of CAConstraint/CAConstraintLayoutManager for iOS that is compatible with equivalent OSX APIs.
Objective-C
37
star
9

Strategist

Algorithms for building strong immutable AIs for round-based games.
Swift
36
star
10

DLPDFRenderer

Lightweight solution for generating multi-page PDFs on iOS from arbitrary HTML input.
Objective-C
30
star
11

Sandbox

A simple to use yet versatile API for dealing with sandboxed file access.
Swift
25
star
12

AsyncOperation

A hassle-free implementation of asynchronous NSOperations/NSBlockOperations.
Swift
22
star
13

median

An implementation of an efficient O(n) median filter in Rust.
Rust
21
star
14

rust-seal

Rust implementation of Needleman-Wunsch & Smith-Waterman sequence alignment
Rust
18
star
15

Blues

A type-safe high-level wrapper around Core Bluetooth for iOS
Swift
17
star
16

apply_attr

A syntax extension providing higher-order attributes to Rust.
Rust
16
star
17

Forest

A collection of persistent immutable trees.
Swift
14
star
18

DLColorPicker

Modular color picker for iOS that uses no image resources for rendering
Objective-C
14
star
19

CollectionViewMultiColumnLayout

A tiled waterfal/mosaic UICollectionViewLayout with support for explicit columns.
Swift
13
star
20

XCConfig

XCConfig is a simple Swift implementation of a parser for ".xcconfig" files.
Swift
10
star
21

EnhancedRoundedRectangle

A reimplementation of SwiftUI's RoundedRectangle with support for non-uniform corner radii
Swift
5
star
22

Sync

Useful synchronization primitives in Swift
Swift
5
star
23

DLLog

NSLog-like logging API with support for level and context filtering.
Objective-C
3
star
24

SortedCollection

A sorted collection type for safely performing binary search operations in Swift
Swift
2
star
25

ScopedDefaults

Type-safe and structured UserDefaults in Swift
Swift
2
star
26

github-labels

Set of reasonable default labels and pleasant colors for GitHub projects
2
star
27

var_float

A universal representation of IEEE 754 floating-point numbers convertible between arbitrary precisions.
C++
2
star
28

tryexpand

Test harness for ui tests of macro expansion and its diagnostics
Rust
2
star
29

sitrep

Frontend-agnostic progress reporting for Rust
Rust
2
star
30

talks

Collection of tech-talks I've given over the last years
1
star
31

lasean

Latent Semantic Analysis (LSA) for Ruby.
Ruby
1
star
32

PseudoRandom

Implementations of pseudo-random number generators in Swift
Swift
1
star
33

Interner

Swift implementation of general-purpose interners for every use case
Swift
1
star
34

Captain

Type-safe iOS app routing/coordination
Swift
1
star