<commit_before>extern crate termios;

use std::fs::File;
use std::io;
use std::io::prelude::*;
use std::str;

use std::os::unix::io::AsRawFd;
use std::os::unix::io::RawFd;

use termios::*;

fn setup_fd(fd: RawFd) -> io::Result<()> {
    let mut termios = try!(Termios::from_fd(fd));

    termios.c_iflag = IGNPAR | IGNBRK;
    termios.c_oflag = 0;
    termios.c_cflag = CS8 | CREAD | CLOCAL;
    termios.c_lflag = 0;

    try!(cfsetspeed(&mut termios, B9600));
    try!(tcsetattr(fd, TCSANOW, &termios));
    try!(tcflush(fd, TCIOFLUSH));

    Ok(())
}

fn main() {
    let mut ser_file = match File::open("/dev/ttyUSB0") {
        Ok(val) => val,
        Err(_) => {
            println!("Unable to open port!");
            return;
        }
    };

    match setup_fd(ser_file.as_raw_fd()) {
        Ok(_) => println!("Woo!"),
        Err(_) => {
            println!("Unable to configure serial port!");
            return;
        }
    };

    let mut buffer = [0; 256];
    let mut buf_len = 0;

    match ser_file.read(&mut buffer) {
        Ok(n) => buf_len = n,
        Err(_) => {
            println!("Oh noes couldn't read!");
            return;
        }
    };

    let output = match str::from_utf8(&buffer[..buf_len]) {
        Ok(val) => val,
        Err(_) => {
            println!("Oh noe unable to parse as string!");
            return;
        }
    };

    println!("Got output: {}", output);
}<commit_msg>Use .ok().expect() rather than match, and actually loop over input.<commit_after>extern crate termios;

use std::str;

use std::fs::File;

use std::io;
use std::io::prelude::*;
use std::io::stdout;

use std::os::unix::io::AsRawFd;
use std::os::unix::io::RawFd;

use termios::*;

fn setup_fd(fd: RawFd) -> io::Result<()> {
    let mut termios = try!(Termios::from_fd(fd));

    termios.c_iflag = IGNPAR | IGNBRK;
    termios.c_oflag = 0;
    termios.c_cflag = CS8 | CREAD | CLOCAL;
    termios.c_lflag = 0;

    try!(cfsetspeed(&mut termios, B9600));
    try!(tcsetattr(fd, TCSANOW, &termios));
    try!(tcflush(fd, TCIOFLUSH));

    Ok(())
}

fn main() {
    let mut ser_file = File::open("/dev/ttyUSB0").ok().expect("Unable to open file handle!");

    setup_fd(ser_file.as_raw_fd()).ok().expect("Unable to configure serial port!");

    let mut buffer = [0; 256];
    let mut buf_len = 0;
    
    while match ser_file.read(&mut buffer) { Ok(n) => {buf_len = n; true}, Err(_) => false} {
        if buf_len <= 0 {
            break;
        }
        print!("{}", str::from_utf8(&buffer[..buf_len]).ok().expect("Oh noes unable to parse as string!"));
        assert!(stdout().flush().is_ok());
    }

    println!("");
}
{'fraction_non_alphanumeric': 0.18613413715146948, 'fraction_numerical': 0.01092690278824416, 'mean_word_length': 2.6620689655172414, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4170732', 'n_tokens_mistral': 1008, 'n_tokens_neox': 972, 'n_words': 214}