syn_solidity/type/
mapping.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
use crate::{kw, SolIdent, Spanned, Type, VariableDeclaration};
use proc_macro2::Span;
use std::{
    fmt,
    hash::{Hash, Hasher},
};
use syn::{
    parenthesized,
    parse::{Parse, ParseStream},
    token::Paren,
    Result, Token,
};

/// A mapping type: `mapping(uint key => string value)`
#[derive(Clone)]
pub struct TypeMapping {
    pub mapping_token: kw::mapping,
    pub paren_token: Paren,
    pub key: Box<Type>,
    pub key_name: Option<SolIdent>,
    pub fat_arrow_token: Token![=>],
    pub value: Box<Type>,
    pub value_name: Option<SolIdent>,
}

impl PartialEq for TypeMapping {
    fn eq(&self, other: &Self) -> bool {
        self.key == other.key && self.value == other.value
    }
}

impl Eq for TypeMapping {}

impl Hash for TypeMapping {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.key.hash(state);
        self.value.hash(state);
    }
}

impl fmt::Display for TypeMapping {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "mapping({} ", self.key)?;
        if let Some(key_name) = &self.key_name {
            write!(f, "{key_name} ")?;
        }
        write!(f, "=> {} ", self.value)?;
        if let Some(value_name) = &self.value_name {
            write!(f, "{value_name}")?;
        }
        f.write_str(")")
    }
}

impl fmt::Debug for TypeMapping {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("TypeMapping")
            .field("key", &self.key)
            .field("key_name", &self.key_name)
            .field("value", &self.value)
            .field("value_name", &self.value_name)
            .finish()
    }
}

impl Parse for TypeMapping {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        let content;
        Ok(Self {
            mapping_token: input.parse()?,
            paren_token: parenthesized!(content in input),
            key: content.parse()?,
            key_name: content.call(SolIdent::parse_opt)?,
            fat_arrow_token: content.parse()?,
            value: content.parse()?,
            value_name: content.call(SolIdent::parse_opt)?,
        })
    }
}

impl Spanned for TypeMapping {
    fn span(&self) -> Span {
        let span = self.mapping_token.span;
        span.join(self.paren_token.span.join()).unwrap_or(span)
    }

    fn set_span(&mut self, span: Span) {
        self.mapping_token.span = span;
        self.paren_token = Paren(span);
        self.key.set_span(span);
        if let Some(key_name) = &mut self.key_name {
            key_name.set_span(span);
        }
        self.value.set_span(span);
        if let Some(value_name) = &mut self.value_name {
            value_name.set_span(span);
        }
    }
}

impl TypeMapping {
    /// Returns a `VariableDeclaration` corresponding to this mapping's key.
    pub fn key_var(&self) -> VariableDeclaration {
        VariableDeclaration::new_with((*self.key).clone(), None, self.key_name.clone())
    }

    /// Returns a `VariableDeclaration` corresponding to this mapping's value.
    pub fn value_var(&self) -> VariableDeclaration {
        VariableDeclaration::new_with((*self.value).clone(), None, self.value_name.clone())
    }
}