Skip to content

Instantly share code, notes, and snippets.

@thebluefish
Created May 11, 2023 17:07
Show Gist options
  • Save thebluefish/596ee2b9826b5312bcb19f202cd27225 to your computer and use it in GitHub Desktop.
Save thebluefish/596ee2b9826b5312bcb19f202cd27225 to your computer and use it in GitHub Desktop.
Tests serializing tsync example types to and from strings using serde
use chrono::{NaiveDate, NaiveDateTime};
use serde::{Deserialize, Serialize};
use tsync::tsync;
fn main() {
let msg = serde_json::to_string(&Message::Response(Response {
id: "Foo".into(),
result: NaiveDate::from_ymd(2016, 7, 8).and_hms(9, 10, 11),
})).unwrap();
println!("made: {msg}");
println!("got {:#?}", serde_json::from_str::<Message>(&msg).unwrap());
let external_msg = serde_json::to_string(&ExternalMessage::Response(Response {
id: "Bar".into(),
result: NaiveDate::from_ymd(2016, 7, 8).and_hms(9, 10, 11),
})).unwrap();
println!("made: {external_msg}");
println!("got {:#?}", serde_json::from_str::<ExternalMessage>(&external_msg).unwrap());
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[serde(tag = "last_precedent")]
#[tsync]
enum Message {
/// Per Enum case Docs One
UnitCaseLeft,
/// Per Enum case Docs Two
RequestLongTake {
id: String,
method: String,
params: i32,
},
Response(Response),
}
/// The default enum conversion uses external tagging
#[derive(Debug, Serialize, Deserialize)]
#[tsync]
enum ExternalMessage {
/// Per Enum case Docs One
UnitCaseLeft,
/// Per Enum case Docs Two
RequestLongTake {
id: String,
method: String,
params: i32,
},
/// Newtype variant with exactly one variable
Response(Response),
}
#[derive(Debug, Serialize, Deserialize)]
#[tsync]
struct Response {
id: String,
result: NaiveDateTime,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment