|
1 | 1 | # depub: minimise visibility
|
2 | 2 |
|
| 3 | +## Overview |
| 4 | + |
3 | 5 | When working on medium or large sized Rust code bases, it can be hard to know
|
4 | 6 | whether the visibility of functions, structs, and so on are still at the
|
5 | 7 | minimum required. For example, sometimes functions that once needed to be `pub`
|
6 |
| -now only need to be `pub(crate)`. |
| 8 | +now only need to be `pub(crate)`, `pub(super)`, or simply private. |
| 9 | + |
| 10 | +`depub` minimises the visibility of such items in files passed to it, using a |
| 11 | +user-specified command (e.g. `cargo check`) as an oracle to tell if its |
| 12 | +reduction of an item's visibility is valid or not. In essence, `depub` does a |
| 13 | +string search for `pub`, replaces it with `pub crate` and sees if a test |
| 14 | +command still succeeds. If it does, it keeps that visibility, otherwise it |
| 15 | +replaces with the original and tries the next item. Note that `depub` is |
| 16 | +inherently destructive: it overwrites files as it operates, so do not run it on |
| 17 | +source code that you do not want altered! |
| 18 | + |
| 19 | +The list of visibilities that `depub` considers is, in order: `pub`, |
| 20 | +`pub(crate)`, `pub(super)`, and private (i.e. no `pub` keyword at all). `depub` |
| 21 | +searches for `pub`/`pub(crate)`/`pub(super)` instances, reduces their |
| 22 | +visibility by one level, and tries the oracle command. If it succeeds, it tries |
| 23 | +the next lower level until private visibility has been reached. |
| 24 | + |
| 25 | +Since reducing the visibility of one item can enable other items' visibility to |
| 26 | +be reduced, `depub` keeps running "rounds" until a fixed point has been |
| 27 | +reached. The maximum number of rounds is equal to the number of visible items |
| 28 | +in the code base, though in practise 2 or 3 rounds are likely to be all that is |
| 29 | +needed. |
7 | 30 |
|
8 |
| -`depub` minimises the visibility of such elements in a code base: in essence, |
9 |
| -it does a string search for `pub`, replaces it with `pub crate` and sees if a |
10 |
| -test command still succeeds. If it does, it keeps that visibility; otherwise it |
11 |
| -tries the next in the list. As this suggests, `depub` is destructive: it |
12 |
| -overwrites files, so do not run it on source code that you do not want altered! |
13 | 31 |
|
14 |
| -For example, if you want to reduce visibility of a normal build, `cd` to your |
15 |
| -Rust code base and execute: |
| 32 | +## Usage |
| 33 | + |
| 34 | +`depub`'s usage is as follows: |
16 | 35 |
|
17 | 36 | ```
|
18 |
| -$ /path/to/depub -- -c "cargo check" |
| 37 | +depub -c <command> file_1 [... file_n] |
19 | 38 | ```
|
20 | 39 |
|
21 |
| -The string specified with `-c` is passed straight to `/bin/sh` so can be any |
22 |
| -shell command. If you want to test multiple features you might use: |
| 40 | +where `<command>` is a string to be passed to `/bin/sh -c` for execution to |
| 41 | +determine whether the altered source code is still valid. |
| 42 | + |
| 43 | +To reduce the visibility of a normal Rust project, `cd` to your Rust code base |
| 44 | +and execute: |
23 | 45 |
|
24 | 46 | ```
|
25 |
| -$ /path/to/depub -- -c "cargo check && cargo check --features f1" |
| 47 | +$ find . -name "*.rs" | \ |
| 48 | + xargs /path/to/depub -c "cargo check && cargo check --test" |
26 | 49 | ```
|
27 | 50 |
|
28 | 51 | `depub` informs you of its progress. After it is finished, `diff` your code
|
29 |
| -base, and accept those of its recommendations you think appropriate. |
| 52 | +base, and accept those of its recommendations you think appropriate. Note that |
| 53 | +`depub` currently uses string search and replace, so it will merrily change the |
| 54 | +string `pub` in a command into `pub(crate)` -- you should not expect to accept |
| 55 | +its recommendations without at least a cursory check. |
| 56 | + |
| 57 | + |
| 58 | +## Using with libraries |
| 59 | + |
| 60 | +Running `depub` on a library will tend to reduce all its intentionally `pub` |
| 61 | +functions to private visibility. You can weed these out manually after `depub` |
| 62 | +has run, but this can be tedious, and may also have reduced the visibility of a |
| 63 | +cascade of other items. |
| 64 | + |
| 65 | +To avoid this, use one or more users of the library in the oracle command as part |
| 66 | +of your oracle. Temporarily alter their `Cargo.toml` to point to the local |
| 67 | +version of your libary and use a command such as: |
| 68 | + |
| 69 | +``` |
| 70 | +$ find . -name "*.rs" | \ |
| 71 | + xargs /path/to/depub -c " \ |
| 72 | + cargo check && cargo check --test && \ |
| 73 | + cd /path/to/lib && cargo check && cargo check --test" |
| 74 | +``` |
0 commit comments