easygradecalculator 0.1.3

easy grade calculator
Documentation
/*
# Easy Average Grade Calculator 

This tool is a easy average grade average calulcator written in Rust. It takes a list of graded to calculated the average and can be used as a library by other applications.

See a more complex [grade calulcator](https://www.thebestdegree.com/grade-calculator/) for weighted grade calculation for college or high school students and teachers. 

These tools are all implemented in Rust.

## Usage ##

The easy grade calulcator can either read the input grades from arguments or a text file as shown below:

```bash
grade-avg 3.5 2.5
```

Another way to use the tool would be to create a txt file as the input grades. Create a input file `grades.txt` with the contents:

```
1.5
3.5
2.5
```

The average for these grades can be calculcated as below: 



```bash
cat ~/grades.txt | grade-avg
```
*/

use easygradecalculator::Grade;
use std::env;
use std::io::{self, Read};

fn main() -> Result<(), std::io::Error> {
    let args = env::args().collect::<Vec<String>>();
    let input = if args.len() == 1 {
        let mut buffer = String::new();
        io::stdin().read_to_string(&mut buffer)?;
        parse_input(&buffer)
    } else {
        args.split_at(1).1.to_vec()
    };
    let mut grades = Vec::new();
    for arg in input {
        match Grade::from(&arg) {
            Ok(grade) => grades.push(grade),
            Err(_) => eprintln!("Ignoring {}, because it cannot be parsed", arg),
        }
    }

    if let Some(avg_grade) = easygradecalculator::avg(&grades) {
        println!(
            "{1}, {0} <{2:.5}>",
            avg_grade,
            avg_grade.verbal(),
            easygradecalculator::avg_prec(&grades).unwrap_or(0.0)
        );
    } else {
        eprintln!("Could not calculate average!")
    }
    Ok(())
}

fn parse_input(buffer: &str) -> Vec<String> {
    buffer
        .split_ascii_whitespace()
        .map(|e| e.to_string())
        .collect::<Vec<String>>()
}