data_reader/reader/
mod.rs

1// This file is a part of the Rust Data Reader Library
2// Copyright 2018 Robert Carson
3//
4//    Licensed under the Apache License, Version 2.0 (the "License");
5//    you may not use this file except in compliance with the License.
6//    You may obtain a copy of the License at
7//
8//        http://www.apache.org/licenses/LICENSE-2.0
9//
10//    Unless required by applicable law or agreed to in writing, software
11//    distributed under the License is distributed on an "AS IS" BASIS,
12//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13//    See the License for the specific language governing permissions and
14//    limitations under the License.
15
16use anyhow::Error;
17use std::str::FromStr;
18use std::str;
19
20#[macro_use]
21#[doc(hidden)]
22mod macro_src;
23
24/// Contains various float type readers
25pub mod float_reader;
26/// Contains various integer type readers
27pub mod int_reader;
28/// Contains various primitive type readers that don't fit into the other reader modules
29pub mod prim_reader;
30/// Contains various unsigned integer type readers
31pub mod uint_reader;
32/// Contains the results from parse_txt or load_txt!
33pub mod reader_results;
34/// Contains the functions that will parse a file and return a RawReaderResults
35pub mod parser;
36pub(crate) mod parser_core;
37/// Contains a couple functions that are useful for parsing files
38pub mod parser_utility;
39
40pub use self::float_reader::*;
41pub use self::int_reader::*;
42pub use self::prim_reader::*;
43pub use self::uint_reader::*;
44pub use self::macro_src::*;
45pub use self::reader_results::*;
46pub use self::parser::parse_txt;
47pub use self::parser_utility::*;
48
49//This value is similar in value to the one found in BurntSushi's CSV buffer size
50//Our's is just 4x as large.
51const BUF_SIZE: usize = 8 * (1 << 12);
52///The type of delimiter that we can use
53pub enum Delimiter {
54    WhiteSpace,
55    Any(u8),
56}
57
58///ReaderParams tells us what our reader should be doing.
59///
60///delimiter - the delimiter that tells us what our data fields are seperated by
61///
62/// skip_header - an optional field that tells us whether or not we should skip so many lines that are not
63///     comment lines from the beginning of the file
64///
65/// skip_footer - an optional field that tells us whether or not we should skip so many lines that are not
66///     comment lines from the end of the file
67///
68/// usecols - an optional field that tells us what column numbers we should be using from the data field
69///     where these values should be >= 0. Values are 0 indexed here.
70///
71/// max_rows - an optional field that tells us the maximum number of rows we should use from the file
72/// row_format - a required field that tells us whether or not the file should be read in row major or column major
73///              Using ..Default::default() it defaults to being true to preserve old behavior of the code.
74// is_string - an optional field that tells us if the string passed is a string or file
75pub struct ReaderParams {
76    pub comments: Option<u8>,
77    pub delimiter: Delimiter,
78    pub skip_header: Option<usize>,
79    pub skip_footer: Option<usize>,
80    pub usecols: Option<Vec<usize>>,
81    pub max_rows: Option<usize>,
82    pub row_format: bool,
83    // pub is_string: Option<bool>,
84}
85
86///You can use the default constructor like this:
87///
88///let params = ReaderParams::default(); or you could do
89///
90///something like -     
91///let params = ReaderParams{
92///        comments: Some(b'%'),
93///        ..Default::default()
94///};
95impl Default for ReaderParams {
96    fn default() -> ReaderParams {
97        ReaderParams {
98            comments: Some(b'#'),
99            delimiter: Delimiter::WhiteSpace,
100            skip_header: None,
101            skip_footer: None,
102            usecols: None,
103            max_rows: None,
104            row_format: true,
105            // is_string: None,
106        }
107    }
108}