Skip to content

Commit

Permalink
renaming the reader module
Browse files Browse the repository at this point in the history
  • Loading branch information
alensiljak committed Sep 29, 2023
1 parent f19a372 commit ede1e71
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub mod amount;
mod balance;
pub mod commodity;
mod directives;
mod directive_reader;
mod reader;
pub mod history;
mod iterator;
pub mod journal;
Expand Down
2 changes: 2 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ impl<'j, T: Read> Parser<'j, T> {
}
}

/// Parse given input.
/// Fill the Journal with parsed elements.
pub fn parse(&mut self) {
loop {
match self.reader.read_line(&mut self.buffer) {
Expand Down
38 changes: 24 additions & 14 deletions src/directive_reader.rs → src/reader.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
//! Directive Reader
//! Reads directives from the given source.
//! The Journal Reader.
//! Reads directives from the given source and returns them as an iterator.

use std::io::{Read, Cursor, BufReader};
use std::io::{Read, Cursor, BufReader, BufRead};

use crate::directives::DirectiveType;

pub fn read<T: Read>(source: T) -> DirectiveIter<T> {
pub fn create_reader<T: Read>(source: T) -> DirectiveIter<T> {
let iter = DirectiveIter::new(source);
iter
}

pub fn read_str<T: Read>(source: &str) -> DirectiveIter<Cursor<&str>> {
pub fn create_str_reader<T: Read>(source: &str) -> DirectiveIter<Cursor<&str>> {
let cursor = Cursor::new(source);
let iter: DirectiveIter<Cursor<&str>> = DirectiveIter::new(cursor);
// read(cursor)
Expand All @@ -19,7 +19,8 @@ pub fn read_str<T: Read>(source: &str) -> DirectiveIter<Cursor<&str>> {

pub struct DirectiveIter<T: Read> {
// source: T,
reader: BufReader<T>
reader: BufReader<T>,
buffer: String
}

impl<T: Read> DirectiveIter<T> {
Expand All @@ -28,7 +29,8 @@ impl<T: Read> DirectiveIter<T> {

Self {
// source,
reader
reader,
buffer: String::new()
}
}
}
Expand All @@ -39,6 +41,11 @@ impl<T: Read> Iterator for DirectiveIter<T> {
fn next(self: &mut DirectiveIter<T>) -> Option<Self::Item> {
// Read lines and recognise the directive.
// TODO: Read line.
match self.reader.read_line(&mut self.buffer) {
Ok(result) => println!("Result: {:?}", result),
Err(error) => panic!("Error: {:?}", error)
};

// TODO: Recognise directive, if any.
// TODO: Read additional lines, if needed (like for Xact).
// TODO: Parse and return the directive.
Expand All @@ -55,24 +62,27 @@ impl<T: Read> Iterator for DirectiveIter<T> {
mod tests {
use std::io::Cursor;

use crate::directive_reader::read_str;

use super::DirectiveIter;
use crate::reader::create_str_reader;

#[test]
fn basic_test() {
let content = "blah blah";
// let reader = DirectiveIter::new();

let output = read_str::<Cursor<&str>>(content);
let output = create_str_reader::<Cursor<&str>>(content);

todo!("incomplete");
let mut counter = 0;
for item in output {
println!("item: {:?}", item);
counter += 1;
}
assert_eq!(3, counter);
}

// #[test]
fn iterator_test() {
let content = "blah blah";
//let iter = DirectiveIter::new();
let iter = read_str::<Cursor<&str>>(content);
let iter = create_str_reader::<Cursor<&str>>(content);

for x in iter {
println!("Directive: {:?}", x);
Expand Down

0 comments on commit ede1e71

Please sign in to comment.