sml-kit 0.1.1

Read German smart meters in Rust: SML (Smart Message Language) decoder and encoder, sans-I/O, no_std, no allocator, no dependencies
Documentation
//! Read a meter through an infrared head in a blocking loop.
//!
//! ```text
//! stty -F /dev/ttyUSB0 9600 raw -echo min 1  # Linux; macOS: stty -f
//! cargo run --example serial -- /dev/ttyUSB0
//! xxd -r -p tests/corpus/emh-ehz-apollon77.hex | cargo run --example serial
//! ```
//!
//! Without a path it reads raw bytes from stdin. The serial port must be configured
//! beforehand (typically 9600 baud, 8N1, blocking reads: `min 1`): sml-kit opens no port
//! itself, and a read that returns 0 bytes ends the loop as end of input.

use std::io::Read;

use sml_kit::io::{FrameReader, ReadError};
use sml_kit::{Obis, readout};

fn main() -> std::io::Result<()> {
    let source: Box<dyn Read> = match std::env::args().nth(1) {
        Some(path) => Box::new(std::fs::File::open(path)?),
        None => Box::new(std::io::stdin()),
    };
    for frame in FrameReader::new(source) {
        let frame = match frame {
            Ok(frame) => frame,
            Err(ReadError::Frame(e)) => {
                eprintln!("skipped a damaged frame: {e}");
                continue;
            }
            // A quiet line: keep reading.
            Err(ReadError::Io(e))
                if matches!(e.kind(), std::io::ErrorKind::TimedOut | std::io::ErrorKind::WouldBlock) =>
            {
                continue;
            }
            Err(ReadError::Io(e)) => return Err(e),
        };
        match readout(&frame) {
            Ok(list) => {
                let show = |obis| list.quantity(obis).map_or_else(|| "-".to_owned(), |q| q.to_string());
                println!(
                    "{}  A+ {}  A- {}  P {}",
                    list.server_id,
                    show(Obis::ENERGY_IMPORT),
                    show(Obis::ENERGY_EXPORT),
                    show(Obis::POWER)
                );
            }
            Err(e) => eprintln!("no readings: {e} (try `cargo run --example decode` on a capture)"),
        }
    }
    Ok(())
}