Consolidate ledger serialization code

The new read_entries() works, but is overly-contrained. Not
using that function yet, but adding it here in the hopes some
Rust guru will tell us how to get that lifetime constraint out
of there.

Fixes #517
This commit is contained in:
Greg Fitzgerald
2018-07-02 00:09:41 -06:00
committed by Greg Fitzgerald
parent e7b7dfebf5
commit ec7e50b37d
3 changed files with 18 additions and 9 deletions

View File

@@ -5,7 +5,7 @@
use bank::Bank;
use entry::Entry;
use serde_json;
use std::io::{self, Write};
use std::io::{self, BufRead, Error, ErrorKind, Write};
pub struct EntryWriter<'a, W> {
bank: &'a Bank,
@@ -46,6 +46,17 @@ impl<'a, W: Write> EntryWriter<'a, W> {
}
}
pub fn read_entry(s: String) -> io::Result<Entry> {
serde_json::from_str(&s).map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
}
// TODO: How to implement this without attaching the input's lifetime to the output?
pub fn read_entries<'a, R: BufRead>(
reader: &'a mut R,
) -> impl Iterator<Item = io::Result<Entry>> + 'a {
reader.lines().map(|s| read_entry(s?))
}
#[cfg(test)]
mod tests {
use super::*;