csvconv 0.1.0

CSV Delimiter Converter.
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use rayon::iter::ParallelIterator;
use rayon::prelude::*;
use std::fmt;
use std::fs::create_dir_all;
use std::path::PathBuf;
use std::{
    fs::{read_dir, File},
    io::{BufRead, BufReader, BufWriter, Write},
};
use unicode_segmentation::UnicodeSegmentation;

use clap::Parser;

const DEFAULT_NAME: &str = "formatted";

/// CSV Delimiter Converter.
#[derive(Parser, Debug)]
struct Cli {
    /// Original string delimiter. Must be one character.
    original_delimiter: String,

    /// New string delimiter. Must be one character.
    new_delimiter: String,

    /// File or directory path. Directory search is recursive.
    path: std::path::PathBuf,

    /// Name of the output file (resp. directory) for the formatted result (resp. results).
    #[arg(short, long, default_value_t = String::from(DEFAULT_NAME))]
    output: String,

    /// Checks if the file is valid csv by counting delimiters in the lines.
    #[arg(short, long)]
    check: bool,
}

struct CliInfo {
    original_sep: String,
    new_sep: String,
    path: std::path::PathBuf,
    check: bool,
}

impl CliInfo {
    fn new(value: &Cli) -> Self {
        CliInfo {
            original_sep: value.original_delimiter.to_owned(),
            new_sep: value.new_delimiter.to_owned(),
            path: value.path.to_owned(),
            check: value.check,
        }
    }
}

fn main() -> Result<(), FileError> {
    let mut args = Cli::parse();

    if !args.path.is_dir() && args.output == DEFAULT_NAME {
        args.output = String::from(format!("{}.csv", DEFAULT_NAME))
    }

    let args = args;

    if let Some(input_name) = args.path.to_str() {
        if input_name == args.output {
            return Err(FileError::OutputWithSameName(OutputWithSameNameError {}));
        }
    }

    if !args.path.is_dir() {
        let pb = MultiProgress::new();
        return parse_file(&CliInfo::new(&args), PathBuf::from(args.output), &pb);
    } else {
        let parent = args.path.parent().expect("Could not get parent folder.");

        create_dir_all(parent.join(&args.output))?;

        let pb = MultiProgress::new();

        visit_dirs(&args.path, &args, parent, &pb)?
    }

    Ok(())
}

fn process_file(
    args: &Cli,
    file: &Result<std::fs::DirEntry, std::io::Error>,
    parent: &std::path::Path,
    pb: &MultiProgress,
) {
    let mut cli_info = CliInfo::new(args);
    let file = file.as_ref().ok();

    if file.is_none() {
        println!("File couldn't be processed.");
    }

    let file = file.unwrap();
    cli_info.path = file.path();

    if let Err(error) = parse_file(&cli_info, parent.join(&args.output).join(file.path()), pb) {
        println!("{:?}", cli_info.path);
        println!("{:?}", parent.join(&args.output).join(file.path()));
        println!(
            "File {:?} couldn't be processed. {:?}.",
            file.file_name(),
            error
        );
    }
}

fn parse_file(args: &CliInfo, file_to_write: PathBuf, pb: &MultiProgress) -> Result<(), FileError> {
    create_dir_all(
        file_to_write
            .parent()
            .expect("Could not get parent folder."),
    )?;

    if args.new_sep.graphemes(true).count() != 1 {
        return Err(FileError::Delimiter(DelimiterError {
            invalid_delimiter: args.new_sep.to_owned(),
        }));
    }

    let file_to_read = File::open(&args.path)?;

    let total_size = file_to_read.metadata()?.len();
    let pb2 = pb.add(ProgressBar::new(total_size));

    let sty = ProgressStyle::with_template(
        "{spinner:.green} [{msg} {wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({eta})",
    )
    .unwrap()
    .progress_chars("#>-");

    pb2.set_style(sty);

    let mut size_seen = 0;

    let file_to_write = File::create(file_to_write)?;

    let mut reader = BufReader::new(file_to_read);
    let mut writer = BufWriter::new(file_to_write);

    let first_line_result = process_line(
        &mut reader,
        &mut writer,
        &mut size_seen,
        &pb2,
        &args.original_sep,
        &args.new_sep,
        args.check,
    )?;

    match first_line_result {
        LineProcessingResult::EndOfFile => {
            pb2.finish_with_message(format!("File {:?} done!", &args.path.file_name().unwrap()));
            Ok(())
        }
        LineProcessingResult::Some(count_from_first_line) => run_lines_with_consistency_check(
            &mut reader,
            &mut writer,
            &mut size_seen,
            &pb2,
            args,
            count_from_first_line,
        ),
        LineProcessingResult::Any => {
            run_lines_without_consistency_check(reader, writer, size_seen, &pb2, args)
        }
    }
}

fn run_lines_without_consistency_check(
    mut reader: BufReader<File>,
    mut writer: BufWriter<File>,
    mut size_seen: usize,
    pb: &ProgressBar,
    args: &CliInfo,
) -> Result<(), FileError> {
    loop {
        let next_line_result = process_line(
            &mut reader,
            &mut writer,
            &mut size_seen,
            pb,
            &args.original_sep,
            &args.new_sep,
            args.check,
        )?;

        if let LineProcessingResult::EndOfFile = next_line_result {
            pb.finish_and_clear();

            break;
        }
    }

    Ok(())
}

fn run_lines_with_consistency_check(
    reader: &mut BufReader<File>,
    writer: &mut BufWriter<File>,
    size_seen: &mut usize,
    pb: &ProgressBar,
    args: &CliInfo,
    number_to_compare: usize,
) -> Result<(), FileError> {
    let mut line_number = 2;

    loop {
        let next_line_result = process_line(
            reader,
            writer,
            size_seen,
            pb,
            &args.original_sep,
            &args.new_sep,
            args.check,
        )?;

        match next_line_result {
            LineProcessingResult::Some(number_in_this_line) => {
                if number_to_compare != number_in_this_line {
                    let error = CountError {
                        delimiters_at_header: number_to_compare,
                        delimiters_at_line: number_in_this_line,
                        line_number,
                    };
                    pb.finish_with_message(format!(
                        "File {:?}: {error:?}",
                        args.path.file_name().unwrap()
                    ));
                    break;
                }
            }
            LineProcessingResult::EndOfFile => {
                pb.finish_with_message(format!("File {:?} done!", args.path.file_name().unwrap()));
                break;
            }
            LineProcessingResult::Any => (),
        }

        line_number += 1;
    }

    Ok(())
}

fn get_number_of_delimiters(buffer: &str, original_sep: &str) -> usize {
    buffer
        .graphemes(true)
        .filter(|&x| x == original_sep.graphemes(true).last().unwrap())
        .count()
}

fn process_line(
    reader: &mut BufReader<File>,
    writer: &mut BufWriter<File>,
    size_seen: &mut usize,
    pb: &ProgressBar,
    original_sep: &str,
    new_sep: &str,
    check_consistency: bool,
) -> Result<LineProcessingResult, std::io::Error> {
    let mut buffer = String::new();

    if let Ok(0) = reader.read_line(&mut buffer) {
        return Ok(LineProcessingResult::EndOfFile);
    }

    if check_consistency {
        let number_of_delimiters = get_number_of_delimiters(&buffer, original_sep);

        let buffer = buffer.replace(original_sep, new_sep);
        writer.write_all(buffer.as_bytes())?;

        *size_seen += buffer.len();
        pb.set_position(*size_seen as u64);

        Ok(LineProcessingResult::Some(number_of_delimiters))
    } else {
        let buffer = buffer.replace(original_sep, new_sep);
        writer.write_all(buffer.as_bytes())?;

        *size_seen += buffer.len();
        pb.set_position(*size_seen as u64);

        Ok(LineProcessingResult::Any)
    }
}

fn visit_dirs(
    dir: &PathBuf,
    args: &Cli,
    parent: &std::path::Path,
    pb: &MultiProgress,
) -> Result<(), FileError> {
    if dir.is_dir() {
        read_dir(dir)?.par_bridge().for_each(|file| {
            if let Ok(x) = file {
                let path = x.path();
                if path.is_dir() {
                    visit_dirs(&path, args, parent, pb)
                        .expect("Unexpected non-directory inside visit_dirs loop");
                } else {
                    process_file(args, &Ok(x), parent, pb);
                }
            }
        });

        Ok(())
    } else {
        Err(FileError::FileIsDirectory(FileIsDirectoryError {}))
    }
}

enum LineProcessingResult {
    Some(usize),
    Any,
    EndOfFile,
}

#[derive(Debug)]
enum FileError {
    IoError(std::io::Error),
    DifferentCount(CountError),
    Delimiter(DelimiterError),
    FileIsDirectory(FileIsDirectoryError),
    OutputWithSameName(OutputWithSameNameError),
}

impl From<std::io::Error> for FileError {
    fn from(err: std::io::Error) -> FileError {
        FileError::IoError(err)
    }
}

impl From<CountError> for FileError {
    fn from(err: CountError) -> FileError {
        FileError::DifferentCount(err)
    }
}

#[derive(Debug)]
pub struct CountError {
    delimiters_at_header: usize,
    delimiters_at_line: usize,
    line_number: usize,
}

impl fmt::Display for CountError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(
            &format!(
                "{} delimiters at header, while {} at line {}.",
                self.delimiters_at_header, self.delimiters_at_line, self.line_number
            ),
            f,
        )
    }
}

impl std::error::Error for CountError {}

#[derive(Debug)]
pub struct DelimiterError {
    invalid_delimiter: String,
}

impl fmt::Display for DelimiterError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(
            &format!(
                "{} is not a valid delimiter. Please select a one-character delimiter",
                self.invalid_delimiter
            ),
            f,
        )
    }
}

impl std::error::Error for DelimiterError {}

#[derive(Debug)]
pub struct FileIsDirectoryError {}

impl fmt::Display for FileIsDirectoryError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt("", f)
    }
}

impl std::error::Error for FileIsDirectoryError {}

#[derive(Debug)]
pub struct OutputWithSameNameError {}

impl fmt::Display for OutputWithSameNameError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt("", f)
    }
}

impl std::error::Error for OutputWithSameNameError {}