0

I'm trying to make a deserializer for the FullOrder struct from the CEX.IO exchange (https://cex.io/rest-api#/definitions/FullOrder). It unfortunately doesn't contain a consistent definition, and has fields as labelled below:

ta:{symbol2}    string  total amount in current currency (Maker)
tta:{symbol2}   string  total amount in current currency (Taker)
fa:{symbol2}    string  fee amount in current currency (Maker)
tfa:{symbol2}   string  fee amount in current currency (Taker)
a:{symbol1}:cds string  credit, debit and saldo merged amount in current currency

So, for example, it could be tta:USD in one message or tta:EUR in another.

I've been following this guide (https://serde.rs/deserialize-struct.html) in creating my struct, and I've got all the way through it bar one thing - the final part requires specifying all the fields.

In the field visitor, I have the following to handle the fields in advance:

if value.starts_with("ta:") {
    Ok(Field::TotalMakerAmount)
} else if value.starts_with("tta:") {
    Ok(Field::TotalTakerAmount)
} else if value.starts_with("fa:") {
    Ok(Field::FeeMakerAmount)
} else if value.starts_with("tfa:") {
    Ok(Field::FeeTakerAmount)
} else if value.starts_with("a:") {
    Ok(Field::CreditDebitSalvo)

                    

But the deserialize_struct requires a list of all the fields to visit. Is there anyway I can get a hold of all fields dynamically to pass through here? Or have I reached the extreme of what a serde deserializer can do?

Thanks

AdmiralJonB
  • 2,038
  • 3
  • 23
  • 27
  • It might be easier to use serde derive no a struct, but this is definitely possible as the derive can do it. Here's some helpful stuff for derive: https://serde.rs/field-attrs.html Also as far as I can tell deserialize_struct is just a hint to what fields could be there but I could be wrong about that so I'm not making this an answer. – Buzzec Feb 25 '21 at 04:30
  • Hmm... I'm not sure I can see how you can do it with derive attributes, I can see you can rename a field, but I don't see anything about fields with changing names. – AdmiralJonB Feb 25 '21 at 10:26
  • @Buzzec You are correct that deserialize_struct works as a hint, I passed in an empty array and it worked fine. Feel free to work it up as an answer and I'll accept - or I'll write one up after a few days. – AdmiralJonB Feb 25 '21 at 12:09

0 Answers0