Compare commits

..

5 commits

Author SHA1 Message Date
1a4694bad5 Add API struct 2024-02-02 19:57:43 +00:00
5ec9079456 Avoid unnecessary item paths 2024-02-02 19:57:03 +00:00
7d42a56199 Add error enum 2024-02-02 19:56:49 +00:00
1da05e29e1 Fix serialization/deserialization of status codes 2024-02-02 19:56:49 +00:00
375377f56a Use type-based data collections 2024-02-02 19:56:40 +00:00

View file

@ -4,17 +4,6 @@ use serde_repr::{Deserialize_repr, Serialize_repr};
use std::{borrow::Borrow, collections::HashMap, net::IpAddr}; use std::{borrow::Borrow, collections::HashMap, net::IpAddr};
use thiserror::Error; use thiserror::Error;
#[derive(Debug)]
pub struct ValueError(String);
impl std::fmt::Display for ValueError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "Invalid value given! Reason: {}", self.0)
}
}
impl std::error::Error for ValueError {}
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum Error { pub enum Error {
#[error("unsupported API version {0}")] #[error("unsupported API version {0}")]
@ -87,7 +76,7 @@ impl Fronius {
pub fn get_inverter_realtime_data_device<C: DataCollection>( pub fn get_inverter_realtime_data_device<C: DataCollection>(
&self, &self,
device_id: DeviceId, device_id: DeviceId,
) -> Result<CumulationInverterData, Error> { ) -> Result<C, Error> {
let device_id = u8::from(device_id).to_string(); let device_id = u8::from(device_id).to_string();
let response: CommonResponseBody<_> = self.make_request( let response: CommonResponseBody<_> = self.make_request(
@ -178,14 +167,18 @@ struct ApiVersion {
pub struct DeviceId(u8); pub struct DeviceId(u8);
impl TryFrom<u8> for DeviceId { #[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
type Error = ValueError; #[error("invalid device ID, must be less than 100: {0}")]
pub struct InvalidDeviceId(u8);
fn try_from(device_id: u8) -> Result<Self, ValueError> { impl TryFrom<u8> for DeviceId {
type Error = InvalidDeviceId;
fn try_from(device_id: u8) -> Result<Self, InvalidDeviceId> {
if device_id <= 99 { if device_id <= 99 {
Ok(Self(device_id)) Ok(Self(device_id))
} else { } else {
Err(ValueError("device id not in range!".into())) Err(InvalidDeviceId(device_id))
} }
} }
} }