Skip to content

Commit

Permalink
chore: derive bincode on all trees (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
KennedyTedesco committed Feb 3, 2023
1 parent 8eb278d commit b7a2f5b
Show file tree
Hide file tree
Showing 38 changed files with 209 additions and 134 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ ara_reporting = { version = "0.6.1" }
schemars = { version = "0.8.11" }
serde = { version = "1.0.149", features = ["derive"] }
serde_json = { version = "1.0.89" }
bincode = { version = "2.0.0-rc.2" }

[profile.release]
opt-level = 3
Expand Down
4 changes: 3 additions & 1 deletion src/lexer/byte_string.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use bincode::Decode;
use bincode::Encode;
use std::ops::Deref;
use std::ops::DerefMut;
use std::str::from_utf8;
Expand All @@ -11,7 +13,7 @@ use serde::Serialize;
///
/// The Trunk lexer and parser work mainly with byte strings because
/// valid PHP code is not required to be valid UTF-8.
#[derive(PartialOrd, PartialEq, Eq, Clone, Hash)]
#[derive(PartialOrd, PartialEq, Eq, Clone, Hash, Encode, Decode)]
pub struct ByteString {
pub bytes: Vec<u8>,
pub length: usize,
Expand Down
8 changes: 5 additions & 3 deletions src/tree/comment.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use bincode::Decode;
use bincode::Encode;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;

use crate::lexer::byte_string::ByteString;

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case", tag = "type")]
pub enum CommentFormat {
SingleLine,
Expand All @@ -13,15 +15,15 @@ pub enum CommentFormat {
Document,
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct Comment {
pub position: usize,
pub format: CommentFormat,
pub content: ByteString,
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct CommentGroup {
pub comments: Vec<Comment>,
Expand Down
6 changes: 4 additions & 2 deletions src/tree/definition/attribute.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use bincode::Decode;
use bincode::Encode;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;
Expand All @@ -7,15 +9,15 @@ use crate::tree::identifier::Identifier;
use crate::tree::utils::CommaSeparated;
use crate::tree::Node;

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct AttributeGroupDefinition {
pub hash_left_bracket: usize,
pub members: CommaSeparated<AttributeDefinition>,
pub right_bracket: usize,
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct AttributeDefinition {
pub name: Identifier,
Expand Down
12 changes: 7 additions & 5 deletions src/tree/definition/class.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use bincode::Decode;
use bincode::Encode;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;
Expand All @@ -15,7 +17,7 @@ use crate::tree::token::Keyword;
use crate::tree::utils::CommaSeparated;
use crate::tree::Node;

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct ClassDefinition {
pub comments: CommentGroup,
Expand All @@ -29,29 +31,29 @@ pub struct ClassDefinition {
pub body: ClassDefinitionBody,
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct ClassDefinitionExtends {
pub extends: Keyword,
pub parent: TemplatedIdentifier,
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct ClassDefinitionImplements {
pub implements: Keyword,
pub interfaces: CommaSeparated<TemplatedIdentifier>,
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct ClassDefinitionBody {
pub left_brace: usize,
pub members: Vec<ClassDefinitionMember>,
pub right_brace: usize,
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case", tag = "type", content = "value")]
pub enum ClassDefinitionMember {
Constant(ClassishConstantDefinition),
Expand Down
6 changes: 4 additions & 2 deletions src/tree/definition/constant.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use bincode::Decode;
use bincode::Encode;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;
Expand All @@ -11,7 +13,7 @@ use crate::tree::identifier::Identifier;
use crate::tree::token::Keyword;
use crate::tree::Node;

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct ConstantDefinition {
pub comments: CommentGroup,
Expand All @@ -23,7 +25,7 @@ pub struct ConstantDefinition {
pub semicolon: usize,
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct ClassishConstantDefinition {
pub comments: CommentGroup,
Expand Down
24 changes: 13 additions & 11 deletions src/tree/definition/enum.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use bincode::Decode;
use bincode::Encode;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;
Expand All @@ -13,14 +15,14 @@ use crate::tree::token::Keyword;
use crate::tree::utils::CommaSeparated;
use crate::tree::Node;

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case", tag = "type", content = "data")]
pub enum EnumDefinition {
Backed(BackedEnumDefinition),
Unit(UnitEnumDefinition),
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct UnitEnumDefinition {
pub comments: CommentGroup,
Expand All @@ -31,30 +33,30 @@ pub struct UnitEnumDefinition {
pub body: UnitEnumBodyDefinition,
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct EnumImplementsDefinition {
pub implements: Keyword,
pub interfaces: CommaSeparated<TemplatedIdentifier>,
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct UnitEnumBodyDefinition {
pub left_brace: usize,
pub members: Vec<UnitEnumMemberDefinition>,
pub right_brace: usize,
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case", tag = "type", content = "value")]
pub enum UnitEnumMemberDefinition {
Case(UnitEnumCaseDefinition),
Method(MethodDefinition),
Constant(ClassishConstantDefinition),
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct UnitEnumCaseDefinition {
pub attributes: Vec<AttributeGroupDefinition>,
Expand All @@ -63,7 +65,7 @@ pub struct UnitEnumCaseDefinition {
pub semicolon: usize,
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct BackedEnumDefinition {
pub comments: CommentGroup,
Expand All @@ -75,30 +77,30 @@ pub struct BackedEnumDefinition {
pub body: BackedEnumBodyDefinition,
}

#[derive(Debug, Clone, Hash, Eq, PartialEq, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, Clone, Hash, Eq, PartialEq, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(tag = "type", content = "value")]
pub enum BackedEnumTypeDefinition {
String(usize, Identifier),
Int(usize, Identifier),
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct BackedEnumBodyDefinition {
pub left_brace: usize,
pub members: Vec<BackedEnumMemberDefinition>,
pub right_brace: usize,
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case", tag = "type", content = "value")]
pub enum BackedEnumMemberDefinition {
Case(BackedEnumCaseDefinition),
Method(MethodDefinition),
Constant(ClassishConstantDefinition),
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct BackedEnumCaseDefinition {
pub attributes: Vec<AttributeGroupDefinition>,
Expand Down
24 changes: 13 additions & 11 deletions src/tree/definition/function.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use bincode::Decode;
use bincode::Encode;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;
Expand All @@ -15,14 +17,14 @@ use crate::tree::utils::CommaSeparated;
use crate::tree::variable::Variable;
use crate::tree::Node;

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct FunctionLikeReturnTypeDefinition {
pub colon: usize,
pub type_definition: TypeDefinition,
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct FunctionLikeParameterDefinition {
pub comments: CommentGroup,
Expand All @@ -33,14 +35,14 @@ pub struct FunctionLikeParameterDefinition {
pub default: Option<FunctionLikeParameterDefaultValueDefinition>,
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct FunctionLikeParameterDefaultValueDefinition {
pub equals: usize,
pub value: Expression,
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct FunctionLikeParameterListDefinition {
pub comments: CommentGroup,
Expand All @@ -49,7 +51,7 @@ pub struct FunctionLikeParameterListDefinition {
pub right_parenthesis: usize,
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct FunctionDefinition {
pub comments: CommentGroup,
Expand All @@ -62,7 +64,7 @@ pub struct FunctionDefinition {
pub body: BlockStatement,
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct MethodParameterDefinition {
pub attributes: Vec<AttributeGroupDefinition>,
Expand All @@ -74,7 +76,7 @@ pub struct MethodParameterDefinition {
pub default: Option<FunctionLikeParameterDefaultValueDefinition>,
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct MethodParameterListDefinition {
pub comments: CommentGroup,
Expand All @@ -83,7 +85,7 @@ pub struct MethodParameterListDefinition {
pub right_parenthesis: usize,
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct MethodTypeConstraintDefinition {
pub comments: CommentGroup,
Expand All @@ -92,22 +94,22 @@ pub struct MethodTypeConstraintDefinition {
pub type_definition: TypeDefinition,
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct MethodTypeConstraintGroupDefinition {
pub comments: CommentGroup,
pub r#where: Keyword,
pub constraints: CommaSeparated<MethodTypeConstraintDefinition>,
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum MethodBodyDefinition {
Concrete(BlockStatement),
Abstract(usize),
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct MethodDefinition {
pub comments: CommentGroup,
Expand Down
10 changes: 6 additions & 4 deletions src/tree/definition/interface.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use bincode::Decode;
use bincode::Encode;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;
Expand All @@ -13,7 +15,7 @@ use crate::tree::token::Keyword;
use crate::tree::utils::CommaSeparated;
use crate::tree::Node;

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct InterfaceDefinition {
pub comments: CommentGroup,
Expand All @@ -25,22 +27,22 @@ pub struct InterfaceDefinition {
pub body: InterfaceDefinitionBody,
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct InterfaceDefinitionExtends {
pub extends: Keyword,
pub parents: CommaSeparated<TemplatedIdentifier>,
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct InterfaceDefinitionBody {
pub left_brace: usize,
pub members: Vec<InterfaceDefinitionMember>,
pub right_brace: usize,
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, Encode, Decode, JsonSchema)]
#[serde(rename_all = "snake_case", tag = "type", content = "value")]
pub enum InterfaceDefinitionMember {
Constant(ClassishConstantDefinition),
Expand Down
Loading

0 comments on commit b7a2f5b

Please sign in to comment.