Skip to content

Commit

Permalink
2.0.0: Update deps, refactoring, add doc comments test
Browse files Browse the repository at this point in the history
  • Loading branch information
Wulf committed Aug 10, 2023
1 parent 3bf21a1 commit 2323480
Show file tree
Hide file tree
Showing 13 changed files with 238 additions and 119 deletions.
40 changes: 25 additions & 15 deletions Cargo.lock

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

12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "tsync"
description = "Generate typescript types from rust code."
version = "1.7.0"
version = "2.0.0"
readme = "README.md"
repository = "https://github.com/Wulf/tsync"
license = "MIT OR Apache-2.0"
Expand All @@ -11,11 +11,11 @@ authors = ["Haris <4259838+Wulf@users.noreply.github.com>"]
edition = "2018"

[dependencies]
structopt = "0.3.22"
syn = { version = "1.0.75", features = ["full", "extra-traits"] }
proc-macro2 = "1.0.52"
quote = "1.0.23"
walkdir = "2.3.2"
structopt = "0.3.26"
syn = { version = "2.0.28", features = ["full", "extra-traits"] }
proc-macro2 = "1.0.66"
quote = "1.0.32"
walkdir = "2.3.3"
tsync-macro = "0.1.0"
convert_case = "0.6.0"

Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ fn process_rust_file(
uses_typeinterface: bool,
) {
if debug {
dbg!(uses_typeinterface);

println!(
"processing rust file: {:?}",
input_path.clone().into_os_string().into_string().unwrap()
Expand Down
67 changes: 30 additions & 37 deletions src/to_typescript/enums.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::typescript::convert_type;
use crate::{utils, BuildState};
use convert_case::{Case, Casing};
use syn::__private::ToTokens;
use crate::typescript::convert_type;

static RENAME_RULES: &[(&str, convert_case::Case)] = &[
("lowercase", Case::Lower),
Expand All @@ -17,7 +17,7 @@ static RENAME_RULES: &[(&str, convert_case::Case)] = &[
/// Conversion of Rust Enum to Typescript using external tagging as per https://serde.rs/enum-representations.html
/// however conversion will adhere to the `serde` `tag` such that enums are intenrally tagged
/// (while the other forms such as adjacent tagging aren't supported).
/// `renameAll` attributes for the name of the tag will also be adhered to.
/// `rename_all` attributes for the name of the tag will also be adhered to.
impl super::ToTypescript for syn::ItemEnum {
fn convert_to_ts(self, state: &mut BuildState, debug: bool, uses_typeinterface: bool) {
// check we don't have any tuple structs that could mess things up.
Expand All @@ -33,8 +33,7 @@ impl super::ToTypescript for syn::ItemEnum {
println!("#[tsync] failed for enum {}", self.ident);
}
return;
}
else {
} else {
is_newtype = true;
}
}
Expand All @@ -52,21 +51,21 @@ impl super::ToTypescript for syn::ItemEnum {

if is_single {
if utils::has_attribute_arg("derive", "Serialize_repr", &self.attrs) {
make_numeric_enum(self, state, casing, uses_typeinterface)
add_numeric_enum(self, state, casing, uses_typeinterface)
} else {
make_enum(self, state, casing, uses_typeinterface)
add_enum(self, state, casing, uses_typeinterface)
}
} else if let Some(tag_name) = utils::get_attribute_arg("serde", "tag", &self.attrs) {
make_variant(tag_name, self, state, casing, uses_typeinterface)
add_internally_tagged_enum(tag_name, self, state, casing, uses_typeinterface)
} else {
make_externally_tagged_variant(self, state, casing, uses_typeinterface)
add_externally_tagged_enum(self, state, casing, uses_typeinterface)
}
}
}

/// This convert an all unit enums to a union of const strings in Typescript.
/// It will ignore any discriminants.
fn make_enum(
fn add_enum(
exported_struct: syn::ItemEnum,
state: &mut BuildState,
casing: Option<Case>,
Expand Down Expand Up @@ -115,7 +114,7 @@ fn make_enum(
/// }
/// ```
///
fn make_numeric_enum(
fn add_numeric_enum(
exported_struct: syn::ItemEnum,
state: &mut BuildState,
casing: Option<Case>,
Expand Down Expand Up @@ -188,7 +187,7 @@ fn make_numeric_enum(
/// Cat = 1,
/// }
/// ```
fn make_variant(
fn add_internally_tagged_enum(
tag_name: String,
exported_struct: syn::ItemEnum,
state: &mut BuildState,
Expand All @@ -203,18 +202,17 @@ fn make_variant(
));

for variant in exported_struct.variants.iter() {

// Assumes that non-newtype tuple variants have already been filtered out
let is_newtype = variant.fields.iter().fold(false, |state, v| {
state || v.ident.is_none()
});
let is_newtype = variant
.fields
.iter()
.fold(false, |state, v| state || v.ident.is_none());
if is_newtype {
// TODO: Generate newtype structure
// This should contain the discriminant plus all fields of the inner structure as a flat structure
// TODO: Check for case where discriminant name matches an inner structure field name
// We should reject clashes
}
else {
} else {
state.types.push('\n');
state.types.push_str(&format!(
" | {interface_name}__{variant_name}",
Expand All @@ -228,9 +226,10 @@ fn make_variant(

for variant in exported_struct.variants {
// Assumes that non-newtype tuple variants have already been filtered out
let is_newtype = variant.fields.iter().fold(false, |state, v| {
state || v.ident.is_none()
});
let is_newtype = variant
.fields
.iter()
.fold(false, |state, v| state || v.ident.is_none());
if !is_newtype {
state.types.push('\n');
let comments = utils::get_comments(variant.attrs);
Expand Down Expand Up @@ -261,7 +260,7 @@ fn make_variant(
}

/// This follows serde's default approach of external tagging
fn make_externally_tagged_variant(
fn add_externally_tagged_enum(
exported_struct: syn::ItemEnum,
state: &mut BuildState,
casing: Option<Case>,
Expand All @@ -284,27 +283,21 @@ fn make_externally_tagged_variant(
variant.ident.to_string()
};
// Assumes that non-newtype tuple variants have already been filtered out
let is_newtype = variant.fields.iter().fold(false, |state, v| {
state || v.ident.is_none()
});
let is_newtype = variant
.fields
.iter()
.fold(false, |state, v| state || v.ident.is_none());

if is_newtype {
// add discriminant
state.types.push_str(&format!(
" | {{ \"{}\":",
field_name
));
state.types.push_str(&format!(" | {{ \"{}\":", field_name));
for field in variant.fields {
state.types.push_str(&format!(
" {}",
convert_type(&field.ty).ts_type,
));
state
.types
.push_str(&format!(" {}", convert_type(&field.ty).ts_type,));
}
state
.types
.push_str(&format!(" }}"));
}
else {
state.types.push_str(&format!(" }}"));
} else {
// add discriminant
state.types.push_str(&format!(
" | {{\n{}\"{}\": {{",
Expand Down
Loading

0 comments on commit 2323480

Please sign in to comment.