File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ pub mod context;
2+ pub mod error;
3+ pub mod registry;
14use code0_flow:: flow_queue:: service:: { Message , RabbitmqClient } ;
25use std:: sync:: Arc ;
36
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments