cairo_lang_defs/
plugin.rs1use std::any::Any;
2use std::ops::Deref;
3use std::sync::Arc;
4
5use cairo_lang_diagnostics::Severity;
6use cairo_lang_filesystem::cfg::CfgSet;
7use cairo_lang_filesystem::db::Edition;
8use cairo_lang_filesystem::ids::CodeMapping;
9use cairo_lang_syntax::node::ast;
10use cairo_lang_syntax::node::db::SyntaxGroup;
11use cairo_lang_syntax::node::ids::SyntaxStablePtrId;
12use cairo_lang_utils::ordered_hash_set::OrderedHashSet;
13use smol_str::SmolStr;
14
15pub trait GeneratedFileAuxData: std::fmt::Debug + Sync + Send {
17 fn as_any(&self) -> &dyn Any;
18 fn eq(&self, other: &dyn GeneratedFileAuxData) -> bool;
19}
20
21#[derive(Clone, Debug)]
22pub struct DynGeneratedFileAuxData(pub Arc<dyn GeneratedFileAuxData>);
23impl DynGeneratedFileAuxData {
24 pub fn new<T: GeneratedFileAuxData + 'static>(aux_data: T) -> Self {
25 DynGeneratedFileAuxData(Arc::new(aux_data))
26 }
27}
28impl Deref for DynGeneratedFileAuxData {
29 type Target = Arc<dyn GeneratedFileAuxData>;
30
31 fn deref(&self) -> &Self::Target {
32 &self.0
33 }
34}
35impl PartialEq for DynGeneratedFileAuxData {
36 fn eq(&self, that: &DynGeneratedFileAuxData) -> bool {
37 GeneratedFileAuxData::eq(&*self.0, &*that.0)
38 }
39}
40impl Eq for DynGeneratedFileAuxData {}
41
42pub struct PluginGeneratedFile {
44 pub name: SmolStr,
46 pub content: String,
48 pub code_mappings: Vec<CodeMapping>,
51 pub aux_data: Option<DynGeneratedFileAuxData>,
53 pub diagnostics_note: Option<String>,
57}
58
59#[derive(Default)]
61pub struct PluginResult {
62 pub code: Option<PluginGeneratedFile>,
64 pub diagnostics: Vec<PluginDiagnostic>,
66 pub remove_original_item: bool,
68}
69
70#[derive(Clone, Debug, Eq, Hash, PartialEq)]
71pub struct PluginDiagnostic {
72 pub stable_ptr: SyntaxStablePtrId,
73 pub message: String,
74 pub severity: Severity,
75}
76impl PluginDiagnostic {
77 pub fn error(stable_ptr: impl Into<SyntaxStablePtrId>, message: String) -> PluginDiagnostic {
78 PluginDiagnostic { stable_ptr: stable_ptr.into(), message, severity: Severity::Error }
79 }
80 pub fn warning(stable_ptr: impl Into<SyntaxStablePtrId>, message: String) -> PluginDiagnostic {
81 PluginDiagnostic { stable_ptr: stable_ptr.into(), message, severity: Severity::Warning }
82 }
83}
84
85pub struct MacroPluginMetadata<'a> {
88 pub cfg_set: &'a CfgSet,
90 pub declared_derives: &'a OrderedHashSet<String>,
92 pub allowed_features: &'a OrderedHashSet<SmolStr>,
94 pub edition: Edition,
96}
97
98pub trait MacroPlugin: std::fmt::Debug + Sync + Send {
101 fn generate_code(
105 &self,
106 db: &dyn SyntaxGroup,
107 item_ast: ast::ModuleItem,
108 metadata: &MacroPluginMetadata<'_>,
109 ) -> PluginResult;
110
111 fn declared_attributes(&self) -> Vec<String>;
117
118 fn declared_derives(&self) -> Vec<String> {
124 Vec::new()
125 }
126
127 fn executable_attributes(&self) -> Vec<String> {
133 Vec::new()
134 }
135
136 fn phantom_type_attributes(&self) -> Vec<String> {
140 Vec::new()
141 }
142}
143
144#[derive(Default)]
146pub struct InlinePluginResult {
147 pub code: Option<PluginGeneratedFile>,
148 pub diagnostics: Vec<PluginDiagnostic>,
150}
151
152pub trait InlineMacroExprPlugin: std::fmt::Debug + Sync + Send {
153 fn generate_code(
157 &self,
158 db: &dyn SyntaxGroup,
159 item_ast: &ast::ExprInlineMacro,
160 metadata: &MacroPluginMetadata<'_>,
161 ) -> InlinePluginResult;
162
163 fn documentation(&self) -> Option<String> {
165 None
166 }
167}
168
169pub trait NamedPlugin: Default + 'static {
171 const NAME: &'static str;
172}