rune/compile/
meta_info.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
use core::fmt;

use crate::alloc;
use crate::alloc::prelude::*;
use crate::compile::meta;
use crate::{Hash, Item, ItemBuf};

/// Provides an owned human-readable description of a meta item.
#[derive(Debug)]
#[non_exhaustive]
pub struct MetaInfo {
    /// The kind of the item.
    kind: MetaInfoKind,
    /// The hash of the meta item.
    hash: Hash,
    /// The item being described.
    item: Option<ItemBuf>,
}

impl MetaInfo {
    /// Construct a new meta info.
    pub(crate) fn new(kind: &meta::Kind, hash: Hash, item: Option<&Item>) -> alloc::Result<Self> {
        Ok(Self {
            kind: MetaInfoKind::from_kind(kind),
            hash,
            item: item.map(|item| item.try_to_owned()).transpose()?,
        })
    }
}

impl fmt::Display for MetaInfo {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        struct Name<'a>(Hash, Option<&'a Item>);

        impl fmt::Display for Name<'_> {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                if let Some(item) = self.1 {
                    item.fmt(f)
                } else {
                    self.0.fmt(f)
                }
            }
        }

        let name = Name(self.hash, self.item.as_deref());

        match self.kind {
            MetaInfoKind::Type => {
                write!(fmt, "type {name}")?;
            }
            MetaInfoKind::Struct => {
                write!(fmt, "struct {name}")?;
            }
            MetaInfoKind::Variant => {
                write!(fmt, "variant {name}")?;
            }
            MetaInfoKind::Enum => {
                write!(fmt, "enum {name}")?;
            }
            MetaInfoKind::Macro => {
                write!(fmt, "macro {name}")?;
            }
            MetaInfoKind::AttributeMacro => {
                write!(fmt, "attribute macro {name}")?;
            }
            MetaInfoKind::Function => {
                write!(fmt, "fn {name}")?;
            }
            MetaInfoKind::Associated => {
                write!(fmt, "associated fn {name}")?;
            }
            MetaInfoKind::Closure => {
                write!(fmt, "closure {name}")?;
            }
            MetaInfoKind::AsyncBlock => {
                write!(fmt, "async block {name}")?;
            }
            MetaInfoKind::Const => {
                write!(fmt, "const {name}")?;
            }
            MetaInfoKind::ConstFn => {
                write!(fmt, "const fn {name}")?;
            }
            MetaInfoKind::Import => {
                write!(fmt, "import {name}")?;
            }
            MetaInfoKind::Alias => {
                write!(fmt, "import {name}")?;
            }
            MetaInfoKind::Module => {
                write!(fmt, "module {name}")?;
            }
            MetaInfoKind::Trait => {
                write!(fmt, "trait {name}")?;
            }
        }

        Ok(())
    }
}

#[derive(Debug, Clone, Copy)]
pub(crate) enum MetaInfoKind {
    Type,
    Struct,
    Variant,
    Enum,
    Macro,
    AttributeMacro,
    Function,
    Associated,
    Closure,
    AsyncBlock,
    Const,
    ConstFn,
    Import,
    Alias,
    Module,
    Trait,
}

impl MetaInfoKind {
    fn from_kind(value: &meta::Kind) -> Self {
        match value {
            meta::Kind::Type { .. } => MetaInfoKind::Type,
            meta::Kind::Struct { .. } => MetaInfoKind::Struct,
            meta::Kind::Variant { .. } => MetaInfoKind::Variant,
            meta::Kind::Enum { .. } => MetaInfoKind::Enum,
            meta::Kind::Macro { .. } => MetaInfoKind::Macro,
            meta::Kind::AttributeMacro { .. } => MetaInfoKind::AttributeMacro,
            meta::Kind::Function {
                associated: None, ..
            } => MetaInfoKind::Function,
            meta::Kind::Function {
                associated: Some(..),
                ..
            } => MetaInfoKind::Associated,
            meta::Kind::Closure { .. } => MetaInfoKind::Closure,
            meta::Kind::AsyncBlock { .. } => MetaInfoKind::AsyncBlock,
            meta::Kind::Const { .. } => MetaInfoKind::Const,
            meta::Kind::ConstFn { .. } => MetaInfoKind::ConstFn,
            meta::Kind::Import { .. } => MetaInfoKind::Import,
            meta::Kind::Alias { .. } => MetaInfoKind::Alias,
            meta::Kind::Module { .. } => MetaInfoKind::Module,
            meta::Kind::Trait { .. } => MetaInfoKind::Trait,
        }
    }
}