Add serde from rocket; drop depend on serde

This commit is contained in:
Paul van Tilburg 2023-01-16 20:07:56 +01:00
parent 365b847313
commit e6b0357670
Signed by: paul
GPG Key ID: C6DE073EDA9EEC4D
6 changed files with 34 additions and 19 deletions

1
Cargo.lock generated
View File

@ -1669,7 +1669,6 @@ dependencies = [
"once_cell",
"reqwest",
"rocket",
"serde",
"thiserror",
"toml",
"url",

View File

@ -19,7 +19,6 @@ md-5 = "0.10.5"
once_cell = "1.9.0"
reqwest = { version = "0.11.6", features = ["cookies", "json"] }
rocket = { version = "0.5.0-rc.2", features = ["json"] }
serde = "1.0.116"
thiserror = "1.0.38"
toml = "0.5.6"
url = "2.2.2"

View File

@ -22,8 +22,11 @@ use std::sync::Mutex;
use once_cell::sync::Lazy;
use rocket::fairing::AdHoc;
use rocket::serde::json::Json;
use rocket::{get, routes, Build, Rocket};
use serde::{Deserialize, Serialize};
use rocket::{
get, routes,
serde::{Deserialize, Serialize},
Build, Rocket,
};
use self::update::update_loop;
@ -32,6 +35,7 @@ static STATUS: Lazy<Mutex<Option<Status>>> = Lazy::new(|| Mutex::new(None));
/// The configuration loaded additionally by Rocket.
#[derive(Debug, Deserialize)]
#[serde(crate = "rocket::serde")]
struct Config {
/// The service-specific configuration
service: services::Config,
@ -39,6 +43,7 @@ struct Config {
/// The current photovoltaic invertor status.
#[derive(Clone, Copy, Debug, Serialize)]
#[serde(crate = "rocket::serde")]
struct Status {
/// Current power production (W)
current_w: f32,

View File

@ -4,8 +4,7 @@ pub(crate) mod hoymiles;
pub(crate) mod my_autarco;
use enum_dispatch::enum_dispatch;
use rocket::async_trait;
use serde::Deserialize;
use rocket::{async_trait, serde::Deserialize};
use crate::Status;

View File

@ -9,8 +9,10 @@ use std::sync::Arc;
use chrono::{DateTime, Local, TimeZone};
use md5::{Digest, Md5};
use reqwest::{cookie::Jar as CookieJar, Client, ClientBuilder, Url};
use rocket::{async_trait, serde::json::Value as JsonValue};
use serde::{Deserialize, Deserializer, Serialize};
use rocket::{
async_trait,
serde::{json::Value as JsonValue, Deserialize, Deserializer, Serialize},
};
use url::ParseError;
use crate::{
@ -34,6 +36,7 @@ const POLL_INTERVAL: u64 = 300;
/// The configuration necessary to access the Hoymiles.
#[derive(Debug, Deserialize)]
#[serde(crate = "rocket::serde")]
pub(crate) struct Config {
/// The username of the account to login with
username: String,
@ -89,7 +92,7 @@ fn api_url() -> Result<Url, ParseError> {
/// instead of `null`. If the response is not deserializable object, the JSON value is preserved
/// for debugging purposes.
#[derive(Debug, Deserialize)]
#[serde(untagged)]
#[serde(crate = "rocket::serde", untagged)]
enum StringOrObject<'a, T> {
/// The value is an object (deserializable as type `T`).
Object(T),
@ -103,10 +106,10 @@ enum StringOrObject<'a, T> {
fn from_empty_str_or_object<'de, D, T>(deserializer: D) -> Result<Option<T>, D::Error>
where
D: Deserializer<'de>,
D::Error: serde::de::Error,
D::Error: rocket::serde::de::Error,
T: Deserialize<'de>,
{
use serde::de::Error;
use rocket::serde::de::Error;
match <StringOrObject<'_, T>>::deserialize(deserializer) {
Ok(StringOrObject::String(s)) if s.is_empty() => Ok(None),
@ -125,9 +128,9 @@ where
fn from_date_time_str<'de, D>(deserializer: D) -> Result<DateTime<Local>, D::Error>
where
D: Deserializer<'de>,
D::Error: serde::de::Error,
D::Error: rocket::serde::de::Error,
{
use serde::de::Error;
use rocket::serde::de::Error;
let s = <&str>::deserialize(deserializer)?;
Local
@ -141,9 +144,9 @@ where
fn from_float_str<'de, D>(deserializer: D) -> Result<f32, D::Error>
where
D: Deserializer<'de>,
D::Error: serde::de::Error,
D::Error: rocket::serde::de::Error,
{
use serde::de::Error;
use rocket::serde::de::Error;
let s = <&str>::deserialize(deserializer)?;
s.parse::<f32>().map_err(D::Error::custom)
@ -155,9 +158,9 @@ where
fn from_integer_str<'de, D>(deserializer: D) -> Result<u16, D::Error>
where
D: Deserializer<'de>,
D::Error: serde::de::Error,
D::Error: rocket::serde::de::Error,
{
use serde::de::Error;
use rocket::serde::de::Error;
let s = <&str>::deserialize(deserializer)?;
s.parse::<u16>().map_err(D::Error::custom)
@ -165,6 +168,7 @@ where
/// The request passed to the API login endpoint.
#[derive(Debug, Serialize)]
#[serde(crate = "rocket::serde")]
struct ApiLoginRequest {
/// The body of the API login request.
body: ApiLoginRequestBody,
@ -189,6 +193,7 @@ impl ApiLoginRequest {
/// The request body passed to the API login endpoint.
#[derive(Debug, Serialize)]
#[serde(crate = "rocket::serde")]
struct ApiLoginRequestBody {
/// The username to login with.
password: String,
@ -198,6 +203,7 @@ struct ApiLoginRequestBody {
/// The response returned by the API login endpoint.
#[derive(Debug, Deserialize)]
#[serde(crate = "rocket::serde")]
struct ApiLoginResponse {
/// The status (error) code as a string: 0 for OK, another number for error.
#[serde(deserialize_with = "from_integer_str")]
@ -212,6 +218,7 @@ struct ApiLoginResponse {
/// The response data returned by the API login endpoint.
#[derive(Debug, Deserialize)]
#[serde(crate = "rocket::serde")]
struct ApiLoginResponseData {
/// The token to be used as cookie for API data requests.
token: String,
@ -219,6 +226,7 @@ struct ApiLoginResponseData {
/// The request passed to the API data endpoint.
#[derive(Debug, Serialize)]
#[serde(crate = "rocket::serde")]
struct ApiDataRequest {
/// The body of the API data request.
body: ApiDataRequestBody,
@ -235,6 +243,7 @@ impl ApiDataRequest {
/// The request body passed to the API data endpoint.
#[derive(Debug, Serialize)]
#[serde(crate = "rocket::serde")]
struct ApiDataRequestBody {
/// The ID of the Hoymiles station.
sid: u32,
@ -242,6 +251,7 @@ struct ApiDataRequestBody {
/// The response returned by the API data endpoint.
#[derive(Debug, Deserialize)]
#[serde(crate = "rocket::serde")]
struct ApiDataResponse {
/// The status (error) code as a string: 0 for OK, another number for error.
#[serde(deserialize_with = "from_integer_str")]
@ -256,6 +266,7 @@ struct ApiDataResponse {
/// The response data returned by the API data endpoint.
#[derive(Debug, Deserialize)]
#[serde(crate = "rocket::serde")]
struct ApiDataResponseData {
/// Energy produced today (Wh)
#[serde(deserialize_with = "from_float_str")]

View File

@ -5,8 +5,7 @@
//! See also: <https://my.autarco.com>.
use reqwest::{Client, ClientBuilder, StatusCode, Url};
use rocket::async_trait;
use serde::Deserialize;
use rocket::{async_trait, serde::Deserialize};
use url::ParseError;
use crate::{
@ -22,6 +21,7 @@ const POLL_INTERVAL: u64 = 300;
/// The configuration necessary to access the My Autarco API.
#[derive(Debug, Deserialize)]
#[serde(crate = "rocket::serde")]
pub(crate) struct Config {
/// The username of the account to login with
username: String,
@ -60,6 +60,7 @@ fn api_url(site_id: &str, endpoint: &str) -> Result<Url, ParseError> {
/// The energy data returned by the energy API endpoint.
#[derive(Debug, Deserialize)]
#[serde(crate = "rocket::serde")]
struct ApiEnergy {
/// Total energy produced today (kWh)
// pv_today: u32,
@ -71,6 +72,7 @@ struct ApiEnergy {
/// The power data returned by the power API endpoint.
#[derive(Debug, Deserialize)]
#[serde(crate = "rocket::serde")]
struct ApiPower {
/// Current power production (W)
pv_now: u32,