4 ms·
I think protocol buffers is pretty similar to what you're looking for.
by ZitchDog 5y ago
I think protocol buffers is pretty similar to what you're looking for.
- brundolf 5y agoMaybe. Those seem pretty low-level though; I'm thinking of something abstract enough to be used with TypeScript or GraphQL just as easily as Rust or Kotlin. So byte-layouts are likely too specific. (Correct me if I'm wrong about how Protocol Buffers work) I think Serde is the closest thing I've seen to what I'm getting at; the way it can freely translate between XML and JSON and Rust structures without any help. But of course it's not a standalone schema language.
- trevyn 5y agoFor those who don’t know, much of Serde’s magic is powered by Rust procedural derive macros, which generate (i.e. “derive”) code from a Rust struct definition. You can, of course, write your own derive macros, and apply several derive macros to a single struct. I wrote some to derive SQLite schemas and migrations and queries and auto-generate frontend TypeScript RPC code. So my source of truth is the Rust struct, and that sort of magically generates everything else. I don’t know if this is a good idea, but it’s been pretty fun so far. :-) There are also derive-macro-powered crates to do GraphQL, other flavors of SQL, WASM bindings, etc etc.
- bobbylarrybobby 5y agoJust to drive the point home, serde does solve the n^2 → 2n problem: serialized data is first deserialized to serde's intermediate data model (consisting of, like, 30 or so different types), from which a bespoke Rust object is produced. So you define how your types (de)serialize oblivious to the fact that serde_json, say, will be on the other end doing the actual (de)serialization.