I'm creating a subroutine in rust which converts a custom datatype which represents an object's attributes and it's values into that object, but I have no idea how to do so...
I'm doing this because I want to create objects from the users input, my plan is to first take the input and parse it into the custom data type and then turn that data into the object.
My problem is that, when iterating through the custom data type, I get to the string that represents the attribute and I don't know how to check if that string is a real attribute or how to assign the value to that attribute.
This is what I've tried so far but now I'm stuck:
enum Value {
String(String),
Float(f64),
Boolean(bool),
Object(String, Vec<(String, Value)>),
} // Vec<(String, Value)>, E.g. ["Kind", Object("Particle", [("energy", 4.3), ("shape", Object("Sphere", [("radius", 3.2)]))])]
fn create_spell(properties: Vec<(String, Value)>) -> Spell {
let mut spell = Spell::new();
fn create_spell_inner(properties: Vec<(String, Value)>, spell: &mut Spell) {
for property in properties{
match property {
// String = attribute name of current object
// Value = attribtue value of current object
}
}
}
return spell;
}
Create spell takes in the custom data type and returns the object, Spell, or at least that's what it's meant to do.
If you would like to see the rest of the code as of writing this, here you go, but it is a mess of mostly unused functions, structs and enums: https://github.com/CocytusDEDI/MMSpellbook/blob/14a8b5ff31476b99cdec2ebbc28ec647367f2635/src/main.rs
You can implement the
FromStrtrait for your custom types: docs. It's already implemented for f64, etc. However, that could be needlessly time-consuming if serde works for your purposes (credit to @vallentin).Another note: You declare
create_spell_innerbut never call it. You should probably just put thatforloop in the body ofcreate_spell.