Skip to content

Commit b0b946b

Browse files
authored
Merge pull request #48 from KSXGitHub/documentation
Improve documentation
2 parents 82ecfda + 78a43a4 commit b0b946b

File tree

6 files changed

+63
-1
lines changed

6 files changed

+63
-1
lines changed

src/data_tree.rs

+14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ pub use Reflection as DataTreeReflection;
77
use super::size::Size;
88

99
/// Disk usage data of a filesystem tree.
10+
///
11+
/// **Construction:** There are 3 main ways to create a `DataTree`:
12+
/// * Use [`FsTreeBuilder`](crate::fs_tree_builder::FsTreeBuilder) to create it from the real
13+
/// filesystem.
14+
/// * Use [`TreeBuilder`](crate::tree_builder::TreeBuilder) to create it from a representation
15+
/// of a filesystem.
16+
/// * Use [`Reflection`].
17+
///
18+
/// **Visualization:** Use the [`Visualizer`](crate::visualizer::Visualizer) struct to create an
19+
/// ASCII chart that visualizes `DataTree`.
20+
///
21+
/// **Serialization and deserialization:** _(feature: `json`)_ `DataTree` does not implement
22+
/// `Serialize` and `Deserialize` traits directly, instead, it can be converted into/from a
23+
/// [`Reflection`] which implements these traits.
1024
#[derive(Debug, PartialEq, Eq)]
1125
pub struct DataTree<Name, Data: Size> {
1226
name: Name,

src/data_tree/reflection.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ use serde::{Deserialize, Serialize};
2424
/// * To safely convert a `Reflection` into a valid `DataTree`,
2525
/// use [`par_try_into_tree`](Self::par_try_into_tree).
2626
///
27-
/// **Serialization and deserialization:** Requires enabling the `json` feature to enable `serde`.
27+
/// **Serialization and deserialization:** _(feature: `json`)_ `Reflection` implements
28+
/// `Serialize` and `Deserialize` traits, this allows functions in `serde_json` to convert
29+
/// a `Reflection` into/from JSON.
2830
#[derive(Debug, Clone, PartialEq, Eq)]
2931
#[cfg_attr(feature = "json", derive(Deserialize, Serialize))]
3032
#[cfg_attr(feature = "json", serde(rename_all = "kebab-case"))]

src/fs_tree_builder.rs

+20
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,25 @@ use std::{
1212
};
1313

1414
/// Build a [`DataTree`] from a directory tree using [`From`] or [`Into`].
15+
///
16+
/// **Example:**
17+
///
18+
/// ```no_run
19+
/// # use parallel_disk_usage::fs_tree_builder::FsTreeBuilder;
20+
/// use parallel_disk_usage::{
21+
/// data_tree::DataTree,
22+
/// os_string_display::OsStringDisplay,
23+
/// reporter::{ErrorOnlyReporter, ErrorReport},
24+
/// size::Bytes,
25+
/// size_getters::GET_APPARENT_SIZE,
26+
/// };
27+
/// let builder = FsTreeBuilder {
28+
/// root: std::env::current_dir().unwrap(),
29+
/// get_data: GET_APPARENT_SIZE,
30+
/// reporter: ErrorOnlyReporter::new(ErrorReport::SILENT),
31+
/// };
32+
/// let data_tree: DataTree<OsStringDisplay, Bytes> = builder.into();
33+
/// ```
1534
#[derive(Debug)]
1635
pub struct FsTreeBuilder<Data, GetData, Report>
1736
where
@@ -34,6 +53,7 @@ where
3453
GetData: Fn(&Metadata) -> Data + Sync,
3554
Report: Reporter<Data> + Sync,
3655
{
56+
/// Create a [`DataTree`] from an [`FsTreeBuilder`].
3757
fn from(builder: FsTreeBuilder<Data, GetData, Report>) -> Self {
3858
let FsTreeBuilder {
3959
root,

src/tree_builder.rs

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ where
3434
GetInfo: Fn(&Path) -> Info<Name, Data> + Copy + Send + Sync,
3535
JoinPath: Fn(&Path, &Name) -> Path + Copy + Send + Sync,
3636
{
37+
/// Create a [`DataTree`] from a [`TreeBuilder`].
3738
fn from(builder: TreeBuilder<Path, Name, Data, GetInfo, JoinPath>) -> Self {
3839
let TreeBuilder {
3940
path,

src/visualizer.rs

+24
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,30 @@ use super::{data_tree::DataTree, size::Size};
1616
use std::{fmt::Display, num::NonZeroUsize};
1717

1818
/// Visualize a [`DataTree`].
19+
///
20+
/// The fields of the struct are the construction parameters of the ASCII chart.
21+
/// The [`Display`] trait can be used to create the ASCII chart.
22+
///
23+
/// **Example:**
24+
///
25+
/// ```no_run
26+
/// # use parallel_disk_usage::data_tree::DataTree;
27+
/// # use parallel_disk_usage::os_string_display::OsStringDisplay;
28+
/// # use parallel_disk_usage::size::Bytes;
29+
/// # use parallel_disk_usage::bytes_format::BytesFormat;
30+
/// # use parallel_disk_usage::visualizer::{Visualizer, Direction, ColumnWidthDistribution};
31+
/// # fn _wrapper(create_data_tree: fn() -> DataTree<OsStringDisplay, Bytes>) {
32+
/// let data_tree: DataTree<OsStringDisplay, Bytes> = create_data_tree();
33+
/// let visualizer = Visualizer {
34+
/// data_tree: &data_tree,
35+
/// bytes_format: BytesFormat::MetricUnits,
36+
/// direction: Direction::BottomUp,
37+
/// column_width_distribution: ColumnWidthDistribution::total(100),
38+
/// max_depth: std::num::NonZeroUsize::new(10).unwrap(),
39+
/// };
40+
/// println!("{}", visualizer);
41+
/// # }
42+
/// ```
1943
#[derive(Debug)]
2044
pub struct Visualizer<'a, Name, Data>
2145
where

src/visualizer/display.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ where
77
Name: Display,
88
Data: Size + Into<u64>,
99
{
10+
/// Create the ASCII chart.
1011
fn fmt(&self, formatter: &mut Formatter<'_>) -> Result<(), Error> {
1112
let write = |line: &String| writeln!(formatter, "{}", line);
1213
match self.direction {

0 commit comments

Comments
 (0)