lukaslueg / railroad

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

Use railroad in C# #16

Closed mercurial-moon closed 3 weeks ago

mercurial-moon commented 2 months ago

Hi I'd like to use railroad in a c# .net application. I plan to use https://docs.rs/rnet/latest/rnet/ for getting that done.

As per rnet docs, I need to

Could you kindly let me know regarding the first 3 steps, which struct do I need to put #[derive(Net)] tag on and do you expose any standalone functions so i can put the #[net] attribute on them?

lukaslueg commented 2 months ago

I'm not familiar with rnet per se. Give the documentation you pasted, you're supposed to create your own stub-crate (to be compiled as a cdylib, which railroad by itself is not), and add FFI-functions to that crate. As far as railroad's types are concerned, you'd have to newtype those types in order to be able to #derive[] the necessary glue code, e.g. via

#[derive(Net)]
struct NetSequence(railroad::Sequence);

#[derive(Net)]
struct NetChoice(railroad::Choice);

// ...

/// Forward to the `ToString`-implementation without exposing it
#[net]
fn to_svg(input: ...) -> String {
    input.to_string()
}
mercurial-moon commented 2 months ago

Hi thanks for the detailed explanation, I was researching a bit more on this topic and it seems that generics (which your library makes heavy use of) doesn't play well with cross-language interop due to some limitations.

Can i just change all 'N' into string do you think that would work out?

lukaslueg commented 2 months ago

all 'N' into string

I don't understand what that means.

Yes, you probably need some glue code in order to un-generify the types. If you're looking to just render stuff (as opposed to adding new primitives), you may be able to get away by generating input for railroad_dsl and use that instead: All you need is nine types in C#-world to represent railroad_dsl's primitives, generate an input-string from those, and let railroad_dsl render the output.

mercurial-moon commented 3 weeks ago

Hi, thanks, I ended up using railroad_dsl via command line from C#.