0

I want to create a static instance at compile time (not runtime) from JSON data loaded from a file (similar to constexpr):

Example:

// a file in the project /path/to/project/data.json
{
  "field1" : "a"
}
// project code:

use serde_json::json;

struct MyStruct {
    field1: String
}


// this is what I have, created at runtime
let json = json!(include!("data.json"));
let instance = serde_json::from_value::<MyStruct>(json);

// what I want:
// compile checked and created "statically"
let compiled_instance : MyStruct = static_instance_from_file!("data.json"); // how can this be verified by the compiler?
user2722968
  • 13,636
  • 2
  • 46
  • 67
Avba
  • 14,822
  • 20
  • 92
  • 192

1 Answers1

0

You could use build.rs to generate Rust source code in OUT_DIR and include! it into the project.

Kornel
  • 97,764
  • 37
  • 219
  • 309