syn_solidity/ident/
path.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
use crate::{SolIdent, Spanned};
use proc_macro2::{Ident, Span};
use std::{
    fmt,
    ops::{Deref, DerefMut},
};
use syn::{
    ext::IdentExt,
    parse::{Parse, ParseStream},
    punctuated::Punctuated,
    Result, Token,
};

/// Create a [`SolPath`] from a list of identifiers.
#[macro_export]
macro_rules! sol_path {
    () => { $crate::SolPath::new() };

    ($($e:expr),+) => {{
        let mut path = $crate::SolPath::new();
        $(path.push($crate::SolIdent::from($e));)+
        path
    }};
}

/// A list of identifiers, separated by dots.
///
/// This is never parsed as empty.
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct SolPath(Punctuated<SolIdent, Token![.]>);

impl Deref for SolPath {
    type Target = Punctuated<SolIdent, Token![.]>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for SolPath {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl fmt::Display for SolPath {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for (i, ident) in self.0.iter().enumerate() {
            if i > 0 {
                f.write_str(".")?;
            }
            ident.fmt(f)?;
        }
        Ok(())
    }
}

impl fmt::Debug for SolPath {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_list().entries(&self.0).finish()
    }
}

impl FromIterator<SolIdent> for SolPath {
    fn from_iter<T: IntoIterator<Item = SolIdent>>(iter: T) -> Self {
        Self(iter.into_iter().collect())
    }
}

impl Parse for SolPath {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        // Modified from: `syn::Path::parse_mod_style`
        let mut segments = Punctuated::new();
        loop {
            if !input.peek(Ident::peek_any) {
                break;
            }
            segments.push_value(input.parse()?);
            if !input.peek(Token![.]) {
                break;
            }
            segments.push_punct(input.parse()?);
        }

        if segments.is_empty() {
            Err(input.parse::<SolIdent>().unwrap_err())
        } else if segments.trailing_punct() {
            Err(input.error("expected path segment after `.`"))
        } else {
            Ok(Self(segments))
        }
    }
}

impl Spanned for SolPath {
    fn span(&self) -> Span {
        self.0.span()
    }

    fn set_span(&mut self, span: Span) {
        self.0.set_span(span);
    }
}

impl Default for SolPath {
    fn default() -> Self {
        Self::new()
    }
}

impl SolPath {
    pub const fn new() -> Self {
        Self(Punctuated::new())
    }

    pub fn first(&self) -> &SolIdent {
        self.0.first().unwrap()
    }

    pub fn first_mut(&mut self) -> &mut SolIdent {
        self.0.first_mut().unwrap()
    }

    pub fn last(&self) -> &SolIdent {
        self.0.last().unwrap()
    }

    pub fn last_mut(&mut self) -> &mut SolIdent {
        self.0.last_mut().unwrap()
    }
}