changeset 6:29be98f95b18

Add util: String -> Record adapter
author Lewin Bormann <lbo@spheniscida.de>
date Sat, 30 Jan 2016 21:31:21 +0000
parents 173f7aba48cf
children f68ce3a86754
files src/lib.rs src/util.rs
diffstat 2 files changed, 32 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib.rs	Sat Jan 30 21:00:44 2016 +0000
+++ b/src/lib.rs	Sat Jan 30 21:31:21 2016 +0000
@@ -1,6 +1,7 @@
+pub mod closure_mr;
 pub mod formats;
 pub mod mapreducer;
-pub mod closure_mr;
+pub mod util;
 
 #[test]
 fn it_works() {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/util.rs	Sat Jan 30 21:31:21 2016 +0000
@@ -0,0 +1,30 @@
+use std::convert::From;
+use mapreducer::Record;
+use std::fmt;
+
+/// Transforms an iterator<string> into an iterator<Record>. It yields
+/// records with the key being the position of the current record, starting with
+/// 1.
+pub struct RecordIterator {
+    i: Box<Iterator<Item=String>>,
+    counter: u64,
+}
+
+impl RecordIterator {
+    fn new(it: Box<Iterator<Item=String>>) -> RecordIterator {
+        RecordIterator { i: it, counter: 0 }
+    }
+}
+
+impl Iterator for RecordIterator {
+    type Item = Record;
+    fn next(&mut self) -> Option<Record> {
+        match self.i.next() {
+            None => None,
+            Some(val) => {
+                self.counter += 1;
+                Some(Record { key: fmt::format(format_args!("{}", self.counter)), value: val })
+            }
+        }
+    }
+}