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
use std::collections::HashMap;
use std::hash::Hash;
use std::mem;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TypeParams {
    Encode(HashMap<syn::Ident, syn::TypeParam>),
    Decode(HashMap<syn::Ident, syn::TypeParam>),
    Both {
        encode: HashMap<syn::Ident, syn::TypeParam>,
        decode: HashMap<syn::Ident, syn::TypeParam>
    }
}

impl TypeParams {
    pub fn try_merge(&mut self, s: proc_macro2::Span, other: Self) -> syn::Result<()> {
        match (&mut *self, other) {
            (Self::Encode(e1), Self::Encode(e2)) => {
                try_merge(s, e1, e2)?
            }
            (Self::Decode(d1), Self::Decode(d2)) => {
                try_merge(s, d1, d2)?
            }
            (Self::Encode(e), Self::Decode(d)) => {
                *self = Self::Both { encode: mem::take(e), decode: d }
            }
            (Self::Decode(d), Self::Encode(e)) => {
                *self = Self::Both { encode: e, decode: mem::take(d) }
            }
            (Self::Encode(e1), Self::Both { encode: e2, decode }) => {
                try_merge(s, e1, e2)?;
                *self = Self::Both { encode: mem::take(e1), decode }
            }
            (Self::Decode(d1), Self::Both { encode, decode: d2 }) => {
                try_merge(s, d1, d2)?;
                *self = Self::Both { encode, decode: mem::take(d1) }
            }
            (Self::Both { encode: e1, .. }, Self::Encode(e2)) => {
                try_merge(s, e1, e2)?
            }
            (Self::Both { decode: d1, .. }, Self::Decode(d2)) => {
                try_merge(s, d1, d2)?
            }
            (Self::Both { encode: e1, decode: d1 }, Self::Both { encode: e2, decode: d2 }) => {
                try_merge(s, e1, e2)?;
                try_merge(s, d1, d2)?
            }
        }
        Ok(())
    }

    pub fn get_encode(&self, id: &syn::Ident) -> Option<&syn::TypeParam> {
        match self {
            Self::Decode(_) => None,
            Self::Encode(e) => e.get(id),
            Self::Both { encode, .. } => encode.get(id),
        }
    }

    pub fn get_decode(&self, id: &syn::Ident) -> Option<&syn::TypeParam> {
        match self {
            Self::Encode(_) => None,
            Self::Decode(d) => d.get(id),
            Self::Both { decode, .. } => decode.get(id)
        }
    }
}

fn try_merge<K, V>(s: proc_macro2::Span, a: &mut HashMap<K, V>, b: HashMap<K, V>) -> syn::Result<()>
where
    K: Eq + Hash
{
    for (k, v) in b.into_iter() {
        if a.contains_key(&k) {
            return Err(syn::Error::new(s, "duplicate type parameter"))
        }
        a.insert(k, v);
    }
    Ok(())
}