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
use anyhow::Result;
use serde::{Deserialize, Serialize};

use crate::validate::{errors, parser};

#[derive(Debug, Clone, Deserialize, Default, Serialize)]
pub struct Metadata {
    pub name: String,
    pub symbol: String,
    pub description: String,
    pub seller_fee_basis_points: u16,
    pub image: String,
    pub animation_url: Option<String>,
    pub external_url: Option<String>,
    pub attributes: Vec<Attribute>,
    pub collection: Option<Collection>,
    pub properties: Property,
}

impl Metadata {
    pub fn validate(self) -> Result<()> {
        parser::check_name(&self.name)?;
        parser::check_symbol(&self.symbol)?;
        parser::check_url(&self.image)?;
        parser::check_seller_fee_basis_points(self.seller_fee_basis_points)?;

        Ok(())
    }

    pub fn validate_strict(self) -> Result<()> {
        if self.animation_url.is_none() {
            return Err(errors::ValidateError::MissingAnimationUrl.into());
        } else {
            parser::check_url(&self.animation_url.unwrap())?;
        }

        if self.collection.is_none() {
            return Err(errors::ValidateError::MissingCollection.into());
        }

        if self.external_url.is_none() {
            return Err(errors::ValidateError::MissingExternalUrl.into());
        } else {
            parser::check_url(&self.external_url.unwrap())?;
        }

        parser::check_name(&self.name)?;
        parser::check_symbol(&self.symbol)?;
        parser::check_url(&self.image)?;
        parser::check_seller_fee_basis_points(self.seller_fee_basis_points)?;

        Ok(())
    }
}

#[derive(Debug, Clone, Deserialize, Default, Serialize)]
pub struct Collection {
    pub name: String,
    pub family: String,
}

#[derive(Debug, Clone, Deserialize, Default, Serialize)]
pub struct Property {
    pub files: Vec<FileAttr>,
    pub category: String,
}

#[derive(Debug, Clone, Deserialize, Default, Serialize)]
pub struct Attribute {
    pub trait_type: String,
    pub value: String,
}

#[derive(Debug, Clone, Deserialize, Default, Serialize)]
pub struct FileAttr {
    pub uri: String,
    #[serde(rename = "type")]
    pub file_type: String,
}