Skip to content

Commit 8b96cb7

Browse files
committed
api+json: Reform JsonData into a struct
1 parent cac4efd commit 8b96cb7

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

src/app.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub use sub::Sub;
44

55
use crate::{
66
args::{Args, Quantity},
7-
json_data::JsonData,
7+
json_data::{JsonData, UnitAndTree},
88
reporter::{ErrorOnlyReporter, ErrorReport, ProgressAndErrorReporter, ProgressReport},
99
runtime_error::RuntimeError,
1010
size::{Bytes, Size},
@@ -59,9 +59,10 @@ impl App {
5959
} = self.args;
6060
let direction = Direction::from_top_down(top_down);
6161

62-
let json_data = stdin()
62+
let unit_and_tree = stdin()
6363
.pipe(serde_json::from_reader::<_, JsonData>)
64-
.map_err(RuntimeError::DeserializationFailure)?;
64+
.map_err(RuntimeError::DeserializationFailure)?
65+
.unit_and_tree;
6566

6667
macro_rules! visualize {
6768
($reflection:expr, $bytes_format: expr) => {{
@@ -79,9 +80,9 @@ impl App {
7980
}};
8081
}
8182

82-
let visualization = match json_data {
83-
JsonData::Bytes(reflection) => visualize!(reflection, bytes_format),
84-
JsonData::Blocks(reflection) => visualize!(reflection, ()),
83+
let visualization = match unit_and_tree {
84+
UnitAndTree::Bytes(reflection) => visualize!(reflection, bytes_format),
85+
UnitAndTree::Blocks(reflection) => visualize!(reflection, ()),
8586
};
8687

8788
print!("{}", visualization); // it already ends with "\n", println! isn't needed here.

src/app/sub.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{
22
args::Fraction,
33
data_tree::{DataTree, DataTreeReflection},
44
fs_tree_builder::FsTreeBuilder,
5-
json_data::JsonData,
5+
json_data::{JsonData, UnitAndTree},
66
os_string_display::OsStringDisplay,
77
reporter::ParallelReporter,
88
runtime_error::RuntimeError,
@@ -19,7 +19,7 @@ where
1919
Data: Size + Into<u64> + Serialize + Send + Sync,
2020
Report: ParallelReporter<Data> + Sync,
2121
GetData: Fn(&Metadata) -> Data + Copy + Sync,
22-
DataTreeReflection<String, Data>: Into<JsonData>,
22+
DataTreeReflection<String, Data>: Into<UnitAndTree>,
2323
{
2424
/// List of files and/or directories.
2525
pub files: Vec<PathBuf>,
@@ -48,7 +48,7 @@ where
4848
Data: Size + Into<u64> + Serialize + Send + Sync,
4949
Report: ParallelReporter<Data> + Sync,
5050
GetData: Fn(&Metadata) -> Data + Copy + Sync,
51-
DataTreeReflection<String, Data>: Into<JsonData>,
51+
DataTreeReflection<String, Data>: Into<UnitAndTree>,
5252
{
5353
/// Run the sub program.
5454
pub fn run(self) -> Result<(), RuntimeError> {
@@ -116,11 +116,12 @@ where
116116
};
117117

118118
if json_output {
119-
let json_data: JsonData = data_tree
119+
let unit_and_tree: UnitAndTree = data_tree
120120
.into_reflection() // I really want to use std::mem::transmute here but can't.
121121
.par_convert_names_to_utf8() // TODO: allow non-UTF8 somehow.
122122
.expect("convert all names from raw string to UTF-8")
123123
.into();
124+
let json_data = JsonData { unit_and_tree };
124125
return serde_json::to_writer(stdout(), &json_data)
125126
.map_err(RuntimeError::SerializationFailure);
126127
}

src/json_data.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,23 @@ use derive_more::{From, TryInto};
77
#[cfg(feature = "json")]
88
use serde::{Deserialize, Serialize};
99

10-
/// Output of the program with `--json-output` flag as well as
11-
/// input of the program with `--json-input` flag.
10+
/// The `"unit"` field and the `"tree"` field of [`JsonData`].
1211
#[derive(Debug, Clone, From, TryInto)]
1312
#[cfg_attr(feature = "json", derive(Deserialize, Serialize))]
1413
#[cfg_attr(feature = "json", serde(tag = "unit", content = "tree"))]
15-
pub enum JsonData {
14+
pub enum UnitAndTree {
1615
/// Tree where data is [bytes](Bytes).
1716
Bytes(Reflection<String, Bytes>),
1817
/// Tree where data is [blocks](Blocks).
1918
Blocks(Reflection<String, Blocks>),
2019
}
20+
21+
/// Output of the program with `--json-output` flag as well as
22+
/// input of the program with `--json-input` flag.
23+
#[derive(Debug, Clone)]
24+
#[cfg_attr(feature = "json", derive(Deserialize, Serialize))]
25+
pub struct JsonData {
26+
/// The `"unit"` field and the `"tree"` field.
27+
#[cfg_attr(feature = "json", serde(flatten))]
28+
pub unit_and_tree: UnitAndTree,
29+
}

0 commit comments

Comments
 (0)