pinata_sdk/api/
metadata.rs1use std::collections::HashMap;
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Deserialize, Serialize)]
5#[serde(untagged)]
6pub enum MetadataValue {
8 String(String),
10 Float(f64),
12 Integer(u64),
14 Delete,
16}
17
18pub type MetadataKeyValues = HashMap<String, MetadataValue>;
20
21#[derive(Debug, Serialize)]
22pub struct PinMetadata {
24 #[serde(skip_serializing_if = "Option::is_none")]
25 pub name: Option<String>,
27 pub keyvalues: MetadataKeyValues,
29}
30
31#[derive(Debug, Deserialize)]
32pub struct PinListMetadata {
37 pub name: Option<String>,
39 pub keyvalues: Option<MetadataKeyValues>,
41}
42
43#[derive(Debug, Serialize)]
44#[serde(rename_all = "camelCase")]
45pub struct ChangePinMetadata {
48 pub ipfs_pin_hash: String,
50
51 #[serde(flatten)]
52 pub metadata: PinMetadata,
54}
55
56#[cfg(test)]
57mod tests {
58 use std::collections::HashMap;
59 use serde_json::Value;
60 use super::{PinMetadata, MetadataValue};
61
62 #[test]
63 fn test_serialization_of_metadata() {
64 let mut keyvalues = HashMap::new();
65 keyvalues.insert("string".to_string(), MetadataValue::String("value".to_string()));
66 keyvalues.insert("number".to_string(), MetadataValue::Integer(10));
67 keyvalues.insert("delete".to_string(), MetadataValue::Delete);
68
69 let data = PinMetadata {
70 name: None,
71 keyvalues,
72 };
73
74 let json_value: Value = serde_json::from_str(
75 &serde_json::to_string(&data).unwrap()
76 ).unwrap();
77
78 if let Value::Object(object) = json_value {
87 if object.contains_key("name") {
88 assert!(false, "name fields of metadata should not exists")
89 }
90
91 if let Value::Object(keyvalues) = object.get("keyvalues").unwrap() {
92 if let Value::Null = keyvalues.get("delete").unwrap() { } else {
93 assert!(false, "keyvalues.delete should be null");
94 }
95
96 if let Value::String(string) = keyvalues.get("string").unwrap() {
97 assert_eq!("value", string);
98 } else {
99 assert!(false, "keyvalues.string is not a string");
100 }
101
102 if let Value::Number(number) = keyvalues.get("number").unwrap() {
103 assert_eq!(10, number.as_u64().unwrap());
104 } else {
105 assert!(false, "keyvalues.number is not a number");
106 }
107
108
109 } else {
110 assert!(false, "keyvalues fields of metadata should be an object");
111 }
112 } else {
113 assert!(false, "metadata not serialized as object");
114 }
115 }
116}