Skip to content

Commit

Permalink
Adding error handling to read file methds
Browse files Browse the repository at this point in the history
  • Loading branch information
jlechem committed Sep 22, 2024
1 parent 2172d77 commit 6a3e591
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 21 deletions.
2 changes: 1 addition & 1 deletion filesort-rs/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"kind": "bin"
}
},
"args": ["--input-file","test.txt","--output-file","out.txt"],
"args": ["--input-file","foo.txt","--output-file","out.txt"],
"cwd": "${workspaceFolder}"
},
{
Expand Down
10 changes: 0 additions & 10 deletions filesort-rs/out.txt
Original file line number Diff line number Diff line change
@@ -1,10 +0,0 @@
1
2
3
4
44
5
6
7
8
9
4 changes: 3 additions & 1 deletion filesort-rs/src/file_sort.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::io::Error;

pub trait FileSort {
fn read(&mut self);
fn read(&mut self) -> Result<bool,Error>;
fn write(&mut self);
fn sort(&mut self);
}
8 changes: 5 additions & 3 deletions filesort-rs/src/line_sort.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::file_sort::FileSort;

use std::fs::File;
use std::io::{BufRead, BufReader, Write};
use std::io::{BufRead, BufReader, Write, Error};

pub struct LineSort {
lines: Vec<String>,
Expand All @@ -22,8 +22,8 @@ impl LineSort {
}

impl FileSort for LineSort {
fn read(&mut self) {
let file = File::open(&self.input_file).expect("input file doesn't exist");
fn read(&mut self) -> Result<bool,Error> {
let file = File::open(&self.input_file)?;

let buffer: BufReader<File> = BufReader::new(file);

Expand All @@ -34,6 +34,8 @@ impl FileSort for LineSort {
self.lines.push(new_line);
}
}

return Ok(true);
}

fn write(&mut self) {
Expand Down
36 changes: 33 additions & 3 deletions filesort-rs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,18 @@ fn main() {
let mut linesort: LineSort = LineSort::new(args.input_file, args.output_file, args.descending);

println!("Filesort start\n");
println!("Reading file data\n");
println!("Reading file data (by Line)\n");

linesort.read();
let result = linesort.read();

match result {
Ok(_res) => {
},
Err(e) => {
eprintln!("An error occured with the input file\n{}", e);
return;
}
}

println!("Soritng data\n");

Expand All @@ -33,8 +42,29 @@ fn main() {
else {
let mut wordsort: WordSort = WordSort::new(args.input_file, args.output_file, args.descending);

wordsort.read();
println!("Filesort start\n");
println!("Reading file data (by Word)\n");

let result = wordsort.read();

match result {
Ok(_res) => {
},
Err(e) => {
eprintln!("An error occured with the input file\n{}", e);
return;
}
}

println!("Soritng data\n");

wordsort.sort();

println!("Writing file data\n");

wordsort.write();

println!("Filesort complete\n");

}
}
8 changes: 5 additions & 3 deletions filesort-rs/src/word_sort.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::file_sort::FileSort;

use std::fs::File;
use std::io::{BufRead, BufReader, Write};
use std::io::{BufRead, BufReader, Write, Error};

pub struct WordSort {
lines: Vec<String>,
Expand All @@ -22,14 +22,16 @@ impl WordSort {
}

impl FileSort for WordSort {
fn read(&mut self) {
let file = File::open(&self.input_file).expect("input file doesn't exist");
fn read(&mut self) -> Result<bool,Error> {
let file = File::open(&self.input_file)?;

let buffer: BufReader<File> = BufReader::new(file);

for line in buffer.lines() {
self.lines.push(line.unwrap_or_default());
}

return Ok(true);
}

fn write(&mut self) {
Expand Down

0 comments on commit 6a3e591

Please sign in to comment.