1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
use crate::{
    add_thousands_separator,
    args::{Arguments, ResultFormat::*},
    my_print, split_and_insert, write_xlsx, FileExtension, FileInfo, Key, MyResult,
    PathBufExtension, PathInfo, TotalInfo, CSV_FILENAME, SEPARATOR, XLSX_FILENAME,
};
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use std::{
    fs::{File, OpenOptions},
    io::Write,
    path::PathBuf,
    //thread,
};

/// Grouped file information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GroupInfo {
    /// Key Information
    #[serde(rename = "File information")]
    pub key: Key,
    /// File Paths
    #[serde(rename = "Paths")]
    pub paths: Vec<PathBuf>, // Arc<[PathBuf]> for immutable data
    /// Number of identical files with the same size and blake3 hash
    #[serde(rename = "Number of identical files")]
    pub num_file: usize,
    /// Sum of individual file sizes declared in paths
    #[serde(
        rename = "Sum of file sizes",
        serialize_with = "add_thousands_separator"
    )]
    pub sum_size: usize,
}

impl GroupInfo {
    /// Print GroupInfo fields in chosen format
    pub fn print_formatted(
        &self,
        arguments: &Arguments,
        write: &mut Box<&mut dyn Write>,
    ) -> MyResult<()> {
        match &arguments.result_format {
            Json => {
                // Serialize GroupInfo to a JSON string.
                let serialized = serde_json::to_string_pretty(self)?;
                writeln!(write, "{serialized}\n")?;
            }
            Yaml => {
                // Serialize GroupInfo to a YAML string.
                let serialized = serde_yaml::to_string(self)?;
                writeln!(*write, "{serialized}")?;
            }
            Personal => {
                writeln!(
                    write,
                    "size: {} bytes",
                    split_and_insert(self.key.size, SEPARATOR)
                )?;
                writeln!(write, "hash: {}", self.key.hash.clone().unwrap_or_default())?;
                writeln!(write, "Paths: {:#?}", self.paths)?;
                writeln!(write, "Number of identical files: {}", self.num_file)?;
                writeln!(
                    write,
                    "Sum of file sizes: {} bytes\n",
                    split_and_insert(self.sum_size, SEPARATOR)
                )?;
            }
        }

        Ok(())
    }

    /// Update hash
    pub fn update_hash(&self, arguments: &Arguments, procedure: u8) -> Vec<FileInfo> {
        self.paths
            .clone()
            .into_par_iter() // rayon parallel iterator
            .map(|path| {
                let key = match path.get_hash(arguments, procedure) {
                    Ok(hash) => Key {
                        size: self.key.size,
                        hash,
                    },
                    Err(why) => {
                        eprintln!("fn update_hash()");
                        eprintln!("path: {:#?}", path.display());
                        panic!("Error getting path hash: {why}")
                    }
                };

                FileInfo { key, path }
            })
            .collect()
    }

    /// Convert [`GroupInfo`] to Vec<[`PathInfo`]>
    pub fn flatten(&self) -> Vec<PathInfo> {
        self.paths
            .clone()
            .into_par_iter() // rayon parallel iterator
            .map(|path| PathInfo {
                size: self.key.size,
                hash: self.key.hash.clone(),
                path: path.to_owned(),
                num_file: self.num_file,
                sum_size: self.sum_size,
            })
            .collect()
    }
}

pub trait GroupExtension {
    /**
    Get identical files from the hash of the first bytes or the entire file.

    If procedure = 2, get the hash of the first bytes.

    If procedure = 3, get the hash of the entire file.
    */
    fn get_identical_files(&self, arguments: &Arguments, procedure: u8) -> Vec<GroupInfo>;

    /**
    Sort the list of identical files.

    Two options:

    1. Sort by number of identical files and then by (file size, hash);
    2. Sort by (file size, hash). `default`
    */
    fn sort_identical_files(&mut self, arguments: &Arguments);

    /// Print identical files
    fn print_identical_files(&self, arguments: &Arguments) -> MyResult<()>;

    /// Get Total Info
    fn get_total_info(&self, arguments: &Arguments, total_num_files: usize) -> TotalInfo;

    /// Convert Vec<[`GroupInfo`]> to Vec<[`PathInfo`]>
    fn get_path_info(&self) -> Vec<PathInfo>;

    /// Export to CSV format
    fn export_to_csv(&self, dir_path: PathBuf) -> MyResult<()>;

    /// Export to XLSX format
    fn export_to_xlsx(&self, dir_path: PathBuf) -> MyResult<()>;
}

impl GroupExtension for [GroupInfo] {
    fn get_identical_files(&self, arguments: &Arguments, procedure: u8) -> Vec<GroupInfo> {
        let identical_hash: Vec<GroupInfo> = self
            .par_iter() // rayon parallel iterator
            .flat_map(|group_info| {
                group_info
                    .update_hash(arguments, procedure)
                    .get_grouped_files(arguments, procedure)
            })
            .collect();

        identical_hash
    }

    fn sort_identical_files(&mut self, arguments: &Arguments) {
        if arguments.sort {
            // Sort by number of identical files and then by (file size, hash).
            self.par_sort_unstable_by_key(|group_info| {
                (
                    group_info.num_file,
                    group_info.key.size,
                    group_info.key.hash.clone(),
                )
            });
        } else {
            // Sort by (file size, hash) and then by number of identical files.
            self.par_sort_unstable_by_key(|group_info| {
                (group_info.key.size, group_info.key.hash.clone())
            });
        }
    }

    fn print_identical_files(&self, arguments: &Arguments) -> MyResult<()> {
        let all_buffer: Vec<u8> = self
            .par_chunks(rayon::current_num_threads())
            .flat_map(|groups_info| -> MyResult<Vec<u8>> {
                let mut buffer: Vec<u8> = Vec::new();
                let mut write: Box<&mut dyn Write> = Box::new(&mut buffer);
                groups_info
                    .iter()
                    .try_for_each(|group_info| -> MyResult<()> {
                        group_info.print_formatted(arguments, &mut write)
                    })?;
                Ok(buffer)
            })
            .flatten()
            .collect();

        my_print(&all_buffer)?;
        Ok(())
    }

    fn get_total_info(&self, arguments: &Arguments, total_num_files: usize) -> TotalInfo {
        // Takes two closures and potentially runs them in parallel.
        let (total_num_identical, total_size) = rayon::join(
            || self.par_iter().map(|group_info| group_info.num_file).sum(),
            || self.par_iter().map(|group_info| group_info.sum_size).sum(),
        );

        /*
        let (result_a, result_b) = thread::scope(|s| {
            let thread_a = s.spawn(|| self.par_iter().map(|group_info| group_info.num_file).sum());
            let thread_b = s.spawn(|| self.par_iter().map(|group_info| group_info.sum_size).sum());

            // Wait for background thread to procedure.
            // Call join() on each handle to make sure all the threads finish.
            // join() returns immediately when the associated thread procedures.
            (thread_a.join(), thread_b.join())
        });

        let (total_num_identical, total_size) = match (result_a, result_b) {
            (Ok(sum_a), Ok(sum_b)) => (sum_a, sum_b),
            _ => panic!("thread::scope failed!"),
        };
        */

        TotalInfo {
            algorithm: arguments.algorithm,
            total_num_files,
            total_num_identical,
            total_num_hashes: self.len(),
            total_size,
        }
    }

    fn get_path_info(&self) -> Vec<PathInfo> {
        self.par_iter() // rayon parallel iterator
            .flat_map(|group_info| group_info.flatten())
            .collect()
    }

    fn export_to_csv(&self, mut dir_path: PathBuf) -> MyResult<()> {
        dir_path.push(CSV_FILENAME); // dir_path + filename
        eprintln!("Write CSV File: {:?}", dir_path);

        // Open a file in write-only mode
        let file: File = match OpenOptions::new()
            .read(true)
            .write(true)
            .create(true)
            .truncate(true) // replace the file
            .open(&dir_path)
        {
            Ok(f) => f,
            Err(error) => {
                eprintln!("fn export_to_csv()");
                eprintln!("Couldn't create {:?}", dir_path);
                panic!("Error: {error}");
            }
        };

        let mut writer = csv::WriterBuilder::new()
            .delimiter(b';')
            .has_headers(true)
            .quote_style(csv::QuoteStyle::Necessary) // NonNumeric
            .from_writer(file);

        for path_info in self.get_path_info() {
            writer.serialize(path_info)?;
        }

        writer.flush()?;

        Ok(())
    }

    fn export_to_xlsx(&self, mut dir_path: PathBuf) -> MyResult<()> {
        dir_path.push(XLSX_FILENAME); // dir_path + filename
        eprintln!("Write XLSX File: {:?}", dir_path);

        write_xlsx(&self.get_path_info(), "Identical Files", dir_path)?;

        Ok(())
    }
}