mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-08-14 14:26:56 +00:00
Harden file_io exercises: remove magic number, dedupe paths
- file_io3: replace the hardcoded expected file size (117) with SAMPLE_TEXT.len(), so the check is derived from the actual content instead of an unexplained magic number. - file_io3: use create_dir_all instead of create_dir, and add a sample_file_path() helper shared by main/create_required_files/ file_cleanup to avoid three separate ad-hoc path constructions. - file_io1/file_io2: hoist the sample text into a SAMPLE_TEXT constant used by both the exercise and its solution, removing duplicated literals. No behavioral change: all three solutions were rebuilt and run locally, producing identical output (file size still 117, permissions still non-readonly) with no leftover files.
This commit is contained in:
parent
47bc98784b
commit
68f8369cb4
@ -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.");
|
||||
}
|
||||
|
||||
@ -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);
|
||||
})?;
|
||||
}
|
||||
|
||||
@ -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(())
|
||||
|
||||
@ -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.");
|
||||
}
|
||||
|
||||
@ -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);
|
||||
})?;
|
||||
}
|
||||
|
||||
@ -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(())
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user