Skip to content

Commit

Permalink
serde-metadata: serialize metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
tinybeachthor committed Oct 28, 2024
1 parent fd98a5a commit d3b3857
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 3 deletions.
123 changes: 123 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ darling = "0.20"
quote = "1"
syn = "2"
proc-macro2 = "1"
insta = "1"
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
my-rust
pkgs.taplo
pkgs.cargo-deny
pkgs.cargo-insta
];
};
};
Expand Down
6 changes: 5 additions & 1 deletion serde-metadata/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
16 changes: 16 additions & 0 deletions serde-metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,30 @@ use serde::Serialize;
pub struct Metadata {
items: IndexMap<String, String>,
}
impl Metadata {
pub fn new() -> Self {
Self {
items: IndexMap::new(),
}
}
}
impl Deref for Metadata {
type Target = IndexMap<String, String>;

fn deref(&self) -> &Self::Target {
&self.items
}
}
impl From<IndexMap<String, String>> for Metadata {
fn from(items: IndexMap<String, String>) -> Self {
Self { items }
}
}

pub trait SerdeMetadata: Serialize {
type METADATA: Serialize + Default;

fn get_metadata(&self) -> Self::METADATA {
Self::METADATA::default()
}
}
16 changes: 14 additions & 2 deletions serde-metadata/tests/simple.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use indexmap::indexmap;
use serde::Serialize;
use serde_metadata::{Metadata, SerdeMetadata};

Expand All @@ -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(),
}),
}
}
}
Expand All @@ -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());
}
5 changes: 5 additions & 0 deletions serde-metadata/tests/snapshots/simple__get_metadata.snap
Original file line number Diff line number Diff line change
@@ -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"}}}

0 comments on commit d3b3857

Please sign in to comment.