mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-12-28 06:49:19 +00:00
File samples replaced by programatically generated files
This commit is contained in:
parent
23c482b72e
commit
61824af087
@ -1,3 +0,0 @@
|
|||||||
This is the first line of the text.
|
|
||||||
This is the second line.
|
|
||||||
And this is the third and the last line.
|
|
||||||
@ -1 +0,0 @@
|
|||||||
This is the file content.
|
|
||||||
@ -1,14 +1,17 @@
|
|||||||
use std::fs;
|
use std::fs;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
const TEST_FILE_NAME: &str = "SampleTextFile.txt";
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
||||||
let read_str_result = fs::read_to_string("exercises/24_file_io/SampleFilesFolder/SampleTextFile.txt");
|
create_required_files();
|
||||||
|
|
||||||
|
let read_str_result = fs::read_to_string(TEST_FILE_NAME);
|
||||||
|
|
||||||
match read_str_result {
|
match read_str_result {
|
||||||
Ok(contents) => {
|
Ok(contents) => {
|
||||||
// TODO : What should the read string would be ?
|
// TODO : What would be the expected text ?
|
||||||
let expected_string =
|
assert_eq!(, contents);
|
||||||
assert_eq!(expected_string, contents);
|
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("Error reading file: {}", e);
|
eprintln!("Error reading file: {}", e);
|
||||||
@ -16,3 +19,14 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn create_required_files(){
|
||||||
|
let file_path = Path::new(TEST_FILE_NAME);
|
||||||
|
|
||||||
|
if file_path.exists() == false {
|
||||||
|
fs::write(&file_path, "This is the file content.").unwrap();
|
||||||
|
println!("File created.");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,18 +1,22 @@
|
|||||||
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io::{BufRead, BufReader, BufWriter, Write};
|
use std::io::{BufRead, BufReader, BufWriter, Write};
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
const TEST_FILE_NAME: &str = "MultiLineTextFile.txt";
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
||||||
let input_file = fs::File::open("exercises/24_file_io/SampleFilesFolder/MultiLineTextFile.txt");
|
create_required_files();
|
||||||
|
let input_file = fs::File::open(TEST_FILE_NAME);
|
||||||
|
|
||||||
if input_file.is_err() {
|
if input_file.is_err() {
|
||||||
eprintln!("Input file open error : {}", input_file.as_ref().unwrap_err());
|
eprintln!("Input file open error : {}", input_file.as_ref().unwrap_err());
|
||||||
assert!(false);
|
assert!(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO : How can we create a BufReader using input_file as parameter
|
// TODO : How to create a new BufReader using input file
|
||||||
let buffered_input_file = ;
|
let buffered_input_file =;
|
||||||
|
|
||||||
let output_file = fs::File::create("MultiLineOutputFile.txt");
|
let output_file = fs::File::create("MultiLineOutputFile.txt");
|
||||||
|
|
||||||
@ -40,4 +44,17 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
println!("{} : lines processed", line_number - 1);
|
println!("{} : lines processed", line_number - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_required_files(){
|
||||||
|
let file_path = Path::new(TEST_FILE_NAME);
|
||||||
|
|
||||||
|
if file_path.exists() == false {
|
||||||
|
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).unwrap();
|
||||||
|
println!("File created.");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -1,22 +1,22 @@
|
|||||||
|
use std::fs;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
||||||
|
create_required_files();
|
||||||
let mut path_buffer = PathBuf::new();
|
let mut path_buffer = PathBuf::new();
|
||||||
|
|
||||||
path_buffer.push("exercises");
|
|
||||||
path_buffer.push("24_file_io");
|
|
||||||
path_buffer.push("SampleFilesFolder");
|
path_buffer.push("SampleFilesFolder");
|
||||||
path_buffer.push("MultiLineTextFile.txt");
|
path_buffer.push("MultiLineTextFile.txt");
|
||||||
|
|
||||||
// TODO : How we can get the metadata from path_buffer ?
|
// TODO : How to get metadata using path_buffer ?
|
||||||
let meta_data_result = path_buffer.
|
let meta_data_result = path_buffer.
|
||||||
|
|
||||||
if let Ok(meta_data) = meta_data_result {
|
if let Ok(meta_data) = meta_data_result {
|
||||||
println!("Metadata about the file : {:?}", path_buffer);
|
println!("Metadata about the file : {:?}", path_buffer);
|
||||||
println!("File creation time {:?}", meta_data.created().unwrap());
|
println!("File creation time {:?}", meta_data.created().unwrap());
|
||||||
println!("File size {}", meta_data.len());
|
println!("File size {}", meta_data.len());
|
||||||
assert_eq!(meta_data.len(), 101);
|
assert_eq!(meta_data.len(), 117);
|
||||||
println!("File permissions {:?}", meta_data.permissions());
|
println!("File permissions {:?}", meta_data.permissions());
|
||||||
assert_eq!(meta_data.permissions().readonly(), false);
|
assert_eq!(meta_data.permissions().readonly(), false);
|
||||||
}else {
|
}else {
|
||||||
@ -27,4 +27,25 @@ fn main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_required_files(){
|
||||||
|
let file_path = PathBuf::from("SampleFilesFolder/MultiLineTextFile.txt");
|
||||||
|
|
||||||
|
let dir_path = file_path.parent().unwrap();
|
||||||
|
|
||||||
|
if dir_path.exists() == false {
|
||||||
|
fs::create_dir(dir_path).unwrap();
|
||||||
|
println!("Created directory {:?}", dir_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
if file_path.exists() == false {
|
||||||
|
|
||||||
|
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).unwrap();
|
||||||
|
println!("File created.");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -1,3 +0,0 @@
|
|||||||
This is the first line of the text.
|
|
||||||
This is the second line.
|
|
||||||
And this is the third and the last line.
|
|
||||||
@ -1 +0,0 @@
|
|||||||
This is the file content.
|
|
||||||
@ -1,8 +1,12 @@
|
|||||||
use std::fs;
|
use std::fs;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
const TEST_FILE_NAME: &str = "SampleTextFile.txt";
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
||||||
let read_str_result = fs::read_to_string("solutions/24_file_io/SampleFilesFolder/SampleTextFile.txt");
|
create_required_files();
|
||||||
|
|
||||||
|
let read_str_result = fs::read_to_string(TEST_FILE_NAME);
|
||||||
|
|
||||||
match read_str_result {
|
match read_str_result {
|
||||||
Ok(contents) => {
|
Ok(contents) => {
|
||||||
@ -15,21 +19,13 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
fn create_required_files(){
|
||||||
fn test_working_directory() {
|
let file_path = Path::new(TEST_FILE_NAME);
|
||||||
let working_directory_result = std::path::Path::new(".").canonicalize();
|
|
||||||
|
|
||||||
match working_directory_result {
|
if !file_path.exists() {
|
||||||
Ok(working_directory) => {
|
fs::write(file_path, "This is the file content.").unwrap();
|
||||||
println!("The working directory is {:?}", working_directory);
|
println!("File created.");
|
||||||
}
|
|
||||||
Err(error) => {
|
|
||||||
println!("Error: {:?}", error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -1,10 +1,14 @@
|
|||||||
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io::{BufRead, BufReader, BufWriter, Write};
|
use std::io::{BufRead, BufReader, BufWriter, Write};
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
const TEST_FILE_NAME: &str = "MultiLineTextFile.txt";
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
||||||
let input_file = fs::File::open("solutions/24_file_io/SampleFilesFolder/MultiLineTextFile.txt");
|
create_required_files();
|
||||||
|
let input_file = fs::File::open(TEST_FILE_NAME);
|
||||||
|
|
||||||
if input_file.is_err() {
|
if input_file.is_err() {
|
||||||
eprintln!("Input file open error : {}", input_file.as_ref().unwrap_err());
|
eprintln!("Input file open error : {}", input_file.as_ref().unwrap_err());
|
||||||
@ -39,4 +43,17 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
println!("{} : lines processed", line_number - 1);
|
println!("{} : lines processed", line_number - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_required_files(){
|
||||||
|
let file_path = Path::new(TEST_FILE_NAME);
|
||||||
|
|
||||||
|
if file_path.exists() == false {
|
||||||
|
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).unwrap();
|
||||||
|
println!("File created.");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -1,11 +1,11 @@
|
|||||||
|
use std::fs;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
||||||
|
create_required_files();
|
||||||
let mut path_buffer = PathBuf::new();
|
let mut path_buffer = PathBuf::new();
|
||||||
|
|
||||||
path_buffer.push("solutions");
|
|
||||||
path_buffer.push("24_file_io");
|
|
||||||
path_buffer.push("SampleFilesFolder");
|
path_buffer.push("SampleFilesFolder");
|
||||||
path_buffer.push("MultiLineTextFile.txt");
|
path_buffer.push("MultiLineTextFile.txt");
|
||||||
|
|
||||||
@ -15,7 +15,7 @@ fn main() {
|
|||||||
println!("Metadata about the file : {:?}", path_buffer);
|
println!("Metadata about the file : {:?}", path_buffer);
|
||||||
println!("File creation time {:?}", meta_data.created().unwrap());
|
println!("File creation time {:?}", meta_data.created().unwrap());
|
||||||
println!("File size {}", meta_data.len());
|
println!("File size {}", meta_data.len());
|
||||||
assert_eq!(meta_data.len(), 101);
|
assert_eq!(meta_data.len(), 117);
|
||||||
println!("File permissions {:?}", meta_data.permissions());
|
println!("File permissions {:?}", meta_data.permissions());
|
||||||
assert_eq!(meta_data.permissions().readonly(), false);
|
assert_eq!(meta_data.permissions().readonly(), false);
|
||||||
}else {
|
}else {
|
||||||
@ -26,4 +26,25 @@ fn main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_required_files(){
|
||||||
|
let file_path = PathBuf::from("SampleFilesFolder/MultiLineTextFile.txt");
|
||||||
|
|
||||||
|
let dir_path = file_path.parent().unwrap();
|
||||||
|
|
||||||
|
if dir_path.exists() == false {
|
||||||
|
fs::create_dir(dir_path).unwrap();
|
||||||
|
println!("Created directory {:?}", dir_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
if file_path.exists() == false {
|
||||||
|
|
||||||
|
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).unwrap();
|
||||||
|
println!("File created.");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user