forked from rustcoreutils/posixutils-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsplit.rs
1176 lines (1022 loc) · 37 KB
/
csplit.rs
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (c) 2024 Hemi Labs, Inc.
//
// This file is part of the posixutils-rs project covered under
// the MIT License. For the full license text, please see the LICENSE
// file in the root directory of this project.
// SPDX-License-Identifier: MIT
//
// TODO:
// - err on line num == 0
//
use clap::Parser;
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
use regex::Regex;
use std::fs::{self, File, OpenOptions};
use std::io::{self, BufRead, Error, ErrorKind, Read, Write};
use std::path::PathBuf;
/// csplit - split files based on context
#[derive(Parser)]
#[command(version, about)]
struct Args {
/// Name the created files prefix 00, prefix 01, ..., prefixn.
#[arg(short = 'f', long, default_value = "xx")]
prefix: String,
/// Leave previously created files intact. By default, csplit shall remove created files if an error occurs.
#[arg(short, long, default_value_t = false)]
keep: bool,
/// Use number decimal digits to form filenames for the file pieces.
#[arg(short, long, default_value_t = 2)]
num: u8,
/// Suppress the output of file size messages.
#[arg(short, long, default_value_t = false)]
suppress: bool,
/// File to read as input.
filename: PathBuf,
/// Operands defining context on which to split.
operands: Vec<String>,
}
enum Operand {
Rx(Regex, isize, bool),
LineNum(usize),
Repeat(usize),
}
struct SplitOps {
ops: Vec<Operand>,
}
/// A structure that presents settings for creating files
struct OutputState {
/// A String representing the prefix to be added to file name.
prefix: String,
/// An usize representing the current line number in the file reader.
in_line_no: usize,
/// A String representing the suffix to be added to file name.
suffix: String,
/// A u32 representing the length of the suffix.
suffix_len: u8,
/// representing an output file if set to None, the output stream is not redirected to any file.
outf: Option<File>,
}
impl OutputState {
fn new(prefix: &str, suffix_len: u8) -> OutputState {
OutputState {
prefix: String::from(prefix),
in_line_no: 1,
suffix: String::new(),
suffix_len,
outf: None,
}
}
/// Increments the suffix of the output filename.
///
/// This function increments the suffix of the output filename in numeric order, starting from "00"
/// and incrementing by 1 each time until the maximum value specified by `suffix_len` is reached.
/// If the maximum suffix is reached, an error is returned.
///
/// # Arguments
///
/// * `self` - A mutable reference to the `OutputState` struct.
///
/// # Returns
///
/// * `Result<(), &'static str>` - `Ok(())` if the suffix is successfully incremented, otherwise an error message.
///
/// # Panics
///
/// This function will panic if `suffix_len` is less than 2, as the minimum suffix length is 2.
///
fn incr_suffix(&mut self) -> Result<(), &'static str> {
assert!(self.suffix_len >= 2); // Minimum suffix length is 2
if self.suffix.is_empty() {
self.suffix = format!("{:01$}", 0, self.suffix_len as usize);
return Ok(());
}
let mut number: u32 = self.suffix.parse().unwrap_or(0);
number += 1;
if number >= 10_u32.pow(self.suffix_len.into()) {
return Err("maximum suffix reached");
}
self.suffix = format!("{:01$}", number, self.suffix_len as usize);
Ok(())
}
/// Opens the output file for writing.
///
/// This function opens the output file for writing. If the output file is already open, it does nothing.
/// Otherwise, it increments the suffix of the output filename and creates a new file with the updated filename.
///
/// # Arguments
///
/// * `self` - A mutable reference to the `OutputState` struct.
///
/// # Returns
///
/// * `io::Result<()>` - `Ok(())` if the output file is successfully opened or already open, otherwise an error indicating the failure to open the file.
///
/// # Errors
///
/// Returns an error if there is a problem creating or opening the output file.
///
fn open_output(&mut self) -> io::Result<String> {
if self.outf.is_some() {
return Ok(String::new());
}
let inc_res = self.incr_suffix();
if let Err(e) = inc_res {
return Err(Error::new(ErrorKind::Other, e));
}
let out_fn = format!("{}{}", self.prefix, self.suffix);
let f = OpenOptions::new()
.read(false)
.write(true)
.create(true)
.truncate(true)
.open(&out_fn)?;
self.outf = Some(f);
Ok(out_fn)
}
fn close_output(&mut self) {
if self.outf.is_some() {
self.outf = None;
}
}
}
/// Splits a file based on specified conditions.
///
/// This function splits a file based on the provided splitting options and writes
/// the resulting parts to separate output files. It reads the input file line by
/// line, applies the splitting options to determine where to split the file, and
/// writes the parts to output files accordingly.
///
/// # Arguments
///
/// * `args` - The arguments specifying the file to split and the splitting options.
/// * `ctx` - The context containing the splitting operations.
/// * `new_files` - A mutable reference to a vector containing the names of the new output files.
///
/// # Returns
///
/// * `io::Result<()>` - `Ok(())` if the file is successfully split and the parts are written to output files,
/// or an `io` error.
///
fn csplit_file(args: &Args, ctx: SplitOps, new_files: &mut Vec<String>) -> io::Result<()> {
let mut split_options = ctx.ops;
// open file, or stdin
let file: Box<dyn Read> = {
if args.filename == PathBuf::from("-") {
Box::new(io::stdin().lock())
} else {
Box::new(fs::File::open(&args.filename)?)
}
};
let mut state = OutputState::new(&args.prefix, args.num);
let mut reader = io::BufReader::new(file);
let mut lines = String::new();
loop {
let mut line = String::new();
let n_read = reader.read_line(&mut line)?;
if n_read == 0 {
process_lines(&mut lines, &mut state, new_files, args.suppress)?;
break;
}
if split_options.is_empty() {
lines.push_str(&line);
continue;
}
match split_options.first().unwrap() {
Operand::LineNum(num) => {
if *num == state.in_line_no {
if lines.ends_with('\n') && lines != "\n" {
lines.pop();
}
process_lines(&mut lines, &mut state, new_files, args.suppress)?;
state.in_line_no = 1;
lines = String::new();
lines.push_str(&line);
if split_options.len() > 1 {
if let Operand::Repeat(repeat) = &mut split_options[1] {
*repeat -= 1;
if *repeat == 0 {
split_options.remove(0);
split_options.remove(0);
}
}
}
} else {
lines.push_str(&line);
}
}
Operand::Rx(regex, offset, skip) => {
if line == "\n" {
line = String::new();
}
if regex.is_match(&line) {
match offset.cmp(&0) {
std::cmp::Ordering::Less => {
let mut lines_vec: Vec<&str> = lines.lines().collect();
let mut removed_lines_string = String::new();
let length = lines_vec.len();
if length >= offset.unsigned_abs() {
let removed_lines =
lines_vec.split_off(length - offset.unsigned_abs());
removed_lines_string = removed_lines.join("\n");
removed_lines_string.push('\n');
}
lines = lines_vec.join("\n");
if !lines.is_empty() {
if *skip {
lines.clear();
} else {
if lines.ends_with('\n') {
lines.pop();
}
process_lines(
&mut lines,
&mut state,
new_files,
args.suppress,
)?;
}
}
lines = removed_lines_string;
if line.is_empty() {
lines.push('\n');
} else {
lines.push_str(&line);
}
}
std::cmp::Ordering::Equal => {
if *skip {
lines.clear();
if line.is_empty() {
line = "\n".to_string();
}
lines.push_str(&line);
} else {
if lines.ends_with('\n') {
lines.pop();
}
process_lines(&mut lines, &mut state, new_files, args.suppress)?;
if line.is_empty() {
lines = "\n".to_string();
} else {
lines = line;
}
}
}
std::cmp::Ordering::Greater => {
if line.is_empty() {
lines.push('\n');
} else {
lines.push_str(&line);
}
for _ in 0..*offset - 1 {
let mut new_line = String::new();
let n_read = reader.read_line(&mut new_line)?;
if n_read == 0 {
process_lines(
&mut lines,
&mut state,
new_files,
args.suppress,
)?;
break;
}
lines.push_str(&new_line);
}
if *skip {
lines.clear();
} else {
if lines.ends_with('\n') {
lines.pop();
}
process_lines(&mut lines, &mut state, new_files, args.suppress)?;
lines = String::new();
}
}
}
match split_options.len() {
1 => {
split_options.remove(0);
}
us if us > 1 => {
if let Operand::Repeat(repeat) = &mut split_options[1] {
*repeat -= 1;
if *repeat == 0 {
split_options.remove(0);
split_options.remove(0);
}
} else {
split_options.remove(0);
}
}
_ => {}
}
} else {
if line.is_empty() {
line = "\n".to_string();
}
lines.push_str(&line);
}
}
_ => {}
}
state.in_line_no += 1;
}
Ok(())
}
/// Processes lines and writes them to output.
///
/// This function is responsible for processing lines and writing them to the output
/// according to the specified conditions. It opens the output stream, writes the lines
/// to a file, adds the file name to the list of new files, and closes the output stream.
///
/// # Arguments
///
/// * `lines` - The lines to be processed and written.
/// * `state` - The current output state.
/// * `new_files` - The list of new files.
///
/// # Returns
///
/// * `io::Result<()>` - `Ok(())` if the lines are successfully processed and written to output,
/// or an `io` error.
///
fn process_lines(
lines: &mut String,
state: &mut OutputState,
new_files: &mut Vec<String>,
suppress: bool,
) -> io::Result<()> {
let file_name = state.open_output()?;
state.outf.as_mut().unwrap().write_all(lines.as_bytes())?;
new_files.push(file_name);
if !suppress {
println!("{}\n", lines.len());
}
state.close_output();
Ok(())
}
/// Finds the position of the delimiter in the input string, or None if the delimiter is not found.
///
/// # Arguments
///
/// * `s` - The input string to search in.
/// * `delim` - The character to search for.
///
/// # Returns
///
/// * `Option<usize>` - Some(position) if the delimiter is found, None otherwise.
///
/// ```
fn escaped_end_pos(s: &str, delim: char) -> Option<usize> {
let mut first = true;
let mut escaped = false;
for (i, ch) in s.chars().enumerate() {
if first {
if ch != delim {
return None;
}
first = false;
} else if escaped {
escaped = false;
} else if ch == '\\' {
escaped = true;
} else if ch == delim {
return Some(i);
}
}
None
}
/// Parses an operation string of the form `/regex/offset` or `%regex/offset` or `{n}` or `1..9`
///
/// # Arguments
///
/// * `opstr` - The string to parse
/// * `delim` - The character that indicates the start of the operation string. If it is `'%'`, the operation is in skip mode.
///
/// # Returns
///
/// * `Result<Operand, std::io::Error>` - `Ok(Operand)` if the operation string is parsed successfully, otherwise an error indicating the failure to parse the operation string.
fn parse_op_rx(opstr: &str, delim: char) -> io::Result<Operand> {
// delimiter indicates skip-mode
let is_skip = delim == '%';
// find where regex string ends, and (optionally) offset begins
let res = escaped_end_pos(opstr, delim);
if res.is_none() {
return Err(Error::new(ErrorKind::Other, "invalid regex str"));
}
// parse string sandwiched between two delimiter chars
let end_pos = res.unwrap();
let re_str = &opstr[1..end_pos];
let res = Regex::new(re_str);
if res.is_err() {
return Err(Error::new(ErrorKind::Other, "invalid regex"));
}
let re = res.unwrap();
// reference offset string
let mut offset_str = &opstr[end_pos + 1..];
// if empty, we are done
if offset_str.is_empty() {
return Ok(Operand::Rx(re, 0, is_skip));
}
// skip optional leading '+'
if offset_str.starts_with('+') {
offset_str = &opstr[end_pos + 2..];
}
// parse offset number, positive or negative
match offset_str.parse::<isize>() {
Ok(n) => Ok(Operand::Rx(re, n, is_skip)),
Err(_e) => Err(Error::new(ErrorKind::Other, "invalid regex offset")),
}
}
/// Parses a repeat operand from a string.
///
/// This function parses a repeat operand from the input string. The repeat operand is specified
/// within curly braces, indicating the number of times a certain pattern should be repeated.
///
/// # Arguments
///
/// * `opstr` - A string slice containing the operand to parse.
///
/// # Returns
///
/// * `io::Result<Operand>` - The parsed operand if successful, otherwise an error indicating
/// the failure to parse the operand.
///
/// # Errors
///
/// Returns an error if the input string does not match the expected format or if there is a
/// problem parsing the operand.
///
fn parse_op_repeat(opstr: &str) -> io::Result<Operand> {
// a regex fully describes what must be parsed
let re = Regex::new(r"^\{(\d*|[*])}$").unwrap();
// grab and parse capture #1, if matched
match re.captures(opstr) {
None => {}
Some(caps) => {
let numstr = caps.get(1).unwrap().as_str();
if numstr == "*" {
return Ok(Operand::Repeat(usize::MAX));
}
match numstr.parse::<usize>() {
Ok(n) => return Ok(Operand::Repeat(n)),
Err(_e) => {}
}
}
}
// error cases fall through to here
Err(Error::new(ErrorKind::Other, "invalid repeating operand"))
}
/// Parses a line number operand from a string.
///
/// This function parses a line number operand from the input string. The line number operand
/// specifies a simple positive integer indicating the line number at which to perform a split.
///
/// # Arguments
///
/// * `opstr` - A string slice containing the operand to parse.
///
/// # Returns
///
/// * `io::Result<Operand>` - The parsed operand if successful, otherwise an error indicating
/// the failure to parse the operand.
///
/// # Errors
///
/// Returns an error if the input string cannot be parsed as a positive integer or if there is
/// a problem parsing the operand.
///
fn parse_op_linenum(opstr: &str) -> io::Result<Operand> {
// parse simple positive integer
match opstr.parse::<usize>() {
Ok(n) => Ok(Operand::LineNum(n)),
Err(e) => {
let msg = format!("{}", e);
Err(Error::new(ErrorKind::Other, msg))
}
}
}
/// Parses operands from command-line arguments.
///
/// This function parses operands from the command-line arguments provided in the `Args` struct.
/// It iterates over each operand string, determines its type based on the first character,
/// and delegates parsing to specialized functions for regex patterns, line numbers, or repeats.
///
/// # Arguments
///
/// * `args` - A reference to the `Args` struct containing the command-line arguments.
///
/// # Returns
///
/// * `io::Result<SplitOps>` - The parsed operands wrapped in a `SplitOps` struct if successful,
/// otherwise an error indicating the failure to parse the operands.
///
/// # Errors
///
/// Returns an error if any of the operand strings are invalid or if there is a problem parsing
/// the operands.
///
fn parse_operands(args: &Args) -> io::Result<SplitOps> {
let mut ops = Vec::new();
for opstr in &args.operands {
let first_ch = opstr.chars().next().unwrap();
let op = {
match first_ch {
'/' => parse_op_rx(opstr, '/')?,
'%' => parse_op_rx(opstr, '%')?,
'{' => parse_op_repeat(opstr)?,
'1'..='9' => parse_op_linenum(opstr)?,
_ => return Err(Error::new(ErrorKind::Other, "invalid operand")),
}
};
ops.push(op);
}
Ok(SplitOps { ops })
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
setlocale(LocaleCategory::LcAll, "");
textdomain(env!("PROJECT_NAME"))?;
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;
let args = Args::parse();
let ctx = parse_operands(&args)?;
let mut exit_code = 0;
let mut new_files = vec![];
if let Err(err) = csplit_file(&args, ctx, &mut new_files) {
exit_code = 1;
eprintln!("{}: {}", args.filename.display(), err);
if !args.keep {
for file_name in new_files.iter() {
fs::remove_file(file_name).unwrap();
}
}
}
std::process::exit(exit_code)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_escaped_end_pos() {
// Test with escape characters
assert_eq!(escaped_end_pos("/def/", '/'), Some(4));
// Test with multiple escape characters
assert_eq!(escaped_end_pos("/defabc\\\\/", '/'), Some(9));
// Test with no delimiter found
assert_eq!(escaped_end_pos("abcdef", '/'), None);
// Test with delimiter at the beginning of the string
assert_eq!(escaped_end_pos("/abcdef", '/'), None);
// Test with empty string
assert_eq!(escaped_end_pos("", '/'), None);
}
#[test]
fn test_incr_suffix() {
// Test incrementing suffix with length 2
let mut state = OutputState::new("prefix", 2);
assert_eq!(state.suffix, "");
for i in 0..12 {
assert!(state.incr_suffix().is_ok());
assert_eq!(state.suffix, format!("{:02}", i));
}
// Test incrementing suffix with length 3
let mut state = OutputState::new("prefix", 3);
assert_eq!(state.suffix, "");
for i in 0..12 {
assert!(state.incr_suffix().is_ok());
assert_eq!(state.suffix, format!("{:03}", i));
}
}
#[test]
fn test_parse_op_rx_valid_without_offset() {
let opstr = "/pattern/";
let delim = '/';
match parse_op_rx(opstr, delim) {
Ok(Operand::Rx(regex, offset, is_skip)) => {
assert_eq!(regex.as_str(), "pattern");
assert_eq!(offset, 0);
assert!(!is_skip);
}
_ => panic!("Expected Ok(Operand::Rx)"),
}
}
#[test]
fn test_parse_op_rx_valid_with_positive_offset() {
let opstr = "/pattern/+3";
let delim = '/';
match parse_op_rx(opstr, delim) {
Ok(Operand::Rx(regex, offset, is_skip)) => {
assert_eq!(regex.as_str(), "pattern");
assert_eq!(offset, 3);
assert!(!is_skip);
}
_ => panic!("Expected Ok(Operand::Rx)"),
}
}
#[test]
fn test_parse_op_rx_valid_with_negative_offset() {
let opstr = "/pattern/-2";
let delim = '/';
match parse_op_rx(opstr, delim) {
Ok(Operand::Rx(regex, offset, is_skip)) => {
assert_eq!(regex.as_str(), "pattern");
assert_eq!(offset, -2);
assert!(!is_skip);
}
_ => panic!("Expected Ok(Operand::Rx)"),
}
}
#[test]
fn test_parse_op_rx_valid_with_leading_plus() {
let opstr = "/pattern/+5";
let delim = '/';
match parse_op_rx(opstr, delim) {
Ok(Operand::Rx(regex, offset, is_skip)) => {
assert_eq!(regex.as_str(), "pattern");
assert_eq!(offset, 5);
assert!(!is_skip);
}
_ => panic!("Expected Ok(Operand::Rx)"),
}
}
#[test]
fn test_parse_op_rx_valid_with_skip_mode() {
let opstr = "%pattern%";
let delim = '%';
match parse_op_rx(opstr, delim) {
Ok(Operand::Rx(regex, offset, is_skip)) => {
assert_eq!(regex.as_str(), "pattern");
assert_eq!(offset, 0);
assert!(is_skip);
}
_ => panic!("Expected Ok(Operand::Rx)"),
}
}
#[test]
fn test_parse_op_rx_invalid() {
let opstr = "/pattern";
let delim = '/';
match parse_op_rx(opstr, delim) {
Err(e) => {
assert_eq!(e.kind(), ErrorKind::Other);
assert_eq!(e.to_string(), "invalid regex str");
}
_ => panic!("Expected Err"),
}
}
#[test]
fn test_parse_op_repeat_valid() {
let opstr = "{5}";
match parse_op_repeat(opstr) {
Ok(Operand::Repeat(n)) => assert_eq!(n, 5),
_ => panic!("Expected Ok(Operand::Repeat)"),
}
}
#[test]
fn test_parse_op_repeat_invalid_non_numeric() {
let opstr = "{abc}";
match parse_op_repeat(opstr) {
Err(e) => {
assert_eq!(e.kind(), ErrorKind::Other);
assert_eq!(e.to_string(), "invalid repeating operand");
}
_ => panic!("Expected Err"),
}
}
#[test]
fn test_parse_op_repeat_invalid_missing_braces() {
let opstr = "5";
match parse_op_repeat(opstr) {
Err(e) => {
assert_eq!(e.kind(), ErrorKind::Other);
assert_eq!(e.to_string(), "invalid repeating operand");
}
_ => panic!("Expected Err"),
}
}
#[test]
fn test_parse_op_linenum_valid() {
let opstr = "10";
match parse_op_linenum(opstr) {
Ok(Operand::LineNum(n)) => assert_eq!(n, 10),
_ => panic!("Expected Ok(Operand::LineNum)"),
}
}
#[test]
fn test_parse_op_linenum_invalid_non_numeric() {
let opstr = "abc";
match parse_op_linenum(opstr) {
Err(e) => {
assert_eq!(e.kind(), ErrorKind::Other);
assert_eq!(e.to_string(), "invalid digit found in string");
}
_ => panic!("Expected Err"),
}
}
#[test]
fn test_parse_operands() {
// Test valid operands
let args = Args {
prefix: String::from("xx"),
keep: false,
num: 2,
suppress: false,
filename: PathBuf::from("test.txt"),
operands: vec![
String::from("/pattern/+1"),
String::from("%skip%10"),
String::from("15"),
String::from("{3}"),
],
};
match parse_operands(&args) {
Ok(ops) => {
assert_eq!(ops.ops.len(), 4);
match &ops.ops[0] {
Operand::Rx(re, offset, _) => {
assert_eq!(re.as_str(), "pattern");
assert_eq!(*offset, 1);
}
_ => panic!("Expected Operand::Rx"),
}
match &ops.ops[1] {
Operand::Rx(re, offset, _) => {
assert_eq!(re.as_str(), "skip");
assert_eq!(*offset, 10);
}
_ => panic!("Expected Operand::Rx"),
}
match &ops.ops[2] {
Operand::LineNum(n) => assert_eq!(*n, 15),
_ => panic!("Expected Operand::LineNum"),
}
match &ops.ops[3] {
Operand::Repeat(n) => assert_eq!(*n, 3),
_ => panic!("Expected Operand::Repeat"),
}
}
_ => panic!("Expected Ok(SplitOps)"),
}
}
#[test]
fn test_split_text_file() {
// Test valid operands
let args = Args {
prefix: String::from("txt"),
keep: false,
num: 2,
suppress: false,
filename: PathBuf::from("tests/assets/test_file.txt"),
operands: vec![String::from("5"), String::from("{3}")],
};
let ctx = parse_operands(&args).unwrap();
let mut new_files = vec![];
match csplit_file(&args, ctx, &mut new_files) {
Ok(_) => {}
Err(e) => {
if !args.keep {
for file_name in new_files.iter() {
fs::remove_file(file_name).unwrap();
}
}
panic!("{e}");
}
}
let mut file = File::open("txt00").unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
let expected = String::from("1sdfghnm\n2sadsgdhjmf\n3zcxbncvm vbm\n4asdbncv");
assert_eq!(contents, expected);
let mut file = File::open("txt03").unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
let expected = String::from("13\n14\n15\n16\n17");
assert_eq!(contents, expected);
fs::remove_file("txt00").unwrap();
fs::remove_file("txt01").unwrap();
fs::remove_file("txt02").unwrap();
fs::remove_file("txt03").unwrap();
}
#[test]
fn test_split_c_file_1() {
// Test valid operands
let args = Args {
prefix: String::from("c_file"),
keep: false,
num: 2,
suppress: false,
filename: PathBuf::from("tests/assets/test_file_c"),
operands: vec![
String::from(r"%main\(%"),
String::from("/^}/+1"),
String::from("{3}"),
],
};
let ctx = parse_operands(&args).unwrap();
let mut new_files = vec![];
match csplit_file(&args, ctx, &mut new_files) {
Ok(_) => {}
Err(e) => {
if !args.keep {
for file_name in new_files.iter() {
fs::remove_file(file_name).unwrap();
}
}
panic!("{e}");
}
}
let mut file = File::open("c_file00").unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
let expected =
String::from("int main() {\n printf(\"Hello, world!\\n\");\n return 0;\n}");
assert_eq!(contents, expected);
let mut file = File::open("c_file03").unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
let expected =
String::from("\nvoid func3() {\n printf(\"This is function 3\\n\");\n}\n");
assert_eq!(contents, expected);
fs::remove_file("c_file00").unwrap();
fs::remove_file("c_file01").unwrap();
fs::remove_file("c_file02").unwrap();
fs::remove_file("c_file03").unwrap();
}
#[test]
fn test_split_c_file_2() {
// Test valid operands
let args = Args {
prefix: String::from("c_file_2_"),
keep: false,
num: 2,
suppress: false,
filename: PathBuf::from("tests/assets/test_file_c"),
operands: vec![
String::from(r"%main\(%+1"),
String::from("/^}/+1"),
String::from("{3}"),
],
};
let ctx = parse_operands(&args).unwrap();
let mut new_files = vec![];
match csplit_file(&args, ctx, &mut new_files) {
Ok(_) => {}
Err(e) => {
if !args.keep {
for file_name in new_files.iter() {
fs::remove_file(file_name).unwrap();
}
}
panic!("{e}");
}
}
let mut file = File::open("c_file_2_00").unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
let expected = String::from(" printf(\"Hello, world!\\n\");\n return 0;\n}");
assert_eq!(contents, expected);
let mut file = File::open("c_file_2_03").unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
let expected =
String::from("\nvoid func3() {\n printf(\"This is function 3\\n\");\n}\n");
assert_eq!(contents, expected);