Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/actions/setup-environment/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: "Setup Environment"
description: "Setup Environment"
runs:
using: "composite"
steps:
- uses: actions-rust-lang/setup-rust-toolchain@v1
- uses: taiki-e/install-action@nextest
36 changes: 36 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: pre-commit

on:
pull_request:
branches:
- "main"
push:
branches:
- "main"

jobs:
pre-commit:
runs-on: ubuntu-latest
timeout-minutes: 10

steps:
- name: Check out the repo
uses: actions/checkout@v4
with:
fetch-depth: 0
repository: ${{ github.event.pull_request.head.repo.full_name }}
ref: ${{ github.event.pull_request.head.ref }}

- name: Setup backend
uses: ./.github/actions/setup-environment



- uses: pre-commit/[email protected]

- uses: stefanzweifel/git-auto-commit-action@v5
# Always commit changes even if pre-commit failed
if: always() && github.event_name == 'pull_request'
with:
commit_message: "Automated pre-commit update"
push_options: "--no-verify"
24 changes: 24 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Test
on:
pull_request:
branches:
- "main"
push:
branches:
- "main"

jobs:
test:
runs-on: ubuntu-latest
timeout-minutes: 10

steps:
- name: Check out the repo
uses: actions/checkout@v4
- name: Setup Environment
uses: ./.github/actions/setup-environment
- name: Run tests
run: |
~/.cargo/bin/cargo-nextest nextest run --workspace
env:
CARGO_TERM_COLOR: always
2 changes: 0 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
default_language_version:
python: python3.13
repos:


Expand Down
88 changes: 87 additions & 1 deletion Cargo.lock

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

3 changes: 0 additions & 3 deletions codegen-sdk-ast/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
pub use codegen_sdk_cst::*;
fn main() {
println!("Hello, world!");
}
6 changes: 3 additions & 3 deletions codegen-sdk-common/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use bytes::{Bytes, BytesMut};
use tree_sitter::{self, TextProvider};
pub fn named_children_without_field_names<'a>(node: tree_sitter::Node) -> Vec<tree_sitter::Node> {
use tree_sitter::{self};
pub fn named_children_without_field_names(node: tree_sitter::Node) -> Vec<tree_sitter::Node> {
let mut children = Vec::new();
for (index, child) in node.named_children(&mut node.walk()).enumerate() {
if node.field_name_for_named_child(index as u32) == None {
if node.field_name_for_named_child(index as u32).is_none() {
children.push(child);
}
}
Expand Down
11 changes: 5 additions & 6 deletions codegen-sdk-cst-generator/src/generator.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::parser::{Children, Fields, Node, TypeDefinition};
use convert_case::{Case, Casing};
use crate::parser::Node;
use enum_generator::generate_enum;
use naming::normalize_type_name;
use state::State;
use std::{collections::HashSet, error::Error, fmt::format};
use std::{collections::HashSet, error::Error};
use struct_generator::generate_struct;
mod enum_generator;
mod format;
Expand All @@ -22,7 +21,7 @@ pub(crate) fn generate_cst(node_types: &Vec<Node>) -> Result<String, Box<dyn Err
let mut state = State::default();
let mut nodes = HashSet::new();
for node in node_types {
if node.subtypes.len() > 0 {
if !node.subtypes.is_empty() {
state
.variants
.insert(normalize_type_name(&node.type_name), node.subtypes.clone());
Expand All @@ -38,10 +37,10 @@ pub(crate) fn generate_cst(node_types: &Vec<Node>) -> Result<String, Box<dyn Err
continue;
}
nodes.insert(name.clone());
if name == "" {
if name.is_empty() {
continue;
}
if node.subtypes.len() > 0 {
if !node.subtypes.is_empty() {
generate_enum(&node.subtypes, &mut state, &name, true);
} else {
generate_struct(node, &mut state, &name);
Expand Down
12 changes: 5 additions & 7 deletions codegen-sdk-cst-generator/src/generator/enum_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ fn get_cases(
) {
for t in variants {
let normalized_variant_name = normalize_type_name(&t.type_name);
if normalized_variant_name == "" {
if normalized_variant_name.is_empty() {
continue;
}
let variant_name = override_variant_name
.clone()
.unwrap_or_else(|| &normalized_variant_name);
let variant_name = override_variant_name.unwrap_or_else(|| &normalized_variant_name);
let prefix = format!("{}::{}", "Self", variant_name);
if let Some(variants) = state.variants.get(&normalized_variant_name) {
get_cases(variants, cases, state, Some(variant_name), existing_cases);
Expand All @@ -42,7 +40,7 @@ pub fn generate_enum(
));
for t in variants {
let variant_name = normalize_type_name(&t.type_name);
if variant_name == "" {
if variant_name.is_empty() {
continue;
}
state
Expand All @@ -57,8 +55,8 @@ pub fn generate_enum(
let mut existing_cases = Vec::new();
get_cases(variants, &mut cases, state, None, &mut existing_cases);
if anonymous_nodes {
for (name, variant_name) in state.anonymous_nodes.iter() {
if name == "" {
for (name, _variant_name) in state.anonymous_nodes.iter() {
if name.is_empty() {
continue;
}
if existing_cases.contains(name) {
Expand Down
14 changes: 3 additions & 11 deletions codegen-sdk-cst-generator/src/generator/format.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
use std::{
fs::File,
io::{BufWriter, Write},
};

use prettyplease;
use syn;

pub fn format_cst(cst: &str) -> String {
let parsed = syn::parse_str::<syn::File>(&cst)
let parsed = syn::parse_str::<syn::File>(cst)
.map_err(|e| {
println!("{:#?}", e);
e
})
.unwrap();
let formatted = prettyplease::unparse(&parsed);
formatted

prettyplease::unparse(&parsed)
}
4 changes: 2 additions & 2 deletions codegen-sdk-cst-generator/src/generator/naming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ fn escape_char(c: char) -> String {
c.to_string()
}
pub fn normalize_string(string: &str) -> String {
let escaped = String::from_iter(string.chars().map(|c| escape_char(c)));
let escaped = String::from_iter(string.chars().map(escape_char));
escaped
}
pub fn normalize_type_name(type_name: &str) -> String {
let mut cased = type_name.to_string();
if type_name.chars().any(|c| c.is_ascii_alphabetic()) {
cased = cased.to_case(Case::Pascal);
}
let escaped = String::from_iter(cased.chars().map(|c| get_char_mapping(c)));
let escaped = String::from_iter(cased.chars().map(get_char_mapping));
debug_assert!(
escaped
.chars()
Expand Down
Loading