diff --git a/Cargo.lock b/Cargo.lock index 8e74384..5af7dc1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -196,7 +196,7 @@ dependencies = [ [[package]] name = "tsync" -version = "2.0.0" +version = "2.0.1" dependencies = [ "convert_case", "proc-macro2", diff --git a/Cargo.toml b/Cargo.toml index 1620861..32dd894 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "tsync" description = "Generate typescript types from rust code." -version = "2.0.0" +version = "2.0.1" readme = "README.md" repository = "https://github.com/Wulf/tsync" license = "MIT OR Apache-2.0" diff --git a/src/utils.rs b/src/utils.rs index e048d1f..b6fc4ab 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,5 +1,5 @@ use quote::ToTokens; -use syn::{punctuated::Punctuated, Attribute, MetaNameValue, Token}; +use syn::{punctuated::Punctuated, Attribute, ExprPath, MetaNameValue, Token}; pub fn has_attribute(needle: &str, attributes: &[syn::Attribute]) -> bool { attributes.iter().any(|attr| { @@ -11,6 +11,9 @@ pub fn has_attribute(needle: &str, attributes: &[syn::Attribute]) -> bool { } /// Get the value matching an attribute and argument combination +/// +/// For #[serde(tag = "type")], get_attribute_arg("serde", "tag", attributes) will return Some("type") +/// For #[derive(Serialize_repr)], get_attribute_arg("derive", "Serialize_repr", attributes) will return Some("Serialize_repr") pub fn get_attribute_arg(needle: &str, arg: &str, attributes: &[syn::Attribute]) -> Option { if let Some(attr) = get_attribute(needle, attributes) { // check if attribute list contains the argument we are interested in @@ -40,6 +43,37 @@ pub fn get_attribute_arg(needle: &str, arg: &str, attributes: &[syn::Attribute]) ); if name_value_pairs.is_err() { + let comma_seperated_values = ::syn::parse::Parser::parse2( + Punctuated::::parse_terminated, + group.stream(), + ); + + if comma_seperated_values.is_err() { + continue; + } + + let comma_seperated_values = comma_seperated_values.unwrap(); + + for comma_seperated_value in comma_seperated_values { + match comma_seperated_value { + syn::Expr::Path(expr_path) => { + let segments = expr_path.path.segments; + + if segments.is_empty() { + continue; + } + + if segments[0].ident.to_string().eq(arg) { + found = true; + value = String::from(arg); + + break; + } + } + _ => continue, + } + } + continue; } diff --git a/test/enum/rust.rs b/test/enum/rust.rs index e957ab9..06b6f77 100644 --- a/test/enum/rust.rs +++ b/test/enum/rust.rs @@ -56,12 +56,3 @@ enum AnimalTwo { DogLongExtra = 2, Cat, } - -/// Integer enums should follow rust discrimination if literals (doesn't evaluate expression) -#[derive(Serialize_repr)] -#[tsync] -enum Foo { - Bar, // 0 - Baz = 123, // 123 - Quux, // 124 -} diff --git a/test/enum/typescript.d.ts b/test/enum/typescript.d.ts index a4bafad..e337703 100644 --- a/test/enum/typescript.d.ts +++ b/test/enum/typescript.d.ts @@ -61,7 +61,3 @@ type Animal = type AnimalTwo = | "dog_long_extra" | "cat"; - -/** Integer enums should follow rust discrimination if literals (doesn't evaluate expression) */ -type Foo = - | "Bar" | "Baz" | "Quux"; diff --git a/test/enum/typescript.ts b/test/enum/typescript.ts index eacf84c..94bbbd6 100644 --- a/test/enum/typescript.ts +++ b/test/enum/typescript.ts @@ -61,7 +61,3 @@ export type Animal = export type AnimalTwo = | "dog_long_extra" | "cat"; - -/** Integer enums should follow rust discrimination if literals (doesn't evaluate expression) */ -export type Foo = - | "Bar" | "Baz" | "Quux"; diff --git a/test/enum_numeric/rust.rs b/test/enum_numeric/rust.rs new file mode 100644 index 0000000..2b0be3a --- /dev/null +++ b/test/enum_numeric/rust.rs @@ -0,0 +1,10 @@ +use tsync::tsync; + +/// Integer enums should follow rust discrimination if literals (doesn't evaluate expression) +#[derive(Serialize_repr)] +#[tsync] +enum Foo { + Bar, // 0 + Baz = 123, // 123 + Quux, // 124 +} diff --git a/test/enum_numeric/tsync.sh b/test/enum_numeric/tsync.sh new file mode 100755 index 0000000..1764bd6 --- /dev/null +++ b/test/enum_numeric/tsync.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" + +cd $SCRIPT_DIR + +cargo run -- -i rust.rs -o typescript.d.ts +cargo run -- -i rust.rs -o typescript.ts \ No newline at end of file diff --git a/test/enum_numeric/typescript.d.ts b/test/enum_numeric/typescript.d.ts new file mode 100644 index 0000000..cf04740 --- /dev/null +++ b/test/enum_numeric/typescript.d.ts @@ -0,0 +1,8 @@ +/* This file is generated and managed by tsync */ + +/** Integer enums should follow rust discrimination if literals (doesn't evaluate expression) */ +declare enum Foo { + Bar = 0, + Baz = 123, + Quux = 124, +} diff --git a/test/enum_numeric/typescript.ts b/test/enum_numeric/typescript.ts new file mode 100644 index 0000000..4f06b60 --- /dev/null +++ b/test/enum_numeric/typescript.ts @@ -0,0 +1,8 @@ +/* This file is generated and managed by tsync */ + +/** Integer enums should follow rust discrimination if literals (doesn't evaluate expression) */ +export enum Foo { + Bar = 0, + Baz = 123, + Quux = 124, +}