lukaslueg / railroad

A library to produce syntax diagrams as Scalable Vector Graphics
MIT License
138 stars 6 forks source link

Make more node attributes public or add getter methods #15

Open Danie-1 opened 2 months ago

Danie-1 commented 2 months ago

I would like to use this library for rendering railroad diagrams for regular expressions. I would like to be able to use the current types to represent a railroad diagram, but I don't necessarily always want to use them to generate an svg image. I would like to also have the option to render the nodes as a text diagram, for example:

   Group 1     ANY  
   ╔══════╗    OF   
─┬─╫─abcd─╫─┬─┬a-z┬─
 │ ╚══════╝ │ ╰A-Z╯ 
 ╰───<  <───╯       

In order to do this, I was thinking of making my own trait (similar to your Node trait) which would be used for rendering as text. However, I can't implement the trait on your types, because attributes like "text" on Comment are private. I could get around this issue with some work, but I think it would be much easier if I could access the attributes.

Would you be happy to either have attributes (like text) made public, or have some getter methods like

impl Comment {
    pub fn text(&self) -> &str {
        &self.text
    }
}

?

I would be happy to make a PR for this change, if you are happy with one of the above mentioned solutions.

lukaslueg commented 2 months ago

Given that this lib is rather minimal and basically has no logic beyond computing the geometry of the graphical representation, supporting text-output would mean re-implementing all of the logic outside this library. This would be fine, but this lib would neither provide anything beyond opaque type definitions, and consumers would be unable to use any of the generic functionality (e.g. switching backends). If you look at the implementation of the various primitives (like Comment, or Choice), you'll most likely find that you'd have to implement 99% of the functionality yourself anyway. So I consider this to be a non-goal of this library.