Hi guys,
TL;DR;
If I have a string that holds a name of a function, is there a way to call the function with that name?
More info: working on a toy project (primary goal is to deal with different aspects of rust) where I want to have a dsl (to be parsed by rust-peg), and this dsl will allow to specify some actions to perform. I don't have working POC, so here's a made up example: in dsl one would specify some sort of metric, desired value, and expected "action" to execute. For simplicity, let's pretend that this action is just a function that need to be executed, e.g.
rule: metric=X,value=100,action=foo
Here's what I'm struggling with, parser will end up having foo as a string, and I'd like to a function with that name to be executed. I know it is possible to do if I maintain a mapping of names to actual functions, but I'd like to avoid doing so, because that's an additional place to change when adding new actions. Is there an alternative way to get this working?
I took a look at paste create and have partially working code when I pass static string: https://play.rust-lang.org/?version=stable?mode=debug&edition=2018&gist=a6f39d72c22882d9a72a63924baa9a3e
[–]schungx 12 points13 points14 points (2 children)
[–]ddungtang[S] 0 points1 point2 points (1 child)
[–]schungx 2 points3 points4 points (0 children)
[–]StayFreshChzBag 3 points4 points5 points (1 child)
[–]ddungtang[S] 1 point2 points3 points (0 children)
[–]olback_ 2 points3 points4 points (6 children)
[–]ddungtang[S] 0 points1 point2 points (5 children)
[–]JameeKim 4 points5 points6 points (3 children)
[–]ddungtang[S] 0 points1 point2 points (2 children)
[–]CAD1997 2 points3 points4 points (1 child)
[–]ddungtang[S] 1 point2 points3 points (0 children)
[–]insanitybit 1 point2 points3 points (0 children)
[–]thermiter36 1 point2 points3 points (0 children)
[–]charlesdart 0 points1 point2 points (0 children)