1use crate::ast::Program;
2use crate::error::{RuneError, Severity};
3use crate::span::Span;
4use crate::symbol::{SymbolId, SymbolTable};
5use crate::value::Value;
6use std::any::Any;
7use std::collections::BTreeMap;
8
9pub type VerbResult = Result<Value, RuneError>;
11
12pub trait Verb: Send + Sync {
18 fn name(&self) -> &str;
20
21 fn call(&self, args: &[Value], ctx: &mut EvalContext) -> VerbResult;
23
24 fn signature(&self) -> Option<VerbSignature> { None }
26}
27
28#[derive(Debug, Clone)]
30pub struct VerbSignature {
31 pub positional: Vec<ArgInfo>,
32 pub named: Vec<ArgInfo>,
33 pub returns: Option<String>,
34}
35
36#[derive(Debug, Clone)]
38pub struct ArgInfo {
39 pub name: String,
40 pub expected_type: Option<String>,
41 pub required: bool,
42}
43
44pub trait Module: Send + Sync {
50 fn name(&self) -> &str;
52
53 fn version(&self) -> ModuleVersion;
55
56 fn verbs(&self) -> &[Box<dyn Verb>];
58
59 fn constructs(&self) -> &[Box<dyn Construct>] { &[] }
62}
63
64#[derive(Debug, Clone, Copy, PartialEq, Eq)]
66pub struct ModuleVersion {
67 pub major: u16,
68 pub minor: u16,
69 pub patch: u16,
70}
71
72impl ModuleVersion {
73 pub fn new(major: u16, minor: u16, patch: u16) -> Self {
74 Self { major, minor, patch }
75 }
76}
77
78impl std::fmt::Display for ModuleVersion {
79 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80 write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
81 }
82}
83
84pub trait Construct: Send + Sync {
100 fn name(&self) -> &str;
102
103 fn enter(
106 &self,
107 args: &[Value],
108 ctx: &mut EvalContext,
109 ) -> VerbResult;
110
111 fn exit(
116 &self,
117 enter_value: Value,
118 body_value: Value,
119 ctx: &mut EvalContext,
120 ) -> VerbResult;
121}
122
123#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
131#[repr(u8)]
132pub enum LogLevel {
133 Emergency = 0,
134 Alert = 1,
135 Critical = 2,
136 Error = 3,
137 Warning = 4,
138 Notice = 5,
139 Info = 6,
140 Debug = 7,
141}
142
143impl LogLevel {
144 pub fn from_symbol_name(name: &str) -> Option<Self> {
146 match name {
147 "emergency" => Some(Self::Emergency),
148 "alert" => Some(Self::Alert),
149 "critical" => Some(Self::Critical),
150 "error" => Some(Self::Error),
151 "warning" => Some(Self::Warning),
152 "notice" => Some(Self::Notice),
153 "info" => Some(Self::Info),
154 "debug" => Some(Self::Debug),
155 _ => None,
156 }
157 }
158
159 pub fn as_symbol_name(&self) -> &'static str {
161 match self {
162 Self::Emergency => "emergency",
163 Self::Alert => "alert",
164 Self::Critical => "critical",
165 Self::Error => "error",
166 Self::Warning => "warning",
167 Self::Notice => "notice",
168 Self::Info => "info",
169 Self::Debug => "debug",
170 }
171 }
172}
173
174pub struct LogContext<'a> {
177 pub module: &'a str,
178 pub namespace: &'a str,
179 pub span: Span,
180}
181
182pub trait Logger: Send + Sync {
184 fn log(&self, level: LogLevel, message: &str, context: &LogContext);
185}
186
187#[derive(Debug, Clone, Copy, PartialEq, Eq)]
193pub enum ConstraintPhase {
194 Load,
196 Eval,
198 Both,
200}
201
202pub trait Constraint: Send + Sync {
204 fn code(&self) -> &str;
206
207 fn phase(&self) -> ConstraintPhase;
209
210 fn validate_load(
212 &self,
213 _metadata: &ProgramMetadata,
214 _program: &Program,
215 _ctx: &ConstraintContext,
216 ) -> Result<(), ConstraintViolation> {
217 Ok(())
218 }
219
220 fn validate_eval(
222 &self,
223 _event: &ConstraintEvent,
224 _ctx: &ConstraintContext,
225 ) -> Result<(), ConstraintViolation> {
226 Ok(())
227 }
228}
229
230#[derive(Debug, Clone)]
232pub struct ProgramMetadata {
233 pub name: String,
234 pub version: Option<i64>,
235 pub namespace: Option<String>,
236 pub requires: Vec<RequireEntry>,
237 pub custom: BTreeMap<String, Value>,
238}
239
240#[derive(Debug, Clone)]
242pub struct RequireEntry {
243 pub module: String,
244 pub version_constraint: Option<String>,
245}
246
247pub struct ConstraintContext<'a> {
249 pub catalog: &'a dyn ErrorCatalog,
251}
252
253#[derive(Debug, Clone)]
255pub struct ConstraintEvent {
256 pub kind: String,
257 pub span: Span,
258 pub data: BTreeMap<String, Value>,
259}
260
261#[derive(Debug, Clone)]
263pub struct ConstraintViolation {
264 pub code: String,
265 pub severity: Severity,
266 pub message: String,
267 pub span: Option<Span>,
268 pub suggestion: Option<String>,
269}
270
271pub trait ErrorCatalog: Send + Sync {
277 fn entries(&self) -> &[ErrorEntry];
279
280 fn get(&self, code: &str) -> Option<&ErrorEntry>;
282
283 fn render(&self, code: &str, params: &[(&str, &str)]) -> String;
285}
286
287#[derive(Debug, Clone)]
289pub struct ErrorEntry {
290 pub code: String,
291 pub prefix: String,
292 pub title: String,
293 pub template: String,
294 pub severity: Severity,
295 pub reference: Option<String>,
296}
297
298pub trait FoundationClass: Send + Sync {
304 fn name(&self) -> &str;
306
307 fn fields(&self) -> &[FoundationField];
309
310 fn instantiate(&self, fields: &BTreeMap<SymbolId, Value>) -> Result<Value, RuneError>;
312
313 fn methods(&self) -> &[Box<dyn Verb>];
315}
316
317#[derive(Debug, Clone)]
319pub struct FoundationField {
320 pub name: String,
321 pub type_hint: String,
322 pub required: bool,
323 pub default: Option<Value>,
324}
325
326pub struct EvalContext<'a> {
333 pub host: &'a mut dyn Any,
334 pub span: Span,
335 pub symbols: &'a SymbolTable,
336 named_args: Option<BTreeMap<SymbolId, Value>>,
337}
338
339impl<'a> EvalContext<'a> {
340 pub fn new(host: &'a mut dyn Any, span: Span, symbols: &'a SymbolTable) -> Self {
341 Self { host, span, symbols, named_args: None }
342 }
343
344 pub fn host_ref<T: 'static>(&self) -> Result<&T, RuneError> {
346 self.host.downcast_ref::<T>().ok_or_else(|| {
347 RuneError::type_error("Host state type mismatch", Some(self.span))
348 })
349 }
350
351 pub fn host_mut<T: 'static>(&mut self) -> Result<&mut T, RuneError> {
353 let span = self.span;
354 self.host.downcast_mut::<T>().ok_or_else(|| {
355 RuneError::type_error("Host state type mismatch", Some(span))
356 })
357 }
358
359 pub fn set_named_args(&mut self, args: BTreeMap<SymbolId, Value>) {
360 self.named_args = Some(args);
361 }
362
363 pub fn named_args(&self) -> Option<&BTreeMap<SymbolId, Value>> {
364 self.named_args.as_ref()
365 }
366}