hpl_toolkit/schema/
default_impl.rs

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
use super::{Number, Schema, SchemaValue, ToSchema};
use crate::utils::VecMap;
use anchor_lang::prelude::*;
use std::collections::HashMap;

macro_rules! impl_to_schema_for_unsigned {
    ($($t:ty)+) => {
        $(
            impl ToSchema for $t {
                fn schema() -> Schema {
                    Schema::Number
                }

                fn schema_value(&self) -> SchemaValue {
                    SchemaValue::Number(Number::U64(self.clone().into()))
                }
            }
        )+
    };
}
impl_to_schema_for_unsigned!(u8 u16 u32 u64);

macro_rules! impl_to_schema_for_signed {
    ($($t:ty)+) => {
        $(
            impl ToSchema for $t {
                fn schema() -> Schema {
                    Schema::Number
                }

                fn schema_value(&self) -> SchemaValue {
                    SchemaValue::Number(Number::I64(self.clone().into()))
                }
            }
        )+
    };
}
impl_to_schema_for_signed!(i8 i16 i32 i64);

macro_rules! impl_to_schema_for_float {
    ($($t:ty)+) => {
        $(
            impl ToSchema for $t {
                fn schema() -> Schema {
                    Schema::Number
                }

                fn schema_value(&self) -> SchemaValue {
                    SchemaValue::Number(Number::F64(self.clone().into()))
                }
            }
        )+
    };
}
impl_to_schema_for_float!(f32 f64);

macro_rules! impl_to_schema_for_array {
    ($($n:literal)+) => {
        $(
            impl<T: ToSchema> ToSchema for [T; $n] {
                fn schema() -> Schema {
                    Schema::Array(Box::new(T::schema()))
                }

                fn schema_value(&self) -> SchemaValue {
                    SchemaValue::Array(self.iter().map(|i| i.schema_value()).collect())
                }
            }
        )+
    };
}
impl_to_schema_for_array!(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);

impl ToSchema for String {
    fn schema() -> Schema {
        Schema::String
    }

    fn schema_value(&self) -> SchemaValue {
        SchemaValue::String(self.clone().trim_matches(char::from(0)).to_string())
    }
}

impl<T: ToSchema> ToSchema for Vec<T> {
    fn schema() -> Schema {
        Schema::Array(Box::new(T::schema()))
    }

    fn schema_value(&self) -> SchemaValue {
        SchemaValue::Array(self.iter().map(|i| i.schema_value()).collect())
    }
}

impl ToSchema for Pubkey {
    fn schema() -> Schema {
        Schema::Pubkey
    }

    fn schema_value(&self) -> SchemaValue {
        SchemaValue::Pubkey(self.clone())
    }
}

impl ToSchema for bool {
    fn schema() -> Schema {
        Schema::Bool
    }

    fn schema_value(&self) -> SchemaValue {
        SchemaValue::Bool(self.clone())
    }
}

impl<T: ToSchema> ToSchema for Option<T> {
    fn schema() -> Schema {
        Schema::Option(Box::new(T::schema()))
    }

    fn schema_value(&self) -> SchemaValue {
        if self.is_some() {
            self.as_ref().unwrap().schema_value()
        } else {
            SchemaValue::Null
        }
    }
}

impl<K: ToString, V: ToSchema> ToSchema for HashMap<K, V> {
    fn schema() -> Schema {
        Schema::VecMap(Box::new(Schema::String), Box::new(V::schema()))
    }

    fn schema_value(&self) -> SchemaValue {
        let mut schema = VecMap::new();
        self.iter().for_each(|(key, value)| {
            schema.insert(key.to_string().schema_value(), value.schema_value());
        });
        SchemaValue::VecMap(schema)
    }
}

impl ToSchema for Vec<(String, String)> {
    fn schema() -> Schema {
        Schema::Array(Box::new(Schema::Array(Box::new(Schema::String))))
    }

    fn schema_value(&self) -> SchemaValue {
        SchemaValue::Array(
            self.iter()
                .map(|(key, value)| {
                    SchemaValue::Array(vec![
                        SchemaValue::String(key.clone()),
                        SchemaValue::String(value.clone()),
                    ])
                })
                .collect(),
        )
    }
}