Skip to content

Commit

Permalink
Getting word sorting to work.
Browse files Browse the repository at this point in the history
  • Loading branch information
jlechem committed Sep 22, 2024
1 parent 58d26b0 commit 79e4ebb
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
2 changes: 1 addition & 1 deletion filesort-rs/src/file_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ use std::io::Error;

pub trait FileSort {
fn read(&mut self) -> Result<bool,Error>;
fn write(&mut self);
fn write(&mut self) -> Result<bool,Error>;
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
Expand Up @@ -38,12 +38,14 @@ impl FileSort for LineSort {
return Ok(true);
}

fn write(&mut self) {
let mut file: File = File::create(&self.output_file).expect("Unable to create output file {0}");
fn write(&mut self) -> Result<bool,Error>{
let mut file: File = File::create(&self.output_file)?;

for line in &self.lines {
write!(file,"{}\n", line).expect("Unable to write data to file.")
write!(file,"{}\n", line)?;
}

return Ok(true);
}

fn sort(&mut self) {
Expand Down
22 changes: 20 additions & 2 deletions filesort-rs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,16 @@ fn main() {

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

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

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

println!("Filesort complete\n");
}
Expand All @@ -62,7 +71,16 @@ fn main() {

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

wordsort.write();
let result = wordsort.write();

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

println!("Filesort complete\n");

Expand Down
21 changes: 16 additions & 5 deletions filesort-rs/src/word_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,29 @@ impl FileSort for WordSort {
let buffer: BufReader<File> = BufReader::new(file);

for line in buffer.lines() {
self.lines.push(line.unwrap_or_default());
}
let new_line:String = line.unwrap_or_default();

if !new_line.is_empty() {
let words = new_line.split_whitespace();

for word in words {
self.lines.push(word.to_string());
}

}
}

return Ok(true);
}

fn write(&mut self) {
let mut file: File = File::create(&self.output_file).expect("Unable to create output file {0}");
fn write(&mut self) -> Result<bool,Error>{
let mut file: File = File::create(&self.output_file)?;

for line in &self.lines {
write!(file,"{}\n", line).expect("Unable to write data to file.")
write!(file,"{}\n", line)?;
}

return Ok(true);
}

fn sort(&mut self) {
Expand Down

0 comments on commit 79e4ebb

Please sign in to comment.