|
28 | 28 | /// List of files and/or directories. |
29 | 29 | pub files: Vec<PathBuf>, |
30 | 30 | /// Print JSON data instead of an ASCII chart. |
31 | | - pub json_output: bool, |
| 31 | + pub json_output: Option<JsonOutputParam>, |
32 | 32 | /// Format to be used to [`display`](size::Size::display) the sizes returned by [`size_getter`](Self::size_getter). |
33 | 33 | pub bytes_format: Size::DisplayFormat, |
34 | 34 | /// The direction of the visualization. |
|
49 | 49 | pub min_ratio: Fraction, |
50 | 50 | /// Preserve order of entries. |
51 | 51 | 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, |
56 | 52 | } |
57 | 53 |
|
58 | 54 | impl<Size, SizeGetter, HardlinksHandler, Report> Sub<Size, SizeGetter, HardlinksHandler, Report> |
|
78 | 74 | reporter, |
79 | 75 | min_ratio, |
80 | 76 | no_sort, |
81 | | - omit_json_shared_details, |
82 | | - omit_json_shared_summary, |
83 | 77 | } = self; |
84 | 78 |
|
85 | 79 | let max_depth = max_depth.get(); |
@@ -141,22 +135,26 @@ where |
141 | 135 |
|
142 | 136 | GLOBAL_STATUS_BOARD.clear_line(0); |
143 | 137 |
|
144 | | - if json_output { |
| 138 | + if let Some(json_output) = json_output { |
| 139 | + let JsonOutputParam { |
| 140 | + shared_details, |
| 141 | + shared_summary, |
| 142 | + } = json_output; |
145 | 143 | let tree = data_tree |
146 | 144 | .into_reflection() // I really want to use std::mem::transmute here but can't. |
147 | 145 | .par_convert_names_to_utf8() // TODO: allow non-UTF8 somehow. |
148 | 146 | .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 { |
150 | 148 | JsonShared::default() |
151 | 149 | } else { |
152 | 150 | let mut shared = deduplication_record |
153 | 151 | .map_err(HardlinksHandler::convert_error)? |
154 | 152 | .pipe(HardlinksHandler::json_report)? |
155 | 153 | .unwrap_or_default(); |
156 | | - if omit_json_shared_details { |
| 154 | + if !shared_details { |
157 | 155 | shared.details = None; |
158 | 156 | } |
159 | | - if omit_json_shared_summary { |
| 157 | + if !shared_summary { |
160 | 158 | shared.summary = None; |
161 | 159 | } |
162 | 160 | shared |
@@ -188,6 +186,30 @@ where |
188 | 186 | } |
189 | 187 | } |
190 | 188 |
|
| 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 | + |
191 | 213 | /// Subroutines used by [`Sub`] to deduplicate sizes of detected hardlinks and report about it. |
192 | 214 | pub trait HardlinkSubroutines<Size: size::Size>: DeduplicateSharedSize<Size> { |
193 | 215 | /// Convert the error to runtime error. |
|
0 commit comments