1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#![deny(missing_docs)]

//! Type definitions for ERC-based non-fungible token (NFT) metadata.
//!
//! While [EIP-721](https://eips.ethereum.org/EIPS/eip-721#specification) defines a "ERC-721 Metadata JSON Schema", in practice
//! it is rarely used. Instead, this crate implements the more popular [OpenSea metadata standard](https://docs.opensea.io/docs/metadata-standards).
//!
//! This crate does not attempt to perform validation more than what is strictly necessary. Since every secondary
//! market will use the fields in the metadata in a different way, it is up to the crate consumer to make sure the fields are appropriately populated.

use rgb::RGB8;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use url::Url;

/// Metadata for a token.
///
/// While even an empty object is "valid" metadata, this crate takes a more opinionated approach.
/// The following fields are strictly required: [`name`](Metadata::name), [`description`](Metadata::description), [`image`](Metadata::image).
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Metadata {
    /// URL to image of the item.
    pub image: Url,
    /// External URL to another site.
    pub external_url: Option<Url>,
    /// Human-readable description of the item.
    pub description: String,
    /// Name of the item.
    pub name: String,
    /// Attributes for the item.
    #[cfg_attr(feature = "serde", serde(default))]
    pub attributes: Vec<AttributeEntry>,
    /// Background color of the item.
    /// When serialized, it takes the form of a 6-character hexadecimal string without a `#`.
    #[cfg_attr(feature = "serde", serde(with = "rgb8_fromhex_opt", default))]
    pub background_color: Option<RGB8>,
    /// URL to multi-media attachment for the item.
    pub animation_url: Option<Url>,
    /// URL to a YouTube video.
    pub youtube_url: Option<Url>,
}

/// A key-value pair of attributes for an item.
#[cfg_attr(
    feature = "serde",
    derive(Serialize, Deserialize),
    serde(rename_all = "snake_case"),
    serde(untagged)
)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum AttributeEntry {
    /// Textual attribute.
    String {
        /// Name of the trait.
        trait_type: String,
        /// Value of the attribute.
        value: String,
    },
    /// Numerical attribute.
    Number {
        /// Name of the trait.
        trait_type: String,
        /// Value of the attribute.
        value: u64,
        /// How the attribute should be displayed.
        display_type: Option<DisplayType>,
    },
}

/// How a numerical attribute should be displayed.
#[cfg_attr(
    feature = "serde",
    derive(Serialize, Deserialize),
    serde(rename_all = "snake_case")
)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum DisplayType {
    /// As a number.
    Number,
    /// As a boost percentage.
    BoostPercentage,
    /// As a boost number.
    BoostNumber,
    /// As a date.
    Date,
}

#[cfg(feature = "serde")]
mod rgb8_fromhex_opt {
    use rgb::{ComponentSlice, RGB8};
    use serde::{de::Error, Deserializer, Serialize, Serializer};

    pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<RGB8>, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s: Vec<u8> = hex::deserialize(deserializer)?;
        if s.len() != 3 {
            Err(D::Error::custom("expected color hex string"))
        } else {
            Ok(Some(RGB8 {
                r: s[0],
                g: s[1],
                b: s[2],
            }))
        }
    }

    pub fn serialize<S>(value: &Option<RGB8>, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match value {
            Some(value) => hex::serialize(value.as_slice().to_vec(), serializer),
            None => None::<()>.serialize(serializer),
        }
    }

    #[cfg(test)]
    mod tests {
        use rgb::RGB8;
        use serde::{Deserialize, Serialize};

        #[derive(Serialize, Deserialize, Debug)]
        struct Target {
            #[serde(with = "crate::rgb8_fromhex_opt")]
            color: Option<RGB8>,
        }

        #[test]
        fn from_json() {
            let s = r#"{ "color": "f2f2f2" }"#;
            let target: Target = serde_json::from_str(s).unwrap();
            assert_eq!(
                target.color,
                Some(RGB8 {
                    r: 242,
                    g: 242,
                    b: 242
                })
            );
        }

        #[test]
        fn to_json() {
            let target = Target {
                color: Some(RGB8 {
                    r: 242,
                    g: 242,
                    b: 242,
                }),
            };
            let s = serde_json::to_string(&target).unwrap();
            assert_eq!(s, r#"{"color":"f2f2f2"}"#)
        }

        #[test]
        fn from_notcolor_json() {
            let s = r#"{ "color": "f2f2f2f2" }"#;
            let target = serde_json::from_str::<Target>(s);
            assert!(target.is_err());
        }
    }
}

#[cfg(feature = "serde")]
#[cfg(test)]
mod tests {
    use crate::Metadata;

    const PLANETPASS_ITEM: &str = r#"
    {
        "image": "https://assets.wanderers.ai/file/planetpass/vid/0/0.mp4",
        "description": "Visit this planet and get a free Rocketeer NFT from Alucard.eth!",
        "name": "Rocketeer X",
        "attributes": [
          {
            "trait_type": "Core",
            "value": "Vortex"
          },
          {
            "trait_type": "Satellite",
            "value": "Protoplanets"
          },
          {
            "trait_type": "Feature",
            "value": "Icy"
          },
          {
            "trait_type": "Ship",
            "value": "Docking"
          },
          {
            "trait_type": "Space",
            "value": "Green Sun"
          },
          {
            "trait_type": "Terrain",
            "value": "Layers"
          },
          {
            "trait_type": "Faction",
            "value": "Coalition for Uncorrupted Biology"
          },
          {
            "trait_type": "Atmosphere",
            "value": "Alpen Glow"
          }
        ]
      }
    "#;

    #[test]
    pub fn planetpass() {
        let metadata = serde_json::from_str::<Metadata>(PLANETPASS_ITEM);
        assert!(metadata.is_ok());
    }
}