diff --git a/Cargo.lock b/Cargo.lock index 63cd6d0..4201df1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -60,6 +60,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "either" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" + [[package]] name = "getrandom" version = "0.2.10" @@ -71,6 +77,15 @@ dependencies = [ "wasi", ] +[[package]] +name = "itertools" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.9" @@ -149,6 +164,7 @@ version = "0.1.0" dependencies = [ "argh", "csv", + "itertools", "rand", "rand_chacha", "serde", diff --git a/Cargo.toml b/Cargo.toml index 284e949..422830d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ edition = "2021" [dependencies] argh = "0.1.12" csv = "1.2.2" +itertools = "0.13.0" rand = "0.8.5" rand_chacha = "0.3.1" serde = "1.0.188" diff --git a/src/io.rs b/src/io.rs index 0c22a96..631103f 100644 --- a/src/io.rs +++ b/src/io.rs @@ -1,20 +1,66 @@ -use super::repartition::*; +use std::fs; +use std::process::Command; -const FORMAT_TEXT: &'static str = " +use super::repartition::*; +use itertools::Itertools; + +impl Repartition { + pub fn to_json(&self) -> String { + let participants = self + .0 + .iter() + .flat_map(|t| vec![t.from.clone(), t.to.clone()]) + .dedup() + .collect_vec(); + let mut str_nodes = String::new(); + for p in participants { + str_nodes += &format!( + "{{ + \"data\": {{ + \"id\":\"{}\", + \"label\":\"{}\" + }}, + }},", + p.name, p.name + ); + } + let mut str_edges = String::new(); + for t in &self.0 { + str_edges += &format!( + "{{ + \"data\": {{ + \"id\":\"{}-{}\", + \"label\":\"{}€\", + \"source\":\"{}\", + \"target\":\"{}\", + }} + }},", + t.from.name, + t.to.name, + t.amount as f32 / 100., + t.from.name, + t.to.name, + ) + } + "[".to_owned() + &str_nodes + "," + &str_edges + "]" + } +} + +const FORMAT_STRING: &'static str = "
-

Balance totale (doit être égal à zéro) : {}€

+

Balance totale (doit être égal à zéro) : TOTAL_BALANCE €