How to change source code values automatically?

Let's say I have a struct

struct Foo {
  color: String
}

impl Default for Foo {
  fun default() -> Self {
    Foo { color: "red".to_string(), }
  }
}

Now, if the file ~/.configs/Foo/settings.conf exists, and it has a field color: <color_name> I want my src code to change the default value of a color so next time I don't have to do I/O on the settings file again?

What options do I have? (lower latency option is preferred) An optimal way to read settings from the file, set them up once without a need to do IO, and set default values every time the program is executed. Do I write some meta program that updates the source code?

thanks

Do you mean, users of the program have a config file that determines the default? If so, there's no way to avoid all IO. There are crates for configuration or for just deserialization. Then to avoid IO on subsequent calls to default, use once_cell (which will eventually be part of std) or similar.

Or do you mean, compilers of the program have a config file that determines the default (which will be the same for every execution of the compiled program)? At a low level there's include_str; at a higher level, you could combine a config file approach with a build script.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.