Skip to content

Commit c83b15b

Browse files
committed
feat: started working on context and a function registry
1 parent e9a7600 commit c83b15b

4 files changed

Lines changed: 97 additions & 0 deletions

File tree

src/context/mod.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/// A simple context that holds variable bindings in nested layers.
2+
/// Each layer is a map from String identifiers to Values.
3+
use std::collections::HashMap;
4+
use tucana::shared::{ReferenceValue, Value};
5+
6+
use crate::error::RuntimeError;
7+
8+
#[derive(Debug)]
9+
pub struct ContextReference {
10+
pub primary_level: i32,
11+
pub secondary_level: i32,
12+
pub tertiary_level: Option<i32>,
13+
}
14+
15+
#[derive(Debug)]
16+
pub struct Context {
17+
/// A stack of environments: layer 0 is the outermost.
18+
layers: HashMap<ContextReference, Result<Value, RuntimeError>>,
19+
}
20+
21+
impl Context {
22+
/// Create a new, empty context.
23+
pub fn new() -> Self {
24+
Context {
25+
layers: HashMap::new(),
26+
}
27+
}
28+
29+
/// Look up a name, searching from innermost outward.
30+
pub fn get(&self, reference: &ReferenceValue) -> Option<&Result<Value, RuntimeError>> {
31+
for (context, value) in self.layers.iter() {
32+
if context.primary_level != reference.primary_level {
33+
continue;
34+
}
35+
36+
if context.secondary_level != reference.secondary_level {
37+
continue;
38+
}
39+
40+
if context.tertiary_level != reference.tertiary_level {
41+
continue;
42+
}
43+
44+
return Some(value);
45+
}
46+
None
47+
}
48+
}

src/error/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use std::{
2+
error::Error,
3+
fmt::{Display, Formatter},
4+
};
5+
6+
#[derive(Debug)]
7+
pub struct RuntimeError {}
8+
9+
impl Error for RuntimeError {}
10+
11+
impl Display for RuntimeError {
12+
fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
13+
write!(f, "&self.function_name.as_str()")
14+
}
15+
}

src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
pub mod context;
2+
pub mod error;
3+
pub mod registry;
14
use code0_flow::flow_queue::service::{Message, RabbitmqClient};
25
use std::sync::Arc;
36

src/registry/mod.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use crate::{context::Context, error::RuntimeError};
2+
use std::collections::HashMap;
3+
use tucana::shared::Value;
4+
5+
pub type HandlerFn = fn(&[Value], &mut Context) -> Result<Value, RuntimeError>;
6+
7+
/// Holds all registered handlers.
8+
pub struct FunctionStore {
9+
functions: HashMap<String, HandlerFn>,
10+
}
11+
12+
impl FunctionStore {
13+
/// Create a new, empty store.
14+
pub fn new() -> Self {
15+
FunctionStore {
16+
functions: HashMap::new(),
17+
}
18+
}
19+
20+
/// Look up a handler by its ID.
21+
pub fn get(&self, id: &str) -> Option<&HandlerFn> {
22+
self.functions.get(id)
23+
}
24+
25+
/// Execute all the registration closures to populate the map.
26+
fn populate(&mut self, regs: Vec<(&'static str, HandlerFn)>) {
27+
for (id, func) in regs {
28+
self.functions.insert(id.to_string(), func);
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)