Skip to content

Commit

Permalink
tdoc trait has two versions now
Browse files Browse the repository at this point in the history
  • Loading branch information
amitu committed Nov 22, 2024
1 parent fdc66b5 commit 617f25d
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 22 deletions.
3 changes: 3 additions & 0 deletions fastn-resolved-to-js/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ license.workspace = true
repository.workspace = true
homepage.workspace = true

[features]
owned-tdoc = ["fastn-resolved/owned-tdoc"]

[dependencies]
fastn-js.workspace = true
fastn-resolved.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions fastn-resolved-to-js/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,10 @@ pub(crate) fn get_set_property_values_for_provided_component_properties(

// Attempt to retrieve component or web component arguments
doc.get_opt_component(component_name)
.map(|v| &v.arguments)
.map(|v| v.arguments.clone())
.or(doc
.get_opt_web_component(component_name)
.map(|v| &v.arguments))
.map(|v| v.arguments.clone()))
.map(|arguments| {
// Collect valid arguments matching the provided properties and their set property values
arguments
Expand Down
4 changes: 4 additions & 0 deletions fastn-resolved/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ license = "BSD-3-Clause"
repository.workspace = true
homepage.workspace = true

[features]
owned-tdoc = []

[dependencies]
serde.workspace = true

10 changes: 10 additions & 0 deletions fastn-resolved/src/tdoc.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
#[cfg(feature = "owned-tdoc")]
pub trait TDoc {
fn get_opt_function(&self, name: &str) -> Option<fastn_resolved::Function>;
fn get_opt_record(&self, name: &str) -> Option<fastn_resolved::Record>;
fn name(&self) -> &str;
fn get_opt_component(&self, name: &str) -> Option<fastn_resolved::ComponentDefinition>;
fn get_opt_web_component(&self, name: &str) -> Option<fastn_resolved::WebComponentDefinition>;
}

#[cfg(not(feature = "owned-tdoc"))]
pub trait TDoc {
fn get_opt_function(&self, name: &str) -> Option<&fastn_resolved::Function>;
fn get_opt_record(&self, name: &str) -> Option<&fastn_resolved::Record>;
Expand Down
2 changes: 1 addition & 1 deletion ftd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ thiserror.workspace = true
tracing.workspace = true
fastn-js.workspace = true
indexmap.workspace = true
fastn-resolved.workspace = true
fastn-resolved = { workspace = true, features = ["owned-tdoc"] }
fastn-builtins.workspace = true
fastn-resolved-to-js.workspace = true

Expand Down
2 changes: 1 addition & 1 deletion ftd/src/html/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl FunctionGenerator {
for function in node_data
.bag
.values()
.filter_map(|v| v.function(node_data.name.as_str(), 0).ok().cloned())
.filter_map(|v| v.function(node_data.name.as_str(), 0).ok().clone())
{
vector.push(self.get_function(function)?)
}
Expand Down
18 changes: 9 additions & 9 deletions ftd/src/interpreter/tdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,7 @@ impl<'a> TDoc<'a> {
line_number: usize,
) -> ftd::interpreter::Result<fastn_resolved::Function> {
let initial_thing = self.get_initial_thing(name, line_number)?.0;
initial_thing.function(self.name, line_number).cloned()
Ok(initial_thing.function(self.name, line_number)?.clone())
}

pub fn search_function(
Expand Down Expand Up @@ -2213,27 +2213,27 @@ impl<'a> TDoc<'a> {
}

impl<'a> fastn_resolved::tdoc::TDoc for TDoc<'a> {
fn get_opt_function(&self, name: &str) -> Option<&Function> {
fn get_opt_function(&self, name: &str) -> Option<Function> {
let initial_thing = self.get_reexport_thing(name, 0).ok()?.0;
initial_thing.function(self.name, 0).ok()
initial_thing.function(self.name, 0).ok().clone()
}

fn get_opt_record(&self, name: &str) -> Option<&Record> {
fn get_opt_record(&self, name: &str) -> Option<Record> {
let initial_thing = self.get_reexport_thing(name, 0).ok()?.0;
initial_thing.record(self.name, 0).ok()
initial_thing.record(self.name, 0).ok().clone()
}

fn name(&self) -> &str {
self.name
}

fn get_opt_component(&self, name: &str) -> Option<&ComponentDefinition> {
fn get_opt_component(&self, name: &str) -> Option<ComponentDefinition> {
let initial_thing = self.get_reexport_thing(name, 0).ok()?.0;
initial_thing.component_ref()
initial_thing.clone().component()
}

fn get_opt_web_component(&self, name: &str) -> Option<&fastn_resolved::WebComponentDefinition> {
fn get_opt_web_component(&self, name: &str) -> Option<fastn_resolved::WebComponentDefinition> {
let initial_thing = self.get_reexport_thing(name, 0).ok()?.0;
initial_thing.web_component(self.name, 0).ok()
initial_thing.web_component(self.name, 0).ok().clone()
}
}
18 changes: 9 additions & 9 deletions ftd/src/interpreter/things/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ pub trait ThingExt {
&self,
doc_id: &str,
line_number: usize,
) -> ftd::interpreter::Result<&fastn_resolved::Record>;
) -> ftd::interpreter::Result<fastn_resolved::Record>;
fn web_component(
&self,
doc_id: &str,
line_number: usize,
) -> ftd::interpreter::Result<&fastn_resolved::WebComponentDefinition>;
) -> ftd::interpreter::Result<fastn_resolved::WebComponentDefinition>;
fn function(
&self,
doc_id: &str,
line_number: usize,
) -> ftd::interpreter::Result<&fastn_resolved::Function>;
) -> ftd::interpreter::Result<fastn_resolved::Function>;
}

impl ThingExt for Thing {
Expand All @@ -53,9 +53,9 @@ impl ThingExt for Thing {
&self,
doc_id: &str,
line_number: usize,
) -> ftd::interpreter::Result<&fastn_resolved::Record> {
) -> ftd::interpreter::Result<fastn_resolved::Record> {
match self {
ftd::interpreter::Thing::Record(v) => Ok(v),
ftd::interpreter::Thing::Record(v) => Ok(v.clone()),
t => ftd::interpreter::utils::e2(
format!("Expected Record, found: `{:?}`", t),
doc_id,
Expand All @@ -68,9 +68,9 @@ impl ThingExt for Thing {
&self,
doc_id: &str,
line_number: usize,
) -> ftd::interpreter::Result<&fastn_resolved::WebComponentDefinition> {
) -> ftd::interpreter::Result<fastn_resolved::WebComponentDefinition> {
match self {
ftd::interpreter::Thing::WebComponent(v) => Ok(v),
ftd::interpreter::Thing::WebComponent(v) => Ok(v.to_owned()),
t => ftd::interpreter::utils::e2(
format!("Expected WebComponent, found: `{:?}`", t),
doc_id,
Expand All @@ -83,9 +83,9 @@ impl ThingExt for Thing {
&self,
doc_id: &str,
line_number: usize,
) -> ftd::interpreter::Result<&fastn_resolved::Function> {
) -> ftd::interpreter::Result<fastn_resolved::Function> {
match self {
ftd::interpreter::Thing::Function(v) => Ok(v),
ftd::interpreter::Thing::Function(v) => Ok(v.to_owned()),
t => ftd::interpreter::utils::e2(
format!("Expected Function, found: `{:?}`", t),
doc_id,
Expand Down

0 comments on commit 617f25d

Please sign in to comment.