Skip to content

Commit

Permalink
Passing reference to fastn_resolved::tdoc::TDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
Arpita-Jaiswal committed Nov 21, 2024
1 parent 05c431d commit ee1ed21
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 76 deletions.
7 changes: 7 additions & 0 deletions fastn-resolved/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,11 @@ impl Definition {
_ => None,
}
}

pub fn component_ref(&self) -> Option<&fastn_resolved::ComponentDefinition> {
match self {
fastn_resolved::Definition::Component(v) => Some(v),
_ => None,
}
}
}
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.to_owned().function(node_data.name.as_str(), 0).ok())
.filter_map(|v| v.function(node_data.name.as_str(), 0).ok().cloned())
{
vector.push(self.get_function(function)?)
}
Expand Down
119 changes: 54 additions & 65 deletions ftd/src/interpreter/tdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl<'a> TDoc<'a> {
&'a self,
name: &'a str,
line_number: usize,
) -> ftd::interpreter::Result<&fastn_resolved::Record> {
) -> ftd::interpreter::Result<fastn_resolved::Record> {
match self.get_thing(name, line_number)? {
ftd::interpreter::Thing::Record(r) => Ok(r),
t => self.err(
Expand All @@ -85,7 +85,7 @@ impl<'a> TDoc<'a> {
&'a self,
name: &'a str,
line_number: usize,
) -> ftd::interpreter::Result<&fastn_resolved::OrType> {
) -> ftd::interpreter::Result<fastn_resolved::OrType> {
match self.get_thing(name, line_number)? {
ftd::interpreter::Thing::OrType(ot) => Ok(ot),
t => self.err(
Expand Down Expand Up @@ -125,7 +125,7 @@ impl<'a> TDoc<'a> {
&'a self,
name: &'a str,
line_number: usize,
) -> ftd::interpreter::Result<&fastn_resolved::Variable> {
) -> ftd::interpreter::Result<fastn_resolved::Variable> {
match self.get_thing(name, line_number)? {
ftd::interpreter::Thing::Variable(r) => Ok(r),
t => self.err(
Expand All @@ -141,7 +141,7 @@ impl<'a> TDoc<'a> {
&'a self,
line_number: usize,
name: &'a str,
) -> ftd::interpreter::Result<&fastn_resolved::Value> {
) -> ftd::interpreter::Result<fastn_resolved::Value> {
use ftd::interpreter::PropertyValueExt;
// TODO: name can be a.b.c, and a and a.b are records with right fields
match self.get_thing(name, line_number)? {
Expand Down Expand Up @@ -873,7 +873,7 @@ impl<'a> TDoc<'a> {
&'a self,
name: &'a str,
line_number: usize,
) -> ftd::interpreter::Result<&ftd::interpreter::Thing> {
) -> ftd::interpreter::Result<ftd::interpreter::Thing> {
let name = name
.strip_prefix(ftd::interpreter::utils::REFERENCE)
.or_else(|| name.strip_prefix(ftd::interpreter::utils::CLONE))
Expand All @@ -882,16 +882,16 @@ impl<'a> TDoc<'a> {
let (initial_thing, remaining) = self.get_initial_thing(name, line_number)?;

if let Some(remaining) = remaining {
return get_thing_(self, line_number, remaining.as_str(), &initial_thing);
return get_thing_(self, line_number, remaining.as_str(), initial_thing);
}
return Ok(initial_thing);

fn get_thing_(
doc: &ftd::interpreter::TDoc,
line_number: usize,
name: &str,
thing: &ftd::interpreter::Thing,
) -> ftd::interpreter::Result<&ftd::interpreter::Thing> {
thing: ftd::interpreter::Thing,
) -> ftd::interpreter::Result<ftd::interpreter::Thing> {
use ftd::interpreter::PropertyValueExt;
use itertools::Itertools;

Expand Down Expand Up @@ -961,7 +961,7 @@ impl<'a> TDoc<'a> {
is_static: !mutable,
});
if let Some(remaining) = remaining {
return get_thing_(doc, line_number, &remaining, &thing);
return get_thing_(doc, line_number, &remaining, thing);
}
return Ok(thing);
}
Expand Down Expand Up @@ -993,7 +993,7 @@ impl<'a> TDoc<'a> {
| Some(fastn_resolved::PropertyValue::Clone { name, .. }) => {
let (initial_thing, name) = doc.get_initial_thing(name, line_number)?;
if let Some(remaining) = name {
get_thing_(doc, line_number, remaining.as_str(), &initial_thing)?
get_thing_(doc, line_number, remaining.as_str(), initial_thing)?
} else {
initial_thing
}
Expand All @@ -1019,7 +1019,7 @@ impl<'a> TDoc<'a> {
}
};
if let Some(remaining) = remaining {
return get_thing_(doc, line_number, &remaining, &thing);
return get_thing_(doc, line_number, &remaining, thing);
}
Ok(thing)
}
Expand All @@ -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)
initial_thing.function(self.name, line_number).cloned()
}

pub fn search_function(
Expand All @@ -1042,7 +1042,7 @@ impl<'a> TDoc<'a> {
let name = ftd_p1::AccessModifier::remove_modifiers(name);
let initial_thing = try_ok_state!(self.search_initial_thing(name.as_str(), line_number)?).0;
Ok(ftd::interpreter::StateWithThing::new_thing(
initial_thing.function(self.name, line_number)?,
initial_thing.function(self.name, line_number)?.clone(),
))
}

Expand Down Expand Up @@ -1754,7 +1754,7 @@ impl<'a> TDoc<'a> {
&'a self,
name: &'a str,
line_number: usize,
) -> ftd::interpreter::Result<(&ftd::interpreter::Thing, Option<String>)> {
) -> ftd::interpreter::Result<(ftd::interpreter::Thing, Option<String>)> {
self.get_initial_thing_with_inherited(name, line_number, &Default::default())
}

Expand All @@ -1763,7 +1763,7 @@ impl<'a> TDoc<'a> {
name: &'a str,
line_number: usize,
inherited_variables: &ftd::VecMap<(String, Vec<usize>)>,
) -> ftd::interpreter::Result<(&ftd::interpreter::Thing, Option<String>)> {
) -> ftd::interpreter::Result<(ftd::interpreter::Thing, Option<String>)> {
let name = name
.strip_prefix(ftd::interpreter::utils::REFERENCE)
.or_else(|| name.strip_prefix(ftd::interpreter::utils::CLONE))
Expand All @@ -1781,44 +1781,39 @@ impl<'a> TDoc<'a> {

let name = self.resolve_name(name);

return get_reexport_thing(self, &name, line_number);

fn get_reexport_thing(
tdoc: &TDoc,
name: &str,
line_number: usize,
) -> ftd::interpreter::Result<(&ftd::interpreter::Thing, Option<String>)> {
let (splited_name, remaining_value) = if let Ok(function_name) =
ftd::interpreter::utils::get_function_name(name, tdoc.name, line_number)
{
(function_name, None)
} else {
ftd::interpreter::utils::get_doc_name_and_remaining(name, tdoc.name, line_number)
};
self.get_reexport_thing(&name, line_number)
.map(|(thing, remaining)| (thing.clone(), remaining))
}

let (thing_name, remaining) =
match tdoc.bag().get(splited_name.as_str()).map(ToOwned::to_owned) {
Some(a) => (a, remaining_value),
None => match tdoc.bag().get(name).map(|v| (v.to_owned(), None)) {
Some(a) => a,
None => {
return tdoc.err(
"not found",
splited_name,
"get_initial_thing",
line_number,
);
}
},
};
fn get_reexport_thing(
&self,
name: &str,
line_number: usize,
) -> ftd::interpreter::Result<(&ftd::interpreter::Thing, Option<String>)> {
let (splited_name, remaining_value) = if let Ok(function_name) =
ftd::interpreter::utils::get_function_name(name, self.name, line_number)
{
(function_name, None)
} else {
ftd::interpreter::utils::get_doc_name_and_remaining(name, self.name, line_number)
};

if let ftd::interpreter::Thing::Export { ref from, .. } = thing_name {
let thing_name = get_reexport_thing(tdoc, from, line_number)?.0;
return Ok((thing_name, remaining));
}
let (thing_name, remaining) = match self.bag().get(splited_name.as_str()) {
Some(a) => (a, remaining_value),
None => match self.bag().get(name).map(|v| (v, None)) {
Some(a) => a,
None => {
return self.err("not found", splited_name, "get_initial_thing", line_number);
}
},
};

Ok((thing_name, remaining))
if let ftd::interpreter::Thing::Export { ref from, .. } = thing_name {
let thing_name = self.get_reexport_thing(from, line_number)?.0;
return Ok((thing_name, remaining));
}

Ok((thing_name, remaining))
}

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

impl<'a> fastn_resolved::tdoc::TDoc for TDoc<'a> {
fn get_opt_function(&self, name: &str) -> Option<Function> {
let initial_thing = self.get_initial_thing(name, 0).ok()?.0;
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()
}

fn get_opt_record(&self, name: &str) -> Option<Record> {
match self.get_thing(name, 0).ok()? {
ftd::interpreter::Thing::Record(r) => Some(r),
_ => None,
}
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()
}

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

fn get_opt_component(&self, name: &str) -> Option<&ComponentDefinition> {
match self.get_thing(name, 0).ok()? {
ftd::interpreter::Thing::Component(c) => Some(c),
_ => None,
}
let initial_thing = self.get_reexport_thing(name, 0).ok()?.0;
initial_thing.component_ref()
}

fn get_opt_web_component(&self, name: &str) -> Option<fastn_resolved::WebComponentDefinition> {
match self.get_thing(name, 0).ok()? {
ftd::interpreter::Thing::WebComponent(c) => Some(c),
_ => None,
}
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()
}
}
36 changes: 28 additions & 8 deletions ftd/src/interpreter/things/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,20 @@ pub trait ThingExt {
line_number: usize,
) -> ftd::interpreter::Result<fastn_resolved::Variable>;
fn record(
self,
&self,
doc_id: &str,
line_number: usize,
) -> ftd::interpreter::Result<&fastn_resolved::Record>;
fn web_component(
&self,
doc_id: &str,
line_number: usize,
) -> ftd::interpreter::Result<fastn_resolved::Record>;
) -> ftd::interpreter::Result<&fastn_resolved::WebComponentDefinition>;
fn function(
self,
&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 @@ -45,10 +50,10 @@ impl ThingExt for Thing {
}

fn record(
self,
&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),
t => ftd::interpreter::utils::e2(
Expand All @@ -59,11 +64,26 @@ impl ThingExt for Thing {
}
}

fn web_component(
&self,
doc_id: &str,
line_number: usize,
) -> ftd::interpreter::Result<&fastn_resolved::WebComponentDefinition> {
match self {
ftd::interpreter::Thing::WebComponent(v) => Ok(v),
t => ftd::interpreter::utils::e2(
format!("Expected WebComponent, found: `{:?}`", t),
doc_id,
line_number,
),
}
}

fn function(
self,
&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),
t => ftd::interpreter::utils::e2(
Expand Down
4 changes: 2 additions & 2 deletions ftd/src/interpreter/things/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl PropertyValueExt for fastn_resolved::PropertyValue {
self,
doc: &ftd::interpreter::TDoc,
line_number: usize, // Todo: Remove this line number instead use self.line_number()
) -> ftd::interpreter::Result<&fastn_resolved::Value> {
) -> ftd::interpreter::Result<fastn_resolved::Value> {
self.resolve_with_inherited(doc, line_number, &Default::default())
}

Expand All @@ -143,7 +143,7 @@ impl PropertyValueExt for fastn_resolved::PropertyValue {
doc: &ftd::interpreter::TDoc,
line_number: usize,
inherited_variables: &ftd::VecMap<(String, Vec<usize>)>,
) -> ftd::interpreter::Result<&fastn_resolved::Value> {
) -> ftd::interpreter::Result<fastn_resolved::Value> {
match self {
fastn_resolved::PropertyValue::Value { value, .. } => Ok(value),
fastn_resolved::PropertyValue::Reference { name, kind, .. }
Expand Down

0 comments on commit ee1ed21

Please sign in to comment.