Skip to content

Commit e6b130c

Browse files
committed
feat(api)!: pass params into Sub::json_output
1 parent f516d15 commit e6b130c

2 files changed

Lines changed: 35 additions & 14 deletions

File tree

src/app.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use clap::Parser;
1717
use hdd::any_path_is_in_hdd;
1818
use pipe_trait::Pipe;
1919
use std::{io::stdin, time::Duration};
20+
use sub::JsonOutputParam;
2021
use sysinfo::Disks;
2122

2223
#[cfg(unix)]
@@ -263,13 +264,11 @@ impl App {
263264
reporter: <$size_getter as CreateReporter<$progress>>::create_reporter(report_error),
264265
bytes_format: <$size_getter as GetSizeUtils>::formatter(bytes_format),
265266
files,
266-
json_output,
267+
json_output: JsonOutputParam::from_cli_flags(json_output, omit_json_shared_details, omit_json_shared_summary),
267268
column_width_distribution,
268269
max_depth,
269270
min_ratio,
270271
no_sort,
271-
omit_json_shared_details,
272-
omit_json_shared_summary,
273272
}
274273
.run(),
275274
)*} };

src/app/sub.rs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ where
2828
/// List of files and/or directories.
2929
pub files: Vec<PathBuf>,
3030
/// Print JSON data instead of an ASCII chart.
31-
pub json_output: bool,
31+
pub json_output: Option<JsonOutputParam>,
3232
/// Format to be used to [`display`](size::Size::display) the sizes returned by [`size_getter`](Self::size_getter).
3333
pub bytes_format: Size::DisplayFormat,
3434
/// The direction of the visualization.
@@ -49,10 +49,6 @@ where
4949
pub min_ratio: Fraction,
5050
/// Preserve order of entries.
5151
pub no_sort: bool,
52-
/// Do not output `.shared.details` in the JSON output.
53-
pub omit_json_shared_details: bool,
54-
/// Do not output `.shared.summary` in the JSON output.
55-
pub omit_json_shared_summary: bool,
5652
}
5753

5854
impl<Size, SizeGetter, HardlinksHandler, Report> Sub<Size, SizeGetter, HardlinksHandler, Report>
@@ -78,8 +74,6 @@ where
7874
reporter,
7975
min_ratio,
8076
no_sort,
81-
omit_json_shared_details,
82-
omit_json_shared_summary,
8377
} = self;
8478

8579
let max_depth = max_depth.get();
@@ -141,22 +135,26 @@ where
141135

142136
GLOBAL_STATUS_BOARD.clear_line(0);
143137

144-
if json_output {
138+
if let Some(json_output) = json_output {
139+
let JsonOutputParam {
140+
shared_details,
141+
shared_summary,
142+
} = json_output;
145143
let tree = data_tree
146144
.into_reflection() // I really want to use std::mem::transmute here but can't.
147145
.par_convert_names_to_utf8() // TODO: allow non-UTF8 somehow.
148146
.expect("convert all names from raw string to UTF-8");
149-
let shared = if omit_json_shared_details && omit_json_shared_summary {
147+
let shared = if !shared_details && !shared_summary {
150148
JsonShared::default()
151149
} else {
152150
let mut shared = deduplication_record
153151
.map_err(HardlinksHandler::convert_error)?
154152
.pipe(HardlinksHandler::json_report)?
155153
.unwrap_or_default();
156-
if omit_json_shared_details {
154+
if !shared_details {
157155
shared.details = None;
158156
}
159-
if omit_json_shared_summary {
157+
if !shared_summary {
160158
shared.summary = None;
161159
}
162160
shared
@@ -188,6 +186,30 @@ where
188186
}
189187
}
190188

189+
/// Value to pass to [`Sub::json_output`] to decide how much details should be
190+
/// put in the output JSON object.
191+
#[derive(Debug, Clone, Copy)]
192+
pub struct JsonOutputParam {
193+
/// Whether to include `.shared.details` in the JSON output.
194+
pub shared_details: bool,
195+
/// Whether to include `.shared.summary` in the JSON output.
196+
pub shared_summary: bool,
197+
}
198+
199+
impl JsonOutputParam {
200+
/// Infer from the CLI flags.
201+
pub(super) fn from_cli_flags(
202+
output_json: bool,
203+
omit_shared_details: bool,
204+
omit_shared_summary: bool,
205+
) -> Option<Self> {
206+
output_json.then_some(JsonOutputParam {
207+
shared_details: !omit_shared_details,
208+
shared_summary: !omit_shared_summary,
209+
})
210+
}
211+
}
212+
191213
/// Subroutines used by [`Sub`] to deduplicate sizes of detected hardlinks and report about it.
192214
pub trait HardlinkSubroutines<Size: size::Size>: DeduplicateSharedSize<Size> {
193215
/// Convert the error to runtime error.

0 commit comments

Comments
 (0)