Skip to content

Commit

Permalink
Added fastn_type::Thing
Browse files Browse the repository at this point in the history
  • Loading branch information
Arpita-Jaiswal committed Nov 13, 2024
1 parent c6fb87a commit 13509f6
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 54 deletions.
54 changes: 54 additions & 0 deletions fastn-type/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,57 @@ mod or_type;
pub use or_type::{OrType, OrTypeVariant};

pub type Map<T> = std::collections::BTreeMap<String, T>;

#[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<fastn_type::ComponentDefinition> {
match self {
fastn_type::Thing::Component(v) => Some(v),
_ => None,
}
}
}
2 changes: 2 additions & 0 deletions ftd/src/html/functions.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use ftd::interpreter::ThingExt;

pub struct FunctionGenerator {
id: String,
}
Expand Down
2 changes: 2 additions & 0 deletions ftd/src/html/variable_dependencies.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use ftd::interpreter::ThingExt;

pub struct VariableDependencyGenerator<'a> {
pub id: &'a str,
pub doc: &'a ftd::interpreter::TDoc<'a>,
Expand Down
1 change: 1 addition & 0 deletions ftd/src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion ftd/src/interpreter/tdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
74 changes: 21 additions & 53 deletions ftd/src/interpreter/things/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<fastn_type::Variable>;
fn record(
self,
doc_id: &str,
line_number: usize,
},
) -> ftd::interpreter::Result<fastn_type::Record>;
fn function(
self,
doc_id: &str,
line_number: usize,
) -> ftd::interpreter::Result<fastn_type::Function>;
}

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,
Expand All @@ -70,7 +45,7 @@ impl Thing {
}
}

pub(crate) fn record(
fn record(
self,
doc_id: &str,
line_number: usize,
Expand All @@ -85,7 +60,7 @@ impl Thing {
}
}

pub(crate) fn function(
fn function(
self,
doc_id: &str,
line_number: usize,
Expand All @@ -99,11 +74,4 @@ impl Thing {
),
}
}

pub(crate) fn component(self) -> Option<fastn_type::ComponentDefinition> {
match self {
ftd::interpreter::Thing::Component(v) => Some(v),
_ => None,
}
}
}

0 comments on commit 13509f6

Please sign in to comment.