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.
#[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<GraphMapNodesFnTy>
functions: Vec<Box<GraphMapNodesFnTy>>,
translator: Rc<translator::Translator>
}
impl GraphMapNodesTy {
pub fn from(
f: GraphMapNodes,
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<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 super::format_helpers::graph_map_nodes_ty_from::*;
let function: Box<GraphMapNodesFnTy> =
// 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<GraphMapNodesTy> for Box<GraphMapNodesFnTy> {
fn from(g: GraphMapNodesTy) -> Self {
g.function
impl GraphMapNodesTy {
pub fn generate<'a>(
&self
) -> Box<dyn Fn(petgraph::prelude::NodeIndex, &'a RSsystem) -> String + 'a>
{
todo!()
}
}
// 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>(
@ -562,7 +562,7 @@ pub fn bisimilar(
#[allow(clippy::type_complexity)]
fn generate_node_pringting_fn<'a>(
node_display: &'a Vec<NodeDisplay>,
node_display: &[NodeDisplay],
graph: &graph::RSgraph,
translator: Rc<Translator>,
) -> 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
// (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<dyn Fn(petgraph::prelude::NodeIndex, &RSsystem) -> 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::<Vec<_>>();
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<Graph<RSsystem, RSlabel>>,
) -> Box<dyn Fn(
&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>
{
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();