Skip to main content

harn_vm/vm/
builtin.rs

1use std::borrow::Cow;
2
3/// Runtime kind for a registered VM builtin.
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub enum VmBuiltinKind {
6    Sync,
7    Async,
8}
9
10/// Lightweight arity metadata for registered builtins.
11///
12/// This is descriptive metadata only. Runtime behavior still flows through
13/// the existing parser/typechecker and builtin handlers.
14#[derive(Clone, Copy, Debug, PartialEq, Eq)]
15pub enum VmBuiltinArity {
16    Exact(usize),
17    Min(usize),
18    Range { min: usize, max: usize },
19    Variadic,
20}
21
22/// Discoverable metadata for a VM builtin.
23#[derive(Clone, Debug, PartialEq, Eq)]
24pub struct VmBuiltinMetadata {
25    name: Cow<'static, str>,
26    kind: VmBuiltinKind,
27    signature: Option<Cow<'static, str>>,
28    arity: Option<VmBuiltinArity>,
29    category: Option<Cow<'static, str>>,
30    doc: Option<Cow<'static, str>>,
31}
32
33impl VmBuiltinMetadata {
34    pub fn sync(name: impl Into<Cow<'static, str>>) -> Self {
35        Self::new(name, VmBuiltinKind::Sync)
36    }
37
38    pub fn async_builtin(name: impl Into<Cow<'static, str>>) -> Self {
39        Self::new(name, VmBuiltinKind::Async)
40    }
41
42    pub const fn sync_static(name: &'static str) -> Self {
43        Self::new_static(name, VmBuiltinKind::Sync)
44    }
45
46    pub const fn async_static(name: &'static str) -> Self {
47        Self::new_static(name, VmBuiltinKind::Async)
48    }
49
50    fn new(name: impl Into<Cow<'static, str>>, kind: VmBuiltinKind) -> Self {
51        Self {
52            name: name.into(),
53            kind,
54            signature: None,
55            arity: None,
56            category: None,
57            doc: None,
58        }
59    }
60
61    const fn new_static(name: &'static str, kind: VmBuiltinKind) -> Self {
62        Self {
63            name: Cow::Borrowed(name),
64            kind,
65            signature: None,
66            arity: None,
67            category: None,
68            doc: None,
69        }
70    }
71
72    pub(crate) fn with_kind(mut self, kind: VmBuiltinKind) -> Self {
73        self.kind = kind;
74        self
75    }
76
77    pub fn signature_static(mut self, signature: &'static str) -> Self {
78        self.signature = Some(Cow::Borrowed(signature));
79        self
80    }
81
82    pub fn signature_owned(mut self, signature: impl Into<Cow<'static, str>>) -> Self {
83        self.signature = Some(signature.into());
84        self
85    }
86
87    pub fn arity(mut self, arity: VmBuiltinArity) -> Self {
88        self.arity = Some(arity);
89        self
90    }
91
92    pub fn category_static(mut self, category: &'static str) -> Self {
93        self.category = Some(Cow::Borrowed(category));
94        self
95    }
96
97    pub fn category_owned(mut self, category: impl Into<Cow<'static, str>>) -> Self {
98        self.category = Some(category.into());
99        self
100    }
101
102    pub fn doc_static(mut self, doc: &'static str) -> Self {
103        self.doc = Some(Cow::Borrowed(doc));
104        self
105    }
106
107    pub fn doc_owned(mut self, doc: impl Into<Cow<'static, str>>) -> Self {
108        self.doc = Some(doc.into());
109        self
110    }
111
112    pub fn name(&self) -> &str {
113        self.name.as_ref()
114    }
115
116    pub fn kind(&self) -> VmBuiltinKind {
117        self.kind
118    }
119
120    pub fn signature(&self) -> Option<&str> {
121        self.signature.as_deref()
122    }
123
124    pub fn arity_metadata(&self) -> Option<VmBuiltinArity> {
125        self.arity
126    }
127
128    pub fn category(&self) -> Option<&str> {
129        self.category.as_deref()
130    }
131
132    pub fn doc(&self) -> Option<&str> {
133        self.doc.as_deref()
134    }
135}