supmcu_rs/
lib.rs

1//! # pumpkin_supmcu-rs
2//!
3//! This crate is a rust rewrite of the [pumpkin_supmcu](https://gitlab.com/pumpkin-space-systems/public/pumpkin-supmcu) python package.
4//! Its purpose is to interact with modules by disovering and parsing telemetry data and communicating via I2C
5
6use i2cdev::linux::LinuxI2CError;
7use supmcu::parsing::{SupMCUValue, TelemetryType};
8use thiserror::Error;
9
10pub mod supmcu;
11
12#[derive(Error, Debug)]
13pub enum SupMCUError {
14    #[error("IoError: {0}")]
15    IoError(#[from] std::io::Error),
16    #[error("{device} (addr {address}): {error}")]
17    I2CDevError {
18        device: String,
19        address: u16,
20        error: LinuxI2CError,
21    },
22    #[error("Failed sending command over I2C ({0:#04x}) {1}")]
23    I2CCommandError(u16, String),
24    #[error("Failed reading telemetry over I2C ({0:#04x}) {1}")]
25    I2CTelemetryError(u16, String),
26    #[error("ParsingError: {0}")]
27    ParsingError(#[from] ParsingError),
28    #[error("Failed to find {0} telemetry item at index {1}")]
29    TelemetryIndexError(TelemetryType, usize),
30    #[error("module@{0:#04X}: {1} returned a non-ready response.  Try increasing `response_delay`")]
31    NonReadyError(u16, String),
32    #[error("Failed to validate data with checksum.")]
33    ValidationError,
34    #[error("SupMCUModuleDefinition not found. Have you run discover?")]
35    MissingDefinitionError,
36    #[error("AsyncError: {0}")]
37    AsyncError(#[from] tokio::task::JoinError),
38    #[error("JSONError: {0}")]
39    JSONError(#[from] serde_json::Error),
40    #[error("Module not found: {0} {1}")]
41    ModuleNotFound(String, u16),
42    #[error("Unexpected value for {0}: {1}")]
43    UnexpectedValue(String, SupMCUValue),
44    #[error("Unknown telemetry name {0}")]
45    UnknownTelemName(String),
46}
47
48impl From<std::string::FromUtf8Error> for SupMCUError {
49    fn from(e: std::string::FromUtf8Error) -> Self {
50        SupMCUError::ParsingError(ParsingError::StringParsingError(e))
51    }
52}
53
54#[derive(Error, Debug)]
55pub enum ParsingError {
56    #[error("Failed to convert bytes into object: {0}")]
57    InvalidBytes(String),
58    #[error("Invalid format string {0} for bytes {1:?}")]
59    InvalidFormatString(String, Vec<u8>),
60    #[error("Invalid format character {0}")]
61    InvalidFormatCharacter(char),
62    #[error("Failed to parse primitive bytes: {0}")]
63    ByteParsingError(#[from] std::io::Error),
64    #[error("Failed to parse UTF-8 encoded string")]
65    StringParsingError(#[from] std::string::FromUtf8Error),
66    #[error("Failed to parse command name from version string {0}")]
67    VersionParsingError(String),
68    #[error("Error parsing command {0}")]
69    CommandParsingError(String),
70    #[error("Unknown MCU ID {0}")]
71    McuIdParsingError(u8),
72}