diff --git a/ftd/src/executor/test.rs b/ftd/src/executor/test.rs index e87208f31c..461bd33e40 100644 --- a/ftd/src/executor/test.rs +++ b/ftd/src/executor/test.rs @@ -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::default_bag().keys() { + for thing in ftd::interpreter::default::get_default_bag().keys() { executor.bag.remove(thing); } let expected_json = serde_json::to_string_pretty(&executor).unwrap(); diff --git a/ftd/src/interpreter/main.rs b/ftd/src/interpreter/main.rs index ae34cca2c1..c3664a3d1e 100644 --- a/ftd/src/interpreter/main.rs +++ b/ftd/src/interpreter/main.rs @@ -78,7 +78,7 @@ impl InterpreterState { fn new(id: String) -> InterpreterState { InterpreterState { id, - bag: ftd::interpreter::default::default_bag(), + bag: ftd::interpreter::default::get_default_bag().clone(), ..Default::default() } } diff --git a/ftd/src/interpreter/test.rs b/ftd/src/interpreter/test.rs index 1f0de8e030..85ba03bf35 100644 --- a/ftd/src/interpreter/test.rs +++ b/ftd/src/interpreter/test.rs @@ -73,7 +73,7 @@ pub fn interpret_helper( #[track_caller] fn p(s: &str, t: &str, fix: bool, file_location: &std::path::PathBuf) { let mut i = interpret_helper("foo", s).unwrap_or_else(|e| panic!("{:?}", e)); - for thing in ftd::interpreter::default::default_bag().keys() { + for thing in ftd::interpreter::default::get_default_bag().keys() { i.data.remove(thing); } let expected_json = serde_json::to_string_pretty(&i).unwrap(); diff --git a/ftd/src/interpreter/things/default.rs b/ftd/src/interpreter/things/default.rs index 240b4ceed0..cf8985a068 100644 --- a/ftd/src/interpreter/things/default.rs +++ b/ftd/src/interpreter/things/default.rs @@ -7597,7 +7597,7 @@ pub fn default_bag() -> indexmap::IndexMap { value: ftd::interpreter::Value::Record { name: ftd::interpreter::FTD_COLOR.to_string(), fields: std::iter::IntoIterator::into_iter([( - "light".to_string(), + "light".to_string().to_string(), ftd::interpreter::PropertyValue::Value { value: ftd::interpreter::Value::String { @@ -7879,7 +7879,7 @@ pub fn default_bag() -> indexmap::IndexMap { line_number: 0, }, ), ( - "dark".to_string(), + "dark".to_string().to_string(), ftd::interpreter::PropertyValue::Value { value: ftd::interpreter::Value::String { text: "#65b693".to_string() @@ -9784,75 +9784,7 @@ pub fn default_bag() -> indexmap::IndexMap { line_number: 0, }), ), - // mask-position - ( - ftd::interpreter::FTD_MASK_MULTI_DATA.to_string(), - ftd::interpreter::Thing::Record(ftd::interpreter::Record { - name: ftd::interpreter::FTD_MASK_MULTI_DATA.to_string(), - fields: std::iter::IntoIterator::into_iter([ - ftd::interpreter::Field { - name: "image".to_string(), - kind: ftd::interpreter::Kind::record(ftd::interpreter::FTD_MASK_IMAGE_DATA) - .into_kind_data(), - mutable: false, - value: None, - access_modifier: Default::default(), - line_number: 0, - }, - ftd::interpreter::Field { - name: "size".to_string(), - kind: ftd::interpreter::Kind::or_type(ftd::interpreter::FTD_MASK_SIZE) - .into_kind_data() - .into_optional(), - mutable: false, - value: None, - access_modifier: Default::default(), - line_number: 0, - }, - ftd::interpreter::Field { - name: "size-x".to_string(), - kind: ftd::interpreter::Kind::or_type(ftd::interpreter::FTD_MASK_SIZE) - .into_kind_data() - .into_optional(), - mutable: false, - value: None, - access_modifier: Default::default(), - line_number: 0, - }, - ftd::interpreter::Field { - name: "size-y".to_string(), - kind: ftd::interpreter::Kind::or_type(ftd::interpreter::FTD_MASK_SIZE) - .into_kind_data() - .into_optional(), - mutable: false, - value: None, - access_modifier: Default::default(), - line_number: 0, - }, - ftd::interpreter::Field { - name: "repeat".to_string(), - kind: ftd::interpreter::Kind::or_type(ftd::interpreter::FTD_MASK_REPEAT) - .into_kind_data() - .into_optional(), - mutable: false, - value: None, - access_modifier: Default::default(), - line_number: 0, - }, - ftd::interpreter::Field { - name: "position".to_string(), - kind: ftd::interpreter::Kind::or_type(ftd::interpreter::FTD_MASK_POSITION) - .into_kind_data() - .into_optional(), - mutable: false, - value: None, - access_modifier: Default::default(), - line_number: 0, - }, - ]).collect(), - line_number: 0, - }), - ), + ( ftd::interpreter::FTD_MASK_REPEAT.to_string(), ftd::interpreter::Thing::OrType(ftd::interpreter::OrType { @@ -10075,6 +10007,74 @@ pub fn default_bag() -> indexmap::IndexMap { line_number: 0, }), ), + ( + ftd::interpreter::FTD_MASK_MULTI_DATA.to_string(), + ftd::interpreter::Thing::Record(ftd::interpreter::Record { + name: ftd::interpreter::FTD_MASK_MULTI_DATA.to_string(), + fields: std::iter::IntoIterator::into_iter([ + ftd::interpreter::Field { + name: "image".to_string(), + kind: ftd::interpreter::Kind::record(ftd::interpreter::FTD_MASK_IMAGE_DATA) + .into_kind_data(), + mutable: false, + value: None, + access_modifier: Default::default(), + line_number: 0, + }, + ftd::interpreter::Field { + name: "size".to_string(), + kind: ftd::interpreter::Kind::or_type(ftd::interpreter::FTD_MASK_SIZE) + .into_kind_data() + .into_optional(), + mutable: false, + value: None, + access_modifier: Default::default(), + line_number: 0, + }, + ftd::interpreter::Field { + name: "size-x".to_string(), + kind: ftd::interpreter::Kind::or_type(ftd::interpreter::FTD_MASK_SIZE) + .into_kind_data() + .into_optional(), + mutable: false, + value: None, + access_modifier: Default::default(), + line_number: 0, + }, + ftd::interpreter::Field { + name: "size-y".to_string(), + kind: ftd::interpreter::Kind::or_type(ftd::interpreter::FTD_MASK_SIZE) + .into_kind_data() + .into_optional(), + mutable: false, + value: None, + access_modifier: Default::default(), + line_number: 0, + }, + ftd::interpreter::Field { + name: "repeat".to_string(), + kind: ftd::interpreter::Kind::or_type(ftd::interpreter::FTD_MASK_REPEAT) + .into_kind_data() + .into_optional(), + mutable: false, + value: None, + access_modifier: Default::default(), + line_number: 0, + }, + ftd::interpreter::Field { + name: "position".to_string(), + kind: ftd::interpreter::Kind::or_type(ftd::interpreter::FTD_MASK_POSITION) + .into_kind_data() + .into_optional(), + mutable: false, + value: None, + access_modifier: Default::default(), + line_number: 0, + }, + ]).collect(), + line_number: 0, + }), + ), ( ftd::interpreter::FTD_MASK.to_string(), ftd::interpreter::Thing::OrType(ftd::interpreter::OrType { @@ -10096,7 +10096,6 @@ pub fn default_bag() -> indexmap::IndexMap { None, 0, )), - // todo: mode, position, composite, etc. ], line_number: 0, }), @@ -10105,6 +10104,14 @@ pub fn default_bag() -> indexmap::IndexMap { .collect() } +pub static DEFAULT_BAG: once_cell::sync::OnceCell< + indexmap::IndexMap, +> = once_cell::sync::OnceCell::new(); + +pub fn get_default_bag() -> &'static indexmap::IndexMap { + DEFAULT_BAG.get_or_init(ftd::interpreter::things::default::default_bag) +} + pub fn image_function() -> ftd::interpreter::ComponentDefinition { ftd::interpreter::ComponentDefinition { name: "ftd#image".to_string(), diff --git a/ftd/src/js/element.rs b/ftd/src/js/element.rs index cdccca909e..fbd3b0d535 100644 --- a/ftd/src/js/element.rs +++ b/ftd/src/js/element.rs @@ -147,7 +147,7 @@ pub struct CheckBox { impl CheckBox { pub fn from(component: &ftd::interpreter::Component) -> CheckBox { - let component_definition = ftd::interpreter::default::default_bag() + let component_definition = ftd::interpreter::default::get_default_bag() .get("ftd#checkbox") .unwrap() .clone() @@ -233,7 +233,7 @@ pub struct TextInput { impl TextInput { pub fn from(component: &ftd::interpreter::Component) -> TextInput { - let component_definition = ftd::interpreter::default::default_bag() + let component_definition = ftd::interpreter::default::get_default_bag() .get("ftd#text-input") .unwrap() .clone() @@ -377,7 +377,7 @@ pub struct Iframe { impl Iframe { pub fn from(component: &ftd::interpreter::Component) -> Iframe { - let component_definition = ftd::interpreter::default::default_bag() + let component_definition = ftd::interpreter::default::get_default_bag() .get("ftd#iframe") .unwrap() .clone() @@ -495,7 +495,7 @@ pub struct Code { impl Code { pub fn from(component: &ftd::interpreter::Component, _doc: &ftd::interpreter::TDoc) -> Code { - let component_definition = ftd::interpreter::default::default_bag() + let component_definition = ftd::interpreter::default::get_default_bag() .get("ftd#code") .unwrap() .clone() @@ -619,7 +619,7 @@ pub struct Image { impl Image { pub fn from(component: &ftd::interpreter::Component) -> Image { - let component_definition = ftd::interpreter::default::default_bag() + let component_definition = ftd::interpreter::default::get_default_bag() .get("ftd#image") .unwrap() .clone() @@ -718,7 +718,7 @@ pub struct Video { impl Video { pub fn from(component: &ftd::interpreter::Component) -> Video { - let component_definition = ftd::interpreter::default::default_bag() + let component_definition = ftd::interpreter::default::get_default_bag() .get("ftd#video") .unwrap() .clone() @@ -1124,7 +1124,7 @@ impl InheritedProperties { impl Text { pub fn from(component: &ftd::interpreter::Component) -> Text { - let component_definition = ftd::interpreter::default::default_bag() + let component_definition = ftd::interpreter::default::get_default_bag() .get("ftd#text") .unwrap() .clone() @@ -1190,7 +1190,7 @@ impl Text { impl Integer { pub fn from(component: &ftd::interpreter::Component) -> Integer { - let component_definition = ftd::interpreter::default::default_bag() + let component_definition = ftd::interpreter::default::get_default_bag() .get("ftd#integer") .unwrap() .clone() @@ -1255,7 +1255,7 @@ impl Integer { impl Decimal { pub fn from(component: &ftd::interpreter::Component) -> Decimal { - let component_definition = ftd::interpreter::default::default_bag() + let component_definition = ftd::interpreter::default::get_default_bag() .get("ftd#decimal") .unwrap() .clone() @@ -1320,7 +1320,7 @@ impl Decimal { impl Boolean { pub fn from(component: &ftd::interpreter::Component) -> Boolean { - let component_definition = ftd::interpreter::default::default_bag() + let component_definition = ftd::interpreter::default::get_default_bag() .get("ftd#boolean") .unwrap() .clone() @@ -1385,7 +1385,7 @@ impl Boolean { impl Document { pub fn from(component: &ftd::interpreter::Component) -> Document { - let component_definition = ftd::interpreter::default::default_bag() + let component_definition = ftd::interpreter::default::get_default_bag() .get("ftd#document") .unwrap() .clone() @@ -1636,7 +1636,7 @@ impl DocumentMeta { impl Column { pub fn from(component: &ftd::interpreter::Component) -> Column { - let component_definition = ftd::interpreter::default::default_bag() + let component_definition = ftd::interpreter::default::get_default_bag() .get("ftd#column") .unwrap() .clone() @@ -1702,7 +1702,7 @@ impl Column { impl Row { pub fn from(component: &ftd::interpreter::Component) -> Row { - let component_definition = ftd::interpreter::default::default_bag() + let component_definition = ftd::interpreter::default::get_default_bag() .get("ftd#row") .unwrap() .clone() @@ -1768,7 +1768,7 @@ impl Row { impl ContainerElement { pub fn from(component: &ftd::interpreter::Component) -> ContainerElement { - let component_definition = ftd::interpreter::default::default_bag() + let component_definition = ftd::interpreter::default::get_default_bag() .get("ftd#container") .unwrap() .clone() @@ -1836,7 +1836,7 @@ pub struct Device { impl Device { pub fn from(component: &ftd::interpreter::Component, device: &str) -> Device { - let component_definition = ftd::interpreter::default::default_bag() + let component_definition = ftd::interpreter::default::get_default_bag() .get(device) .unwrap() .clone() @@ -2021,7 +2021,7 @@ pub struct Rive { impl Rive { pub fn from(component: &ftd::interpreter::Component) -> Rive { - let component_definition = ftd::interpreter::default::default_bag() + let component_definition = ftd::interpreter::default::get_default_bag() .get("ftd#rive") .unwrap() .clone() diff --git a/ftd/src/js/mod.rs b/ftd/src/js/mod.rs index 2fcde906df..0414e3570b 100644 --- a/ftd/src/js/mod.rs +++ b/ftd/src/js/mod.rs @@ -23,14 +23,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::default_bag(); + let bag = ftd::interpreter::default::get_default_bag(); let doc = ftd::interpreter::TDoc { name: "", aliases: &ftd::interpreter::default::default_aliases(), - bag: ftd::interpreter::BagOrState::Bag(&bag), + bag: ftd::interpreter::BagOrState::Bag(bag), }; let mut export_asts = vec![]; - for thing in ftd::interpreter::default::default_bag().values() { + for thing in ftd::interpreter::default::get_default_bag().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 { @@ -98,7 +98,7 @@ pub fn document_into_js_ast(document: ftd::interpreter::Document) -> JSAstData { &doc, &mut has_rive_components, )]; - let default_thing_name = ftd::interpreter::default::default_bag() + let default_thing_name = ftd::interpreter::default::get_default_bag() .into_iter() .map(|v| v.0) .collect_vec(); @@ -106,7 +106,7 @@ pub fn document_into_js_ast(document: ftd::interpreter::Document) -> JSAstData { let mut export_asts = vec![]; for (key, thing) in document.data.iter() { - if default_thing_name.contains(key) { + if default_thing_name.contains(&key) { continue; } if let ftd::interpreter::Thing::Component(c) = thing { diff --git a/ftd/src/node/test.rs b/ftd/src/node/test.rs index 9d657f9ae3..559327f8b5 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::default_bag().keys() { + for thing in ftd::interpreter::default::get_default_bag().keys() { node.bag.remove(thing); } let expected_json = serde_json::to_string_pretty(&node).unwrap();