rune/compile/
meta_info.rs
1use core::fmt;
2
3use crate::alloc;
4use crate::alloc::prelude::*;
5use crate::compile::meta;
6use crate::{Hash, Item, ItemBuf};
7
8#[derive(Debug)]
10#[non_exhaustive]
11pub struct MetaInfo {
12 kind: MetaInfoKind,
14 hash: Hash,
16 item: Option<ItemBuf>,
18}
19
20impl MetaInfo {
21 pub(crate) fn new(kind: &meta::Kind, hash: Hash, item: Option<&Item>) -> alloc::Result<Self> {
23 Ok(Self {
24 kind: MetaInfoKind::from_kind(kind),
25 hash,
26 item: item.map(|item| item.try_to_owned()).transpose()?,
27 })
28 }
29}
30
31impl fmt::Display for MetaInfo {
32 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
33 struct Name<'a>(Hash, Option<&'a Item>);
34
35 impl fmt::Display for Name<'_> {
36 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37 if let Some(item) = self.1 {
38 item.fmt(f)
39 } else {
40 self.0.fmt(f)
41 }
42 }
43 }
44
45 let name = Name(self.hash, self.item.as_deref());
46
47 match self.kind {
48 MetaInfoKind::Type => {
49 write!(fmt, "type {name}")?;
50 }
51 MetaInfoKind::Struct => {
52 write!(fmt, "struct {name}")?;
53 }
54 MetaInfoKind::Variant => {
55 write!(fmt, "variant {name}")?;
56 }
57 MetaInfoKind::Enum => {
58 write!(fmt, "enum {name}")?;
59 }
60 MetaInfoKind::Macro => {
61 write!(fmt, "macro {name}")?;
62 }
63 MetaInfoKind::AttributeMacro => {
64 write!(fmt, "attribute macro {name}")?;
65 }
66 MetaInfoKind::Function => {
67 write!(fmt, "fn {name}")?;
68 }
69 MetaInfoKind::Associated => {
70 write!(fmt, "associated fn {name}")?;
71 }
72 MetaInfoKind::Closure => {
73 write!(fmt, "closure {name}")?;
74 }
75 MetaInfoKind::AsyncBlock => {
76 write!(fmt, "async block {name}")?;
77 }
78 MetaInfoKind::Const => {
79 write!(fmt, "const {name}")?;
80 }
81 MetaInfoKind::ConstFn => {
82 write!(fmt, "const fn {name}")?;
83 }
84 MetaInfoKind::Import => {
85 write!(fmt, "import {name}")?;
86 }
87 MetaInfoKind::Alias => {
88 write!(fmt, "import {name}")?;
89 }
90 MetaInfoKind::Module => {
91 write!(fmt, "module {name}")?;
92 }
93 MetaInfoKind::Trait => {
94 write!(fmt, "trait {name}")?;
95 }
96 }
97
98 Ok(())
99 }
100}
101
102#[derive(Debug, Clone, Copy)]
103pub(crate) enum MetaInfoKind {
104 Type,
105 Struct,
106 Variant,
107 Enum,
108 Macro,
109 AttributeMacro,
110 Function,
111 Associated,
112 Closure,
113 AsyncBlock,
114 Const,
115 ConstFn,
116 Import,
117 Alias,
118 Module,
119 Trait,
120}
121
122impl MetaInfoKind {
123 fn from_kind(value: &meta::Kind) -> Self {
124 match value {
125 meta::Kind::Type { .. } => MetaInfoKind::Type,
126 meta::Kind::Struct {
127 enum_hash: Hash::EMPTY,
128 ..
129 } => MetaInfoKind::Struct,
130 meta::Kind::Struct { .. } => MetaInfoKind::Variant,
131 meta::Kind::Enum { .. } => MetaInfoKind::Enum,
132 meta::Kind::Macro => MetaInfoKind::Macro,
133 meta::Kind::AttributeMacro => MetaInfoKind::AttributeMacro,
134 meta::Kind::Function {
135 associated: None, ..
136 } => MetaInfoKind::Function,
137 meta::Kind::Function {
138 associated: Some(..),
139 ..
140 } => MetaInfoKind::Associated,
141 meta::Kind::Closure { .. } => MetaInfoKind::Closure,
142 meta::Kind::AsyncBlock { .. } => MetaInfoKind::AsyncBlock,
143 meta::Kind::Const => MetaInfoKind::Const,
144 meta::Kind::ConstFn => MetaInfoKind::ConstFn,
145 meta::Kind::Import { .. } => MetaInfoKind::Import,
146 meta::Kind::Alias { .. } => MetaInfoKind::Alias,
147 meta::Kind::Module => MetaInfoKind::Module,
148 meta::Kind::Trait => MetaInfoKind::Trait,
149 }
150 }
151}