Help with data structures with self-referential nested elements

⚓ Rust    📅 2026-05-12    👤 surdeus    👁️ 1      

surdeus

I am working on a 2D CAD program that will be used to display wiring diagrams.

I am trying to decide on a good data structure for wires and cables. (I am a completely self taught coder, and never took a Data Structures or Algorithms class).

Currently I have a WireType struct and a CableType struct (Library Types) that store common properties of each object, such as manufacturer, color, overall structure, etc and then a Wire struct and a Cable struct (Project Types). The Project Type structs are instances of the types represented by the Library Type structs within the program. Each Project Type data stores a string key that refers back to its associated Library Type.

All of this data currently gets read in from TOML files via Serde and stored in a master Library or Project struct.

I just switched to using separate structs to represent the data read in from the TOML files vs the data stored in memory, as there are way more data fields on the in-memory versions. (thanks to an answer on a different question.)

Where I am questioning things, is the nested nature of wires and cables. As an example, see the following image of a cable with 4 cables inside it, each with 3 wires.

In my program, i need to be able to reference each individual core of a cable, so I can link it to specific connection points on other entities.

I have a CableCore enum that can contain either a Wire or a Cable, and each cable can have any number of CableCores. This means that CableCores can be nested until recursion depth is reached...

One core of my question (no pun intended), is should I keep Wires separated out, or just treat them as Cables with 1 core within my internal data structures as I can have different data structures in memory than in the file.

The other core of my question, is how to handle nested cables/cores like the above when I need to have a user refer to a specific core or set of cores in a Project file to define connections between other entities. It would be much easier if I had the user define everything via a GUI and keep track of everything behind the scenes, but I want to have the users define everything in TOML files.

Essentially the workflow for the end user of the software is the following:

  1. Define WireTypes and CableTypes in Library files
  2. Define individual Wires and Cables in Project files
  3. Define the Equipment the wires and cables are connecting to in other Project files
  4. Define the Connections between Equipment using a specific Wire/Cable/Core of a Cable. The connection would include what Equipment is on each end, what ConnectionPoint is in use on each Equipment for each end, and then which Wire/Cable/Core of a Cable is connecting those two Equipment instances.

The last step is the piece I am struggling with the most, as I have no easy way to associate user visible IDs to nested cores within a cable, unless I essentially hard code a specific algorithm which is inflexible.

I have an implementation currently that "Works" TM, but the implementation doesn't feel like I am doing things the correct way, repeating myself a lot and backing myself into corners. It also relies on the users defining the core ids in a very specific way that is inflexible.

I apologize for the wall of text, and thank you to anyone in advance who has suggestions / feedback on this.

4 posts - 2 participants

Read full topic

🏷️ Rust_feed