1

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?
0

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.