Struct Reader

Source
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<'_>

Source

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"))
Source

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]);
Source

pub fn execute<T: FromStr>(&self) -> Vec<T>
where <T as FromStr>::Err: Debug,

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]);
Source

pub fn read_header<T: FromStr>(&self, len: usize) -> Vec<Vec<T>>
where <T as FromStr>::Err: Debug,

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.

Source

pub fn read_body<T: FromStr>(&self, header: usize, footer: usize) -> Vec<Vec<T>>
where <T as FromStr>::Err: Debug,

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> recording the value in the body, which are also parsed to usize type.

Source

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);
Source

pub fn read_csv<T: FromStr>(&self, row: usize) -> Vec<T>
where <T as FromStr>::Err: Debug,

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.

Auto Trait Implementations§

§

impl<'a> Freeze for Reader<'a>

§

impl<'a> RefUnwindSafe for Reader<'a>

§

impl<'a> Send for Reader<'a>

§

impl<'a> Sync for Reader<'a>

§

impl<'a> Unpin for Reader<'a>

§

impl<'a> UnwindSafe for Reader<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.