From 13509f670794a20ce6d40c29037339e0d96319e4 Mon Sep 17 00:00:00 2001 From: Arpita-Jaiswal Date: Thu, 14 Nov 2024 01:58:43 +0530 Subject: [PATCH] Added fastn_type::Thing --- fastn-type/src/lib.rs | 54 +++++++++++++++++++ ftd/src/html/functions.rs | 2 + ftd/src/html/variable_dependencies.rs | 2 + ftd/src/interpreter/mod.rs | 1 + ftd/src/interpreter/tdoc.rs | 2 +- ftd/src/interpreter/things/mod.rs | 74 ++++++++------------------- 6 files changed, 81 insertions(+), 54 deletions(-) diff --git a/fastn-type/src/lib.rs b/fastn-type/src/lib.rs index b293a8481..60025c625 100644 --- a/fastn-type/src/lib.rs +++ b/fastn-type/src/lib.rs @@ -36,3 +36,57 @@ mod or_type; pub use or_type::{OrType, OrTypeVariant}; pub type Map = std::collections::BTreeMap; + +#[derive(Debug, Clone, PartialEq, serde::Deserialize, serde::Serialize)] +pub enum Thing { + Record(fastn_type::Record), + OrType(fastn_type::OrType), + OrTypeWithVariant { + or_type: String, + variant: fastn_type::OrTypeVariant, + }, + Variable(fastn_type::Variable), + Component(fastn_type::ComponentDefinition), + WebComponent(fastn_type::WebComponentDefinition), + Function(fastn_type::Function), + Export { + from: String, + to: String, + line_number: usize, + }, +} + +impl Thing { + pub fn name(&self) -> String { + match self { + fastn_type::Thing::Record(r) => r.name.clone(), + fastn_type::Thing::OrType(o) => o.name.clone(), + fastn_type::Thing::OrTypeWithVariant { or_type, .. } => or_type.clone(), + fastn_type::Thing::Variable(v) => v.name.to_string(), + fastn_type::Thing::Component(c) => c.name.to_string(), + fastn_type::Thing::Function(f) => f.name.to_string(), + fastn_type::Thing::WebComponent(w) => w.name.to_string(), + fastn_type::Thing::Export { to, .. } => to.to_string(), + } + } + + pub fn line_number(&self) -> usize { + match self { + Thing::Record(r) => r.line_number, + Thing::Variable(v) => v.line_number, + Thing::Component(c) => c.line_number, + Thing::Function(f) => f.line_number, + Thing::OrType(o) => o.line_number, + Thing::OrTypeWithVariant { variant, .. } => variant.line_number(), + Thing::WebComponent(w) => w.line_number, + Thing::Export { line_number, .. } => *line_number, + } + } + + pub fn component(self) -> Option { + match self { + fastn_type::Thing::Component(v) => Some(v), + _ => None, + } + } +} diff --git a/ftd/src/html/functions.rs b/ftd/src/html/functions.rs index 2a839b8b2..3edec4349 100644 --- a/ftd/src/html/functions.rs +++ b/ftd/src/html/functions.rs @@ -1,3 +1,5 @@ +use ftd::interpreter::ThingExt; + pub struct FunctionGenerator { id: String, } diff --git a/ftd/src/html/variable_dependencies.rs b/ftd/src/html/variable_dependencies.rs index 41b5e9f36..b85ccf482 100644 --- a/ftd/src/html/variable_dependencies.rs +++ b/ftd/src/html/variable_dependencies.rs @@ -1,3 +1,5 @@ +use ftd::interpreter::ThingExt; + pub struct VariableDependencyGenerator<'a> { pub id: &'a str, pub doc: &'a ftd::interpreter::TDoc<'a>, diff --git a/ftd/src/interpreter/mod.rs b/ftd/src/interpreter/mod.rs index ca7f1c98b..1993cc4f6 100644 --- a/ftd/src/interpreter/mod.rs +++ b/ftd/src/interpreter/mod.rs @@ -54,6 +54,7 @@ pub use things::kind::KindExt; pub use things::record::FieldExt; pub use things::value::{PropertyValueExt, PropertyValueSourceExt, ValueExt}; pub use things::variable::VariableExt; +pub use things::ThingExt; #[derive(thiserror::Error, Debug)] pub enum Error { diff --git a/ftd/src/interpreter/tdoc.rs b/ftd/src/interpreter/tdoc.rs index 9bae7b629..36e944683 100644 --- a/ftd/src/interpreter/tdoc.rs +++ b/ftd/src/interpreter/tdoc.rs @@ -2,7 +2,7 @@ use ftd::interpreter::expression::ExpressionExt; use ftd::interpreter::things::component::ComponentDefinitionExt; use ftd::interpreter::things::or_type::OrTypeVariantExt; use ftd::interpreter::things::record::RecordExt; -use ftd::interpreter::FunctionExt; +use ftd::interpreter::{FunctionExt, ThingExt}; #[derive(Debug, PartialEq)] pub struct TDoc<'a> { diff --git a/ftd/src/interpreter/things/mod.rs b/ftd/src/interpreter/things/mod.rs index aa338f73c..f3068d34b 100644 --- a/ftd/src/interpreter/things/mod.rs +++ b/ftd/src/interpreter/things/mod.rs @@ -9,53 +9,28 @@ pub(crate) mod value; pub(crate) mod variable; pub(crate) mod web_component; -#[derive(Debug, Clone, PartialEq, serde::Deserialize, serde::Serialize)] -pub enum Thing { - Record(fastn_type::Record), - OrType(fastn_type::OrType), - OrTypeWithVariant { - or_type: String, - variant: fastn_type::OrTypeVariant, - }, - Variable(fastn_type::Variable), - Component(fastn_type::ComponentDefinition), - WebComponent(fastn_type::WebComponentDefinition), - Function(fastn_type::Function), - Export { - from: String, - to: String, +pub type Thing = fastn_type::Thing; + +pub trait ThingExt { + fn variable( + self, + doc_id: &str, + line_number: usize, + ) -> ftd::interpreter::Result; + fn record( + self, + doc_id: &str, line_number: usize, - }, + ) -> ftd::interpreter::Result; + fn function( + self, + doc_id: &str, + line_number: usize, + ) -> ftd::interpreter::Result; } -impl Thing { - pub(crate) fn name(&self) -> String { - match self { - ftd::interpreter::Thing::Record(r) => r.name.clone(), - ftd::interpreter::Thing::OrType(o) => o.name.clone(), - ftd::interpreter::Thing::OrTypeWithVariant { or_type, .. } => or_type.clone(), - ftd::interpreter::Thing::Variable(v) => v.name.to_string(), - ftd::interpreter::Thing::Component(c) => c.name.to_string(), - ftd::interpreter::Thing::Function(f) => f.name.to_string(), - ftd::interpreter::Thing::WebComponent(w) => w.name.to_string(), - ftd::interpreter::Thing::Export { to, .. } => to.to_string(), - } - } - - pub fn line_number(&self) -> usize { - match self { - Thing::Record(r) => r.line_number, - Thing::Variable(v) => v.line_number, - Thing::Component(c) => c.line_number, - Thing::Function(f) => f.line_number, - Thing::OrType(o) => o.line_number, - Thing::OrTypeWithVariant { variant, .. } => variant.line_number(), - Thing::WebComponent(w) => w.line_number, - Thing::Export { line_number, .. } => *line_number, - } - } - - pub fn variable( +impl ThingExt for Thing { + fn variable( self, doc_id: &str, line_number: usize, @@ -70,7 +45,7 @@ impl Thing { } } - pub(crate) fn record( + fn record( self, doc_id: &str, line_number: usize, @@ -85,7 +60,7 @@ impl Thing { } } - pub(crate) fn function( + fn function( self, doc_id: &str, line_number: usize, @@ -99,11 +74,4 @@ impl Thing { ), } } - - pub(crate) fn component(self) -> Option { - match self { - ftd::interpreter::Thing::Component(v) => Some(v), - _ => None, - } - } }