rustserializationstructdeserializationtoml

Deserialise TOML data into struct with nalgebra types


I have this struct -

#[derive(Deserialize)]
struct body {
    label: String,
    mass: f64,
    pos: Point3<f64>,
    vel: Vector3<f64>,
    obj_col: f32,
}

And I have some data in a toml file I would like to read and deserialise into the struct.

[[objects]]
    label = "Sun"
    mass = 1.9891e30
    pos = [0.0, 0.0, 0.0]
    vel = [0.0, 0.0, 0.0]
    obj_col = 1.00

I've read through the nalgebra docs and it seems serialisation traits exist for the vector/matrix types but it's unclear how I am supposed to implement them. As it stands serde doesn't recognise Point3 and Vector3 as types that can be deserialised.

Has someone else implemented a solution to this problem previously? I'm not sure where to start

As a secondary question, the toml file has multiple objects that will all become individual struct instances. Is there a way to iterate through instances as they're deserialised?


Solution

  • You need to enable the crate feature serde-serialize on nalgebra to get their deserializers.

    If you want to Iterate through the instances to make an individual struct, you'll have to implement a custom Deserializer instead of deriving one.