Skip to content

Commit 43b129f

Browse files
authored
Merge pull request #221 from stepchowfun/typescript-codegen
Add a placeholder TypeScript code generator
2 parents 5116d4f + e521c09 commit 43b129f

File tree

13 files changed

+6465
-26
lines changed

13 files changed

+6465
-26
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
password: ${{ secrets.DOCKER_PASSWORD }}
1717
- uses: stepchowfun/toast/.github/actions/toast@main
1818
with:
19-
tasks: build test_units test_rust_integration lint release run
19+
tasks: build test_units test_rust_integration test_typescript_integration lint release run
2020
docker_repo: stephanmisc/toast
2121
write_remote_cache: ${{ github.event_name == 'push' }}
2222
- run: |

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
/example/target/
55
/integration_tests/rust/src/types.rs
66
/integration_tests/rust/target/
7+
/integration_tests/typescript/node_modules/
78
/target/

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [0.0.2] - 2021-10-09
9+
10+
### Changed
11+
- Renamed `--rust-out-file` to `--rust-out`.
12+
813
## [0.0.1] - 2021-10-08
914

1015
### Added

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "typical"
3-
version = "0.0.1"
3+
version = "0.0.2"
44
authors = ["Stephan Boyer <[email protected]>"]
55
edition = "2018"
66
description = "Algebraic data types for data interchange."

README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Each field also has a type, either explicitly or implicitly. If the type is miss
4343
Now that we've defined some types, we can use Typical to generate the code for serialization and deserialization. For example, you can generate Rust code with the following:
4444

4545
```sh
46-
$ typical generate email_api.t --rust-out-file email_api.rs
46+
$ typical generate email_api.t --rust-out email_api.rs
4747
```
4848

4949
The client and server can then use the generated code to serialize and deserialize messages for mutual communication. If the client and server are written in different languages, you can generate code for each language.
@@ -468,10 +468,10 @@ For a simple enumerated type (such as `Weekday` above), the encoding of a field
468468
Once Typical is [installed](#installation-instructions), you can use it to generate code for a schema called `main.t` with the following:
469469

470470
```sh
471-
$ typical generate main.t --rust-out-file main.rs
471+
$ typical generate main.t --rust-out types.rs --typescript-out types.ts
472472
```
473473

474-
You can change the `--rust-out-file` flag as appropriate to select the programming language.
474+
You can change the `--rust-out flag as appropriate to select the programming language.
475475

476476
Here are the supported command-line options:
477477

@@ -505,7 +505,8 @@ FLAGS:
505505
-h, --help Prints help information
506506
507507
OPTIONS:
508-
--rust-out-file <PATH> Sets the path of the Rust file to emit
508+
--rust-out <PATH> Sets the path of the Rust file to emit
509+
--typescript-out <PATH> Sets the path of the TypeScript file to emit
509510
510511
ARGS:
511512
<SCHEMA_PATH> Sets the path of the schema

example/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
To run this demonstration, first [install Typical](https://github.com/stepchowfun/typical#installation-instructions) if you haven't already. Then issue the following commands in this directory:
44

55
```sh
6-
$ typical generate types.t --rust-out-file src/types.rs
6+
$ typical generate types.t --rust-out src/types.rs
77
$ cargo run
88
```

integration_tests/typescript/package-lock.json

+25-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integration_tests/typescript/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
"main": "index.js",
55
"scripts": {
66
"test": "echo \"Error: no test specified\" && exit 1"
7+
},
8+
"devDependencies": {
9+
"typescript": "^4.4.3"
710
}
811
}

src/generate_rust.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use Direction::{In, Out};
5454
#[allow(clippy::too_many_lines)]
5555
pub fn generate(
5656
typical_version: &str,
57-
schemas: BTreeMap<schema::Namespace, (schema::Schema, PathBuf, String)>,
57+
schemas: &BTreeMap<schema::Namespace, (schema::Schema, PathBuf, String)>,
5858
) -> String {
5959
// Construct a tree of modules and schemas. We start with an empty tree.
6060
let mut tree = Module {
@@ -67,7 +67,7 @@ pub fn generate(
6767

6868
// Populate the tree with all the schemas.
6969
for (namespace, (schema, _, _)) in schemas {
70-
insert_schema(&mut tree, &namespace, schema);
70+
insert_schema(&mut tree, namespace, schema);
7171
}
7272

7373
// Write the code.
@@ -288,7 +288,7 @@ pub trait Deserialize {{
288288
}
289289

290290
// Insert a schema into a module.
291-
fn insert_schema(module: &mut Module, namespace: &schema::Namespace, schema: schema::Schema) {
291+
fn insert_schema(module: &mut Module, namespace: &schema::Namespace, schema: &schema::Schema) {
292292
let mut iter = namespace.components.iter();
293293

294294
if let Some(head) = iter.next() {
@@ -320,7 +320,7 @@ fn insert_schema(module: &mut Module, namespace: &schema::Namespace, schema: sch
320320
module.children.insert(head.clone(), child);
321321
}
322322
} else {
323-
module.schema = schema;
323+
module.schema = schema.clone();
324324
}
325325
}
326326

@@ -1626,7 +1626,7 @@ mod tests {
16261626
validate(&schemas).unwrap();
16271627

16281628
assert_eq!(
1629-
generate("0.0.0", schemas),
1629+
generate("0.0.0", &schemas),
16301630
"\
16311631
// This file was automatically generated by Typical 0.0.0.
16321632
// Visit https://github.com/stepchowfun/typical for more information.

0 commit comments

Comments
 (0)