Skip to content

Commit

Permalink
Impl databake for all AST node types
Browse files Browse the repository at this point in the history
Databake doesn't have any derive for HashMap, so for NonCodeMeta I decided to skip serializing the non_code_nodes. This should be OK for now.

See <unicode-org/icu4x#4266> for the Databake hashmap issue.
  • Loading branch information
adamchalmers committed Nov 9, 2023
1 parent 7272cc9 commit e219a3a
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 29 deletions.
43 changes: 43 additions & 0 deletions src/wasm-lib/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/wasm-lib/kcl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ async-recursion = "1.0.5"
async-trait = "0.1.73"
clap = { version = "4.4.7", features = ["cargo", "derive", "env", "unicode"], optional = true }
dashmap = "5.5.3"
databake = { version = "0.1.6", features = ["derive"] }
derive-docs = { version = "0.1.4" }
#derive-docs = { path = "../derive-docs" }
kittycad = { workspace = true }
Expand Down
98 changes: 70 additions & 28 deletions src/wasm-lib/kcl/src/ast/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::{collections::HashMap, fmt::Write};

use anyhow::Result;
use databake::*;
use parse_display::{Display, FromStr};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
Expand All @@ -20,7 +21,8 @@ use crate::{

mod literal_value;

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
#[databake(path = kcl_lib)]
#[ts(export)]
#[serde(rename_all = "camelCase")]
pub struct Program {
Expand Down Expand Up @@ -349,7 +351,8 @@ macro_rules! impl_value_meta {

pub(crate) use impl_value_meta;

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
#[databake(path = kcl_lib)]
#[ts(export)]
#[serde(tag = "type")]
pub enum BodyItem {
Expand Down Expand Up @@ -388,7 +391,8 @@ impl From<&BodyItem> for SourceRange {
}
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
#[databake(path = kcl_lib)]
#[ts(export)]
#[serde(tag = "type")]
pub enum Value {
Expand Down Expand Up @@ -551,7 +555,8 @@ impl From<&Value> for SourceRange {
}
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
#[databake(path = kcl_lib)]
#[ts(export)]
#[serde(tag = "type")]
pub enum BinaryPart {
Expand Down Expand Up @@ -707,7 +712,8 @@ impl BinaryPart {
}
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
#[databake(path = kcl_lib)]
#[ts(export)]
#[serde(tag = "type")]
pub struct NonCodeNode {
Expand Down Expand Up @@ -755,7 +761,8 @@ impl NonCodeNode {
}
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
#[databake(path = kcl_lib)]
#[ts(export)]
#[serde(rename_all = "camelCase")]
pub enum CommentStyle {
Expand All @@ -765,7 +772,8 @@ pub enum CommentStyle {
Block,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
#[databake(path = kcl_lib)]
#[ts(export)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum NonCodeValue {
Expand Down Expand Up @@ -810,6 +818,19 @@ pub struct NonCodeMeta {
pub start: Vec<NonCodeNode>,
}

impl Bake for NonCodeMeta {
fn bake(&self, env: &CrateEnv) -> TokenStream {
env.insert("kcl_lib");
let start = self.start.bake(env);
databake::quote! {
kcl_lib::NonCodeMeta {
non_code_nodes: HashMap::new(),
start: #start,
}
}
}
}

// implement Deserialize manually because we to force the keys of non_code_nodes to be usize
// and by default the ts type { [statementIndex: number]: NonCodeNode } serializes to a string i.e. "0", "1", etc.
impl<'de> Deserialize<'de> for NonCodeMeta {
Expand Down Expand Up @@ -843,7 +864,8 @@ impl NonCodeMeta {
}
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
#[databake(path = kcl_lib)]
#[ts(export)]
#[serde(tag = "type")]
pub struct ExpressionStatement {
Expand All @@ -854,7 +876,8 @@ pub struct ExpressionStatement {

impl_value_meta!(ExpressionStatement);

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
#[databake(path = kcl_lib)]
#[ts(export)]
#[serde(tag = "type")]
pub struct CallExpression {
Expand Down Expand Up @@ -1122,7 +1145,8 @@ impl PartialEq for Function {
}
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
#[databake(path = kcl_lib)]
#[ts(export)]
#[serde(tag = "type")]
pub struct VariableDeclaration {
Expand Down Expand Up @@ -1272,7 +1296,8 @@ impl VariableDeclaration {
}
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, FromStr, Display)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, FromStr, Display, Bake)]
#[databake(path = kcl_lib)]
#[ts(export)]
#[serde(rename_all = "snake_case")]
#[display(style = "snake_case")]
Expand Down Expand Up @@ -1316,7 +1341,8 @@ impl VariableKind {
}
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
#[databake(path = kcl_lib)]
#[ts(export)]
#[serde(tag = "type")]
pub struct VariableDeclarator {
Expand Down Expand Up @@ -1345,7 +1371,8 @@ impl VariableDeclarator {
}
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
#[databake(path = kcl_lib)]
#[ts(export)]
#[serde(tag = "type")]
pub struct Literal {
Expand Down Expand Up @@ -1415,7 +1442,8 @@ impl From<&Box<Literal>> for MemoryItem {
}
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
#[databake(path = kcl_lib)]
#[ts(export)]
#[serde(tag = "type")]
pub struct Identifier {
Expand Down Expand Up @@ -1451,7 +1479,8 @@ impl Identifier {
}
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
#[databake(path = kcl_lib)]
#[ts(export)]
#[serde(tag = "type")]
pub struct PipeSubstitution {
Expand Down Expand Up @@ -1479,7 +1508,8 @@ impl From<PipeSubstitution> for Value {
}
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
#[databake(path = kcl_lib)]
#[ts(export)]
#[serde(tag = "type")]
pub struct ArrayExpression {
Expand Down Expand Up @@ -1639,7 +1669,8 @@ impl ArrayExpression {
}
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
#[databake(path = kcl_lib)]
#[ts(export)]
#[serde(tag = "type")]
pub struct ObjectExpression {
Expand Down Expand Up @@ -1796,7 +1827,8 @@ impl ObjectExpression {

impl_value_meta!(ObjectExpression);

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
#[databake(path = kcl_lib)]
#[ts(export)]
#[serde(tag = "type")]
pub struct ObjectProperty {
Expand Down Expand Up @@ -1838,7 +1870,8 @@ impl ObjectProperty {
}
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
#[databake(path = kcl_lib)]
#[ts(export)]
#[serde(tag = "type")]
pub enum MemberObject {
Expand Down Expand Up @@ -1884,7 +1917,8 @@ impl From<&MemberObject> for SourceRange {
}
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
#[databake(path = kcl_lib)]
#[ts(export)]
#[serde(tag = "type")]
pub enum LiteralIdentifier {
Expand Down Expand Up @@ -1920,7 +1954,8 @@ impl From<&LiteralIdentifier> for SourceRange {
}
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
#[databake(path = kcl_lib)]
#[ts(export)]
#[serde(tag = "type")]
pub struct MemberExpression {
Expand Down Expand Up @@ -2083,7 +2118,8 @@ pub struct ObjectKeyInfo {
pub computed: bool,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
#[databake(path = kcl_lib)]
#[ts(export)]
#[serde(tag = "type")]
pub struct BinaryExpression {
Expand Down Expand Up @@ -2262,7 +2298,8 @@ pub fn parse_json_value_as_string(j: &serde_json::Value) -> Option<String> {
}
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, FromStr, Display)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, FromStr, Display, Bake)]
#[databake(path = kcl_lib)]
#[ts(export)]
#[serde(rename_all = "snake_case")]
#[display(style = "snake_case")]
Expand Down Expand Up @@ -2330,7 +2367,8 @@ impl BinaryOperator {
}
}
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
#[databake(path = kcl_lib)]
#[ts(export)]
#[serde(tag = "type")]
pub struct UnaryExpression {
Expand Down Expand Up @@ -2408,7 +2446,8 @@ impl UnaryExpression {
}
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, FromStr, Display)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, FromStr, Display, Bake)]
#[databake(path = kcl_lib)]
#[ts(export)]
#[serde(rename_all = "snake_case")]
#[display(style = "snake_case")]
Expand All @@ -2423,7 +2462,8 @@ pub enum UnaryOperator {
Not,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
#[databake(path = kcl_lib)]
#[ts(export)]
#[serde(rename_all = "camelCase", tag = "type")]
pub struct PipeExpression {
Expand Down Expand Up @@ -2581,7 +2621,8 @@ async fn execute_pipe_body(
}
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
#[databake(path = kcl_lib)]
#[ts(export)]
#[serde(tag = "type")]
pub struct FunctionExpression {
Expand Down Expand Up @@ -2631,7 +2672,8 @@ impl FunctionExpression {
}
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
#[databake(path = kcl_lib)]
#[ts(export)]
#[serde(tag = "type")]
pub struct ReturnStatement {
Expand Down
Loading

0 comments on commit e219a3a

Please sign in to comment.