From 06aad711b610fa9ce039f0ccb73261a97e3ed58c Mon Sep 17 00:00:00 2001 From: elvis Date: Sat, 26 Jul 2025 20:16:32 +0200 Subject: [PATCH] Refactoring edge formatting --- src/rsprocess/graph.rs | 120 +++++++++++++++++++++++++++------------ src/rsprocess/presets.rs | 42 +++++++------- 2 files changed, 102 insertions(+), 60 deletions(-) diff --git a/src/rsprocess/graph.rs b/src/rsprocess/graph.rs index d0836f4..cd81bec 100644 --- a/src/rsprocess/graph.rs +++ b/src/rsprocess/graph.rs @@ -88,8 +88,8 @@ impl From<([GraphMapNodes; N], Rc)> for } } -impl From<(&[GraphMapNodes; N], Rc)> for GraphMapNodesTy { - fn from(value: (&[GraphMapNodes; N], Rc)) -> Self { +impl From<(&[GraphMapNodes], Rc)> for GraphMapNodesTy { + fn from(value: (&[GraphMapNodes], Rc)) -> Self { Self::from((value.0.to_vec(), value.1)) } } @@ -143,10 +143,9 @@ impl From<(Vec, Rc)> for GraphMapNodesTy impl GraphMapNodesTy { pub fn generate<'a>( self - ) -> Box String + 'a> + ) -> Box { - let mut accumulator: - Box String + 'a> = + let mut accumulator: Box = super::format_helpers::graph_map_nodes_ty_from::format_hide( Rc::clone(&self.translator) ); @@ -189,79 +188,126 @@ type GraphMapEdgesFnTy = dyn Fn(petgraph::prelude::EdgeIndex, &RSlabel) -> Strin /// Helper structure that holds a formatting function from node as RSsystem to /// string pub struct GraphMapEdgesTy { - function: Vec>, + functions: Vec>, translator: Rc } -impl GraphMapEdgesTy { - pub fn from( - f: GraphMapEdges, - translator: Rc - ) -> Self { +impl From<([GraphMapEdges; N], Rc)> for GraphMapEdgesTy { + fn from(value: ([GraphMapEdges; N], Rc)) -> Self { + Self::from((value.0.to_vec(), value.1)) + } +} + +impl From<(&[GraphMapEdges], Rc)> for GraphMapEdgesTy { + fn from(value: (&[GraphMapEdges], Rc)) -> Self { + Self::from((value.0.to_vec(), value.1)) + } +} + +impl From<(Vec, Rc)> for GraphMapEdgesTy { + fn from(value: (Vec, Rc)) -> Self { use GraphMapEdges::*; use super::format_helpers::graph_map_edges_ty_from::*; - let function: Box = - // rust cant unify closures (they all have different types) so box needs - // to happen inside the match - // we use move because translator is from the env, so we transfer the - // borrow to the struct, also translator needs to be in box, a reference - // is not enough + let mut new = GraphMapEdgesTy {functions: vec![], translator: value.1}; + + for f in value.0 { match f { String { string } => { - format_string(translator, string) + new.functions.push(format_string( + Rc::clone(&new.translator), string)) } Hide => { - format_hide(translator) + new.functions.push(format_hide( + Rc::clone(&new.translator) + )) }, Products => { - format_products(translator) + new.functions.push(format_products( + Rc::clone(&new.translator) + )) }, MaskProducts { mask } => { - format_mask_products(translator, mask) + new.functions.push(format_mask_products( + Rc::clone(&new.translator), mask)) }, Entities => { - format_entities(translator) + new.functions.push(format_entities( + Rc::clone(&new.translator) + )) }, MaskEntities { mask } => { - format_mask_entities(translator, mask) + new.functions.push(format_mask_entities( + Rc::clone(&new.translator), mask)) }, Context => { - format_context(translator) + new.functions.push(format_context( + Rc::clone(&new.translator) + )) }, MaskContext { mask } => { - format_mask_context(translator, mask) + new.functions.push(format_mask_context( + Rc::clone(&new.translator), mask)) }, Union => { - format_union(translator) + new.functions.push(format_union( + Rc::clone(&new.translator) + )) }, MaskUnion { mask } => { - format_mask_union(translator, mask) + new.functions.push(format_mask_union( + Rc::clone(&new.translator), mask)) }, Difference => { - format_difference(translator) + new.functions.push(format_difference( + Rc::clone(&new.translator) + )) }, MaskDifference { mask } => { - format_mask_difference(translator, mask) + new.functions.push(format_mask_difference( + Rc::clone(&new.translator), mask)) }, EntitiesDeleted => { - format_entities_deleted(translator) + new.functions.push(format_entities_deleted( + Rc::clone(&new.translator) + )) }, MaskEntitiesDeleted { mask } => { - format_mask_entities_deleted(translator, mask) + new.functions.push(format_mask_entities_deleted( + Rc::clone(&new.translator), mask)) }, EntitiesAdded => { - format_entities_added(translator) + new.functions.push(format_entities_added( + Rc::clone(&new.translator) + )) }, MaskEntitiesAdded { mask } => { - format_mask_entities_added(translator, mask) + new.functions.push(format_mask_entities_added( + Rc::clone(&new.translator), mask)) }, }; - GraphMapEdgesTy { function } - } + } - pub fn get(&self) -> &GraphMapEdgesFnTy { - &self.function + new + } +} + +impl GraphMapEdgesTy { + pub fn generate( + self + ) -> Box { + let mut accumulator: Box = + super::format_helpers::graph_map_edges_ty_from::format_hide( + Rc::clone(&self.translator) + ); + for f in self.functions { + accumulator = Box::new(move |i, n| { + (accumulator)(i, n) + + &f(i, n) + }) + } + + accumulator } } diff --git a/src/rsprocess/presets.rs b/src/rsprocess/presets.rs index 3427d5c..80b2887 100644 --- a/src/rsprocess/presets.rs +++ b/src/rsprocess/presets.rs @@ -603,35 +603,31 @@ fn generate_node_printing_fn<'a>( #[allow(clippy::type_complexity)] fn generate_edge_printing_fn<'a>( - edge_display: &'a Vec, + edge_display: &[EdgeDisplay], translator: Rc, ) -> Box String + 'a> { // The type cannot be aliased since rust doesnt like generics. // We are iterating over the edge_display and constructing a function // (accumulator) that prints out our formatted nodes. So at each step we // call the previous function and add the next string or function. - let mut accumulator: - Box String> = - Box::new(|_, _| String::new()); - for nd in edge_display { - accumulator = match nd { - EdgeDisplay::Display(d) => { - // retrieve from the graph module the correct formatting - // function - let val = translator.clone(); - Box::new(move |i, n| { - (accumulator)(i, n) - + &graph::GraphMapEdgesTy::from(d.clone(), - val.clone()).get()(i, n) - }) - } - EdgeDisplay::Separator(s) => { - // we have a string so simply add it at the end - Box::new(move |i, n| (accumulator)(i, n) + s) - } - }; - } - accumulator + let edge_display = edge_display.iter().map( + |e| { + match e { + EdgeDisplay::Display(d) => { + d.clone() + }, + EdgeDisplay::Separator(s) => { + graph::GraphMapEdges::String { string: s.clone() } + } + } + } + ).collect::>(); + + let gmet = graph::GraphMapEdgesTy::from( + (edge_display, Rc::clone(&translator)) + ); + + gmet.generate() } use petgraph::visit::IntoNodeReferences;