pub struct Reader<'a> {
pub lines: String,
pub values: Vec<String>,
/* private fields */
}
Expand description
A reader structure for reading specific values of the same file in succession.
§Example
collect a Reader type (same with Builder, Changer):
use self::simple_file_manager::fileapi::FileAPI;
let file = FileAPI::from("filename.gph");
let reader = file.reader();
// read the header of the file
let header = reader.read_header::<usize>(1)[0].clone();
assert_eq!(header, vec![1, 2, 3]);
then you will receive a Vec recording the value in the first line, which are also parsed to usize type.
Fields§
§lines: String
§values: Vec<String>
Implementations§
Source§impl Reader<'_>
impl Reader<'_>
Sourcepub fn read_to_string(&self) -> String
pub fn read_to_string(&self) -> String
Read all text in the file.
§Example
collect a Reader type (same with Builder, Changer):
use self::simple_file_manager::fileapi::FileAPI;
let file = FileAPI::from("filename.gph");
let reader = file.reader();
// read all the lines in the file
let context = reader.read_to_string();
assert_eq!(context, String::from("1,2,3\n4,5,6\n7,8,9\n10,12"))
Sourcepub fn read_value(self, line: usize, row: usize) -> Self
pub fn read_value(self, line: usize, row: usize) -> Self
A function to read a value in this data storage file.
§Example
collect a Reader type (same with Builder, Changer):
use self::simple_file_manager::fileapi::FileAPI;
let file = FileAPI::from("filename.gph").split(',');
let reader = file.reader();
// read the value in the 5th line and 7th row :
// you can consecutive read values:
let results = reader.read_value(1, 2)
.read_value(3, 2)
.read_value(2, 1)
.execute::<usize>(); // after select the values, you will need to execute and receive the values stored in a [Vec].
assert_eq!(results, vec![2, 8, 4]);
Sourcepub fn execute<T: FromStr>(&self) -> Vec<T>
pub fn execute<T: FromStr>(&self) -> Vec<T>
Confirm and receive the selected values.
§Example
collect a Reader type (same with Builder, Changer):
use self::simple_file_manager::fileapi::FileAPI;
let file = FileAPI::from("filename.gph").split(',');
let reader = file.reader();
// read the value in the 5th line and 7th row :
// you can consecutive read values:
let results = reader.read_value(1, 2)
.read_value(3, 2)
.read_value(2, 1)
.execute::<usize>(); // after select the values, you will need to execute and receive the values stored in a [Vec].
assert_eq!(results, vec![2, 8, 4]);
Sourcepub fn read_header<T: FromStr>(&self, len: usize) -> Vec<Vec<T>>
pub fn read_header<T: FromStr>(&self, len: usize) -> Vec<Vec<T>>
Read the specific lines of header and parse them into a certain type.
§Example
collect a Reader type (same with Builder, Changer):
use self::simple_file_manager::fileapi::FileAPI;
let file = FileAPI::from("filename.gph");
let reader = file.reader();
// read the header of the file
let header = reader.read_header::<usize>(1)[0].clone();
assert_eq!(header, vec![1, 2, 3]);
then you will receive a Vec recording the value in the first line, which are also parsed to usize type.
Read the last line and parse them into a certain type.
§Example
collect a Reader type (same with Builder, Changer):
use self::simple_file_manager::fileapi::FileAPI;
let file = FileAPI::from("filename.gph");
let reader = file.reader();
// read the footer of the file
let footer = reader.read_footer::<usize>();
assert_eq!(footer, vec![10, 12]);
then you will receive a Vec recording the value in the last line, which are also parsed to usize type.
Sourcepub fn read_body<T: FromStr>(&self, header: usize, footer: usize) -> Vec<Vec<T>>
pub fn read_body<T: FromStr>(&self, header: usize, footer: usize) -> Vec<Vec<T>>
Read the main context and parse them into a certain type.
§Example
collect a Reader type (same with Builder, Changer):
use self::simple_file_manager::fileapi::FileAPI;
let file = FileAPI::from("filename.gph");
let reader = file.reader();
// read the body
// skip the first two line and the last line.
let body = reader.read_body::<usize>(1, 1);
assert_eq!(body, vec![vec![4, 5, 6], vec![7, 8, 9]]);
Then you will receive a Vec<Vec
Sourcepub fn count_lines(&self) -> usize
pub fn count_lines(&self) -> usize
Count the lines.
§Example
use self::simple_file_manager::fileapi::FileAPI;
let len = FileAPI::from("filename.gph").reader().count_lines();
assert_eq!(len, 4);
Sourcepub fn read_csv<T: FromStr>(&self, row: usize) -> Vec<T>
pub fn read_csv<T: FromStr>(&self, row: usize) -> Vec<T>
A function to read the specific row in this csv file.
§Example
use self::simple_file_manager::fileapi::FileAPI;
// read the specific row of the csv file.
let row = FileAPI::from("filename.gph").reader().read_csv::<usize>(5);
assert_eq!(row, vec![4, 7, 10]);
Then you will receive a Vec recording the data in this row, which are also parsed to usize type.