Implement JSON serialisation for DynamicReport

This commit is contained in:
RunasSudo 2025-05-25 01:23:35 +10:00
parent 33252739d9
commit 3cfcdf6778
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
4 changed files with 21 additions and 5 deletions

2
Cargo.lock generated
View File

@ -682,6 +682,8 @@ dependencies = [
"dyn-eq",
"dyn-hash",
"indexmap",
"serde",
"serde_json",
"sqlx",
"tokio",
]

View File

@ -10,5 +10,7 @@ dyn-clone = "1.0.19"
dyn-eq = "0.1.3"
dyn-hash = "0.2.2"
indexmap = "2.9.0"
serde = "1.0.219"
serde_json = "1.0.140"
sqlx = { version = "0.8", features = [ "runtime-tokio", "sqlite" ] }
tokio = { version = "1.45.0", features = ["full"] }

View File

@ -20,6 +20,7 @@ use chrono::NaiveDate;
use libdrcr::db::DbConnection;
use libdrcr::reporting::builders::register_dynamic_builders;
use libdrcr::reporting::calculator::{steps_as_graphviz, steps_for_targets};
use libdrcr::reporting::dynamic_report::DynamicReport;
use libdrcr::reporting::generate_report;
use libdrcr::reporting::steps::register_lookup_fns;
use libdrcr::reporting::types::{
@ -130,5 +131,8 @@ fn main() {
.unwrap();
println!("Balance sheet:");
println!("{:?}", result);
println!(
"{}",
result.downcast_ref::<DynamicReport>().unwrap().to_json()
);
}

View File

@ -18,12 +18,14 @@
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use crate::QuantityInt;
use super::types::{GenericReportingProduct, ReportingProduct};
/// Represents a dynamically generated report composed of [DynamicReportEntry]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct DynamicReport {
pub title: String,
pub columns: Vec<String>,
@ -109,20 +111,26 @@ impl DynamicReport {
panic!("Called subtotal_for_id on non-Section");
}
}
/// Serialise the report (as JSON) using serde
pub fn to_json(&self) -> String {
serde_json::to_string(self).unwrap()
}
}
impl GenericReportingProduct for DynamicReport {}
impl ReportingProduct for DynamicReport {}
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum DynamicReportEntry {
Section(Section),
LiteralRow(LiteralRow),
#[serde(skip)]
CalculatedRow(CalculatedRow),
Spacer,
}
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Section {
pub text: String,
pub id: Option<String>,
@ -230,7 +238,7 @@ impl Section {
}
}
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct LiteralRow {
pub text: String,
pub quantity: Vec<QuantityInt>,