From 169b89527b532d0d721a09dee6fcd9f3628e4ab4 Mon Sep 17 00:00:00 2001 From: Kevin Nakamura Date: Sat, 23 Dec 2023 11:39:16 +0900 Subject: [PATCH] Add simple serialization example (good starting point for `no_std`). --- examples/simple_serialize.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 examples/simple_serialize.rs diff --git a/examples/simple_serialize.rs b/examples/simple_serialize.rs new file mode 100644 index 0000000..b0c7c46 --- /dev/null +++ b/examples/simple_serialize.rs @@ -0,0 +1,31 @@ +use edn_rs::Serialize; + +struct Foo<'a> { + value: u64, + say: &'a str, +} + +impl Serialize for Foo<'_> { + fn serialize(&self) -> String { + format!("{{:value {}, :say {:?}}}", self.value, self.say) + } +} + +fn serialize() -> String { + let say = "Hello, World!"; + let foo = Foo { + value: 42, + say: say, + }; + + edn_rs::to_string(&foo) +} + +fn main() { + println!("{}", serialize()); +} + +#[test] +fn test_serialize() { + assert_eq!(serialize(), "{:value 42, :say \"Hello, World!\"}"); +}