Skip to content

Commit

Permalink
base for the iterator API
Browse files Browse the repository at this point in the history
  • Loading branch information
alensiljak committed Sep 18, 2023
1 parent c0c09d2 commit f19a372
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
81 changes: 81 additions & 0 deletions src/directive_reader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//! Directive Reader
//! Reads directives from the given source.
use std::io::{Read, Cursor, BufReader};

use crate::directives::DirectiveType;

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

pub fn read_str<T: Read>(source: &str) -> DirectiveIter<Cursor<&str>> {
let cursor = Cursor::new(source);
let iter: DirectiveIter<Cursor<&str>> = DirectiveIter::new(cursor);
// read(cursor)
iter
}

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

impl<T: Read> DirectiveIter<T> {
pub fn new(source: T) -> Self {
let reader = BufReader::new(source);

Self {
// source,
reader
}
}
}

impl<T: Read> Iterator for DirectiveIter<T> {
type Item = DirectiveType;

fn next(self: &mut DirectiveIter<T>) -> Option<Self::Item> {
// Read lines and recognise the directive.
// TODO: Read line.
// TODO: Recognise directive, if any.
// TODO: Read additional lines, if needed (like for Xact).
// TODO: Parse and return the directive.

// return Some(DirectiveType::Xact(Xact::default()))

// TODO: "Return None when complete";
return None
}
}


#[cfg(test)]
mod tests {
use std::io::Cursor;

use crate::directive_reader::read_str;

use super::DirectiveIter;

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

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

todo!("incomplete");
}

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

for x in iter {
println!("Directive: {:?}", x);
}
}
}
4 changes: 3 additions & 1 deletion src/iterator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/*!
* Implemetation of the parser that returns an iterator over the results
* Implemetation of the parser that returns an iterator over the results.
*
* An example on how to use Iterators.
*/

use crate::{directives::DirectiveType, xact::Xact};
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub mod amount;
mod balance;
pub mod commodity;
mod directives;
mod directive_reader;
pub mod history;
mod iterator;
pub mod journal;
Expand Down

0 comments on commit f19a372

Please sign in to comment.