FLamparski / hzgrow-r502

Rust (embedded-hal) driver for the HZ Grow R502 capacitive fingerprint sensor
MIT License
16 stars 1 forks source link

Better API, it gets Results #33

Open FLamparski opened 4 years ago

FLamparski commented 4 years ago

Currently, R502::send_command will return an Ok(T) result if it successfully receives a reply from the device. However, it should rather return an Ok(T) only if it receives a reply and that reply has a status code of Success. This is to enable chaining calls like so:

let search_result = Ok(())
    .and_then(|_| r502.authenticate(0x00000000))
    .and_then(|_| r502.wait_for_image())
    .and_then(|_| r502.process_image(Buffer::Buf1))
    .and_then(|_| r502.search(Buffer::Buf1, 0, 0xff));

match search_result {
    Ok(result) => launch_missiles(),
    Err(Error::ReplyError(..)) => /* request was responded to, but with a status code other than 0x00 */,
    Err(Error::CommsError(..)) => /* error from the underlying connection */
}