khash/
stream.rs

1use crate::*;
2
3/// A streaming kana hash digest.
4///
5/// This type can be used to generate kana mnemonics from any data.
6/// It wraps a type implementing `std::io::Read` and produces a kana mnemonic reading from the stream until its end.
7/// ```
8/// # use khash::Digest;
9/// let input = "Hello world!";
10/// let mnemonic: String = Digest::new(&mut input.as_bytes()).collect(); // Read the bytes from the `input` string and collect the kana mnemonic into a `String` 
11/// ```
12pub struct Digest<'a, T>
13where T: Read
14{
15    input: &'a mut T,
16}
17
18impl<'a, T: Read> Digest<'a, T>
19{
20    /// Create a new stream digest iterator from the input stream.
21    pub fn new(input: &'a mut T) -> Self
22    {
23	Self{input}
24    }
25}
26
27impl<'a, T: Read> Iterator for Digest<'a, T>
28{
29    type Item = String; //TODO: Change this to `char` and keep an internal buffer that we `fmt::write!` the mnemonic digest to instead of `format!`ing it.
30    fn next(&mut self) -> Option<Self::Item>
31    {
32	let mut buffer = [0u8; 2];
33	let mut rd =0;
34	while rd < 2 {
35	    match self.input.read(&mut buffer[rd..]) {
36		Ok(2) => break,
37		Err(_) | Ok(0) => return None,
38		Ok(v) => rd+=v,
39	    }
40	}
41	Some(format!("{}",mnemonic::Digest::new(&buffer[..])))
42    }
43}