From 7d02b647c2c6668c511c728c287a4a22b13876f3 Mon Sep 17 00:00:00 2001 From: Amit Upadhyay Date: Thu, 21 Nov 2024 12:53:44 +0530 Subject: [PATCH] refactor --- fastn-builtins/src/lib.rs | 9 ++++---- fastn-resolved-to-js/src/element.rs | 34 ++++++++++++++--------------- fastn-resolved-to-js/src/lib.rs | 2 +- ftd/src/executor/test.rs | 2 +- ftd/src/interpreter/main.rs | 2 +- ftd/src/interpreter/test.rs | 2 +- ftd/src/js/mod.rs | 6 ++--- ftd/src/node/test.rs | 2 +- v0.5/fastn-compiler/src/compiler.rs | 2 +- v0.5/fastn-compiler/src/js.rs | 4 ++-- v0.5/fastn-compiler/src/tdoc.rs | 20 ++++++++++++----- 11 files changed, 46 insertions(+), 39 deletions(-) diff --git a/fastn-builtins/src/lib.rs b/fastn-builtins/src/lib.rs index d614f8cf3..37a8cb4e7 100644 --- a/fastn-builtins/src/lib.rs +++ b/fastn-builtins/src/lib.rs @@ -10913,12 +10913,11 @@ pub fn fastn_test_function() -> fastn_resolved::ComponentDefinition { } } -pub static DEFAULT_BAG: std::sync::LazyLock< - indexmap::IndexMap, -> = std::sync::LazyLock::new(default_bag); +static BUILTINS: std::sync::LazyLock> = + std::sync::LazyLock::new(default_bag); -pub fn get_default_bag() -> &'static indexmap::IndexMap { - &DEFAULT_BAG +pub fn builtins() -> &'static indexmap::IndexMap { + &BUILTINS } pub fn image_function() -> fastn_resolved::ComponentDefinition { diff --git a/fastn-resolved-to-js/src/element.rs b/fastn-resolved-to-js/src/element.rs index 7ba71bda7..0f9012215 100644 --- a/fastn-resolved-to-js/src/element.rs +++ b/fastn-resolved-to-js/src/element.rs @@ -154,7 +154,7 @@ pub struct CheckBox { impl CheckBox { pub fn from(component: &fastn_resolved::ComponentInvocation) -> CheckBox { - let component_definition = fastn_builtins::get_default_bag() + let component_definition = fastn_builtins::builtins() .get("ftd#checkbox") .unwrap() .clone() @@ -241,7 +241,7 @@ pub struct TextInput { impl TextInput { pub fn from(component: &fastn_resolved::ComponentInvocation) -> TextInput { - let component_definition = fastn_builtins::get_default_bag() + let component_definition = fastn_builtins::builtins() .get("ftd#text-input") .unwrap() .clone() @@ -400,7 +400,7 @@ pub struct Iframe { impl Iframe { pub fn from(component: &fastn_resolved::ComponentInvocation) -> Iframe { - let component_definition = fastn_builtins::get_default_bag() + let component_definition = fastn_builtins::builtins() .get("ftd#iframe") .unwrap() .clone() @@ -521,7 +521,7 @@ impl Code { component: &fastn_resolved::ComponentInvocation, _doc: &dyn fastn_resolved::tdoc::TDoc, ) -> Code { - let component_definition = fastn_builtins::get_default_bag() + let component_definition = fastn_builtins::builtins() .get("ftd#code") .unwrap() .clone() @@ -648,7 +648,7 @@ pub struct Image { impl Image { pub fn from(component: &fastn_resolved::ComponentInvocation) -> Image { - let component_definition = fastn_builtins::get_default_bag() + let component_definition = fastn_builtins::builtins() .get("ftd#image") .unwrap() .clone() @@ -760,7 +760,7 @@ pub struct Audio { impl Audio { pub fn from(component: &fastn_resolved::ComponentInvocation) -> Audio { - let component_definition = fastn_builtins::get_default_bag() + let component_definition = fastn_builtins::builtins() .get("ftd#audio") .unwrap() .clone() @@ -888,7 +888,7 @@ pub struct Video { impl Video { pub fn from(component: &fastn_resolved::ComponentInvocation) -> Video { - let component_definition = fastn_builtins::get_default_bag() + let component_definition = fastn_builtins::builtins() .get("ftd#video") .unwrap() .clone() @@ -1301,7 +1301,7 @@ impl InheritedProperties { impl Text { pub fn from(component: &fastn_resolved::ComponentInvocation) -> Text { - let component_definition = fastn_builtins::get_default_bag() + let component_definition = fastn_builtins::builtins() .get("ftd#text") .unwrap() .clone() @@ -1365,7 +1365,7 @@ impl Text { impl Integer { pub fn from(component: &fastn_resolved::ComponentInvocation) -> Integer { - let component_definition = fastn_builtins::get_default_bag() + let component_definition = fastn_builtins::builtins() .get("ftd#integer") .unwrap() .clone() @@ -1430,7 +1430,7 @@ impl Integer { impl Decimal { pub fn from(component: &fastn_resolved::ComponentInvocation) -> Decimal { - let component_definition = fastn_builtins::get_default_bag() + let component_definition = fastn_builtins::builtins() .get("ftd#decimal") .unwrap() .clone() @@ -1495,7 +1495,7 @@ impl Decimal { impl Boolean { pub fn from(component: &fastn_resolved::ComponentInvocation) -> Boolean { - let component_definition = fastn_builtins::get_default_bag() + let component_definition = fastn_builtins::builtins() .get("ftd#boolean") .unwrap() .clone() @@ -1560,7 +1560,7 @@ impl Boolean { impl Document { pub fn from(component: &fastn_resolved::ComponentInvocation) -> Document { - let component_definition = fastn_builtins::get_default_bag() + let component_definition = fastn_builtins::builtins() .get("ftd#document") .unwrap() .clone() @@ -1845,7 +1845,7 @@ impl DocumentMeta { impl Column { pub fn from(component: &fastn_resolved::ComponentInvocation) -> Column { - let component_definition = fastn_builtins::get_default_bag() + let component_definition = fastn_builtins::builtins() .get("ftd#column") .unwrap() .clone() @@ -1911,7 +1911,7 @@ impl Column { impl Row { pub fn from(component: &fastn_resolved::ComponentInvocation) -> Row { - let component_definition = fastn_builtins::get_default_bag() + let component_definition = fastn_builtins::builtins() .get("ftd#row") .unwrap() .clone() @@ -1977,7 +1977,7 @@ impl Row { impl ContainerElement { pub fn from(component: &fastn_resolved::ComponentInvocation) -> ContainerElement { - let component_definition = fastn_builtins::get_default_bag() + let component_definition = fastn_builtins::builtins() .get("ftd#container") .unwrap() .clone() @@ -2045,7 +2045,7 @@ pub struct Device { impl Device { pub fn from(component: &fastn_resolved::ComponentInvocation, device: &str) -> Device { - let component_definition = fastn_builtins::get_default_bag() + let component_definition = fastn_builtins::builtins() .get(device) .unwrap() .clone() @@ -2247,7 +2247,7 @@ pub struct Rive { impl Rive { pub fn from(component: &fastn_resolved::ComponentInvocation) -> Rive { - let component_definition = fastn_builtins::get_default_bag() + let component_definition = fastn_builtins::builtins() .get("ftd#rive") .unwrap() .clone() diff --git a/fastn-resolved-to-js/src/lib.rs b/fastn-resolved-to-js/src/lib.rs index 9d8c3fdf1..83f3a762e 100644 --- a/fastn-resolved-to-js/src/lib.rs +++ b/fastn-resolved-to-js/src/lib.rs @@ -292,7 +292,7 @@ pub fn default_bag_into_js_ast(doc: &dyn fastn_resolved::tdoc::TDoc) -> Vec InterpreterState { InterpreterState { id, - bag: ftd::interpreter::default::get_default_bag().clone(), + bag: ftd::interpreter::default::builtins().clone(), ..Default::default() } } diff --git a/ftd/src/interpreter/test.rs b/ftd/src/interpreter/test.rs index 3f3b57c13..832e7c53a 100644 --- a/ftd/src/interpreter/test.rs +++ b/ftd/src/interpreter/test.rs @@ -44,7 +44,7 @@ fn p( } } }; - for thing in ftd::interpreter::default::get_default_bag().keys() { + for thing in ftd::interpreter::default::builtins().keys() { i.data.swap_remove(thing); } let expected_json = serde_json::to_string_pretty(&i).unwrap(); diff --git a/ftd/src/js/mod.rs b/ftd/src/js/mod.rs index f0bcda135..52ed73641 100644 --- a/ftd/src/js/mod.rs +++ b/ftd/src/js/mod.rs @@ -17,14 +17,14 @@ pub fn all_js_without_test(package_name: &str) -> String { /// This returns asts of things present in `ftd` module or `default_bag` pub fn default_bag_into_js_ast() -> Vec { let mut ftd_asts = vec![]; - let bag = ftd::interpreter::default::get_default_bag(); + let bag = ftd::interpreter::default::builtins(); let doc = ftd::interpreter::TDoc { name: "", aliases: &ftd::interpreter::default::default_aliases(), bag: ftd::interpreter::BagOrState::Bag(bag), }; let mut export_asts = vec![]; - for thing in ftd::interpreter::default::get_default_bag().values() { + for thing in ftd::interpreter::default::builtins().values() { if let ftd::interpreter::Thing::Variable(v) = thing { ftd_asts.push(v.to_ast(&doc, None, &mut false)); } else if let ftd::interpreter::Thing::Function(f) = thing { @@ -95,7 +95,7 @@ pub fn document_into_js_ast(document: ftd::interpreter::Document) -> JSAstData { &doc, &mut has_rive_components, )]; - let default_thing_name = ftd::interpreter::default::get_default_bag() + let default_thing_name = ftd::interpreter::default::builtins() .into_iter() .map(|v| v.0) .collect_vec(); diff --git a/ftd/src/node/test.rs b/ftd/src/node/test.rs index f74dc1aa0..3da02df78 100644 --- a/ftd/src/node/test.rs +++ b/ftd/src/node/test.rs @@ -76,7 +76,7 @@ fn p(s: &str, t: &str, fix: bool, file_location: &std::path::PathBuf) { let executor = ftd::executor::ExecuteDoc::from_interpreter(doc).unwrap_or_else(|e| panic!("{:?}", e)); let mut node = ftd::node::NodeData::from_rt(executor); - for thing in ftd::interpreter::default::get_default_bag().keys() { + for thing in ftd::interpreter::default::builtins().keys() { node.bag.swap_remove(thing); } let expected_json = serde_json::to_string_pretty(&node).unwrap(); diff --git a/v0.5/fastn-compiler/src/compiler.rs b/v0.5/fastn-compiler/src/compiler.rs index c849ae418..ddea66c09 100644 --- a/v0.5/fastn-compiler/src/compiler.rs +++ b/v0.5/fastn-compiler/src/compiler.rs @@ -23,7 +23,7 @@ impl Compiler { fastn_unresolved::resolver::Input { bag: &self.bag, auto_imports: &self.auto_imports, - builtins: &fastn_builtins::DEFAULT_BAG, + builtins: fastn_builtins::builtins(), } } diff --git a/v0.5/fastn-compiler/src/js.rs b/v0.5/fastn-compiler/src/js.rs index 81437558d..ecf045e25 100644 --- a/v0.5/fastn-compiler/src/js.rs +++ b/v0.5/fastn-compiler/src/js.rs @@ -12,8 +12,8 @@ impl fastn_compiler::Compiler { let needed_symbols = self.needed_symbols(); let doc = fastn_compiler::TDoc { name: "", - bag: &needed_symbols, - default_bag: fastn_builtins::get_default_bag(), + definitions: &needed_symbols, + builtins: fastn_builtins::builtins(), }; // Check if document tree has rive. This is used to add rive script. diff --git a/v0.5/fastn-compiler/src/tdoc.rs b/v0.5/fastn-compiler/src/tdoc.rs index e14a0a2c2..88813559f 100644 --- a/v0.5/fastn-compiler/src/tdoc.rs +++ b/v0.5/fastn-compiler/src/tdoc.rs @@ -1,19 +1,27 @@ pub struct TDoc<'a> { pub name: &'a str, - pub bag: &'a indexmap::IndexMap, - pub default_bag: &'a indexmap::IndexMap, + pub definitions: &'a indexmap::IndexMap, + pub builtins: &'a indexmap::IndexMap, +} + +impl TDoc<'_> { + fn get(&self, name: &str) -> Option<&fastn_resolved::Definition> { + self.definitions + .get(name) + .or_else(|| self.builtins.get(name)) + } } impl<'a> fastn_resolved::tdoc::TDoc for TDoc<'a> { fn get_opt_function(&self, name: &str) -> Option { - match self.bag.get(name).or_else(|| self.default_bag.get(name)) { + match self.get(name) { Some(fastn_resolved::Definition::Function(f)) => Some(f.clone()), _ => None, } } fn get_opt_record(&self, name: &str) -> Option { - match self.bag.get(name).or_else(|| self.default_bag.get(name)) { + match self.get(name) { Some(fastn_resolved::Definition::Record(f)) => Some(f.clone()), _ => None, } @@ -24,14 +32,14 @@ impl<'a> fastn_resolved::tdoc::TDoc for TDoc<'a> { } fn get_opt_component(&self, name: &str) -> Option { - match self.bag.get(name).or_else(|| self.default_bag.get(name)) { + match self.get(name) { Some(fastn_resolved::Definition::Component(f)) => Some(f.clone()), _ => None, } } fn get_opt_web_component(&self, name: &str) -> Option { - match self.bag.get(name).or_else(|| self.default_bag.get(name)) { + match self.get(name) { Some(fastn_resolved::Definition::WebComponent(f)) => Some(f.clone()), _ => None, }