From d3b3857a25c9f504d0e822cdc1fe0b4332b1ab66 Mon Sep 17 00:00:00 2001 From: Martin Toman <25009432+tinybeachthor@users.noreply.github.com> Date: Sun, 27 Oct 2024 21:09:33 -0500 Subject: [PATCH] serde-metadata: serialize metadata --- Cargo.lock | 123 ++++++++++++++++++ Cargo.toml | 1 + flake.nix | 1 + serde-metadata/Cargo.toml | 6 +- serde-metadata/src/lib.rs | 16 +++ serde-metadata/tests/simple.rs | 16 ++- .../tests/snapshots/simple__get_metadata.snap | 5 + 7 files changed, 165 insertions(+), 3 deletions(-) create mode 100644 serde-metadata/tests/snapshots/simple__get_metadata.snap diff --git a/Cargo.lock b/Cargo.lock index 53a13f8..4ed3967 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,18 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "console" +version = "0.15.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e1f83fc076bd6dd27517eacdf25fef6c4dfe5f1d7448bafaaf3a26f13b5e4eb" +dependencies = [ + "encode_unicode", + "lazy_static", + "libc", + "windows-sys", +] + [[package]] name = "darling" version = "0.20.10" @@ -52,6 +64,12 @@ dependencies = [ "syn", ] +[[package]] +name = "encode_unicode" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" + [[package]] name = "env_logger" version = "0.8.4" @@ -108,18 +126,42 @@ dependencies = [ "serde", ] +[[package]] +name = "insta" +version = "1.41.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1f72d3e19488cf7d8ea52d2fc0f8754fc933398b337cd3cbdb28aaeb35159ef" +dependencies = [ + "console", + "lazy_static", + "linked-hash-map", + "similar", +] + [[package]] name = "itoa" version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + [[package]] name = "libc" version = "0.2.158" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" +[[package]] +name = "linked-hash-map" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" + [[package]] name = "log" version = "0.4.22" @@ -238,7 +280,9 @@ name = "serde-metadata" version = "0.1.0-alpha.1" dependencies = [ "indexmap", + "insta", "serde", + "serde_json", ] [[package]] @@ -278,6 +322,12 @@ dependencies = [ "serde", ] +[[package]] +name = "similar" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1de1d4f81173b03af4c0cbed3c898f6bff5b870e4a7f5d6f4057d62a7a4b686e" + [[package]] name = "strsim" version = "0.11.1" @@ -326,3 +376,76 @@ name = "wasi" version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/Cargo.toml b/Cargo.toml index e52050d..ef781c2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,3 +22,4 @@ darling = "0.20" quote = "1" syn = "2" proc-macro2 = "1" +insta = "1" diff --git a/flake.nix b/flake.nix index a638ad8..5b593b5 100644 --- a/flake.nix +++ b/flake.nix @@ -103,6 +103,7 @@ my-rust pkgs.taplo pkgs.cargo-deny + pkgs.cargo-insta ]; }; }; diff --git a/serde-metadata/Cargo.toml b/serde-metadata/Cargo.toml index f38c0fb..60863bc 100644 --- a/serde-metadata/Cargo.toml +++ b/serde-metadata/Cargo.toml @@ -8,4 +8,8 @@ homepage = { workspace = true } [dependencies] indexmap = { workspace = true, features = ["serde"] } -serde = { workspace = true } +serde = { workspace = true, features = ["derive"] } + +[dev-dependencies] +insta = { workspace = true } +serde_json = { workspace = true } diff --git a/serde-metadata/src/lib.rs b/serde-metadata/src/lib.rs index deaf810..444ec94 100644 --- a/serde-metadata/src/lib.rs +++ b/serde-metadata/src/lib.rs @@ -7,6 +7,13 @@ use serde::Serialize; pub struct Metadata { items: IndexMap, } +impl Metadata { + pub fn new() -> Self { + Self { + items: IndexMap::new(), + } + } +} impl Deref for Metadata { type Target = IndexMap; @@ -14,7 +21,16 @@ impl Deref for Metadata { &self.items } } +impl From> for Metadata { + fn from(items: IndexMap) -> Self { + Self { items } + } +} pub trait SerdeMetadata: Serialize { type METADATA: Serialize + Default; + + fn get_metadata(&self) -> Self::METADATA { + Self::METADATA::default() + } } diff --git a/serde-metadata/tests/simple.rs b/serde-metadata/tests/simple.rs index dfc65c4..178637d 100644 --- a/serde-metadata/tests/simple.rs +++ b/serde-metadata/tests/simple.rs @@ -1,3 +1,4 @@ +use indexmap::indexmap; use serde::Serialize; use serde_metadata::{Metadata, SerdeMetadata}; @@ -15,8 +16,12 @@ pub struct SimpleMetadata { impl Default for SimpleMetadata { fn default() -> Self { Self { - a: Default::default(), - b: Default::default(), + a: Metadata::from(indexmap! { + "comment".to_string() => "field: a".to_string(), + }), + b: Metadata::from(indexmap! { + "comment".to_string() => "field: b".to_string(), + }), } } } @@ -27,4 +32,11 @@ impl SerdeMetadata for Simple { #[test] fn get_metadata() { + let simple = Simple { + a: "hello".to_string(), + b: 42, + }; + let metadata = simple.get_metadata(); + + insta::assert_snapshot!(serde_json::to_string(&metadata).unwrap()); } diff --git a/serde-metadata/tests/snapshots/simple__get_metadata.snap b/serde-metadata/tests/snapshots/simple__get_metadata.snap new file mode 100644 index 0000000..321865a --- /dev/null +++ b/serde-metadata/tests/snapshots/simple__get_metadata.snap @@ -0,0 +1,5 @@ +--- +source: serde-metadata/tests/simple.rs +expression: "serde_json::to_string(&metadata).unwrap()" +--- +{"a":{"items":{"comment":"field: a"}},"b":{"items":{"comment":"field: b"}}}