Skip to content

Commit

Permalink
Add DrawingPortId newtype and PortKey safety checks
Browse files Browse the repository at this point in the history
  • Loading branch information
arnodb committed Nov 15, 2024
1 parent aad9e21 commit def75e3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 30 deletions.
20 changes: 11 additions & 9 deletions quirky_binder/src/drawing/graph/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::{btree_map::Entry, BTreeMap};
use crate::{
drawing::{
Drawing, DrawingColumn, DrawingEdge, DrawingNode, DrawingPort, DrawingPortAlign,
DrawingPortSize, DrawingPortsColumn,
DrawingPortId, DrawingPortSize, DrawingPortsColumn,
},
prelude::*,
};
Expand All @@ -26,7 +26,7 @@ const COLORS: [&str; 20] = [
pub struct DrawingHelper<'a, PortKey: Ord, ColorKey: Ord> {
pub columns: Vec<DrawingColumn<'a>>,
pub node_column_and_index: BTreeMap<&'a [Box<str>], (usize, usize)>,
pub output_ports: BTreeMap<PortKey, usize>,
pub output_ports: BTreeMap<PortKey, DrawingPortId>,
pub edges: Vec<DrawingEdge<'a>>,
pub edges_color: BTreeMap<ColorKey, &'static str>,
pub next_color: usize,
Expand Down Expand Up @@ -143,7 +143,7 @@ impl<'a, PortKey: Ord, ColorKey: Ord> DrawingHelper<'a, PortKey, ColorKey> {
from: &PortKey,
from_color_key: ColorKey,
) {
let input_port_id = *port_count;
let input_port_id = DrawingPortId::from(*port_count);
*port_count += 1;

let mut index = port_columns.len();
Expand Down Expand Up @@ -186,7 +186,7 @@ impl<'a, PortKey: Ord, ColorKey: Ord> DrawingHelper<'a, PortKey, ColorKey> {
port_count: &mut usize,
to: PortKey,
) {
let output_port_id = *port_count;
let output_port_id = DrawingPortId::from(*port_count);
*port_count += 1;

let mut index = port_columns.len();
Expand Down Expand Up @@ -217,7 +217,8 @@ impl<'a, PortKey: Ord, ColorKey: Ord> DrawingHelper<'a, PortKey, ColorKey> {
});
}

self.output_ports.insert(to, output_port_id);
let old = self.output_ports.insert(to, output_port_id);
assert!(old.is_none());
}

pub fn push_connected_ports(
Expand All @@ -228,8 +229,8 @@ impl<'a, PortKey: Ord, ColorKey: Ord> DrawingHelper<'a, PortKey, ColorKey> {
from_color_key: ColorKey,
to: PortKey,
) {
let input_port_id = *port_count;
let output_port_id = *port_count + 1;
let input_port_id = DrawingPortId::from(*port_count);
let output_port_id = DrawingPortId::from(*port_count + 1);
*port_count += 2;

port_columns.push(DrawingPortsColumn {
Expand All @@ -250,10 +251,11 @@ impl<'a, PortKey: Ord, ColorKey: Ord> DrawingHelper<'a, PortKey, ColorKey> {

self.push_edge(from, input_port_id, from_color_key);

self.output_ports.insert(to, output_port_id);
let old = self.output_ports.insert(to, output_port_id);
assert!(old.is_none());
}

pub fn push_edge(&mut self, tail_key: &PortKey, head: usize, color_key: ColorKey) {
pub fn push_edge(&mut self, tail_key: &PortKey, head: DrawingPortId, color_key: ColorKey) {
let color = self.get_color_for(color_key);
self.edges.push(DrawingEdge {
tail: self.output_ports[tail_key],
Expand Down
11 changes: 6 additions & 5 deletions quirky_binder/src/drawing/graph/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use super::{
};
use crate::{
drawing::{
Drawing, DrawingPort, DrawingPortAlign, DrawingPortSize, DrawingPortsColumn, DynNode,
Drawing, DrawingPort, DrawingPortAlign, DrawingPortId, DrawingPortSize, DrawingPortsColumn,
DynNode,
},
graph::visit::{visit_node, Visit},
prelude::*,
Expand All @@ -33,7 +34,7 @@ impl<'a> RecordsDrawingHelper<'a> {
let variant = &record_definition[stream.variant_id()];

for d in variant.data() {
let input_port_id = *port_count;
let input_port_id = DrawingPortId::from(*port_count);
*port_count += 1;
port_columns.push(DrawingPortsColumn {
ports: vec![DrawingPort {
Expand Down Expand Up @@ -78,7 +79,7 @@ impl<'a> RecordsDrawingHelper<'a> {
let variant = &record_definition[stream.variant_id()];

for d in variant.data() {
let output_port_id = *port_count;
let output_port_id = DrawingPortId::from(*port_count);
*port_count += 1;
port_columns.push(DrawingPortsColumn {
ports: vec![DrawingPort {
Expand Down Expand Up @@ -120,8 +121,8 @@ impl<'a> RecordsDrawingHelper<'a> {
let variant = &record_definition[stream.variant_id()];

for d in variant.data() {
let input_port_id = *port_count;
let output_port_id = *port_count + 1;
let input_port_id = DrawingPortId::from(*port_count);
let output_port_id = DrawingPortId::from(*port_count + 1);
*port_count += 2;
port_columns.push(DrawingPortsColumn {
ports: vec![
Expand Down
12 changes: 7 additions & 5 deletions quirky_binder/src/drawing/graph/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use truc::record::definition::{DatumId, RecordVariantId};

use super::helper::{drawing_source_node_name, DrawingHelper};
use crate::{
drawing::{Drawing, DrawingPort, DrawingPortAlign, DrawingPortSize, DrawingPortsColumn},
drawing::{
Drawing, DrawingPort, DrawingPortAlign, DrawingPortId, DrawingPortSize, DrawingPortsColumn,
},
graph::visit::{visit_node, Visit},
prelude::*,
};
Expand All @@ -23,7 +25,7 @@ impl<'a> StreamsDrawingHelper<'a> {
stream: &'a NodeSubStream,
depth: usize,
) {
let input_port_id = *port_count;
let input_port_id = DrawingPortId::from(*port_count);
*port_count += 1;
port_columns.push(DrawingPortsColumn {
ports: vec![DrawingPort {
Expand Down Expand Up @@ -68,7 +70,7 @@ impl<'a> StreamsDrawingHelper<'a> {
stream: &'a NodeSubStream,
depth: usize,
) {
let output_port_id = *port_count;
let output_port_id = DrawingPortId::from(*port_count);
*port_count += 1;
port_columns.push(DrawingPortsColumn {
ports: vec![DrawingPort {
Expand Down Expand Up @@ -112,8 +114,8 @@ impl<'a> StreamsDrawingHelper<'a> {
stream: &'a NodeSubStream,
depth: usize,
) {
let input_port_id = *port_count;
let output_port_id = *port_count + 1;
let input_port_id = DrawingPortId::from(*port_count);
let output_port_id = DrawingPortId::from(*port_count + 1);
*port_count += 2;
port_columns.push(DrawingPortsColumn {
ports: vec![
Expand Down
25 changes: 14 additions & 11 deletions quirky_binder/src/drawing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ pub struct DrawingPortsColumn {
pub align: DrawingPortAlign,
}

#[derive(Debug, Clone, Copy, From, Deref)]
pub struct DrawingPortId(usize);

#[derive(Debug)]
pub struct DrawingPort {
pub id: usize,
pub id: DrawingPortId,
pub size: DrawingPortSize,
pub redundant: bool,
}
Expand All @@ -51,8 +54,8 @@ pub enum DrawingPortAlign {

#[derive(Debug)]
pub struct DrawingEdge<'a> {
pub tail: usize,
pub head: usize,
pub tail: DrawingPortId,
pub head: DrawingPortId,
pub color: &'a str,
}

Expand Down Expand Up @@ -286,12 +289,12 @@ where
}
};
let mut go_down_next = false;
for DrawingPort {
id,
size,
redundant,
} in ports
{
for port in ports {
let DrawingPort {
id,
size,
redundant,
} = *port;
if !redundant {
if go_down_next {
top += PORT_Y_DISTANCE;
Expand All @@ -308,8 +311,8 @@ where
+ col * (LABEL_MAX_WIDTH + BLANK_WIDTH)
+ (ports_count + port_col) * PORT_X_DISTANCE,
cy: top,
size: *size,
redundant: *redundant,
size,
redundant,
},
);
}
Expand Down

0 comments on commit def75e3

Please sign in to comment.