Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
amitu committed Nov 21, 2024
1 parent 93b0ec4 commit 7d02b64
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 39 deletions.
9 changes: 4 additions & 5 deletions fastn-builtins/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10913,12 +10913,11 @@ pub fn fastn_test_function() -> fastn_resolved::ComponentDefinition {
}
}

pub static DEFAULT_BAG: std::sync::LazyLock<
indexmap::IndexMap<String, fastn_resolved::Definition>,
> = std::sync::LazyLock::new(default_bag);
static BUILTINS: std::sync::LazyLock<indexmap::IndexMap<String, fastn_resolved::Definition>> =
std::sync::LazyLock::new(default_bag);

pub fn get_default_bag() -> &'static indexmap::IndexMap<String, fastn_resolved::Definition> {
&DEFAULT_BAG
pub fn builtins() -> &'static indexmap::IndexMap<String, fastn_resolved::Definition> {
&BUILTINS
}

pub fn image_function() -> fastn_resolved::ComponentDefinition {
Expand Down
34 changes: 17 additions & 17 deletions fastn-resolved-to-js/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion fastn-resolved-to-js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ pub fn default_bag_into_js_ast(doc: &dyn fastn_resolved::tdoc::TDoc) -> Vec<fast
let mut ftd_asts = vec![];

let mut export_asts = vec![];
for thing in fastn_builtins::get_default_bag().values() {
for thing in fastn_builtins::builtins().values() {
if let fastn_resolved::Definition::Variable(v) = thing {
ftd_asts.push(v.to_ast(doc, None, &mut false));
} else if let fastn_resolved::Definition::Function(f) = thing {
Expand Down
2 changes: 1 addition & 1 deletion ftd/src/executor/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn p(s: &str, t: &str, fix: bool, file_location: &std::path::PathBuf) {
let doc = interpret_helper("foo", s).unwrap_or_else(|e| panic!("{:?}", e));
let mut executor =
ftd::executor::ExecuteDoc::from_interpreter(doc).unwrap_or_else(|e| panic!("{:?}", e));
for thing in ftd::interpreter::default::get_default_bag().keys() {
for thing in ftd::interpreter::default::builtins().keys() {
executor.bag.swap_remove(thing);
}
let expected_json = serde_json::to_string_pretty(&executor).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion ftd/src/interpreter/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl InterpreterState {
fn new(id: String) -> InterpreterState {
InterpreterState {
id,
bag: ftd::interpreter::default::get_default_bag().clone(),
bag: ftd::interpreter::default::builtins().clone(),
..Default::default()
}
}
Expand Down
2 changes: 1 addition & 1 deletion ftd/src/interpreter/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions ftd/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<fastn_js::Ast> {
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 {
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion ftd/src/node/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion v0.5/fastn-compiler/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}

Expand Down
4 changes: 2 additions & 2 deletions v0.5/fastn-compiler/src/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 14 additions & 6 deletions v0.5/fastn-compiler/src/tdoc.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
pub struct TDoc<'a> {
pub name: &'a str,
pub bag: &'a indexmap::IndexMap<String, fastn_resolved::Definition>,
pub default_bag: &'a indexmap::IndexMap<String, fastn_resolved::Definition>,
pub definitions: &'a indexmap::IndexMap<String, fastn_resolved::Definition>,
pub builtins: &'a indexmap::IndexMap<String, fastn_resolved::Definition>,
}

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<fastn_resolved::Function> {
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<fastn_resolved::Record> {
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,
}
Expand All @@ -24,14 +32,14 @@ impl<'a> fastn_resolved::tdoc::TDoc for TDoc<'a> {
}

fn get_opt_component(&self, name: &str) -> Option<fastn_resolved::ComponentDefinition> {
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<fastn_resolved::WebComponentDefinition> {
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,
}
Expand Down

0 comments on commit 7d02b64

Please sign in to comment.