rune_macros/
hash.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
use core::mem::take;

use rune_core::hash::Hash;
use rune_core::item::{ComponentRef, ItemBuf};
use syn::parse::{Parse, ParseStream};

use crate::context::Context;

pub(super) struct Arguments {
    path: syn::Path,
    associated: Option<(syn::Token![.], syn::Ident)>,
}

impl Arguments {
    pub(super) fn new(path: syn::Path) -> Self {
        Self {
            path,
            associated: None,
        }
    }

    /// Build a type item based on the current path.
    pub(crate) fn build_type_item(&self, cx: &Context) -> Result<syn::ExprArray, ()> {
        match crate::item::build_item(&self.path) {
            Ok(type_item) => Ok(type_item),
            Err(error) => {
                cx.error(error);
                Err(())
            }
        }
    }

    /// Construct a type hash from a Rust path.
    pub(crate) fn build_type_hash(&self, cx: &Context) -> Result<Hash, ()> {
        // Construct type hash.
        let mut buf = ItemBuf::new();
        let mut first = self.path.leading_colon.is_some();

        for s in &self.path.segments {
            let ident = s.ident.to_string();

            let c = if take(&mut first) {
                ComponentRef::Crate(&ident)
            } else {
                ComponentRef::Str(&ident)
            };

            if let Err(error) = buf.push(c) {
                cx.error(syn::Error::new_spanned(s, error));
                return Err(());
            }

            match &s.arguments {
                syn::PathArguments::None => {}
                syn::PathArguments::AngleBracketed(generics) => {
                    cx.error(syn::Error::new_spanned(
                        generics,
                        "Generic arguments are not supported",
                    ));
                }
                syn::PathArguments::Parenthesized(generics) => {
                    cx.error(syn::Error::new_spanned(
                        generics,
                        "Generic arguments are not supported",
                    ));
                }
            }
        }

        let base = Hash::type_hash(&buf);

        let hash = if let Some((_, associated)) = &self.associated {
            let name = associated.to_string();
            Hash::associated_function(base, name.as_str())
        } else {
            base
        };

        Ok(hash)
    }
}

impl Parse for Arguments {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let path = input.parse()?;

        let ident = if let Some(colon) = input.parse::<Option<syn::Token![.]>>()? {
            Some((colon, input.parse()?))
        } else {
            None
        };

        Ok(Self {
            path,
            associated: ident,
        })
    }
}