Skip to main content

blockworx/shape/
pin.rs

1//! The pin vocabulary the geometry layer draws in: the document's pin
2//! entity and its value enums, plus the derived readings the drawing and
3//! hit-test paths take off a pin's registers.
4
5use blockworx_doc::geometry::PinSlot;
6
7pub use blockworx_doc::{
8    block_model::Pin,
9    values::{PinDir, PinSide},
10};
11
12/// The slot a pin occupies on its owner's boundary — the edge and the
13/// index, read as one atomic value.
14pub fn slot(pin: &Pin) -> PinSlot {
15    pin.slot
16}
17
18/// Which way a pin's *port body* faces when the pin is drawn as its own
19/// boundary port. `flip_lr` reads `false` as "opposite the edge the pin
20/// occupies", so the interior stub points inward, and `true` as "frozen to
21/// face that edge" — the legacy `port_orientation == Some(side)`. Use this,
22/// never [`slot`]'s side, for every port-view drawing, hit test, and route
23/// anchor.
24pub fn orientation(pin: &Pin) -> PinSide {
25    let slot = slot(pin);
26    if pin.flip_lr {
27        slot.side
28    } else {
29        slot.side.flip()
30    }
31}
32
33/// The opposite sense, used when a pin is rendered as a port. `InOut` has no
34/// sense, so it is unchanged.
35pub fn flipped_dir(dir: PinDir) -> PinDir {
36    match dir {
37        PinDir::Input => PinDir::Output,
38        PinDir::Output => PinDir::Input,
39        PinDir::InOut => PinDir::InOut,
40    }
41}