Ease of manipulating sqlite3, json, other formats outside of Rust


#1

Question:

Of all the formats that Rust can serialize to, which format is easiest to manipulate WITHOUT USING RUST.

Context:

I’m developing my Rust program. I have a bunch of structs, call them S1. I serialize some state to file, state_with_s1.blah .

The, I realize my structs S1 aren’t quite the right type, so I use S2 instead, which is kinda like S1 but not identical.

Now, I need an easy way to transform “state_with_s1.blah” into “state_with_s2.blah” so I can read the state back in to my new program.

So far the best solution seems to be:

  1. serialize data from Rust to sqlite3
  2. when going from S1 to S2, just use SQL to add/drop columns / compute new values
  3. read data back from sqlite3

However, if there are easy to use solutions via json or other formats, I would like to lear about those too.

Thanks!


#2

What’s wrong with writing a throwaway Rust program to do the job of removing fields and computing new ones? That might be a 50 line program which imports your existing struct and pipes it through a From impl to do the conversion, before saving the data to disk again (as JSON, CSV, XML, or whatever) .

Alternarively, depending on the problem domain I might be tempted to reach for Python in this case. Using something like a Jupyter notebook lets you interactively explore the data and refine your conversion. Because it’s a one-off job, you can usually get away with hacking together a script and ignoring error cases you know can’t happen.

The term you are probably looking for is “data munging”. It’s typically associated with big data and data analysis but the general idea is you go from having data in one shape (e.g. struct A) and transform it to have a slightly different shape (struct B). It’s not unique to Rust, but having nice tools like serde, the From trait, rayon, and powerful libraries in all the major formats (serde_json, toml, csv) can help make the job easier.


#3

It’s a matter of flow. a 5-line SQL script is a thought; a 50-line crate takes real effort / context switching.


#4

Applying a simple change to a JSON file will probably take less than 10 lines of code in JavaScript or Python, so I would say it’s no harder than writing an SQL script. JSON seems to be the format that’s easy to manipulate from many languages.