title: Buffering serial ouput and parsing JSON
body: <p>I'm new to rust and struggling with an issue. I'm looking to read output from serial and convert it to JSON.</p>
  
  <p>Below is the section of code that reads in the serial output.</p>
  
  <pre class="lang-rust prettyprint-override"><code>match serialport::open(&amp;port_name) {
      Ok(mut port) =&gt; {
          let mut serial_buf: Vec&lt;u8&gt; = vec![0; 1000];
          loop {
              match port.read(serial_buf.as_mut_slice()) {
                  Ok(t) =&gt; handle_event(&amp;serial_buf[..t]),
                  Err(ref e) if e.kind() == io::ErrorKind::TimedOut =&gt; (),
                  Err(e) =&gt; print!("Port read error: {}\n", e), 
              };
          }
      }
      Err(e) =&gt; {
          println!("Failed to open \"{}\". Error: {}", port_name, e);
          ::std::process::exit(1);
      }
  };
  </code></pre>
  
  <p>Next that output is passed to serde to be parsed. However the output it gets isn't always the complete JSON string so serde breaks. I believe I need to buffer the response here but struggling to fit that in.</p>
  
  <pre class="lang-rust prettyprint-override"><code>fn handle_event(buf: &amp;[u8]) {
      let e_str = match str::from_utf8(buf) {
          Ok(v) =&gt; v,
          Err(e) =&gt; { print!("Str conversion error: {}\n", e); return; }, 
      };
  
      let e = match serde_json::from_str::&lt;Event&gt;(e_str) {
          Ok(e) =&gt; e,
          Err(e) =&gt; { print!("JSON parse error: {}\n", e); return; },
      };
  
      print!("{}", e.name);
  }
  </code></pre>
  
  <p>Thanks in advance for any help.</p>