diff --git a/src/rsprocess/graph.rs b/src/rsprocess/graph.rs index 72168af..e2de6fc 100644 --- a/src/rsprocess/graph.rs +++ b/src/rsprocess/graph.rs @@ -65,6 +65,7 @@ pub fn common_entities( /// Helper structure that specifies what information to display for nodes. #[derive(Clone)] pub enum GraphMapNodes { + String { string: String }, Hide, Entities, MaskEntities { mask: RSset }, @@ -77,54 +78,78 @@ type GraphMapNodesFnTy = /// Helper structure that holds a formatting function from node as RSsystem to /// string pub struct GraphMapNodesTy { - function: Box + functions: Vec>, + translator: Rc } -impl GraphMapNodesTy { - pub fn from( - f: GraphMapNodes, - translator: Rc - ) -> Self { +impl From<([GraphMapNodes; N], Rc)> for GraphMapNodesTy { + fn from(value: ([GraphMapNodes; N], Rc)) -> Self { + Self::from((value.0.to_vec(), value.1)) + } +} + +impl From<(&[GraphMapNodes; N], Rc)> for GraphMapNodesTy { + fn from(value: (&[GraphMapNodes; N], Rc)) -> Self { + Self::from((value.0.to_vec(), value.1)) + } +} + +impl From<(Vec, Rc)> for GraphMapNodesTy { + fn from(value: (Vec, Rc)) -> Self { use GraphMapNodes::*; use super::format_helpers::graph_map_nodes_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 = GraphMapNodesTy {functions: vec![], translator: value.1}; + + for f in value.0 { match f { + String { string } => { + new.functions.push(format_string(string.clone())); + } Hide => { - format_hide(translator) + new.functions.push(format_hide( + Rc::clone(&new.translator) + )); }, 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.clone() + )); }, ExcludeEntities { mask } => { - format_exclude_entities(translator, mask) + new.functions.push(format_exclude_entities( + Rc::clone(&new.translator), + mask.clone() + )); } Context => { - format_context(translator) + new.functions.push(format_context( + Rc::clone(&new.translator) + )); }, }; - GraphMapNodesTy { function } - } + } - pub fn get(&self) -> &GraphMapNodesFnTy { - &self.function + new } } -impl From for Box { - fn from(g: GraphMapNodesTy) -> Self { - g.function +impl GraphMapNodesTy { + pub fn generate<'a>( + &self + ) -> Box String + 'a> + { + todo!() } } + // Edges ----------------------------------------------------------------------- /// Helper structure that specifies what information to display for edges diff --git a/src/rsprocess/presets.rs b/src/rsprocess/presets.rs index 06ab384..e24495c 100644 --- a/src/rsprocess/presets.rs +++ b/src/rsprocess/presets.rs @@ -164,7 +164,7 @@ pub struct Instructions { } // ----------------------------------------------------------------------------- -// Helper Functions +// IO Helper Functions // ----------------------------------------------------------------------------- fn read_file( @@ -562,7 +562,7 @@ pub fn bisimilar( #[allow(clippy::type_complexity)] fn generate_node_pringting_fn<'a>( - node_display: &'a Vec, + node_display: &[NodeDisplay], graph: &graph::RSgraph, translator: Rc, ) -> Box String + 'a> { @@ -570,60 +570,33 @@ fn generate_node_pringting_fn<'a>( // We are iterating over the node_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 node_display { - accumulator = match nd { - NodeDisplay::Display(d) => { - // retrieve from the graph module the correct formatting - // function - let val = translator.clone(); - Box::new(move |i, n| { - (accumulator)(i, n) - + &graph::GraphMapNodesTy::from(d.clone(), - val.clone()).get()(i, n) - }) - }, - NodeDisplay::Separator(s) => { - // we have a string so simply add it at the end - Box::new(move |i, n| (accumulator)(i, n) + s) - }, - // ad hoc since graph information is not available in the graph - // function generation + let node_display = node_display.iter().map( + |e| + match e { + NodeDisplay::Display(d) => d.clone(), + NodeDisplay::Separator(s) => { + graph::GraphMapNodes::String { + string: s.clone() + } + }, NodeDisplay::UncommonEntities => { let common_entities = graph::common_entities(graph); - let val = translator.clone(); - Box::new(move |i, n| { - (accumulator)(i, n) - + &graph::GraphMapNodesTy::from( - graph::GraphMapNodes::ExcludeEntities { - mask: common_entities.clone() - }, - val.clone() - ).get()(i, n) - } - ) + graph::GraphMapNodes::ExcludeEntities { + mask: common_entities.clone() + } }, - // ad hoc since graph information is not available in the graph - // function generation NodeDisplay::MaskUncommonEntities(mask) => { let common_entities = graph::common_entities(graph); - let val = translator.clone(); - Box::new(move |i, n| { - (accumulator)(i, n) - + &graph::GraphMapNodesTy::from( - graph::GraphMapNodes::ExcludeEntities { - mask: common_entities.union(mask) - }, - val.clone() - ).get()(i, n) - } - ) - }, - }; - } - accumulator + graph::GraphMapNodes::ExcludeEntities { + mask: common_entities.union(mask) + } + } + } + ).collect::>(); + + let gmnt = graph::GraphMapNodesTy::from((node_display, Rc::clone(&translator))); + + gmnt.generate() } #[allow(clippy::type_complexity)] @@ -688,14 +661,14 @@ fn generate_node_color_fn<'a>( ) } -use petgraph::visit::IntoEdgeReferences; #[allow(clippy::type_complexity)] fn generate_edge_color_fn<'a>( edge_color: &'a graph::EdgeColor, original_graph: Rc>, ) -> Box, - <&Graph as IntoEdgeReferences>::EdgeRef + <&Graph + as petgraph::visit::IntoEdgeReferences>::EdgeRef ) -> String + 'a> { Box::new( @@ -929,6 +902,8 @@ fn execute( Ok(()) } +/// Interprets file at supplied path, then executes the code specified as +/// instructions inside the file. pub fn run(path: String) -> Result<(), String> { let mut translator = Translator::new();