diff --git a/exercises/24_file_io/file_io1.rs b/exercises/24_file_io/file_io1.rs index 54647dc4..475008fe 100644 --- a/exercises/24_file_io/file_io1.rs +++ b/exercises/24_file_io/file_io1.rs @@ -2,6 +2,8 @@ use std::fs; use std::path::Path; const TEST_FILE_NAME: &str = "SampleTextFile.txt"; +const SAMPLE_TEXT: &str = "This is the file content."; + fn main() -> Result<(), std::io::Error> { create_required_files()?; @@ -25,7 +27,7 @@ fn create_required_files() -> Result<(), std::io::Error> { let file_path = Path::new(TEST_FILE_NAME); if !file_path.exists() { - fs::write(file_path, "This is the file content.")?; + fs::write(file_path, SAMPLE_TEXT)?; } else { println!("File already exist."); } diff --git a/exercises/24_file_io/file_io2.rs b/exercises/24_file_io/file_io2.rs index f19d2a47..279c17dc 100644 --- a/exercises/24_file_io/file_io2.rs +++ b/exercises/24_file_io/file_io2.rs @@ -4,6 +4,9 @@ use std::path::Path; const TEST_INPUT_FILE_NAME: &str = "MultiLineTextFile.txt"; const TEST_OUTPUT_FILE_NAME: &str = "MultiLineOutputFile.txt"; +const SAMPLE_TEXT: &str = "This is the first line of the text. + This is the second line. + And this is the third and the last line."; fn main() -> Result<(), std::io::Error> { create_required_files()?; @@ -45,10 +48,7 @@ fn create_required_files() -> Result<(), std::io::Error> { let file_path = Path::new(TEST_INPUT_FILE_NAME); if !file_path.exists() { - let text = "This is the first line of the text. - This is the second line. - And this is the third and the last line."; - fs::write(file_path, text).inspect_err(|err| { + fs::write(file_path, SAMPLE_TEXT).inspect_err(|err| { eprintln!("Couldn't create the test file : {}", err); })?; } diff --git a/exercises/24_file_io/file_io3.rs b/exercises/24_file_io/file_io3.rs index 7966bfcc..51df5a82 100644 --- a/exercises/24_file_io/file_io3.rs +++ b/exercises/24_file_io/file_io3.rs @@ -2,12 +2,18 @@ use std::fs; use std::io::Error; use std::path::PathBuf; +const SAMPLE_TEXT: &str = "This is the first line of the text. + This is the second line. + And this is the third and the last line."; + +fn sample_file_path() -> PathBuf { + PathBuf::from("SampleFilesFolder/MultiLineTextFile.txt") +} + fn main() -> Result<(), std::io::Error> { create_required_files()?; - let mut path_buffer = PathBuf::new(); - path_buffer.push("SampleFilesFolder"); - path_buffer.push("MultiLineTextFile.txt"); + let path_buffer = sample_file_path(); // TODO : How to get metadata using path_buffer ? let meta_data_result = path_buffer. @@ -17,7 +23,7 @@ fn main() -> Result<(), std::io::Error> { println!("Metadata about the file : {:?}", path_buffer); println!("File creation time {:?}", meta_data.created()); println!("File size {}", meta_data.len()); - assert_eq!(meta_data.len(), 117); + assert_eq!(meta_data.len(), SAMPLE_TEXT.len() as u64); println!("File permissions {:?}", meta_data.permissions()); assert!(!meta_data.permissions().readonly()); } @@ -30,24 +36,21 @@ fn main() -> Result<(), std::io::Error> { } fn create_required_files() -> Result<(), std::io::Error> { - let file_path = PathBuf::from("SampleFilesFolder/MultiLineTextFile.txt"); + let file_path = sample_file_path(); - let dir_path = match file_path.parent(){ + let dir_path = match file_path.parent() { Some(parent) => parent, None => return Err(Error::other("Could not get parent path")), }; if !dir_path.exists() { - fs::create_dir(dir_path).inspect_err(|x| { + fs::create_dir_all(dir_path).inspect_err(|x| { eprintln!("Could not create directory: {:?}", x); })?; } if !file_path.exists() { - let text = "This is the first line of the text. - This is the second line. - And this is the third and the last line."; - fs::write(&file_path, text).inspect_err(|err| { + fs::write(&file_path, SAMPLE_TEXT).inspect_err(|err| { eprintln!("Couldn't create test file: {:?}", err); })?; } @@ -56,22 +59,20 @@ fn create_required_files() -> Result<(), std::io::Error> { } fn file_cleanup() -> Result<(), std::io::Error> { - let mut path_buffer = PathBuf::new(); - - path_buffer.push("SampleFilesFolder"); - path_buffer.push("MultiLineTextFile.txt"); + let path_buffer = sample_file_path(); if path_buffer.exists() { fs::remove_file(&path_buffer).inspect(|_| { println!("Test file removed"); })?; } - path_buffer.pop(); - if path_buffer.exists() { - fs::remove_dir(&path_buffer).inspect(|_| { - println!("Test dir removed"); - })?; + if let Some(dir_path) = path_buffer.parent() { + if dir_path.exists() { + fs::remove_dir(dir_path).inspect(|_| { + println!("Test dir removed"); + })?; + } } Ok(()) diff --git a/solutions/24_file_io/file_io1.rs b/solutions/24_file_io/file_io1.rs index 9331eeea..55d8ceee 100644 --- a/solutions/24_file_io/file_io1.rs +++ b/solutions/24_file_io/file_io1.rs @@ -2,6 +2,8 @@ use std::fs; use std::path::Path; const TEST_FILE_NAME: &str = "SampleTextFile.txt"; +const SAMPLE_TEXT: &str = "This is the file content."; + fn main() -> Result<(), std::io::Error> { create_required_files()?; @@ -9,7 +11,7 @@ fn main() -> Result<(), std::io::Error> { match read_str_result { Ok(contents) => { - assert_eq!("This is the file content.", contents); + assert_eq!(SAMPLE_TEXT, contents); } Err(err) => { eprintln!("File read error. {}", err); @@ -24,7 +26,7 @@ fn create_required_files() -> Result<(), std::io::Error> { let file_path = Path::new(TEST_FILE_NAME); if !file_path.exists() { - fs::write(file_path, "This is the file content.")?; + fs::write(file_path, SAMPLE_TEXT)?; } else { println!("File already exist."); } diff --git a/solutions/24_file_io/file_io2.rs b/solutions/24_file_io/file_io2.rs index bfcaff1e..fd67e70b 100644 --- a/solutions/24_file_io/file_io2.rs +++ b/solutions/24_file_io/file_io2.rs @@ -4,6 +4,9 @@ use std::path::Path; const TEST_INPUT_FILE_NAME: &str = "MultiLineTextFile.txt"; const TEST_OUTPUT_FILE_NAME: &str = "MultiLineOutputFile.txt"; +const SAMPLE_TEXT: &str = "This is the first line of the text. + This is the second line. + And this is the third and the last line."; fn main() -> Result<(), std::io::Error> { create_required_files()?; @@ -44,10 +47,7 @@ fn create_required_files() -> Result<(), std::io::Error> { let file_path = Path::new(TEST_INPUT_FILE_NAME); if !file_path.exists() { - let text = "This is the first line of the text. - This is the second line. - And this is the third and the last line."; - fs::write(file_path, text).inspect_err(|err| { + fs::write(file_path, SAMPLE_TEXT).inspect_err(|err| { eprintln!("Couldn't create the test file : {}", err); })?; } diff --git a/solutions/24_file_io/file_io3.rs b/solutions/24_file_io/file_io3.rs index a68dad6e..d2b20432 100644 --- a/solutions/24_file_io/file_io3.rs +++ b/solutions/24_file_io/file_io3.rs @@ -2,12 +2,18 @@ use std::fs; use std::io::Error; use std::path::PathBuf; +const SAMPLE_TEXT: &str = "This is the first line of the text. + This is the second line. + And this is the third and the last line."; + +fn sample_file_path() -> PathBuf { + PathBuf::from("SampleFilesFolder/MultiLineTextFile.txt") +} + fn main() -> Result<(), std::io::Error> { create_required_files()?; - let mut path_buffer = PathBuf::new(); - path_buffer.push("SampleFilesFolder"); - path_buffer.push("MultiLineTextFile.txt"); + let path_buffer = sample_file_path(); let meta_data_result = path_buffer.metadata(); @@ -16,7 +22,7 @@ fn main() -> Result<(), std::io::Error> { println!("Metadata about the file : {:?}", path_buffer); println!("File creation time {:?}", meta_data.created()); println!("File size {}", meta_data.len()); - assert_eq!(meta_data.len(), 117); + assert_eq!(meta_data.len(), SAMPLE_TEXT.len() as u64); println!("File permissions {:?}", meta_data.permissions()); assert!(!meta_data.permissions().readonly()); } @@ -29,7 +35,7 @@ fn main() -> Result<(), std::io::Error> { } fn create_required_files() -> Result<(), std::io::Error> { - let file_path = PathBuf::from("SampleFilesFolder/MultiLineTextFile.txt"); + let file_path = sample_file_path(); let dir_path = match file_path.parent() { Some(parent) => parent, @@ -37,16 +43,13 @@ fn create_required_files() -> Result<(), std::io::Error> { }; if !dir_path.exists() { - fs::create_dir(dir_path).inspect_err(|x| { + fs::create_dir_all(dir_path).inspect_err(|x| { eprintln!("Could not create directory: {:?}", x); })?; } if !file_path.exists() { - let text = "This is the first line of the text. - This is the second line. - And this is the third and the last line."; - fs::write(&file_path, text).inspect_err(|err| { + fs::write(&file_path, SAMPLE_TEXT).inspect_err(|err| { eprintln!("Couldn't create test file: {:?}", err); })?; } @@ -55,22 +58,20 @@ fn create_required_files() -> Result<(), std::io::Error> { } fn file_cleanup() -> Result<(), std::io::Error> { - let mut path_buffer = PathBuf::new(); - - path_buffer.push("SampleFilesFolder"); - path_buffer.push("MultiLineTextFile.txt"); + let path_buffer = sample_file_path(); if path_buffer.exists() { fs::remove_file(&path_buffer).inspect(|_| { println!("Test file removed"); })?; } - path_buffer.pop(); - if path_buffer.exists() { - fs::remove_dir(&path_buffer).inspect(|_| { - println!("Test dir removed"); - })?; + if let Some(dir_path) = path_buffer.parent() { + if dir_path.exists() { + fs::remove_dir(dir_path).inspect(|_| { + println!("Test dir removed"); + })?; + } } Ok(())