-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(config): Create a config #6
Comments
i made a similar workflow for my game-rs recently i can make a quick pr, what will be the options in the config? |
That would be wonderful, can you please wait for me to add the config first? Note: - toml
+ yaml since it is more user friendly |
yess, I'll be waiting |
I have already written most of the boilerplate code either way, so its ok |
One more request that I have is that before applying any change to the config, show a diff and prompt the user if they want to proceed. |
nicee that's a great idea, I'll get right on it |
Hey, did you have something specific in mind to iterate over the struct fields? i can think of 2 ways it can go:
do you have any preference on which way to go or any other way you can think of? |
I'll try and make a small prototype for that |
yeah bevy_reflect looks like the way to go, or writing a trait and implementing it for every single structure in the config actually, the latter sounds more safe and easy, ig ill take the L |
yes, the latter is immensely more easy as per my limited experience with bevy_reflect another way i can think is, since we can get an index and field name with serde safely, we can make a selection to enum mapper and make a function which takes a value and enum and just updates with match trait will be prettier way to code doe, I'll give it a shot next |
Ayo check this out macros can change dynamically, when we add anything to config we just have to add a macro line, it can even handle different prompts for different types |
Took me some time to learn procedural macros, btw what you linked is a declarative macro Here I made an example of how this could work module
|
Here is the generated code from the above message: pub fn main() {
struct Waffles {
hello: i16,
}
enum Waffles_field {
hello(i16),
}
impl Waffles {
fn get(&self, field: impl ToString) -> anyhow::Result<Waffles_field> {
match field.to_string().as_str() {
"hello" => Ok(Waffles_field::hello(self.hello)),
_ => Err(anyhow::anyhow!("Unknown field.")),
}
}
}
let a = Waffles { hello: 42 };
match a.get("hello").unwrap() {
Waffles_field::hello(value) => println!("{}", value),
_ => ()
};
} PS: I used |
wow, took me some time to understand but this simplifies the whole process, rust macros are awesome lol |
kinda under estimated rust borrow checker, String doesn't implement Copy strikes again, it may take some more time than i expected |
.clone() |
i misunderstood what the error meant, working now, partially added the update method too |
Update function working |
sorry i was in hospital for a few days i was looking into ways of doing it with serde and found this use-serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Serialize, Deserialize)]
struct ExampleStruct {
field_a: i32,
field_b: String,
}
fn main() {
let mut my_struct = ExampleStruct {
field_a: 42,
field_b: String::from("hello"),
};
let mut field_updates = HashMap::new();
field_updates.insert("field_a", 99);
field_updates.insert("field_b", String::from("world"));
for (field_name, field_value) in field_updates {
let field_name = field_name.to_string();
let field_value = serde_json::to_value(&field_value).unwrap();
let mut fields = my_struct.serialize().unwrap();
fields.remove(&field_name);
fields.insert(field_name, field_value);
my_struct = ExampleStruct::deserialize(fields).unwrap();
}
println!("Updated struct: {:?}", my_struct);
} bevy_reflect seems a viable option too |
About
A config is really important to the user, because, with it, a user can modify the behavior of this program to suit his/her needs.
Tasks:
The text was updated successfully, but these errors were encountered: