Skip to content

Commit

Permalink
chore: update the document (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
ImJeremyHe authored Aug 30, 2023
1 parent df4d3e1 commit 2a944a5
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 9 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
authors = ["ImJeremyHe<[email protected]>"]
edition = "2018"
name = "gents"
version = "0.3.1"
version = "0.3.2"
license = "MIT"
description = "generate typescript interfaces from rust code"
repository = "https://github.com/proclml/LogiSheets/tree/master/crates/gents"
keywords = ["typescript", "interface"]
repository = "https://github.com/ImJeremyHe/gents"
keywords = ["Typescript", "interface", "ts-rs", "Rust"]

[dev-dependencies]
gents_derives = {version = "0.3.1", path = "./derives"}
gents_derives = {version = "0.3.2", path = "./derives"}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ this group files generated will be placed in the same directory. **Group** is se
dependecies. Therefore, `gents` requires you specify the *file_name* on structs
or enums and specify the *dir* on group, while `ts-rs` requires specifing the *path* on every item.

- `gents` helps you manage the export files. `gents` gathers all the dependencies automatically.
- `gents` helps you manage the export files. And it gathers all the dependencies automatically.

- Code generated by `ts-rs` is not match our coding style.

Expand All @@ -38,8 +38,8 @@ In your **Rust** code:
You should import `gents` in your Cargo.toml.

```toml
gents = "0.3.0"
gents_derives = "0.3.0"
gents = "0.3"
gents_derives = "0.3"
```

```rust
Expand Down
2 changes: 1 addition & 1 deletion derives/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gents_derives"
version = "0.3.1"
version = "0.3.2"
description = "provides some macros for gents"
authors = ["ImJeremyHe<[email protected]>"]
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion derives/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl<'a> Contianer<'a> {
file_name = Some(s.value());
}
Meta(NameValue(m)) if m.path == RENAME => {
let s = get_lit_str(&m.lit).expect("file_name requires lit str");
let s = get_lit_str(&m.lit).expect("rename requires lit str");
rename = Some(s.value());
}
_ => panic!("unexpected attr"),
Expand Down
51 changes: 51 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,54 @@
//! # gents
//! `gents` is a tool for generating `Typescript` files.
//! You can easily use `serde-json` to establish the communication between `Rust` and `Typescript`.
//! It is useful when you are developing
//! a web service or a wasm project.
//! ## Step1: Derive TS and set the `file_name`.
//! ```
//! use gents_derives::TS;
//!
//! #[derive(TS)]
//! #[ts(file_name = "person.ts")]
//! pub struct Person {
//! pub age: u16,
//! }
//! ```
//!
//! ## Step2: Set your rename policy.
//! Currently, you can set `camelCase` using `rename_all`, or you
//! can rename each field by using `rename`.
//! ```
//! use gents_derives::TS;
//!
//! #[derive(TS)]
//! #[ts(file_name = "person.ts", rename_all = "camelCase")]
//! pub struct Person {
//! pub age: u16,
//! #[ts(rename="name")]
//! pub en_name: String,
//! }
//! ```
//!
//! ## Step3: Register your root structs or enums
//! ```no_run
//! use gents::FileGroup;
//! use gents_derives::TS;
//! #[derive(TS)]
//! #[ts(file_name = "person.ts")]
//! pub struct Person{}
//!
//! fn main() {
//! let mut g = FileGroup::new();
//! g.add::<Person>();
//! g.gen_files("outdir", false); // false for not generating index.ts
//! }
//! ```
//! `.add` adds the target and its dependencies into the `FileGroup` and their files
//! will be generated in the same time.
//!
//! ## Step4: Run the binary
//!

mod descriptor;
mod file_generator;
mod utils;
Expand Down
23 changes: 23 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,34 @@ pub enum Pet {
None,
}

#[derive(TS)]
#[ts(file_name = "skip.ts", rename_all = "camelCase")]
pub struct TestSkip {
pub f1: u16,
#[ts(skip = true)]
pub f2: u32,
pub f3: u64,
}

#[cfg(test)]
mod tests {
use super::*;
use gents::*;

#[test]
fn gen_skip_test() {
let mut manager = DescriptorManager::default();
TestSkip::_register(&mut manager);
let (_, content) = manager.gen_data().into_iter().next().unwrap();
assert_eq!(
content.trim(),
r#"export interface TestSkip {
f1: number
f3: number
}"#
)
}

#[test]
fn gen_data_person_test() {
let mut manager = DescriptorManager::default();
Expand Down

0 comments on commit 2a944a5

Please sign in to comment.