Refactoring with better struct the node printing function

This commit is contained in:
elvis
2025-07-26 19:43:20 +02:00
parent 850d53acf8
commit cf071e4c76
2 changed files with 77 additions and 77 deletions

View File

@ -65,6 +65,7 @@ pub fn common_entities(
/// Helper structure that specifies what information to display for nodes. /// Helper structure that specifies what information to display for nodes.
#[derive(Clone)] #[derive(Clone)]
pub enum GraphMapNodes { pub enum GraphMapNodes {
String { string: String },
Hide, Hide,
Entities, Entities,
MaskEntities { mask: RSset }, MaskEntities { mask: RSset },
@ -77,54 +78,78 @@ type GraphMapNodesFnTy =
/// Helper structure that holds a formatting function from node as RSsystem to /// Helper structure that holds a formatting function from node as RSsystem to
/// string /// string
pub struct GraphMapNodesTy { pub struct GraphMapNodesTy {
function: Box<GraphMapNodesFnTy> functions: Vec<Box<GraphMapNodesFnTy>>,
translator: Rc<translator::Translator>
} }
impl GraphMapNodesTy { impl<const N: usize> From<([GraphMapNodes; N], Rc<translator::Translator>)> for GraphMapNodesTy {
pub fn from( fn from(value: ([GraphMapNodes; N], Rc<translator::Translator>)) -> Self {
f: GraphMapNodes, Self::from((value.0.to_vec(), value.1))
translator: Rc<translator::Translator> }
) -> Self { }
impl<const N: usize> From<(&[GraphMapNodes; N], Rc<translator::Translator>)> for GraphMapNodesTy {
fn from(value: (&[GraphMapNodes; N], Rc<translator::Translator>)) -> Self {
Self::from((value.0.to_vec(), value.1))
}
}
impl From<(Vec<GraphMapNodes>, Rc<translator::Translator>)> for GraphMapNodesTy {
fn from(value: (Vec<GraphMapNodes>, Rc<translator::Translator>)) -> Self {
use GraphMapNodes::*; use GraphMapNodes::*;
use super::format_helpers::graph_map_nodes_ty_from::*; use super::format_helpers::graph_map_nodes_ty_from::*;
let function: Box<GraphMapNodesFnTy> = let mut new = GraphMapNodesTy {functions: vec![], translator: value.1};
// rust cant unify closures (they all have different types) so box needs
// to happen inside the match for f in value.0 {
// 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
match f { match f {
String { string } => {
new.functions.push(format_string(string.clone()));
}
Hide => { Hide => {
format_hide(translator) new.functions.push(format_hide(
Rc::clone(&new.translator)
));
}, },
Entities => { Entities => {
format_entities(translator) new.functions.push(format_entities(
Rc::clone(&new.translator)
));
}, },
MaskEntities { mask } => { MaskEntities { mask } => {
format_mask_entities(translator, mask) new.functions.push(format_mask_entities(
Rc::clone(&new.translator),
mask.clone()
));
}, },
ExcludeEntities { mask } => { ExcludeEntities { mask } => {
format_exclude_entities(translator, mask) new.functions.push(format_exclude_entities(
Rc::clone(&new.translator),
mask.clone()
));
} }
Context => { Context => {
format_context(translator) new.functions.push(format_context(
Rc::clone(&new.translator)
));
}, },
}; };
GraphMapNodesTy { function } }
}
pub fn get(&self) -> &GraphMapNodesFnTy { new
&self.function
} }
} }
impl From<GraphMapNodesTy> for Box<GraphMapNodesFnTy> { impl GraphMapNodesTy {
fn from(g: GraphMapNodesTy) -> Self { pub fn generate<'a>(
g.function &self
) -> Box<dyn Fn(petgraph::prelude::NodeIndex, &'a RSsystem) -> String + 'a>
{
todo!()
} }
} }
// Edges ----------------------------------------------------------------------- // Edges -----------------------------------------------------------------------
/// Helper structure that specifies what information to display for edges /// Helper structure that specifies what information to display for edges

View File

@ -164,7 +164,7 @@ pub struct Instructions {
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Helper Functions // IO Helper Functions
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
fn read_file<T, F>( fn read_file<T, F>(
@ -562,7 +562,7 @@ pub fn bisimilar(
#[allow(clippy::type_complexity)] #[allow(clippy::type_complexity)]
fn generate_node_pringting_fn<'a>( fn generate_node_pringting_fn<'a>(
node_display: &'a Vec<NodeDisplay>, node_display: &[NodeDisplay],
graph: &graph::RSgraph, graph: &graph::RSgraph,
translator: Rc<Translator>, translator: Rc<Translator>,
) -> Box<dyn Fn(petgraph::prelude::NodeIndex, &'a RSsystem) -> String + 'a> { ) -> Box<dyn Fn(petgraph::prelude::NodeIndex, &'a RSsystem) -> String + 'a> {
@ -570,60 +570,33 @@ fn generate_node_pringting_fn<'a>(
// We are iterating over the node_display and constructing a function // We are iterating over the node_display and constructing a function
// (accumulator) that prints out our formatted nodes. So at each step we // (accumulator) that prints out our formatted nodes. So at each step we
// call the previous function and add the next string or function. // call the previous function and add the next string or function.
let mut accumulator: let node_display = node_display.iter().map(
Box<dyn Fn(petgraph::prelude::NodeIndex, &RSsystem) -> String> = |e|
Box::new(|_, _| String::new()); match e {
for nd in node_display { NodeDisplay::Display(d) => d.clone(),
accumulator = match nd { NodeDisplay::Separator(s) => {
NodeDisplay::Display(d) => { graph::GraphMapNodes::String {
// retrieve from the graph module the correct formatting string: s.clone()
// 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
NodeDisplay::UncommonEntities => { NodeDisplay::UncommonEntities => {
let common_entities = graph::common_entities(graph); let common_entities = graph::common_entities(graph);
let val = translator.clone(); graph::GraphMapNodes::ExcludeEntities {
Box::new(move |i, n| { mask: common_entities.clone()
(accumulator)(i, n) }
+ &graph::GraphMapNodesTy::from(
graph::GraphMapNodes::ExcludeEntities {
mask: common_entities.clone()
},
val.clone()
).get()(i, n)
}
)
}, },
// ad hoc since graph information is not available in the graph
// function generation
NodeDisplay::MaskUncommonEntities(mask) => { NodeDisplay::MaskUncommonEntities(mask) => {
let common_entities = graph::common_entities(graph); let common_entities = graph::common_entities(graph);
let val = translator.clone(); graph::GraphMapNodes::ExcludeEntities {
Box::new(move |i, n| { mask: common_entities.union(mask)
(accumulator)(i, n) }
+ &graph::GraphMapNodesTy::from( }
graph::GraphMapNodes::ExcludeEntities { }
mask: common_entities.union(mask) ).collect::<Vec<_>>();
},
val.clone() let gmnt = graph::GraphMapNodesTy::from((node_display, Rc::clone(&translator)));
).get()(i, n)
} gmnt.generate()
)
},
};
}
accumulator
} }
#[allow(clippy::type_complexity)] #[allow(clippy::type_complexity)]
@ -688,14 +661,14 @@ fn generate_node_color_fn<'a>(
) )
} }
use petgraph::visit::IntoEdgeReferences;
#[allow(clippy::type_complexity)] #[allow(clippy::type_complexity)]
fn generate_edge_color_fn<'a>( fn generate_edge_color_fn<'a>(
edge_color: &'a graph::EdgeColor, edge_color: &'a graph::EdgeColor,
original_graph: Rc<Graph<RSsystem, RSlabel>>, original_graph: Rc<Graph<RSsystem, RSlabel>>,
) -> Box<dyn Fn( ) -> Box<dyn Fn(
&Graph<String, String>, &Graph<String, String>,
<&Graph<String, String, petgraph::Directed, u32> as IntoEdgeReferences>::EdgeRef <&Graph<String, String, petgraph::Directed, u32>
as petgraph::visit::IntoEdgeReferences>::EdgeRef
) -> String + 'a> ) -> String + 'a>
{ {
Box::new( Box::new(
@ -929,6 +902,8 @@ fn execute(
Ok(()) Ok(())
} }
/// Interprets file at supplied path, then executes the code specified as
/// instructions inside the file.
pub fn run(path: String) -> Result<(), String> { pub fn run(path: String) -> Result<(), String> {
let mut translator = Translator::new(); let mut translator = Translator::new();