From 6f3e0629c508d491f249aecaec6db4c55d4c2156 Mon Sep 17 00:00:00 2001 From: Tijmen van Nesselrooij Date: Mon, 8 Sep 2025 17:03:35 +0200 Subject: [PATCH] Move shared API code to library project --- Cargo.lock | 10 ++++++++ Cargo.toml | 14 ++++++++--- src/electricity_api/Cargo.toml | 19 +++++++------- src/electricity_api/src/helpers.rs | 14 ----------- src/electricity_api/src/main.rs | 9 ++++--- src/shared_api_lib/Cargo.toml | 8 ++++++ src/shared_api_lib/src/lib.rs | 2 ++ .../src/query_helpers.rs | 0 .../src/year_month.rs | 0 src/solar_api/Cargo.toml | 19 +++++++------- src/solar_api/src/database.rs | 2 +- src/solar_api/src/main.rs | 25 +++++++++++-------- 12 files changed, 72 insertions(+), 50 deletions(-) delete mode 100644 src/electricity_api/src/helpers.rs create mode 100644 src/shared_api_lib/Cargo.toml create mode 100644 src/shared_api_lib/src/lib.rs rename src/{solar_api => shared_api_lib}/src/query_helpers.rs (100%) rename src/{solar_api => shared_api_lib}/src/year_month.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index 66a3a68..dad011f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -294,6 +294,7 @@ dependencies = [ "rusqlite", "serde", "serde_json", + "shared_api_lib", "tokio", "warp", ] @@ -1012,6 +1013,14 @@ dependencies = [ "digest", ] +[[package]] +name = "shared_api_lib" +version = "0.1.0" +dependencies = [ + "chrono", + "log", +] + [[package]] name = "shlex" version = "1.3.0" @@ -1060,6 +1069,7 @@ dependencies = [ "rusqlite", "serde", "serde_json", + "shared_api_lib", "tokio", "warp", ] diff --git a/Cargo.toml b/Cargo.toml index 1a89ee0..a03eed8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,15 @@ [workspace] resolver = "3" -members = [ - "src/electricity_api" -, "src/solar_api"] +members = ["src/electricity_api", "src/shared_api_lib", "src/solar_api"] [workspace.dependencies] +chrono = "0.4.41" +clap = "4.5.46" +colog = "1.3.0" +log = "0.4.27" +rusqlite = "0.37.0" +serde = "1.0.219" +serde_json = "1.0.143" +shared_api_lib = { path = "../shared_api_lib" } +tokio = "1" +warp = "0.4" diff --git a/src/electricity_api/Cargo.toml b/src/electricity_api/Cargo.toml index 87ee009..4c50d14 100644 --- a/src/electricity_api/Cargo.toml +++ b/src/electricity_api/Cargo.toml @@ -4,12 +4,13 @@ version = "0.1.0" edition = "2024" [dependencies] -chrono = "0.4.41" -clap = { version = "4.5.46", features = ["derive"] } -colog = "1.3.0" -log = "0.4.27" -rusqlite = { version = "0.37.0", features = ["bundled"] } -serde = { version = "1.0.219", features = ["derive"] } -serde_json = "1.0.143" -tokio = { version = "1", features = ["full"] } -warp = { version = "0.4", features = ["server"] } +chrono = { workspace = true } +clap = { workspace = true, features = ["derive"] } +colog = { workspace = true } +log = { workspace = true } +rusqlite = { workspace = true, features = ["bundled"] } +serde = { workspace = true, features = ["derive"] } +serde_json = { workspace = true } +shared_api_lib = { path = "../shared_api_lib" } +tokio = { workspace = true, features = ["full"] } +warp = { workspace = true, features = ["server"] } diff --git a/src/electricity_api/src/helpers.rs b/src/electricity_api/src/helpers.rs deleted file mode 100644 index e997535..0000000 --- a/src/electricity_api/src/helpers.rs +++ /dev/null @@ -1,14 +0,0 @@ -use chrono::NaiveDate; - -pub fn try_parse_query_date(value: Option<&String>) -> Option { - match value { - Some(string_value) => match chrono::NaiveDate::parse_from_str(string_value, "%Y-%m-%d") { - Ok(date) => Some(date), - Err(error) => { - log::error!("Failed to parse date {} with error {}", string_value, error); - return None; - } - }, - _ => None, - } -} diff --git a/src/electricity_api/src/main.rs b/src/electricity_api/src/main.rs index 66dc325..1b068ca 100644 --- a/src/electricity_api/src/main.rs +++ b/src/electricity_api/src/main.rs @@ -4,7 +4,6 @@ use warp::Filter; use warp::http::Response; mod database; -mod helpers; #[tokio::main] async fn main() { @@ -55,7 +54,7 @@ async fn serve(configuration: &std::sync::Arc) { .body(String::from("Forbidden")); } - match helpers::try_parse_query_date(query.get("date")) { + match shared_api_lib::query_helpers::try_parse_query_date(query.get("date")) { Some(date) => { let json = get_day_power_json(&date, &configuration.database_path); return Response::builder() @@ -92,14 +91,16 @@ async fn serve(configuration: &std::sync::Arc) { .body(String::from("Forbidden")); } - let maybe_start = helpers::try_parse_query_date(query.get("start")); + let maybe_start = + shared_api_lib::query_helpers::try_parse_query_date(query.get("start")); if maybe_start.is_none() { return Response::builder() .status(400) .body(String::from("Unsupported \"start\" param in query.")); } - let maybe_stop = helpers::try_parse_query_date(query.get("stop")); + let maybe_stop = + shared_api_lib::query_helpers::try_parse_query_date(query.get("stop")); if maybe_stop.is_none() { return Response::builder() .status(400) diff --git a/src/shared_api_lib/Cargo.toml b/src/shared_api_lib/Cargo.toml new file mode 100644 index 0000000..35a1df6 --- /dev/null +++ b/src/shared_api_lib/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "shared_api_lib" +version = "0.1.0" +edition = "2024" + +[dependencies] +chrono = { workspace = true } +log = { workspace = true } diff --git a/src/shared_api_lib/src/lib.rs b/src/shared_api_lib/src/lib.rs new file mode 100644 index 0000000..fce87c5 --- /dev/null +++ b/src/shared_api_lib/src/lib.rs @@ -0,0 +1,2 @@ +pub mod query_helpers; +pub mod year_month; diff --git a/src/solar_api/src/query_helpers.rs b/src/shared_api_lib/src/query_helpers.rs similarity index 100% rename from src/solar_api/src/query_helpers.rs rename to src/shared_api_lib/src/query_helpers.rs diff --git a/src/solar_api/src/year_month.rs b/src/shared_api_lib/src/year_month.rs similarity index 100% rename from src/solar_api/src/year_month.rs rename to src/shared_api_lib/src/year_month.rs diff --git a/src/solar_api/Cargo.toml b/src/solar_api/Cargo.toml index ffd9ce7..38bd334 100644 --- a/src/solar_api/Cargo.toml +++ b/src/solar_api/Cargo.toml @@ -4,12 +4,13 @@ version = "0.1.0" edition = "2024" [dependencies] -chrono = "0.4.41" -clap = { version = "4.5.46", features = ["derive"] } -colog = "1.3.0" -log = "0.4.27" -rusqlite = { version = "0.37.0", features = ["bundled"] } -serde = { version = "1.0.219", features = ["derive"] } -serde_json = "1.0.143" -tokio = { version = "1", features = ["full"] } -warp = { version = "0.4", features = ["server"] } +chrono = { workspace = true } +clap = { workspace = true, features = ["derive"] } +colog = { workspace = true } +log = { workspace = true } +rusqlite = { workspace = true, features = ["bundled"] } +serde = { workspace = true, features = ["derive"] } +serde_json = { workspace = true } +shared_api_lib = { path = "../shared_api_lib" } +tokio = { workspace = true, features = ["full"] } +warp = { workspace = true, features = ["server"] } diff --git a/src/solar_api/src/database.rs b/src/solar_api/src/database.rs index d3a929d..ac46f80 100644 --- a/src/solar_api/src/database.rs +++ b/src/solar_api/src/database.rs @@ -1,4 +1,4 @@ -use crate::year_month::YearMonth; +use shared_api_lib::year_month::YearMonth; pub fn get_day_solar_entities(date: &chrono::NaiveDate, database_path: &str) -> DayEntities { let mut result = DayEntities { diff --git a/src/solar_api/src/main.rs b/src/solar_api/src/main.rs index fa04042..13e5881 100644 --- a/src/solar_api/src/main.rs +++ b/src/solar_api/src/main.rs @@ -1,13 +1,10 @@ +use chrono; use clap::Parser; use std::collections::HashMap; use warp::Filter; use warp::http::Response; -use crate::year_month::YearMonth; - mod database; -mod query_helpers; -mod year_month; #[tokio::main] async fn main() { @@ -38,7 +35,7 @@ async fn serve(configuration: &std::sync::Arc) { })) .map( |query: HashMap, configuration: std::sync::Arc| { - match query_helpers::try_parse_query_date(query.get("date")) { + match shared_api_lib::query_helpers::try_parse_query_date(query.get("date")) { Some(date) => { let json = get_day_solar_json(&date, &configuration.database_path); return Response::builder() @@ -61,14 +58,16 @@ async fn serve(configuration: &std::sync::Arc) { })) .map( |query: HashMap, configuration: std::sync::Arc| { - let maybe_start = query_helpers::try_parse_query_date(query.get("start")); + let maybe_start = + shared_api_lib::query_helpers::try_parse_query_date(query.get("start")); if maybe_start.is_none() { return Response::builder() .status(400) .body(String::from("Unsupported \"start\" param in query.")); } - let maybe_stop = query_helpers::try_parse_query_date(query.get("stop")); + let maybe_stop = + shared_api_lib::query_helpers::try_parse_query_date(query.get("stop")); if maybe_stop.is_none() { return Response::builder() .status(400) @@ -108,14 +107,16 @@ async fn serve(configuration: &std::sync::Arc) { })) .map( |query: HashMap, configuration: std::sync::Arc| { - let maybe_start = query_helpers::try_parse_query_month_year(query.get("start")); + let maybe_start = + shared_api_lib::query_helpers::try_parse_query_month_year(query.get("start")); if maybe_start.is_none() { return Response::builder() .status(400) .body(String::from("Unsupported \"start\" param in query.")); } - let maybe_stop = query_helpers::try_parse_query_month_year(query.get("stop")); + let maybe_stop = + shared_api_lib::query_helpers::try_parse_query_month_year(query.get("stop")); if maybe_stop.is_none() { return Response::builder() .status(400) @@ -239,7 +240,11 @@ struct DaysResponseItem { zever_total_watts: i32, } -fn get_months_solar_json(start: &YearMonth, stop: &YearMonth, database_path: &str) -> String { +fn get_months_solar_json( + start: &shared_api_lib::year_month::YearMonth, + stop: &shared_api_lib::year_month::YearMonth, + database_path: &str, +) -> String { let summaries = database::get_month_solar_summaries(&start, &stop, &database_path); let mut response_model = MonthsResponse { month_logs: Vec::new(),